54 lines
925 B
Go
54 lines
925 B
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func DirectoryFiles(directory, baseUrl string) (Files, error) {
|
|
var files Files
|
|
|
|
absoluteRootPath, err := filepath.Abs(directory)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
basename := string(info.Name())
|
|
relativePath := strings.Replace(path, absoluteRootPath, "", 1)
|
|
|
|
file := File{
|
|
Name: basename,
|
|
Path: path,
|
|
RelativePath: relativePath,
|
|
Date: info.ModTime().Unix(),
|
|
Url: fmt.Sprintf("%s/api/stream?path=%s", baseUrl, url.QueryEscape(relativePath)),
|
|
}
|
|
|
|
file.GenerateHeadAndContentType()
|
|
|
|
if file.IsSupported() {
|
|
files = append(files, file)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return files, nil
|
|
}
|