Changed logic of ScanDirectory() func.
This commit is contained in:
parent
2c8a9e3e61
commit
dc2729ff91
@ -100,7 +100,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
|
|||||||
path += "/"
|
path += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, stats, err := files.ScanDirectory(*directoryPath+path, path)
|
entries, stats, err := files.ScanDirectory(*directoryPath, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error directory scan:", err)
|
log.Println("Error directory scan:", err)
|
||||||
http.Error(w, "Not found", http.StatusNotFound)
|
http.Error(w, "Not found", http.StatusNotFound)
|
||||||
|
@ -22,8 +22,14 @@ type DirEntry struct {
|
|||||||
Size string
|
Size string
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err error) {
|
// ScanDirectory returns entries of directory which is located by its relative
|
||||||
dir, err := os.ReadDir(path)
|
// 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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -37,7 +43,7 @@ func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err
|
|||||||
|
|
||||||
var isDirLink bool
|
var isDirLink bool
|
||||||
if entry.Mode().Type()&os.ModeSymlink != 0 {
|
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)
|
lStat, _ := os.Lstat(slp)
|
||||||
isDirLink = lStat.IsDir()
|
isDirLink = lStat.IsDir()
|
||||||
}
|
}
|
||||||
@ -54,7 +60,7 @@ func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStat, err
|
|||||||
} else {
|
} else {
|
||||||
fileEntries = append(fileEntries, DirEntry{
|
fileEntries = append(fileEntries, DirEntry{
|
||||||
Name: entry.Name(),
|
Name: entry.Name(),
|
||||||
Link: "/file" + urlBase + url.PathEscape(entry.Name()),
|
Link: "/file" + rel + url.PathEscape(entry.Name()),
|
||||||
Datetime: entry.ModTime(),
|
Datetime: entry.ModTime(),
|
||||||
Size: convertFileSize(entry.Size()),
|
Size: convertFileSize(entry.Size()),
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user