modernize fs calls

This commit is contained in:
Pierre Dubouilh 2024-01-21 10:51:56 +01:00 committed by Pierre Dubouilh
parent 50c524cc9b
commit 1861de0d57

View file

@ -12,7 +12,6 @@ import (
"html/template" "html/template"
"io" "io"
"io/fs" "io/fs"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -83,9 +82,9 @@ func humanize(bytes int64) string {
} }
func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path string) { func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path string) {
_files, err := ioutil.ReadDir(fullPath) files, err := os.ReadDir(fullPath)
check(err) check(err)
sort.Slice(_files, func(i, j int) bool { return strings.ToLower(_files[i].Name()) < strings.ToLower(_files[j].Name()) }) sort.Slice(files, func(i, j int) bool { return strings.ToLower(files[i].Name()) < strings.ToLower(files[j].Name()) })
if !strings.HasSuffix(path, "/") { if !strings.HasSuffix(path, "/") {
path += "/" path += "/"
@ -100,20 +99,20 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str
p.Ro = *ro p.Ro = *ro
p.Title = template.HTML(html.EscapeString(title)) p.Title = template.HTML(html.EscapeString(title))
for _, el := range _files { for _, el := range files {
if *skipHidden && strings.HasPrefix(el.Name(), ".") { info, err := el.Info()
continue // dont print hidden files if we're not allowed
}
if !*symlinks && el.Mode()&os.ModeSymlink != 0 {
continue // dont print symlinks if were not allowed
}
el, err := os.Stat(fullPath + "/" + el.Name())
if err != nil { if err != nil {
log.Println("error - cant stat a file", err) log.Println("error - cant stat a file", err)
continue continue
} }
if *skipHidden && strings.HasPrefix(el.Name(), ".") {
continue // dont print hidden files if we're not allowed
}
if *symlinks && info.Mode()&os.ModeSymlink != 0 {
continue // dont follow symlinks if we're not allowed
}
href := url.PathEscape(el.Name()) href := url.PathEscape(el.Name())
name := el.Name() name := el.Name()
@ -127,7 +126,7 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str
} else { } else {
sl := strings.Split(name, ".") sl := strings.Split(name, ".")
ext := strings.ToLower(sl[len(sl)-1]) ext := strings.ToLower(sl[len(sl)-1])
row := rowTemplate{name, template.URL(href), humanize(el.Size()), ext} row := rowTemplate{name, template.URL(href), humanize(info.Size()), ext}
p.RowsFiles = append(p.RowsFiles, row) p.RowsFiles = append(p.RowsFiles, row)
} }
} }
@ -228,7 +227,7 @@ func rpc(w http.ResponseWriter, r *http.Request) {
var err error var err error
var rpc rpcCall var rpc rpcCall
defer exitPath(w, "rpc", rpc) defer exitPath(w, "rpc", rpc)
bodyBytes, err := ioutil.ReadAll(r.Body) bodyBytes, err := io.ReadAll(r.Body)
check(err) check(err)
json.Unmarshal(bodyBytes, &rpc) json.Unmarshal(bodyBytes, &rpc)