chore: stabilize lint and verify builds
This commit is contained in:
@@ -2,7 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -129,6 +129,7 @@ func (s *common) buildObjectKey(tenant *models.Tenant, hash, filename string) st
|
||||
tenantUUID = tenant.UUID.String()
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(filename))
|
||||
|
||||
return path.Join("quyun", tenantUUID, hash+ext)
|
||||
}
|
||||
|
||||
@@ -138,6 +139,7 @@ func (s *common) loadUploadMeta(tempDir string) (*UploadMeta, error) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, errorx.ErrRecordNotFound.WithCause(err).WithMsg("上传会话不存在")
|
||||
}
|
||||
|
||||
return nil, errorx.ErrInternalError.WithCause(err)
|
||||
}
|
||||
defer metaFile.Close()
|
||||
@@ -146,6 +148,7 @@ func (s *common) loadUploadMeta(tempDir string) (*UploadMeta, error) {
|
||||
if err := json.NewDecoder(metaFile).Decode(&meta); err != nil {
|
||||
return nil, errorx.ErrDataCorrupted.WithCause(err).WithMsg("上传会话元信息损坏")
|
||||
}
|
||||
|
||||
return &meta, nil
|
||||
}
|
||||
|
||||
@@ -154,6 +157,7 @@ func (s *common) verifyUploadOwner(meta *UploadMeta, tenantID, userID int64) err
|
||||
if meta.TenantID != tenantID || meta.UserID != userID {
|
||||
return errorx.ErrForbidden.WithMsg("无权访问该上传会话")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -165,8 +169,10 @@ func (s *common) resolveTenant(ctx context.Context, tenantID, userID int64) (*mo
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound.WithMsg("租户不存在")
|
||||
}
|
||||
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
return tenant, nil
|
||||
}
|
||||
if userID == 0 {
|
||||
@@ -178,8 +184,10 @@ func (s *common) resolveTenant(ctx context.Context, tenantID, userID int64) (*mo
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
return tenant, nil
|
||||
}
|
||||
|
||||
@@ -188,6 +196,7 @@ func (s *common) uploadTempDir(localPath string, tenantID int64, uploadID string
|
||||
if tenantID > 0 {
|
||||
tenantKey = strconv.FormatInt(tenantID, 10)
|
||||
}
|
||||
|
||||
return filepath.Join(localPath, "temp", tenantKey, uploadID)
|
||||
}
|
||||
|
||||
@@ -250,6 +259,7 @@ func (s *common) UploadPart(ctx context.Context, tenantID, userID int64, file *m
|
||||
if _, err = io.Copy(dst, src); err != nil {
|
||||
return errorx.ErrInternalError.WithCause(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -292,7 +302,7 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
hasher := md5.New()
|
||||
hasher := sha256.New()
|
||||
var totalSize int64
|
||||
|
||||
for _, partNum := range parts {
|
||||
@@ -341,6 +351,7 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
|
||||
myExisting, err := myQuery.First()
|
||||
if err == nil {
|
||||
os.RemoveAll(tempDir)
|
||||
|
||||
return s.composeUploadResult(myExisting), nil
|
||||
}
|
||||
asset = &models.MediaAsset{
|
||||
@@ -431,6 +442,7 @@ func (s *common) AbortUpload(ctx context.Context, tenantID, userID int64, upload
|
||||
if err := s.verifyUploadOwner(meta, tenantID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.RemoveAll(tempDir)
|
||||
}
|
||||
|
||||
@@ -452,7 +464,7 @@ func (s *common) Upload(
|
||||
|
||||
localPath := s.storage.Config.LocalPath
|
||||
if localPath == "" {
|
||||
localPath = "./storage" // Fallback
|
||||
localPath = "./storage"
|
||||
}
|
||||
tmpDir := filepath.Join(localPath, "temp", "uploads", uuid.NewString())
|
||||
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
|
||||
@@ -465,8 +477,7 @@ func (s *common) Upload(
|
||||
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to create destination file")
|
||||
}
|
||||
|
||||
// Hash calculation while copying
|
||||
hasher := md5.New()
|
||||
hasher := sha256.New()
|
||||
size, err := io.Copy(io.MultiWriter(dst, hasher), src)
|
||||
dst.Close() // Close immediately to allow removal if needed
|
||||
if err != nil {
|
||||
@@ -584,6 +595,7 @@ func (s *common) GetAssetURL(objectKey string) string {
|
||||
return ""
|
||||
}
|
||||
url, _ := s.storage.SignURL("GET", objectKey, 1*time.Hour)
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
@@ -591,6 +603,7 @@ func (s *common) initialMediaStatus(mediaType consts.MediaAssetType) consts.Medi
|
||||
if s.needsMediaProcess(mediaType) {
|
||||
return consts.MediaAssetStatusUploaded
|
||||
}
|
||||
|
||||
return consts.MediaAssetStatusReady
|
||||
}
|
||||
|
||||
@@ -616,6 +629,7 @@ func (s *common) enqueueMediaProcess(ctx context.Context, tenantID int64, asset
|
||||
if err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -626,6 +640,7 @@ func (s *common) enqueueMediaProcess(ctx context.Context, tenantID int64, asset
|
||||
if err := s.job.Add(arg); err != nil {
|
||||
return errorx.ErrInternalError.WithCause(err).WithMsg("添加媒体处理任务失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -656,8 +671,10 @@ func retryCriticalWrite(ctx context.Context, fn func() error) error {
|
||||
} else {
|
||||
time.Sleep(backoffs[attempt])
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -669,6 +686,7 @@ func shouldRetryWrite(err error) bool {
|
||||
if errors.As(err, &appErr) {
|
||||
return false
|
||||
}
|
||||
|
||||
return isTransientDBError(err)
|
||||
}
|
||||
|
||||
@@ -680,5 +698,6 @@ func isTransientDBError(err error) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user