优化目录扫描逻辑

This commit is contained in:
fantasticbin 2025-03-04 14:17:30 +08:00
parent 51f84bea08
commit 91d751df7d

View File

@ -79,7 +79,7 @@ func (c *Crawler) getCodeNum(s string) int {
}
// 获取封面代码列表
func (c *Crawler) getCoverCodeList(files []string) (coverList []string, err error) {
func (c *Crawler) getCoverCodeList(files []string) (coverList []string) {
for _, file := range files {
// 去除域名部分
if strings.IndexRune(file, '@') > 0 {
@ -103,7 +103,7 @@ func (c *Crawler) getCoverCodeList(files []string) (coverList []string, err erro
coverList = append(coverList, fmt.Sprintf(format, strings.ToLower(nameSlice[0]), num))
}
return coverList, nil
return coverList
}
// 获取封面图片
@ -181,38 +181,34 @@ func (c *Crawler) Handle() error {
if err := filepath.Walk(c.avPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
return fmt.Errorf("访问路径 %s 失败: %w", path, err)
}
// 仅处理视频文件
if c.isVideoFile(info.Name()) {
// 获取文件大小和修改时间
fileSize, modTime, err := c.getFileInfo(path)
if err != nil {
return err
}
// 根据文件的大小和修改时间生成唯一的文件标识
uniqueID := fmt.Sprintf("%d-%s", fileSize, modTime)
if _, exists := uniqueFiles[uniqueID]; !exists {
uniqueFiles[uniqueID] = struct{}{}
if !c.isVideoFile(info.Name()) {
return nil
}
fileName := info.Name()
extIndex := strings.LastIndex(info.Name(), ".")
if extIndex != -1 {
fileName = fileName[:extIndex] // 去除扩展名
}
baseName := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
videoFiles = append(videoFiles, fileName)
}
// 获取文件大小和修改时间
fileSize, modTime, err := c.getFileInfo(path)
if err != nil {
return fmt.Errorf("获取文件信息失败 %s: %w", baseName, err)
}
// 根据文件的大小和修改时间生成唯一的文件标识
uniqueID := fmt.Sprintf("%d-%s", fileSize, modTime)
if _, exists := uniqueFiles[uniqueID]; !exists {
uniqueFiles[uniqueID] = struct{}{}
videoFiles = append(videoFiles, baseName)
}
return nil
}); err != nil {
return err
return fmt.Errorf("目录遍历失败: %w", err)
}
coverList, err := c.getCoverCodeList(videoFiles)
if err != nil {
return err
}
coverList := c.getCoverCodeList(videoFiles)
var g errgroup.Group
for _, cover := range coverList {