A working ScanDirectory() function.
This commit is contained in:
parent
8550577726
commit
34a5202ea3
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user