1
0

Changed logic of ScanDirectory() func.

This commit is contained in:
Alexander Andreev 2024-12-30 14:58:03 +04:00
parent 2c8a9e3e61
commit dc2729ff91
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 11 additions and 5 deletions

View File

@ -100,7 +100,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
path += "/"
}
entries, stats, err := files.ScanDirectory(*directoryPath+path, path)
entries, stats, err := files.ScanDirectory(*directoryPath, path)
if err != nil {
log.Println("Error directory scan:", err)
http.Error(w, "Not found", http.StatusNotFound)

View File

@ -22,8 +22,14 @@ type DirEntry struct {
Size string
}
func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err error) {
dir, err := os.ReadDir(path)
// ScanDirectory returns entries of directory which is located by its relative
// path within a base directory.
//
// rel path should start/end with a / symbol.
func ScanDirectory(base, rel string) (entries []DirEntry, stats DirStat, err error) {
abs := base + rel
dir, err := os.ReadDir(abs)
if err != nil {
return
}
@ -37,7 +43,7 @@ func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err
var isDirLink bool
if entry.Mode().Type()&os.ModeSymlink != 0 {
if slp, err := filepath.EvalSymlinks(filepath.Join(path, entry.Name())); err == nil {
if slp, err := filepath.EvalSymlinks(filepath.Join(abs, entry.Name())); err == nil {
lStat, _ := os.Lstat(slp)
isDirLink = lStat.IsDir()
}
@ -54,7 +60,7 @@ func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err
} else {
fileEntries = append(fileEntries, DirEntry{
Name: entry.Name(),
Link: "/file" + urlBase + url.PathEscape(entry.Name()),
Link: "/file" + rel + url.PathEscape(entry.Name()),
Datetime: entry.ModTime(),
Size: convertFileSize(entry.Size()),
})