Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/api/handlers/collection_artwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ func cleanUpReplacedCollectionImage(
}
}

// removeDeletedCollectionArtwork deletes a deleted collection's whole artwork
// directory: every image revision and collage. The row delete has committed,
// so the cleanup outlives a canceled request, bounded by
// collectionImageCleanupTimeout, and a failure only logs.
func removeDeletedCollectionArtwork(ctx context.Context, store blobstore.Store, prefix, collectionID string) {
if store == nil {
return
}
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), collectionImageCleanupTimeout)
defer cancel()
if _, err := store.DeletePrefix(ctx, fmt.Sprintf("%s/%s/", prefix, collectionID)); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Kody Rules high

Orphaned artwork can persist without a durable retry because best-effort deletion runs after the collection row deletion commits, leaving a failure window for crashes or storage errors; immediate deletion also ignores issued-URL protection deadlines. Enqueue the removed artwork references and their protection deadlines in the same database transaction as the collection deletion, then let a retirement worker delete them when eligible.

Kody rule violation: Retire displaced media revisions transactionally and preserve URL deadlines

Prompt for LLM

File internal/api/handlers/collection_artwork.go:

Line 372:

Orphaned artwork can persist without a durable retry because best-effort deletion runs after the collection row deletion commits, leaving a failure window for crashes or storage errors; immediate deletion also ignores issued-URL protection deadlines. Enqueue the removed artwork references and their protection deadlines in the same database transaction as the collection deletion, then let a retirement worker delete them when eligible.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

slog.WarnContext(ctx, "collection delete: removing artwork failed", "component", "api", "collection_id", collectionID, "prefix", prefix, "error", err)
}
}

// isCollectionImageKeyIn reports whether key is an object directly inside dir
// (which carries a trailing slash).
func isCollectionImageKeyIn(dir, key string) bool {
Expand Down
1 change: 1 addition & 0 deletions internal/api/handlers/collections_lifecycle_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func (h *CollectionHandler) DeletePersonalCollection(ctx context.Context, userID
}
return apiError(http.StatusInternalServerError, "internal_error", "Failed to delete collection")
}
removeDeletedCollectionArtwork(ctx, h.ArtworkStore, userCollectionImagePrefix, collectionID)

return nil
}
Expand Down
70 changes: 69 additions & 1 deletion internal/api/handlers/collections_lifecycle_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"testing"

"github.com/Silo-Server/silo-server/internal/blobstore/blobstoretest"
"github.com/Silo-Server/silo-server/internal/models"
"github.com/Silo-Server/silo-server/internal/usercollections"
"github.com/Silo-Server/silo-server/internal/userstore"
Expand All @@ -18,12 +20,16 @@ type lifecycleStore struct {
collection userstore.Collection
mutations int
update userstore.UpdateCollectionInput
deleteErr error
}

func (s *lifecycleStore) GetCollection(context.Context, string) (*userstore.Collection, error) {
return &s.collection, nil
}
func (s *lifecycleStore) DeleteCollection(context.Context, string) error { s.mutations++; return nil }
func (s *lifecycleStore) DeleteCollection(context.Context, string) error {
s.mutations++
return s.deleteErr
}
func (s *lifecycleStore) AddCollectionItem(context.Context, string, string, int) error {
s.mutations++
return nil
Expand Down Expand Up @@ -113,6 +119,68 @@ func TestPersonalCollectionLifecycleProfileAccess(t *testing.T) {
}
}

type failingPrefixStore struct{ *blobstoretest.Memory }

func (failingPrefixStore) DeletePrefix(context.Context, string) (int, error) {
return 0, errors.New("storage unavailable")
}

// Deleting a personal collection reclaims its artwork directory once the row
// is gone. A failed row delete keeps the artwork, and a failed sweep only logs.
func TestDeletePersonalCollectionRemovesArtwork(t *testing.T) {
for _, tc := range []struct {
name string
deleteErr error
failSweep bool
wantErr bool
wantRemoved bool
}{
{name: "deleted", wantRemoved: true},
{name: "row delete fails", deleteErr: errors.New("db down"), wantErr: true},
{name: "sweep fails", failSweep: true},
} {
t.Run(tc.name, func(t *testing.T) {
objects := blobstoretest.New()
for _, key := range []string{
"user-collection-images/c/poster/abc-small.webp",
"user-collection-images/c/collage/def.webp",
"user-collection-images/c2/poster/abc-small.webp",
} {
if err := objects.Put(t.Context(), key, []byte("x")); err != nil {
t.Fatal(err)
}
}
store := &lifecycleStore{collection: userstore.Collection{ID: "c", CreatorProfileID: "owner", CollectionType: "manual"}, deleteErr: tc.deleteErr}
h := NewCollectionHandler(lifecycleProvider{store: store})
h.ArtworkStore = objects
if tc.failSweep {
h.ArtworkStore = failingPrefixStore{objects}
}

err := h.DeletePersonalCollection(t.Context(), 1, "owner", "c")
if (err != nil) != tc.wantErr {
t.Fatalf("delete error = %v, want error %t", err, tc.wantErr)
}
remaining := make([]string, 0, len(objects.Objects))
for key := range objects.Objects {
remaining = append(remaining, key)
}
slices.Sort(remaining)
want := []string{
"user-collection-images/c/collage/def.webp",
"user-collection-images/c/poster/abc-small.webp",
"user-collection-images/c2/poster/abc-small.webp",
}
if tc.wantRemoved {
want = want[2:]
}
if !slices.Equal(remaining, want) {
t.Fatalf("remaining artwork = %v, want %v", remaining, want)
}
})
}
}

func TestPersonalCollectionUpdatePreservesNullableGroupAndImportConfig(t *testing.T) {
store := &lifecycleStore{collection: userstore.Collection{ID: "c", CreatorProfileID: "owner", CollectionType: "mdblist", SourceConfig: `{"url":"https://mdblist.com/lists/user/list","limit":10,"library_ids":[7]}`}}
h := NewCollectionHandler(lifecycleProvider{store: store})
Expand Down
9 changes: 1 addition & 8 deletions internal/api/handlers/library_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,7 @@ func (h *LibraryCollectionHandler) cleanupDeletedCollection(ctx context.Context,
if h.SortPreferenceCleaner != nil {
h.SortPreferenceCleaner.DeleteForCollection(ctx, userstore.CollectionKindLibrary, collectionID)
}
store := h.ArtworkStore
if store == nil {
return
}
prefix := fmt.Sprintf("collection-images/%s/", collectionID)
if _, err := store.DeletePrefix(ctx, prefix); err != nil {
slog.WarnContext(ctx, "collection delete: removing artwork failed", "component", "api", "collection_id", collectionID, "error", err)
}
removeDeletedCollectionArtwork(ctx, h.ArtworkStore, adminCollectionImagePrefix, collectionID)
}

type adminReorderCollectionsRequest struct {
Expand Down