获取时直接传入相对时间,还是0点可能会出问题,使用path.join而不是直接拼接地址

This commit is contained in:
2025-02-16 14:43:52 +08:00
parent 1115bed7e2
commit 5b4d94e905
7 changed files with 46 additions and 24 deletions

View File

@ -3,10 +3,11 @@ package fs
import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto"
"time"
)
type Adapter interface {
GetFileList(path string) ([]dto.File, error)
GetFileList(path string, relDt time.Time) ([]dto.File, error)
}
func GetAdapter() Adapter {

View File

@ -6,6 +6,7 @@ import (
"ZhenTuLocalPassiveAdapter/util"
"fmt"
"os"
"path"
"sort"
"time"
)
@ -14,21 +15,12 @@ type LocalAdapter struct {
StorageConfig config.StorageConfig
}
func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]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)
files, err := os.ReadDir(path.Join(l.StorageConfig.Path, dirPath))
if err != nil {
return nil, err
}
@ -45,8 +37,6 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
if err != nil {
continue
}
// TODO: 0点左右会出问题
relDt := info.ModTime()
startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt)
if err != nil {
continue
@ -57,8 +47,8 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
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()),
Path: dirPath,
Url: path.Join(l.StorageConfig.Path, dirPath, file.Name()),
StartTime: startTime,
EndTime: stopTime,
})