Skip to content

Commit 0fd5dfc

Browse files
Update and repair the local scanning strategy for audio types in the media library
1 parent d0bd421 commit 0fd5dfc

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.idea
22
.VSCodeCounter
33
Ninesong/tmp
4+
NineSong/test.exe
5+
NineSong/test_build.exe
6+
NineSong/.all_in_ai/core/controller_spec.md

NineSong/api/controller/controller_file_entity/scene_audio_db_api_controller/file_entity_controller.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,53 @@ func (ctrl *FileController) ScanDirectory(c *gin.Context) {
2828
}
2929

3030
if err := c.ShouldBind(&req); err != nil {
31+
// 增加日志输出,记录无效请求的详细信息
32+
log.Printf("[扫描请求] 无效请求参数: %v", err)
33+
log.Printf("[扫描请求] 请求原始数据: %+v", c.Request.Form)
3134
controller.ErrorResponse(c, http.StatusBadRequest, "INVALID_REQUEST", "无效的请求格式: "+err.Error())
3235
return
3336
}
3437

38+
// 增加日志输出,记录有效的请求参数
39+
log.Printf("[扫描请求] 开始扫描 - FolderPath: '%s', FolderType: %d, ScanModel: %d",
40+
req.FolderPath, req.FolderType, req.ScanModel)
41+
3542
if req.FolderPath != "" {
3643
if _, err := os.Stat(req.FolderPath); err != nil {
3744
if os.IsNotExist(err) {
45+
log.Printf("[扫描请求] 指定的目录不存在: %s", req.FolderPath)
3846
controller.ErrorResponse(c, http.StatusBadRequest, "DIRECTORY_NOT_FOUND",
3947
fmt.Sprintf("指定的目录不存在: %s", req.FolderPath))
4048
return
4149
}
50+
log.Printf("[扫描请求] 无法访问目录 %s: %v", req.FolderPath, err)
4251
controller.ErrorResponse(c, http.StatusInternalServerError, "DIRECTORY_ACCESS_ERROR",
4352
fmt.Sprintf("无法访问目录: %s (%v)", req.FolderPath, err))
4453
return
4554
}
4655

4756
fileInfo, err := os.Stat(req.FolderPath)
4857
if err != nil {
58+
log.Printf("[扫描请求] 无法获取目录信息 %s: %v", req.FolderPath, err)
4959
controller.ErrorResponse(c, http.StatusInternalServerError, "DIRECTORY_STAT_ERROR",
5060
fmt.Sprintf("无法获取目录信息: %s (%v)", req.FolderPath, err))
5161
return
5262
}
5363
if !fileInfo.IsDir() {
64+
log.Printf("[扫描请求] 路径不是目录: %s", req.FolderPath)
5465
controller.ErrorResponse(c, http.StatusBadRequest, "NOT_A_DIRECTORY",
5566
fmt.Sprintf("路径不是目录: %s", req.FolderPath))
5667
return
5768
}
5869
}
5970

60-
if req.ScanModel == 0 || req.ScanModel == 3 {
61-
if len(req.FolderPath) == 0 {
62-
controller.ErrorResponse(c, http.StatusBadRequest, "INVALID_REQUEST", "扫描模式为新建或删除时,必须提供目录路径")
63-
return
64-
}
65-
}
71+
// 移除与修复逻辑相矛盾的验证 - 现在所有扫描模式都支持空FolderPath(默认扫描所有媒体库)
72+
// if req.ScanModel == 0 || req.ScanModel == 3 {
73+
// if len(req.FolderPath) == 0 {
74+
// controller.ErrorResponse(c, http.StatusBadRequest, "INVALID_REQUEST", "扫描模式为新建或删除时,必须提供目录路径")
75+
// return
76+
// }
77+
// }
6678

