VptPassiveAdapter/fs/local_adapter.go
2025-02-07 22:58:01 +08:00

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
}