All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
114 lines
3.5 KiB
Go
114 lines
3.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// ExtractImageKeysFromContent parses content and returns all image file keys.
|
|
func ExtractImageKeysFromContent(content string) ([]string, error) {
|
|
return extractFileKeysByTypes(content, "image")
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
typeSet := make(map[string]struct{}, len(nodeTypes))
|
|
for _, nodeType := range nodeTypes {
|
|
typeSet[nodeType] = struct{}{}
|
|
}
|
|
|
|
var keys []string
|
|
extractFileKeys(contentData, typeSet, &keys)
|
|
return keys, nil
|
|
}
|
|
|
|
// 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{}:
|
|
if nodeType, ok := v["type"].(string); ok {
|
|
if _, allowed := typeSet[nodeType]; allowed {
|
|
if fileKey, ok := v["fileKey"].(string); ok && fileKey != "" {
|
|
*keys = append(*keys, fileKey)
|
|
}
|
|
}
|
|
}
|
|
for _, value := range v {
|
|
extractFileKeys(value, typeSet, keys)
|
|
}
|
|
case []interface{}:
|
|
for _, item := range v {
|
|
extractFileKeys(item, typeSet, keys)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
}
|