mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-06 23:56:37 +08:00
fix(pages): 收紧部署包与 Agent 同步边界
完成 V2 Phase 0 安全与一致性前置:统一真实归档限额、流式拉取、候选裁剪、保留上传删除语义及 Pages 路由引用锁。
This commit is contained in:
@@ -7,52 +7,76 @@ import (
|
||||
"context"
|
||||
|
||||
uploadcache "github.com/Rain-kl/Wavelet/internal/apps/upload/cache"
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/upload/shared"
|
||||
uploadstats "github.com/Rain-kl/Wavelet/internal/apps/upload/stats"
|
||||
"github.com/Rain-kl/Wavelet/internal/db"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/repository"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// Remove soft-deletes an upload and decrements incremental stats.
|
||||
// Remove soft-deletes an ordinary upload and decrements incremental stats once.
|
||||
func Remove(ctx context.Context, uploadID uint64) (model.Upload, error) {
|
||||
upload, err := repository.GetActiveUploadByID(ctx, uploadID)
|
||||
upload, err := remove(ctx, 0, uploadID, false)
|
||||
if err != nil {
|
||||
return model.Upload{}, err
|
||||
}
|
||||
if err := softDeleteUploadWithStats(ctx, &upload); err != nil {
|
||||
return model.Upload{}, err
|
||||
}
|
||||
upload.Status = model.UploadStatusDeleted
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
// RemoveOwned soft-deletes an upload owned by userID and decrements incremental stats.
|
||||
// RemoveOwned soft-deletes an ordinary upload owned by userID and decrements incremental stats once.
|
||||
func RemoveOwned(ctx context.Context, userID, uploadID uint64) (model.Upload, error) {
|
||||
upload, err := repository.GetActiveUploadByID(ctx, uploadID)
|
||||
upload, err := remove(ctx, userID, uploadID, true)
|
||||
if err != nil {
|
||||
return model.Upload{}, err
|
||||
}
|
||||
if upload.UserID != userID {
|
||||
return model.Upload{}, ErrForbidden
|
||||
}
|
||||
if err := softDeleteUploadWithStats(ctx, &upload); err != nil {
|
||||
return model.Upload{}, err
|
||||
}
|
||||
upload.Status = model.UploadStatusDeleted
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
func softDeleteUploadWithStats(ctx context.Context, upload *model.Upload) error {
|
||||
statsSnapshot := *upload
|
||||
func remove(ctx context.Context, userID, uploadID uint64, owned bool) (model.Upload, error) {
|
||||
var upload model.Upload
|
||||
if err := db.DB(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := repository.SoftDeleteUploadTx(tx, upload); err != nil {
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("id = ?", uploadID).
|
||||
First(&upload).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return uploadstats.ApplyUploadStatsDeltaTx(tx, &statsSnapshot, -1)
|
||||
}); err != nil {
|
||||
if owned && upload.UserID != userID {
|
||||
return ErrForbidden
|
||||
}
|
||||
if upload.Type == shared.ReservedPagesDeploymentType {
|
||||
return ErrReservedUploadType
|
||||
}
|
||||
_, err := RemoveLockedTx(tx, &upload)
|
||||
return err
|
||||
}); err != nil {
|
||||
return model.Upload{}, err
|
||||
}
|
||||
uploadcache.InvalidateUploadMetaCache(ctx, upload.ID)
|
||||
return nil
|
||||
|
||||
InvalidateUploadMetaCache(ctx, uploadID)
|
||||
upload.Status = model.UploadStatusDeleted
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
// RemoveLockedTx performs the idempotent active-to-deleted transition for a row
|
||||
// that the caller has already locked in its surrounding transaction.
|
||||
func RemoveLockedTx(tx *gorm.DB, upload *model.Upload) (bool, error) {
|
||||
rowsAffected, err := repository.SoftDeleteUploadTx(tx, upload)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if err := uploadstats.ApplyUploadStatsDeltaTx(tx, upload, -1); err != nil {
|
||||
return false, err
|
||||
}
|
||||
upload.Status = model.UploadStatusDeleted
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// InvalidateUploadMetaCache invalidates upload metadata after the caller commits its transaction.
|
||||
func InvalidateUploadMetaCache(ctx context.Context, uploadID uint64) {
|
||||
uploadcache.InvalidateUploadMetaCache(ctx, uploadID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user