71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package fs
|
|
|
|
import (
|
|
"ZhenTuLocalPassiveAdapter/config"
|
|
"ZhenTuLocalPassiveAdapter/dto"
|
|
"ZhenTuLocalPassiveAdapter/util"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type LocalAdapter struct {
|
|
StorageConfig config.StorageConfig
|
|
}
|
|
|
|
func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
|
|
if l.StorageConfig.Path == "" {
|
|
return nil, fmt.Errorf("未配置存储路径")
|
|
}
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
if path[0] != '/' {
|
|
path = "/" + path
|
|
}
|
|
if path[len(path)-1] != '/' {
|
|
path = path + "/"
|
|
}
|
|
// 读取文件夹下目录
|
|
files, err := os.ReadDir(l.StorageConfig.Path + path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var fileList []dto.File
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
continue
|
|
}
|
|
if !util.IsVideoFile(file.Name()) {
|
|
continue
|
|
}
|
|
info, err := file.Info()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
// TODO: 0点左右会出问题
|
|
relDt := info.ModTime()
|
|
startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if startTime.Equal(stopTime) {
|
|
stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration))
|
|
}
|
|
fileList = append(fileList, dto.File{
|
|
BasePath: l.StorageConfig.Path,
|
|
Name: file.Name(),
|
|
Path: path,
|
|
Url: fmt.Sprintf("%s%s%s", l.StorageConfig.Path, path, file.Name()),
|
|
StartTime: startTime,
|
|
EndTime: stopTime,
|
|
})
|
|
sort.Slice(fileList, func(i, j int) bool {
|
|
return fileList[i].StartTime.Before(fileList[j].StartTime)
|
|
})
|
|
}
|
|
return fileList, nil
|
|
}
|