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
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -99,28 +98,18 @@ func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string)
|
||||
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)
|
||||
// Verify file exists in tmpfiles table
|
||||
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 {
|
||||
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)
|
||||
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
|
||||
@@ -131,7 +120,7 @@ func (l *CreatePostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Delete from tmpfiles if exists
|
||||
// Delete from tmpfiles
|
||||
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
||||
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
||||
if err != nil {
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -127,28 +126,18 @@ func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (i
|
||||
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)
|
||||
// Verify file exists in tmpfiles table
|
||||
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 {
|
||||
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)
|
||||
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
|
||||
@@ -159,7 +148,7 @@ func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (i
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Delete from tmpfiles if exists
|
||||
// Delete from tmpfiles
|
||||
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
||||
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user