Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(albums): prevent redirect loops from reused slugs

Ahmet Ömerc1655c111442
diff --git a/packages/database/src/album-slugs.ts b/packages/database/src/album-slugs.ts
index 8ade118f..ad4c1254 100644
--- a/packages/database/src/album-slugs.ts
+++ b/packages/database/src/album-slugs.ts
@@ -63,11 +63,11 @@ export function claimAlbumSlug(
),
)
.get()
- if (existing && existing.albumId !== albumId) {
+ if (existing) {
+ if (existing.albumId === albumId && existing.isCurrent) return slug
if (!automatic) throw new CoreError(409, 'Album URL slug already exists')
continue
}
- if (existing?.isCurrent) return slug
db.update(tables.albumSlugs)
.set({ isCurrent: false })
.where(
@@ -78,21 +78,9 @@ export function claimAlbumSlug(
),
)
.run()
- if (existing) {
- db.update(tables.albumSlugs)
- .set({ isCurrent: true })
- .where(
- and(
- eq(tables.albumSlugs.siteId, site.id),
- eq(tables.albumSlugs.slug, slug),
- ),
- )
- .run()
- } else {
- db.insert(tables.albumSlugs)
- .values({ siteId: site.id, slug, albumId, isCurrent: true })
- .run()
- }
+ db.insert(tables.albumSlugs)
+ .values({ siteId: site.id, slug, albumId, isCurrent: true })
+ .run()
return slug
}
throw new Error('Album slug namespace exhausted')
diff --git a/tests/album-slug-persistence.test.ts b/tests/album-slug-persistence.test.ts
index 5c473a09..bea78179 100644
--- a/tests/album-slug-persistence.test.ts
+++ b/tests/album-slug-persistence.test.ts
@@ -77,8 +77,29 @@ test('album slugs remain unique, redirectable, and reserved after deletion', asy
'a-new-title',
)
assert.equal(
- (await writer.update(first.id, { slug: 'my-album' }, access)).slug,
- 'my-album',
+ (await writer.update(first.id, { slug: 'a-new-title' }, access)).slug,
+ 'a-new-title',
+ )
+ await assert.rejects(
+ writer.update(first.id, { slug: 'my-album' }, access),
+ (error) => error instanceof CoreError && error.statusCode === 409,
+ )
+ assert.equal(
+ (await writer.update(first.id, { title: 'My Album', slug: '' }, access))
+ .slug,
+ 'my-album-2',
+ )
+ assert.equal(
+ resolveAlbumPath(db, DEFAULT_SITE, 'my-album')?.slug,
+ 'my-album-2',
+ )
+ assert.equal(
+ resolveAlbumPath(db, DEFAULT_SITE, 'my-album')?.isCanonical,
+ false,
+ )
+ assert.equal(
+ resolveAlbumPath(db, DEFAULT_SITE, 'a-new-title')?.slug,
+ 'my-album-2',
)
await assert.rejects(
@@ -97,7 +118,7 @@ test('album slugs remain unique, redirectable, and reserved after deletion', asy
)
assert.equal(
(await writer.create({ title: 'My Album' }, access)).slug,
- 'my-album-2',
+ 'my-album-3',
)
} finally {
sqlite.close()