From 7de60ad0c089ab07170cb72559087a3239d4f428 Mon Sep 17 00:00:00 2001 From: cialloo Date: Mon, 27 Oct 2025 21:41:23 +0800 Subject: [PATCH] Add archive file key extraction to CreatePost and EditPost logic; enhance file migration handling --- app/internal/logic/createpostlogic.go | 20 ++++++++--- app/internal/logic/editpostlogic.go | 20 ++++++++--- app/internal/utils/content.go | 51 +++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/app/internal/logic/createpostlogic.go b/app/internal/logic/createpostlogic.go index dac1b61..00b5b79 100644 --- a/app/internal/logic/createpostlogic.go +++ b/app/internal/logic/createpostlogic.go @@ -43,6 +43,16 @@ func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.Crea return nil, fmt.Errorf("invalid content format") } + // Extract all archive file keys from content + archiveKeys, err := utils.ExtractArchiveKeysFromContent(req.Content) + if err != nil { + l.Errorf("Failed to parse archive files from content: %v", err) + return nil, fmt.Errorf("invalid content format") + } + + // Combine all file keys + allFileKeys := append(imageKeys, archiveKeys...) + // Extract all hashtags from content hashtags, err := utils.ExtractHashtagsFromContent(req.Content) if err != nil { @@ -61,12 +71,12 @@ func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.Crea coverID.Valid = true } - // Migrate all content images from tmpfiles to files - for _, imageKey := range imageKeys { - _, err := l.migrateFileFromTmpToFiles(tx, imageKey) + // Migrate all content files (images and archives) from tmpfiles to files + for _, fileKey := range allFileKeys { + _, err := l.migrateFileFromTmpToFiles(tx, fileKey) if err != nil { - l.Errorf("Failed to migrate content image %s: %v", imageKey, err) - // Continue with other images, don't fail the whole operation + l.Errorf("Failed to migrate content file %s: %v", fileKey, err) + // Continue with other files, don't fail the whole operation } } diff --git a/app/internal/logic/editpostlogic.go b/app/internal/logic/editpostlogic.go index 037aada..f543f84 100644 --- a/app/internal/logic/editpostlogic.go +++ b/app/internal/logic/editpostlogic.go @@ -56,6 +56,16 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe return nil, fmt.Errorf("invalid content format") } + // Extract all archive file keys from content + archiveKeys, err := utils.ExtractArchiveKeysFromContent(req.Content) + if err != nil { + l.Errorf("Failed to parse archive files from content: %v", err) + return nil, fmt.Errorf("invalid content format") + } + + // Combine all file keys + allFileKeys := append(imageKeys, archiveKeys...) + // Extract all hashtags from content hashtags, err := utils.ExtractHashtagsFromContent(req.Content) if err != nil { @@ -100,12 +110,12 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe } } - // Migrate all content images from tmpfiles to files - for _, imageKey := range imageKeys { - _, err := l.migrateFileFromTmpToFiles(tx, imageKey) + // Migrate all content files (images and archives) from tmpfiles to files + for _, fileKey := range allFileKeys { + _, err := l.migrateFileFromTmpToFiles(tx, fileKey) if err != nil { - l.Errorf("Failed to migrate content image %s: %v", imageKey, err) - // Continue with other images, don't fail the whole operation + l.Errorf("Failed to migrate content file %s: %v", fileKey, err) + // Continue with other files, don't fail the whole operation } } diff --git a/app/internal/utils/content.go b/app/internal/utils/content.go index 2e7845d..d5d415f 100644 --- a/app/internal/utils/content.go +++ b/app/internal/utils/content.go @@ -39,6 +39,57 @@ func extractImageKeys(data interface{}, keys *[]string) { } } +// ExtractArchiveKeysFromContent parses the rich text JSON and extracts all archive fileKey values +func ExtractArchiveKeysFromContent(content string) ([]string, error) { + var contentData map[string]interface{} + if err := json.Unmarshal([]byte(content), &contentData); err != nil { + return nil, err + } + + var archiveKeys []string + extractArchiveKeys(contentData, &archiveKeys) + return archiveKeys, nil +} + +// extractArchiveKeys recursively searches for archive nodes and collects their fileKey values +func extractArchiveKeys(data interface{}, keys *[]string) { + switch v := data.(type) { + case map[string]interface{}: + // Check if this is an archive node + if nodeType, ok := v["type"].(string); ok && nodeType == "archive" { + if fileKey, ok := v["fileKey"].(string); ok && fileKey != "" { + *keys = append(*keys, fileKey) + } + } + // Recurse into all map values + for _, value := range v { + extractArchiveKeys(value, keys) + } + case []interface{}: + // Recurse into all array elements + for _, item := range v { + extractArchiveKeys(item, keys) + } + } +} + +// ExtractAllFileKeysFromContent extracts both image and archive file keys from content +func ExtractAllFileKeysFromContent(content string) ([]string, error) { + imageKeys, err := ExtractImageKeysFromContent(content) + if err != nil { + return nil, err + } + + archiveKeys, err := ExtractArchiveKeysFromContent(content) + if err != nil { + return nil, err + } + + // Combine both slices + allKeys := append(imageKeys, archiveKeys...) + return allKeys, nil +} + // ExtractHashtagsFromContent parses the rich text JSON and extracts all hashtag text values func ExtractHashtagsFromContent(content string) ([]string, error) { var contentData map[string]interface{}