diff --git a/backend/app/services/common.go b/backend/app/services/common.go index 7f0c567..2f03ee6 100644 --- a/backend/app/services/common.go +++ b/backend/app/services/common.go @@ -51,25 +51,6 @@ func (s *common) Options(ctx context.Context) (*common_dto.OptionsResponse, erro } func (s *common) CheckHash(ctx context.Context, tenantID, userID int64, hash string) (*common_dto.UploadResult, error) { - existing, err := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)).First() - if err != nil { - return nil, nil // Not found, proceed to upload - } - - // Found existing file (Global deduplication hit) - - // Check if user already has it (Logic deduplication hit) - myQuery := models.MediaAssetQuery.WithContext(ctx). - Where(models.MediaAssetQuery.Hash.Eq(hash), models.MediaAssetQuery.UserID.Eq(userID)) - if tenantID > 0 { - myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tenantID)) - } - myExisting, err := myQuery.First() - if err == nil { - return s.composeUploadResult(myExisting), nil - } - - // Create new record for this user reusing existing ObjectKey // 优先使用路径租户,避免跨租户写入。 tenant, err := s.resolveTenant(ctx, tenantID, userID) if err != nil { @@ -80,6 +61,29 @@ func (s *common) CheckHash(ctx context.Context, tenantID, userID int64, hash str tid = tenant.ID } + query := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)) + if tid > 0 { + query = query.Where(models.MediaAssetQuery.TenantID.Eq(tid)) + } + existing, err := query.First() + if err != nil { + return nil, nil // Not found, proceed to upload + } + + // Found existing file (deduplication hit) + + // Check if user already has it (Logic deduplication hit) + myQuery := models.MediaAssetQuery.WithContext(ctx). + Where(models.MediaAssetQuery.Hash.Eq(hash), models.MediaAssetQuery.UserID.Eq(userID)) + if tid > 0 { + myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tid)) + } + myExisting, err := myQuery.First() + if err == nil { + return s.composeUploadResult(myExisting), nil + } + + // Create new record for this user reusing existing ObjectKey asset := &models.MediaAsset{ TenantID: tid, UserID: userID, @@ -272,15 +276,19 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for } objectKey := s.buildObjectKey(tenant, hash, meta.Filename) - existing, err := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)).First() + existingQuery := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)) + if tid > 0 { + existingQuery = existingQuery.Where(models.MediaAssetQuery.TenantID.Eq(tid)) + } + existing, err := existingQuery.First() var asset *models.MediaAsset if err == nil { os.Remove(mergedPath) // Delete duplicate myQuery := models.MediaAssetQuery.WithContext(ctx). Where(models.MediaAssetQuery.Hash.Eq(hash), models.MediaAssetQuery.UserID.Eq(userID)) - if tenantID > 0 { - myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tenantID)) + if tid > 0 { + myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tid)) } myExisting, err := myQuery.First() if err == nil { @@ -422,7 +430,11 @@ func (s *common) Upload( var asset *models.MediaAsset // Deduplication Check - existing, err := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)).First() + existingQuery := models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.Hash.Eq(hash)) + if tid > 0 { + existingQuery = existingQuery.Where(models.MediaAssetQuery.TenantID.Eq(tid)) + } + existing, err := existingQuery.First() if err == nil { // Found existing file (Storage Deduplication) os.Remove(tmpPath) // Delete the duplicate we just wrote @@ -431,8 +443,8 @@ func (s *common) Upload( // Check if user already has it (Logic Deduplication) myQuery := models.MediaAssetQuery.WithContext(ctx). Where(models.MediaAssetQuery.Hash.Eq(hash), models.MediaAssetQuery.UserID.Eq(userID)) - if tenantID > 0 { - myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tenantID)) + if tid > 0 { + myQuery = myQuery.Where(models.MediaAssetQuery.TenantID.Eq(tid)) } myExisting, err := myQuery.First() if err == nil {