VptPassiveAdapter/fs/local_adapter.go

83 lines
2.3 KiB
Go

package fs
import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/util"
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"os"
"path"
"sort"
"time"
)
type LocalAdapter struct {
StorageConfig config.StorageConfig
}
func (l *LocalAdapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) {
subCtx, span := tracer.Start(ctx, "GetFileList_local")
defer span.End()
if l.StorageConfig.Path == "" {
span.SetAttributes(attribute.String("error", "未配置存储路径"))
span.SetStatus(codes.Error, "未配置存储路径")
return nil, fmt.Errorf("未配置存储路径")
}
// 读取文件夹下目录
files, err := os.ReadDir(path.Join(l.StorageConfig.Path, dirPath))
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "文件夹读取失败")
return nil, err
}
span.SetAttributes(attribute.Int("file.count", len(files)))
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
}
startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt)
if err != nil {
continue
}
if stopTime.IsZero() {
stopTime = startTime
}
if startTime.Equal(stopTime) {
// 如果文件名没有时间戳,则认为该文件是未录制完成的
// 尝试读取一下视频信息
duration, err := util.GetVideoDuration(subCtx, path.Join(l.StorageConfig.Path, dirPath, file.Name()))
if err != nil {
// 如果还是没有,就按照配置文件里的加起来
stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration))
} else {
stopTime = stopTime.Add(time.Second * time.Duration(duration))
}
}
fileList = append(fileList, dto.File{
BasePath: l.StorageConfig.Path,
Name: file.Name(),
Path: dirPath,
Url: path.Join(l.StorageConfig.Path, dirPath, file.Name()),
StartTime: startTime,
EndTime: stopTime,
})
sort.Slice(fileList, func(i, j int) bool {
return fileList[i].StartTime.Before(fileList[j].StartTime)
})
}
span.SetStatus(codes.Ok, "文件读取成功")
return fileList, nil
}