37 lines
596 B
Go
Raw Normal View History

2022-11-02 22:20:32 -04:00
package utils
import (
"os"
"m1k1o/neko/internal/types"
)
2022-11-19 18:29:21 +01:00
func ListFiles(path string) ([]types.FileListItem, error) {
2022-11-02 22:20:32 -04:00
items, err := os.ReadDir(path)
if err != nil {
return nil, err
}
out := make([]types.FileListItem, len(items))
for i, item := range items {
var itemType string = ""
2022-11-11 20:27:15 -05:00
var size int64 = 0
2022-11-02 22:20:32 -04:00
if item.IsDir() {
itemType = "dir"
} else {
itemType = "file"
2022-11-11 20:27:15 -05:00
info, err := item.Info()
if err == nil {
size = info.Size()
}
2022-11-02 22:20:32 -04:00
}
out[i] = types.FileListItem{
Filename: item.Name(),
Type: itemType,
2022-11-11 20:27:15 -05:00
Size: size,
2022-11-02 22:20:32 -04:00
}
}
2022-11-19 18:29:21 +01:00
return out, nil
2022-11-02 22:20:32 -04:00
}