Files
Blog/app/internal/utils/content.go
cialloo 7de60ad0c0
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m11s
Add archive file key extraction to CreatePost and EditPost logic; enhance file migration handling
2025-10-27 21:41:23 +08:00

136 lines
3.8 KiB
Go

package utils
import (
"encoding/json"
"strings"
)
// 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)
}
}
}
// 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{}
if err := json.Unmarshal([]byte(content), &contentData); err != nil {
return nil, err
}
var hashtags []string
hashtagSet := make(map[string]bool) // Use map to avoid duplicates
extractHashtags(contentData, hashtagSet)
// Convert map keys to slice
for tag := range hashtagSet {
hashtags = append(hashtags, tag)
}
return hashtags, nil
}
// extractHashtags recursively searches for hashtag nodes and collects their text values
func extractHashtags(data interface{}, hashtags map[string]bool) {
switch v := data.(type) {
case map[string]interface{}:
// Check if this is a hashtag node
if nodeType, ok := v["type"].(string); ok && nodeType == "hashtag" {
if text, ok := v["text"].(string); ok && text != "" {
// Remove the # prefix if present
tag := strings.TrimPrefix(text, "#")
if tag != "" {
hashtags[tag] = true
}
}
}
// Recurse into all map values
for _, value := range v {
extractHashtags(value, hashtags)
}
case []interface{}:
// Recurse into all array elements
for _, item := range v {
extractHashtags(item, hashtags)
}
}
}