6779
var dirPaths []string
6880
if req.FolderPath != "" {

NineSong/repository/repository_file_entity/scene_audio/scene_audio_db_repository/repository_db_media_file.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -697,36 +697,41 @@ func (r *mediaFileRepository) GetByFolder(ctx context.Context, folderPath string
697697
func (r *mediaFileRepository) GetFilesWithMissingMetadata(ctx context.Context, folderPath string, folderType int) ([]*scene_audio_db_models.MediaFileMetadata, error) {
698698
coll := r.db.Collection(r.collection)
699699

700-
// 1. 获取数据库中所有文件的最新更新时间
700+
// 1. 获取特定媒体库文件夹下文件的最新更新时间
701701
var latestUpdatedAt time.Time
702-
// 按updated_at降序排序,获取第一条记录
702+
// 按updated_at降序排序,获取该文件夹下的第一条记录
703703
findOpts := options.Find().SetSort(bson.D{{"updated_at", -1}}).SetProjection(bson.D{{"updated_at", 1}}).SetLimit(1)
704+
705+
// 构建过滤条件,只查询指定文件夹下的文件
706+
filter := bson.M{
707+
"path": bson.M{
708+
"$regex": fmt.Sprintf("^%s", regexp.QuoteMeta(filepath.ToSlash(filepath.Clean(folderPath)))),
709+
"$options": "i",
710+
},
711+
}
712+
704713
var latestRecord struct {
705714
UpdatedAt time.Time `bson:"updated_at"`
706715
}
707-
cursor, err := coll.Find(ctx, bson.M{}, findOpts)
716+
cursor, err := coll.Find(ctx, filter, findOpts)
708717
if err != nil {
709-
if errors.Is(err, driver.ErrNoDocuments) {
710-
// 数据库中没有记录,返回空列表
711-
return []*scene_audio_db_models.MediaFileMetadata{}, nil
712-
} else {
713-
return nil, fmt.Errorf("查询最新更新时间失败: %w", err)
718+
return nil, fmt.Errorf("查询最新更新时间失败: %w", err)
719+
}
720+
721+
// 遍历游标获取第一条记录
722+
if cursor.Next(ctx) {
723+
if err := cursor.Decode(&latestRecord); err != nil {
724+
cursor.Close(ctx)
725+
return nil, fmt.Errorf("解码最新更新时间失败: %w", err)
714726
}
727+
latestUpdatedAt = latestRecord.UpdatedAt
715728
} else {
716-
// 遍历游标获取第一条记录
717-
if cursor.Next(ctx) {
718-
if err := cursor.Decode(&latestRecord); err != nil {
719-
return nil, fmt.Errorf("解码最新更新时间失败: %w", err)
720-
}
721-
latestUpdatedAt = latestRecord.UpdatedAt
722-
} else {
723-
// 数据库中没有记录,返回空列表
724-
return []*scene_audio_db_models.MediaFileMetadata{}, nil
725-
}
726-
cursor.Close(ctx)
729+
// 该文件夹下没有记录,设置为时间零值,这样所有文件都会被视为新文件
730+
latestUpdatedAt = time.Time{}
727731
}
732+
cursor.Close(ctx)
728733

729-
log.Printf("DEBUG - 数据库最新更新时间: %v", latestUpdatedAt)
734+
log.Printf("DEBUG - 文件夹 %s 最新更新时间: %v", folderPath, latestUpdatedAt)
730735

731736
// 2. 遍历文件夹,收集所有符合类型的文件及其修改时间
732737
fileModTimeMap := make(map[string]time.Time)
@@ -787,12 +792,12 @@ func (r *mediaFileRepository) GetFilesWithMissingMetadata(ctx context.Context, f
787792
}
788793

789794
// 查询数据库中这些文件的更新时间
790-
filter := bson.M{
795+
fileFilter := bson.M{
791796
"path": bson.M{"$in": filePaths},
792797
}
793-
findOpts = options.Find().SetProjection(bson.M{"path": 1, "updated_at": 1})
798+
findOpts2 := options.Find().SetProjection(bson.M{"path": 1, "updated_at": 1})
794799

795-
cursor, err = coll.Find(ctx, filter, findOpts)
800+
cursor, err = coll.Find(ctx, fileFilter, findOpts2)
796801
if err != nil {
797802
return nil, fmt.Errorf("查询文件更新时间失败: %w", err)
798803
}

NineSong/usecase/usecase_file_entity/file_entity_usecase_basic_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ func (uc *FileUsecase) ProcessDirectory(
440440
libraryFolders = append(libraryFolders, folder)
441441
}
442442
}
443-
if libraryFolders == nil && ScanModel == 2 {
444-
// 默认扫描所有媒体库目录,当传入路径为空且ScanModel为2时
443+
if (libraryFolders == nil || len(libraryFolders) == 0) && (ScanModel == 0 || ScanModel == 2) {
444+
// 默认扫描所有媒体库目录,当传入路径为空且ScanModel为0或2时
445445
library, err := uc.folderRepo.GetAllByType(ctx, 1)
446446
if err != nil {
447447
log.Printf("文件夹查询失败: %v", err)

0 commit comments

Comments
 (0)