2020-04-02 18:17:07 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2023-03-28 20:37:42 +02:00
|
|
|
"path"
|
2020-10-19 09:52:52 +02:00
|
|
|
"path/filepath"
|
2020-04-02 18:17:07 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-28 20:37:42 +02:00
|
|
|
|
|
|
|
"github.com/emersion/go-webdav"
|
2020-04-02 18:17:07 +02:00
|
|
|
)
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
// FileInfo represents a file system entry.
|
2020-04-02 18:17:07 +02:00
|
|
|
type FileInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Abs string `json:"abs"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Dir bool `json:"dir"`
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
func fileDir(dir, sep string) string {
|
|
|
|
if dir != sep && len(dir) > 0 {
|
|
|
|
if dir[len(dir)-1:] == sep {
|
2020-04-02 18:17:07 +02:00
|
|
|
dir = dir[:len(dir)-1]
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
if dir[0:1] != sep {
|
|
|
|
dir = sep + dir
|
2020-04-02 18:17:07 +02:00
|
|
|
}
|
2020-10-19 09:52:52 +02:00
|
|
|
} else {
|
2023-03-28 20:37:42 +02:00
|
|
|
dir = sep
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileInfo creates a FileInfo struct from the os.FileInfo record.
|
|
|
|
func NewFileInfo(file os.FileInfo, dir string) FileInfo {
|
|
|
|
dir = fileDir(dir, PathSeparator)
|
|
|
|
|
|
|
|
result := FileInfo{
|
|
|
|
Name: file.Name(),
|
|
|
|
Abs: filepath.Join(dir, file.Name()),
|
|
|
|
Size: file.Size(),
|
|
|
|
Date: file.ModTime(),
|
|
|
|
Dir: file.IsDir(),
|
2020-04-02 18:17:07 +02:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:37:42 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebFileInfo creates a FileInfo struct from a webdav.FileInfo record.
|
|
|
|
func WebFileInfo(file webdav.FileInfo, dir string) FileInfo {
|
|
|
|
filePath := strings.Trim(file.Path, "/")
|
|
|
|
dir = strings.Trim(dir, "/")
|
2020-04-02 18:17:07 +02:00
|
|
|
result := FileInfo{
|
2023-03-28 20:37:42 +02:00
|
|
|
Name: path.Base(filePath),
|
|
|
|
Abs: "/" + RelName(filePath, dir),
|
|
|
|
Size: file.Size,
|
|
|
|
Date: file.ModTime,
|
|
|
|
Dir: file.IsDir,
|
2020-04-02 18:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileInfos []FileInfo
|
|
|
|
|
|
|
|
func (infos FileInfos) Len() int { return len(infos) }
|
|
|
|
func (infos FileInfos) Swap(i, j int) { infos[i], infos[j] = infos[j], infos[i] }
|
|
|
|
func (infos FileInfos) Less(i, j int) bool {
|
|
|
|
return strings.Compare(infos[i].Abs, infos[j].Abs) == -1
|
|
|
|
}
|
2020-04-04 17:19:34 +02:00
|
|
|
func (infos FileInfos) Abs() (result []string) {
|
|
|
|
for _, info := range infos {
|
|
|
|
result = append(result, info.Abs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-04-02 18:17:07 +02:00
|
|
|
|
|
|
|
func NewFileInfos(infos []os.FileInfo, dir string) FileInfos {
|
|
|
|
var result FileInfos
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
result = append(result, NewFileInfo(info, dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|