Refactor file key extraction in CreatePost and EditPost logic to handle multiple file types; enhance error logging and simplify utility functions
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s

This commit is contained in:
2025-10-29 08:13:08 +08:00
parent 797638e67a
commit cef65deb23
3 changed files with 48 additions and 90 deletions

View File

@@ -5,91 +5,69 @@ import (
"strings"
)
// ExtractImageKeysFromContent parses the rich text JSON and extracts all image fileKey values
// ExtractImageKeysFromContent parses content and returns all image file keys.
func ExtractImageKeysFromContent(content string) ([]string, error) {
var contentData map[string]interface{}
if err := json.Unmarshal([]byte(content), &contentData); err != nil {
return nil, err
}
var imageKeys []string
extractImageKeys(contentData, &imageKeys)
return imageKeys, nil
return extractFileKeysByTypes(content, "image")
}
// extractImageKeys recursively searches for image nodes and collects their fileKey values
func extractImageKeys(data interface{}, keys *[]string) {
switch v := data.(type) {
case map[string]interface{}:
// Check if this is an image node
if nodeType, ok := v["type"].(string); ok && nodeType == "image" {
if fileKey, ok := v["fileKey"].(string); ok && fileKey != "" {
*keys = append(*keys, fileKey)
}
}
// Recurse into all map values
for _, value := range v {
extractImageKeys(value, keys)
}
case []interface{}:
// Recurse into all array elements
for _, item := range v {
extractImageKeys(item, keys)
}
}
}
// ExtractArchiveKeysFromContent parses the rich text JSON and extracts all archive fileKey values
// ExtractArchiveKeysFromContent parses content and returns all archive file keys.
func ExtractArchiveKeysFromContent(content string) ([]string, error) {
return extractFileKeysByTypes(content, "archive")
}
// ExtractAudioKeysFromContent parses content and returns all audio file keys.
func ExtractAudioKeysFromContent(content string) ([]string, error) {
return extractFileKeysByTypes(content, "audio")
}
// ExtractVideoKeysFromContent parses content and returns all video file keys.
func ExtractVideoKeysFromContent(content string) ([]string, error) {
return extractFileKeysByTypes(content, "video")
}
// ExtractAllFileKeysFromContent returns every supported file key (image, archive, audio, video).
func ExtractAllFileKeysFromContent(content string) ([]string, error) {
return extractFileKeysByTypes(content, "image", "archive", "audio", "video")
}
// extractFileKeysByTypes walks the rich text JSON and collects file keys that match allowed node types.
func extractFileKeysByTypes(content string, nodeTypes ...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
typeSet := make(map[string]struct{}, len(nodeTypes))
for _, nodeType := range nodeTypes {
typeSet[nodeType] = struct{}{}
}
var keys []string
extractFileKeys(contentData, typeSet, &keys)
return keys, nil
}
// extractArchiveKeys recursively searches for archive nodes and collects their fileKey values
func extractArchiveKeys(data interface{}, keys *[]string) {
// extractFileKeys recursively searches for nodes whose type appears in typeSet and collects fileKey values.
func extractFileKeys(data interface{}, typeSet map[string]struct{}, 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)
if nodeType, ok := v["type"].(string); ok {
if _, allowed := typeSet[nodeType]; allowed {
if fileKey, ok := v["fileKey"].(string); ok && fileKey != "" {
*keys = append(*keys, fileKey)
}
}
}
// Recurse into all map values
for _, value := range v {
extractArchiveKeys(value, keys)
extractFileKeys(value, typeSet, keys)
}
case []interface{}:
// Recurse into all array elements
for _, item := range v {
extractArchiveKeys(item, keys)
extractFileKeys(item, typeSet, 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{}