Exemple #1
0
// in logs I saw a url that was correct but had "&foo" and other garbage appended
// to it. Redirect to the best matching file in the directory (if there is a file
// that is a prefix of the file that was asked for)
// returns true if redirected
func redirectIfFoundMatching(w http.ResponseWriter, r *http.Request, dir, fileName string) bool {
	var files []string
	ok := false
	if files, ok = filesPerDir[dir]; !ok {
		files = u.ListFilesInDir(dir, true)
		n := len(dir) + 1
		for i, f := range files {
			files[i] = f[n:]
		}
		//logger.Noticef("files in %q: %v", dir, files)
		filesPerDir[dir] = files
	}
	for _, f := range files {
		if strings.HasPrefix(fileName, f) {
			if fileName == f {
				return false
			}
			diff := len(fileName) - len(f)
			url := r.URL.Path
			url = url[:len(url)-diff]
			logger.Noticef("serveFileFromDir(): redirecting %q => %q", r.URL.Path, url)
			http.Redirect(w, r, url, 302)
			return true
		}
	}
	return false
}
func findFileFixes(dir string) {
	n := len(dir)
	allFiles := u.ListFilesInDir(dir, true)
	files := make([]string, 0)
	for _, f := range allFiles {
		f = f[n+1:]
		if isSrcFile(f) {
			f = strings.Replace(f, "/", "\\", -1)
			files = append(files, f)
		}
	}
	needFix := make([]string, 0)
	for _, f := range files {
		tmp := strings.ToLower(f)
		tmp = uppercaseFile(tmp)
		if f != tmp {
			needFix = append(needFix, f)
			fmt.Printf("%s => %s\n", f, tmp)
		}
	}
}