Refactor CreatePost and EditPost logic to migrate images from tmpfiles to files; add utility function to extract image keys from content
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
This commit is contained in:
39
app/internal/utils/content.go
Normal file
39
app/internal/utils/content.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// ExtractImageKeysFromContent parses the rich text JSON and extracts all image fileKey values
|
||||
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
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user