From 34a5202ea329fc61c76f24653bd66e08b63556c7 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 27 Jun 2022 22:50:33 +0400 Subject: [PATCH] A working ScanDirectory() function. --- pkg/files/files.go | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/pkg/files/files.go b/pkg/files/files.go index 56a3b59..22bda6c 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -1,9 +1,8 @@ package files import ( - "io/fs" + "io/ioutil" "net/url" - "path/filepath" "time" ) @@ -22,40 +21,34 @@ type DirEntry struct { Size string } -func ScanDirectory(path string) (entries []DirEntry, stats DirStats, err error) { +func ScanDirectory(path, urlBase string) (entries []DirEntry, stats DirStats, err error) { var dirEntries []DirEntry = make([]DirEntry, 0) var fileEntries []DirEntry = make([]DirEntry, 0) var totalFilesSize int64 = 0 - if err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } + dir, err := ioutil.ReadDir(path) + for _, entry := range dir { - if info.IsDir() { + if entry.IsDir() { dirEntries = append(dirEntries, DirEntry{ - Name: info.Name(), - Link: url.PathEscape("/files"), - Datetime: info.ModTime(), + Name: entry.Name(), + Link: url.PathEscape(entry.Name()) + "/", + Datetime: entry.ModTime(), Size: "DIR", }) stats.Directories++ } else { - _, _, sz := convertFileSize(info.Size()) + _, _, sz := convertFileSize(entry.Size()) fileEntries = append(fileEntries, DirEntry{ - Name: info.Name(), - Link: url.PathEscape("/files"), - Datetime: info.ModTime(), + Name: entry.Name(), + Link: "/file" + urlBase + url.PathEscape(entry.Name()), + Datetime: entry.ModTime(), Size: sz, }) - totalFilesSize += info.Size() + totalFilesSize += entry.Size() stats.Files++ } - - return nil - }); err != nil { - return } _, _, sz := convertFileSize(totalFilesSize)