Refactor file migration logic in CreatePost and EditPost to remove S3 verification; improve error handling for tmpfiles
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s

This commit is contained in:
2025-10-26 14:11:10 +08:00
parent 80e9b961b5
commit 57d8286607
2 changed files with 14 additions and 36 deletions

View File

@@ -9,7 +9,6 @@ import (
"git.cialloo.com/CiallooWeb/Blog/app/internal/types" "git.cialloo.com/CiallooWeb/Blog/app/internal/types"
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils" "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"
) )
@@ -99,28 +98,18 @@ func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string)
return 0, err return 0, err
} }
// Verify file exists in S3 bucket // Verify file exists in tmpfiles table
_, 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 var fileName string
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1` tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName) err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
if err != nil && err != sql.ErrNoRows { if err != nil {
if err == sql.ErrNoRows {
l.Errorf("File not found in tmpfiles with key: %s", fileKey)
return 0, fmt.Errorf("file not found")
}
l.Errorf("Failed to get tmpfile: %v", err) l.Errorf("Failed to get tmpfile: %v", err)
return 0, err return 0, err
} }
if err == sql.ErrNoRows {
// File not in tmpfiles, use file key as name
fileName = fileKey
}
// Insert into files table // Insert into files table
var fileID int64 var fileID int64
@@ -131,7 +120,7 @@ func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string)
return 0, err return 0, err
} }
// Delete from tmpfiles if exists // Delete from tmpfiles
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1` deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey) _, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
if err != nil { if err != nil {

View File

@@ -9,7 +9,6 @@ import (
"git.cialloo.com/CiallooWeb/Blog/app/internal/types" "git.cialloo.com/CiallooWeb/Blog/app/internal/types"
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils" "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"
) )
@@ -127,28 +126,18 @@ func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (i
return 0, err return 0, err
} }
// Verify file exists in S3 bucket // Verify file exists in tmpfiles table
_, 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 var fileName string
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1` tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName) err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
if err != nil && err != sql.ErrNoRows { if err != nil {
if err == sql.ErrNoRows {
l.Errorf("File not found in tmpfiles with key: %s", fileKey)
return 0, fmt.Errorf("file not found")
}
l.Errorf("Failed to get tmpfile: %v", err) l.Errorf("Failed to get tmpfile: %v", err)
return 0, err return 0, err
} }
if err == sql.ErrNoRows {
// File not in tmpfiles, use file key as name
fileName = fileKey
}
// Insert into files table // Insert into files table
var fileID int64 var fileID int64
@@ -159,7 +148,7 @@ func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (i
return 0, err return 0, err
} }
// Delete from tmpfiles if exists // Delete from tmpfiles
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1` deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey) _, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
if err != nil { if err != nil {