fix(canvas): keep uploaded resources in asset library (#388)
This commit is contained in:
1 parent
58defbc3b9
commit
16913ee72c
22 files changed
+1154
-34
No files matched your search
@@ -989,9 +989,17 @@ func (r *Repository) UserStoredFileBytes(userID string) (int64, error) {
|
||||
var total int64
|
||||
err := r.db.Raw(`
|
||||
SELECT
|
||||
(SELECT COALESCE(SUM(size), 0) FROM resources WHERE user_id = ?)
|
||||
COALESCE((
|
||||
SELECT SUM(physical_resources.size)
|
||||
FROM (
|
||||
SELECT MAX(size) AS size
|
||||
FROM resources
|
||||
WHERE user_id = ? AND status = ?
|
||||
GROUP BY COALESCE(NULLIF(provider, ''), 'local'), endpoint, bucket, object_key
|
||||
) AS physical_resources
|
||||
), 0)
|
||||
+ (SELECT COALESCE(SUM(size), 0) FROM session_files WHERE user_id = ?)
|
||||
`, userID, userID).Scan(&total).Error
|
||||
`, userID, model.ResourceStatusReady, userID).Scan(&total).Error
|
||||
return total, err
|
||||
}
|
||||
|
||||
@@ -1053,6 +1061,19 @@ func (r *Repository) Resources(userID string, limit int) ([]model.Resource, erro
|
||||
return resources, err
|
||||
}
|
||||
|
||||
func (r *Repository) ResourceCleanupCandidates(incompleteBefore time.Time, readyBefore time.Time, limit int) ([]model.Resource, error) {
|
||||
var resources []model.Resource
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
err := r.db.Where(
|
||||
"(status IN ? AND updated_at <= ?) OR (status = ? AND created_at <= ?)",
|
||||
[]model.ResourceStatus{model.ResourceStatusPending, model.ResourceStatusFailed}, incompleteBefore,
|
||||
model.ResourceStatusReady, readyBefore,
|
||||
).Order("created_at asc, id asc").Limit(limit).Find(&resources).Error
|
||||
return resources, err
|
||||
}
|
||||
|
||||
func (r *Repository) Assets(userID string) ([]model.Asset, error) {
|
||||
var assets []model.Asset
|
||||
err := r.db.Order("updated_at desc").Find(&assets, "user_id = ?", userID).Error
|
||||
@@ -1073,6 +1094,15 @@ func (r *Repository) AssetForUser(userID string, id string) (*model.Asset, error
|
||||
return &asset, nil
|
||||
}
|
||||
|
||||
func (r *Repository) AssetsForUserIDs(userID string, ids []string) ([]model.Asset, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var assets []model.Asset
|
||||
err := r.db.Find(&assets, "user_id = ? AND id IN ?", userID, ids).Error
|
||||
return assets, err
|
||||
}
|
||||
|
||||
func (r *Repository) UpsertAsset(asset *model.Asset) error {
|
||||
result := r.db.Model(&model.Asset{}).
|
||||
Where("id = ? AND user_id = ?", asset.ID, asset.UserID).
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
var ErrResourceCleanupSetChanged = errors.New("resource cleanup set changed")
|
||||
var ErrResourceCleanupStillReferenced = errors.New("resource cleanup resource is still directly referenced")
|
||||
|
||||
// DeleteDetachedResources removes Resource rows and enqueues physical deletion in one transaction.
|
||||
// JSON references are checked by the service before this call; direct foreign-key-like references
|
||||
// are checked again while the candidate rows are locked to avoid deleting a newly attached object.
|
||||
func (r *Repository) DeleteDetachedResources(resources []model.Resource, deletionJobs []model.ResourceDeletionJob) error {
|
||||
if len(resources) == 0 {
|
||||
return nil
|
||||
}
|
||||
resourceIDs := make([]string, 0, len(resources))
|
||||
for _, resource := range resources {
|
||||
resourceIDs = append(resourceIDs, resource.ID)
|
||||
}
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
var current []model.Resource
|
||||
query := tx.Where("id IN ?", resourceIDs)
|
||||
if r.Dialect() == "postgres" {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
if err := query.Find(¤t).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(current) != len(resources) {
|
||||
return ErrResourceCleanupSetChanged
|
||||
}
|
||||
// The service performs exact JSON parsing before entering this transaction.
|
||||
// This conservative second check closes the common race where an Asset or
|
||||
// canvas is attached after that snapshot but before Resource deletion.
|
||||
documentsByUser := map[string][]string{}
|
||||
for _, resource := range current {
|
||||
if _, loaded := documentsByUser[resource.UserID]; loaded {
|
||||
continue
|
||||
}
|
||||
var assetDocuments []string
|
||||
if err := tx.Model(&model.Asset{}).Where("user_id = ?", resource.UserID).Pluck("payload_json", &assetDocuments).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var canvasDocuments []string
|
||||
if err := tx.Model(&model.CanvasProject{}).Where("user_id = ?", resource.UserID).Pluck("payload_json", &canvasDocuments).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
documentsByUser[resource.UserID] = append(assetDocuments, canvasDocuments...)
|
||||
}
|
||||
for _, resource := range current {
|
||||
storageKey := "resource:" + resource.ID + `"`
|
||||
fileURL := "/api/resources/" + resource.ID + "/"
|
||||
for _, document := range documentsByUser[resource.UserID] {
|
||||
if strings.Contains(document, storageKey) || strings.Contains(document, fileURL) {
|
||||
return ErrResourceCleanupStillReferenced
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, check := range []struct {
|
||||
model any
|
||||
query string
|
||||
}{
|
||||
{&model.Announcement{}, "image_resource_id IN ?"},
|
||||
{&model.AnnouncementImageDraft{}, "resource_id IN ?"},
|
||||
{&model.AssetRepresentation{}, "resource_id IN ?"},
|
||||
{&model.VoiceProfile{}, "sample_resource_id IN ?"},
|
||||
{&model.ShotArtifact{}, "resource_id IN ?"},
|
||||
} {
|
||||
var count int64
|
||||
if err := tx.Model(check.model).Where(check.query, resourceIDs).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return ErrResourceCleanupStillReferenced
|
||||
}
|
||||
}
|
||||
if err := tx.Where("resource_id IN ?", resourceIDs).Delete(&model.ArkPrivateAssetBinding{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(deletionJobs) > 0 {
|
||||
if err := tx.Create(&deletionJobs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
deleted := tx.Where("id IN ?", resourceIDs).Delete(&model.Resource{})
|
||||
if deleted.Error != nil {
|
||||
return deleted.Error
|
||||
}
|
||||
if deleted.RowsAffected != int64(len(resourceIDs)) {
|
||||
return ErrResourceCleanupSetChanged
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -103,6 +103,46 @@ func (r *Repository) ResourceReferenceSnapshot(userID string, excludingAssetID s
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "画布", ID: canvas.ID, Title: canvas.Title, PrimaryJSON: canvas.PayloadJSON})
|
||||
}
|
||||
|
||||
var tasks []model.Task
|
||||
if err := r.db.Select("id", "prompt", "input_json", "result_json").Where("user_id = ?", userID).Find(&tasks).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, task := range tasks {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "任务", ID: task.ID, Title: task.Prompt, PrimaryJSON: task.InputJSON, SecondaryJSON: task.ResultJSON})
|
||||
}
|
||||
|
||||
var sessions []model.Session
|
||||
if err := r.db.Select("id", "prompt", "canvas_snapshot_json", "canvas_ops_json").Where("user_id = ?", userID).Find(&sessions).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, session := range sessions {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "会话", ID: session.ID, Title: session.Prompt, PrimaryJSON: session.CanvasSnapshotJSON, SecondaryJSON: session.CanvasOpsJSON})
|
||||
}
|
||||
|
||||
var messages []model.Message
|
||||
if err := r.db.Select("id", "content", "payload").Where("user_id = ?", userID).Find(&messages).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, message := range messages {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "会话消息", ID: message.ID, Title: message.Content, PrimaryJSON: message.Payload})
|
||||
}
|
||||
|
||||
var taskLogs []model.TaskLog
|
||||
if err := r.db.Select("id", "message", "payload").Where("user_id = ?", userID).Find(&taskLogs).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, taskLog := range taskLogs {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "任务日志", ID: taskLog.ID, Title: taskLog.Message, PrimaryJSON: taskLog.Payload})
|
||||
}
|
||||
|
||||
var results []model.Result
|
||||
if err := r.db.Select("id", "kind", "url", "payload").Where("user_id = ?", userID).Find(&results).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, result := range results {
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "任务结果", ID: result.ID, Title: result.Kind, PrimaryJSON: result.URL, SecondaryJSON: result.Payload})
|
||||
}
|
||||
|
||||
var projects []model.Project
|
||||
if err := r.db.Where("user_id = ?", userID).Find(&projects).Error; err != nil {
|
||||
return snapshot, err
|
||||
@@ -221,6 +261,22 @@ func (r *Repository) ResourceReferenceSnapshot(userID string, excludingAssetID s
|
||||
for _, artifact := range artifacts {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "镜头产物", ID: artifact.ID, Title: artifact.Title, ResourceID: artifact.ResourceID})
|
||||
}
|
||||
|
||||
var announcements []model.Announcement
|
||||
if err := r.db.Where("created_by = ? AND image_resource_id IN ?", userID, resourceIDs).Find(&announcements).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, announcement := range announcements {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "公告", ID: announcement.ID, Title: announcement.Title, ResourceID: announcement.ImageResourceID})
|
||||
}
|
||||
|
||||
var announcementDrafts []model.AnnouncementImageDraft
|
||||
if err := r.db.Where("user_id = ? AND resource_id IN ?", userID, resourceIDs).Find(&announcementDrafts).Error; err != nil {
|
||||
return snapshot, err
|
||||
}
|
||||
for _, draft := range announcementDrafts {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "公告草稿", ID: draft.ResourceID, ResourceID: draft.ResourceID})
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user