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:
@@ -7,7 +7,9 @@ import (
|
|||||||
|
|
||||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||||
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,78 +29,115 @@ func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, err error) {
|
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, err error) {
|
||||||
var coverID sql.NullInt64
|
// Start a transaction
|
||||||
|
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to begin transaction: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
// If cover image key is provided, move it from tmpfiles to files table
|
// Extract all image file keys from content
|
||||||
if req.CoverImageKey != "" {
|
imageKeys, err := utils.ExtractImageKeysFromContent(req.Content)
|
||||||
// Start a transaction
|
if err != nil {
|
||||||
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
l.Errorf("Failed to parse content: %v", err)
|
||||||
if err != nil {
|
return nil, fmt.Errorf("invalid content format")
|
||||||
l.Errorf("Failed to begin transaction: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer tx.Rollback()
|
|
||||||
|
|
||||||
// Get the tmpfile record
|
|
||||||
var fileName string
|
|
||||||
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
|
||||||
err = tx.QueryRowContext(l.ctx, tmpQuery, req.CoverImageKey).Scan(&fileName)
|
|
||||||
if err != nil {
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
l.Errorf("Cover image not found in tmpfiles with key: %s", req.CoverImageKey)
|
|
||||||
return nil, fmt.Errorf("cover image not found")
|
|
||||||
}
|
|
||||||
l.Errorf("Failed to get tmpfile: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert into files table
|
|
||||||
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
|
||||||
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, req.CoverImageKey).Scan(&coverID.Int64)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to insert file record: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
coverID.Valid = true
|
|
||||||
|
|
||||||
// Delete from tmpfiles
|
|
||||||
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
|
||||||
_, err = tx.ExecContext(l.ctx, deleteQuery, req.CoverImageKey)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to delete tmpfile: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert new post
|
|
||||||
var postID int64
|
|
||||||
postQuery := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id`
|
|
||||||
err = tx.QueryRowContext(l.ctx, postQuery, req.Title, req.Content, coverID).Scan(&postID)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to create post: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit transaction
|
|
||||||
if err = tx.Commit(); err != nil {
|
|
||||||
l.Errorf("Failed to commit transaction: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &types.CreatePostResp{
|
|
||||||
PostId: fmt.Sprintf("%d", postID),
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No cover image - just insert the post
|
// Migrate cover image from tmpfiles to files if provided
|
||||||
|
var coverID sql.NullInt64
|
||||||
|
if req.CoverImageKey != "" {
|
||||||
|
fileID, err := l.migrateFileFromTmpToFiles(tx, req.CoverImageKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
coverID.Int64 = fileID
|
||||||
|
coverID.Valid = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate all content images from tmpfiles to files
|
||||||
|
for _, imageKey := range imageKeys {
|
||||||
|
_, err := l.migrateFileFromTmpToFiles(tx, imageKey)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to migrate content image %s: %v", imageKey, err)
|
||||||
|
// Continue with other images, don't fail the whole operation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert new post
|
||||||
var postID int64
|
var postID int64
|
||||||
query := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id`
|
postQuery := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id`
|
||||||
err = l.svcCtx.DB.QueryRowContext(l.ctx, query, req.Title, req.Content, coverID).Scan(&postID)
|
err = tx.QueryRowContext(l.ctx, postQuery, req.Title, req.Content, coverID).Scan(&postID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorf("Failed to create post: %v", err)
|
l.Errorf("Failed to create post: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
l.Errorf("Failed to commit transaction: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &types.CreatePostResp{
|
return &types.CreatePostResp{
|
||||||
PostId: fmt.Sprintf("%d", postID),
|
PostId: fmt.Sprintf("%d", postID),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// migrateFileFromTmpToFiles moves a file from tmpfiles to files table
|
||||||
|
func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (int64, error) {
|
||||||
|
// Check if file already exists in files table
|
||||||
|
var existingID int64
|
||||||
|
checkQuery := `SELECT id FROM files WHERE file_key = $1`
|
||||||
|
err := tx.QueryRowContext(l.ctx, checkQuery, fileKey).Scan(&existingID)
|
||||||
|
if err == nil {
|
||||||
|
// File already exists, return its ID
|
||||||
|
return existingID, nil
|
||||||
|
}
|
||||||
|
if err != sql.ErrNoRows {
|
||||||
|
l.Errorf("Failed to check existing file: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify file exists in S3 bucket
|
||||||
|
_, err = l.svcCtx.S3Client.HeadObject(l.ctx, &s3.HeadObjectInput{
|
||||||
|
Bucket: &l.svcCtx.Config.S3.Bucket,
|
||||||
|
Key: &fileKey,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("File not found in S3 bucket: %s, error: %v", fileKey, err)
|
||||||
|
return 0, fmt.Errorf("file not found in storage")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file name from tmpfiles (if exists)
|
||||||
|
var fileName string
|
||||||
|
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
||||||
|
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
|
||||||
|
if err != nil && err != sql.ErrNoRows {
|
||||||
|
l.Errorf("Failed to get tmpfile: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// File not in tmpfiles, use file key as name
|
||||||
|
fileName = fileKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert into files table
|
||||||
|
var fileID int64
|
||||||
|
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
||||||
|
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, fileKey).Scan(&fileID)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to insert file record: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete from tmpfiles if exists
|
||||||
|
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
||||||
|
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to delete tmpfile: %v", err)
|
||||||
|
// Don't fail the operation for this
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileID, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import (
|
|||||||
|
|
||||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||||
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,62 +29,45 @@ func NewEditPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditPost
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostResp, err error) {
|
func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostResp, err error) {
|
||||||
|
// Start a transaction
|
||||||
|
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to begin transaction: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
// Check if post exists and get old cover_id
|
||||||
|
var oldCoverID sql.NullInt64
|
||||||
|
checkQuery := `SELECT cover_id FROM posts WHERE id = $1`
|
||||||
|
err = tx.QueryRowContext(l.ctx, checkQuery, req.PostId).Scan(&oldCoverID)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
l.Errorf("Post not found with id: %s", req.PostId)
|
||||||
|
return nil, fmt.Errorf("post not found")
|
||||||
|
}
|
||||||
|
l.Errorf("Failed to get post: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract all image file keys from content
|
||||||
|
imageKeys, err := utils.ExtractImageKeysFromContent(req.Content)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to parse content: %v", err)
|
||||||
|
return nil, fmt.Errorf("invalid content format")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate new cover image from tmpfiles to files if provided
|
||||||
var coverID sql.NullInt64
|
var coverID sql.NullInt64
|
||||||
|
|
||||||
// If cover image key is provided, move it from tmpfiles to files table
|
|
||||||
if req.CoverImageKey != "" {
|
if req.CoverImageKey != "" {
|
||||||
// Start a transaction
|
fileID, err := l.migrateFileFromTmpToFiles(tx, req.CoverImageKey)
|
||||||
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorf("Failed to begin transaction: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer tx.Rollback()
|
|
||||||
|
|
||||||
// Get the tmpfile record
|
|
||||||
var fileName string
|
|
||||||
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
|
||||||
err = tx.QueryRowContext(l.ctx, tmpQuery, req.CoverImageKey).Scan(&fileName)
|
|
||||||
if err != nil {
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
l.Errorf("Cover image not found in tmpfiles with key: %s", req.CoverImageKey)
|
|
||||||
return nil, fmt.Errorf("cover image not found")
|
|
||||||
}
|
|
||||||
l.Errorf("Failed to get tmpfile: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get old cover_id to delete later
|
|
||||||
var oldCoverID sql.NullInt64
|
|
||||||
oldCoverQuery := `SELECT cover_id FROM posts WHERE id = $1`
|
|
||||||
err = tx.QueryRowContext(l.ctx, oldCoverQuery, req.PostId).Scan(&oldCoverID)
|
|
||||||
if err != nil {
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
l.Errorf("Post not found with id: %s", req.PostId)
|
|
||||||
return nil, fmt.Errorf("post not found")
|
|
||||||
}
|
|
||||||
l.Errorf("Failed to get old cover: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert into files table
|
|
||||||
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
|
||||||
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, req.CoverImageKey).Scan(&coverID.Int64)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to insert file record: %v", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
coverID.Int64 = fileID
|
||||||
coverID.Valid = true
|
coverID.Valid = true
|
||||||
|
|
||||||
// Delete from tmpfiles
|
// Update post with new cover
|
||||||
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
|
||||||
_, err = tx.ExecContext(l.ctx, deleteQuery, req.CoverImageKey)
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to delete tmpfile: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update post
|
|
||||||
updateQuery := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4`
|
updateQuery := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4`
|
||||||
_, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, coverID, req.PostId)
|
_, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, coverID, req.PostId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -90,8 +75,8 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old cover file if exists
|
// Delete old cover file if exists and different from new one
|
||||||
if oldCoverID.Valid {
|
if oldCoverID.Valid && oldCoverID.Int64 != coverID.Int64 {
|
||||||
deleteOldFileQuery := `DELETE FROM files WHERE id = $1`
|
deleteOldFileQuery := `DELETE FROM files WHERE id = $1`
|
||||||
_, err = tx.ExecContext(l.ctx, deleteOldFileQuery, oldCoverID.Int64)
|
_, err = tx.ExecContext(l.ctx, deleteOldFileQuery, oldCoverID.Int64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -99,34 +84,88 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe
|
|||||||
// Don't fail the whole operation for this
|
// Don't fail the whole operation for this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Commit transaction
|
// No new cover image - just update the post
|
||||||
if err = tx.Commit(); err != nil {
|
updateQuery := `UPDATE posts SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3`
|
||||||
l.Errorf("Failed to commit transaction: %v", err)
|
_, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, req.PostId)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to update post: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.EditPostResp{}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// No new cover image - just update the post
|
// Migrate all content images from tmpfiles to files
|
||||||
query := `UPDATE posts SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3`
|
for _, imageKey := range imageKeys {
|
||||||
result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Title, req.Content, req.PostId)
|
_, err := l.migrateFileFromTmpToFiles(tx, imageKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorf("Failed to update post: %v", err)
|
l.Errorf("Failed to migrate content image %s: %v", imageKey, err)
|
||||||
|
// Continue with other images, don't fail the whole operation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
l.Errorf("Failed to commit transaction: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if post exists
|
|
||||||
rowsAffected, err := result.RowsAffected()
|
|
||||||
if err != nil {
|
|
||||||
l.Errorf("Failed to get rows affected: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
l.Errorf("Post not found with id: %s", req.PostId)
|
|
||||||
return nil, fmt.Errorf("post not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &types.EditPostResp{}, nil
|
return &types.EditPostResp{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// migrateFileFromTmpToFiles moves a file from tmpfiles to files table
|
||||||
|
func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (int64, error) {
|
||||||
|
// Check if file already exists in files table
|
||||||
|
var existingID int64
|
||||||
|
checkQuery := `SELECT id FROM files WHERE file_key = $1`
|
||||||
|
err := tx.QueryRowContext(l.ctx, checkQuery, fileKey).Scan(&existingID)
|
||||||
|
if err == nil {
|
||||||
|
// File already exists, return its ID
|
||||||
|
return existingID, nil
|
||||||
|
}
|
||||||
|
if err != sql.ErrNoRows {
|
||||||
|
l.Errorf("Failed to check existing file: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify file exists in S3 bucket
|
||||||
|
_, err = l.svcCtx.S3Client.HeadObject(l.ctx, &s3.HeadObjectInput{
|
||||||
|
Bucket: &l.svcCtx.Config.S3.Bucket,
|
||||||
|
Key: &fileKey,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("File not found in S3 bucket: %s, error: %v", fileKey, err)
|
||||||
|
return 0, fmt.Errorf("file not found in storage")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file name from tmpfiles (if exists)
|
||||||
|
var fileName string
|
||||||
|
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
||||||
|
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
|
||||||
|
if err != nil && err != sql.ErrNoRows {
|
||||||
|
l.Errorf("Failed to get tmpfile: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
// File not in tmpfiles, use file key as name
|
||||||
|
fileName = fileKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert into files table
|
||||||
|
var fileID int64
|
||||||
|
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
||||||
|
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, fileKey).Scan(&fileID)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to insert file record: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete from tmpfiles if exists
|
||||||
|
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
||||||
|
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
||||||
|
if err != nil {
|
||||||
|
l.Errorf("Failed to delete tmpfile: %v", err)
|
||||||
|
// Don't fail the operation for this
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileID, nil
|
||||||
|
}
|
||||||
|
|||||||
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