A working ScanDirectory() function.
This commit is contained in:
parent
8550577726
commit
34a5202ea3
@ -1,9 +1,8 @@
|
|||||||
package files
|
package files
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,40 +21,34 @@ type DirEntry struct {
|
|||||||
Size string
|
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 dirEntries []DirEntry = make([]DirEntry, 0)
|
||||||
var fileEntries []DirEntry = make([]DirEntry, 0)
|
var fileEntries []DirEntry = make([]DirEntry, 0)
|
||||||
var totalFilesSize int64 = 0
|
var totalFilesSize int64 = 0
|
||||||
|
|
||||||
if err = filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
|
dir, err := ioutil.ReadDir(path)
|
||||||
if err != nil {
|
for _, entry := range dir {
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if info.IsDir() {
|
if entry.IsDir() {
|
||||||
dirEntries = append(dirEntries, DirEntry{
|
dirEntries = append(dirEntries, DirEntry{
|
||||||
Name: info.Name(),
|
Name: entry.Name(),
|
||||||
Link: url.PathEscape("/files"),
|
Link: url.PathEscape(entry.Name()) + "/",
|
||||||
Datetime: info.ModTime(),
|
Datetime: entry.ModTime(),
|
||||||
Size: "DIR",
|
Size: "DIR",
|
||||||
})
|
})
|
||||||
stats.Directories++
|
stats.Directories++
|
||||||
} else {
|
} else {
|
||||||
_, _, sz := convertFileSize(info.Size())
|
_, _, sz := convertFileSize(entry.Size())
|
||||||
fileEntries = append(fileEntries, DirEntry{
|
fileEntries = append(fileEntries, DirEntry{
|
||||||
Name: info.Name(),
|
Name: entry.Name(),
|
||||||
Link: url.PathEscape("/files"),
|
Link: "/file" + urlBase + url.PathEscape(entry.Name()),
|
||||||
Datetime: info.ModTime(),
|
Datetime: entry.ModTime(),
|
||||||
Size: sz,
|
Size: sz,
|
||||||
})
|
})
|
||||||
|
|
||||||
totalFilesSize += info.Size()
|
totalFilesSize += entry.Size()
|
||||||
stats.Files++
|
stats.Files++
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, sz := convertFileSize(totalFilesSize)
|
_, _, sz := convertFileSize(totalFilesSize)
|
||||||
|
Loading…
Reference in New Issue
Block a user