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: retain all active jobs and pin upload batch storage

Timothy Yinb8e6c3576d26
diff --git a/apps/cloud/app/composables/useCloudSiteMedia.ts b/apps/cloud/app/composables/useCloudSiteMedia.ts
index 83c7985c..6335970d 100644
--- a/apps/cloud/app/composables/useCloudSiteMedia.ts
+++ b/apps/cloud/app/composables/useCloudSiteMedia.ts
@@ -319,7 +319,7 @@ export function useCloudMediaWorkspace(props: {
if (saved) await loadPhoto()
return saved
}
- function resumeKey(file: File, albumIds: number[]) {
+ function resumeKey(file: File, albumIds: number[], connection: string) {
return (
[
'chronoframe-upload',
@@ -328,7 +328,7 @@ export function useCloudMediaWorkspace(props: {
file.name,
file.size,
file.lastModified,
- connectionId.value,
+ connection,
].join(':') + (albumIds.length ? ':albums:' + albumIds.join(',') : '')
)
}
@@ -336,14 +336,15 @@ export function useCloudMediaWorkspace(props: {
file: File,
liveVideo: File | undefined,
albumIds: number[],
+ connection: string,
) {
- const cache = resumeKey(file, albumIds),
+ const cache = resumeKey(file, albumIds, connection),
id = localStorage.getItem(cache) || crypto.randomUUID()
await api(base.value + '/uploads', {
method: 'POST',
body: {
requestId: id,
- connectionId: connectionId.value,
+ connectionId: connection,
fileName: file.name,
bytes: file.size,
albumIds,
@@ -434,6 +435,11 @@ export function useCloudMediaWorkspace(props: {
!connectionId.value
)
return
+ const batchConnection = connectionId.value
+ const batchStorage = connections.value.find((c) => c.id === batchConnection)
+ ?.pool_id
+ ? 'platform'
+ : 'private'
const isVideo = (file: File) => file.name.toLowerCase().endsWith('.mov')
const companionFor = (photo: File) =>
files.find(
@@ -473,7 +479,12 @@ export function useCloudMediaWorkspace(props: {
// Existing reservations retain their original selection even if an album was removed.
const hasNewFiles = files
.filter((file) => !isVideo(file))
- .some((file) => !localStorage.getItem(resumeKey(file, chosenAlbums)))
+ .some(
+ (file) =>
+ !localStorage.getItem(
+ resumeKey(file, chosenAlbums, batchConnection),
+ ),
+ )
if (unavailable && hasNewFiles) {
failed.value = true
message.value = t('cloud.transfer.albumsUnavailable')
@@ -492,11 +503,7 @@ export function useCloudMediaWorkspace(props: {
const albumIds = chosenAlbums
uploadSession.total = files.filter((f) => !isVideo(f)).length
uploadSession.completed = 0
- uploadSession.storage = connections.value.find(
- (c) => c.id === connectionId.value,
- )?.pool_id
- ? 'platform'
- : 'private'
+ uploadSession.storage = batchStorage
uploadSession.message = ''
uploadSession.failed = false
uploadOpen.value = false
@@ -511,7 +518,7 @@ export function useCloudMediaWorkspace(props: {
const companion = companionFor(file)
currentFile.value = file.name
progress.value = 0
- await sendFile(file, companion, albumIds)
+ await sendFile(file, companion, albumIds, batchConnection)
uploadSession.completed++
void refreshProcessing()
}
diff --git a/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts b/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts
index 05e15376..98d5a4f7 100644
--- a/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts
+++ b/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts
@@ -632,7 +632,17 @@ export default definePlatformHandler(async (event) => {
return {
photos: await ctx.resources.listPhotos(),
jobs: await sql(
- 'SELECT j.id,j.state,j.attempts,j.error_code,j.photo_id,u.file_name FROM cloud_processing_jobs j LEFT JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id WHERE j.site_id=? AND EXISTS(SELECT 1 FROM cloud_photos p WHERE p.site_id=j.site_id AND p.id=j.photo_id AND p.deleted=0) ORDER BY j.created_at DESC,j.id DESC LIMIT 100',
+ `WITH visible_jobs AS (
+ SELECT j.id,j.state,j.attempts,j.error_code,j.photo_id,j.created_at,u.file_name
+ FROM cloud_processing_jobs j LEFT JOIN cloud_uploads u ON u.site_id=j.site_id AND u.id=j.upload_id
+ WHERE j.site_id=? AND EXISTS(SELECT 1 FROM cloud_photos p WHERE p.site_id=j.site_id AND p.id=j.photo_id AND p.deleted=0)
+ ), recent_history AS (
+ SELECT id FROM visible_jobs WHERE state NOT IN ('queued','running','retry_wait')
+ ORDER BY created_at DESC,id DESC LIMIT 100
+ )
+ SELECT id,state,attempts,error_code,photo_id,file_name FROM visible_jobs
+ WHERE state IN ('queued','running','retry_wait') OR id IN (SELECT id FROM recent_history)
+ ORDER BY created_at DESC,id DESC`,
siteId,
),
}
diff --git a/tests/cloud-upload-albums.test.ts b/tests/cloud-upload-albums.test.ts
index 56f5eaca..88380aa9 100644
--- a/tests/cloud-upload-albums.test.ts
+++ b/tests/cloud-upload-albums.test.ts
@@ -119,7 +119,10 @@ test('upload reservation persists canonical site-owned albums and refuses changi
code + '; return exports.default',
)({}, ...Object.values(deps)) as (
event: RequestEvent,
- ) => Promise<{ id?: string; jobs?: { id: string; photo_id: string }[] }>
+ ) => Promise<{
+ id?: string
+ jobs?: { id: string; photo_id: string; state: string }[]
+ }>
const body = {
requestId: 'upload',
connectionId: 'storage',
@@ -193,6 +196,55 @@ test('upload reservation persists canonical site-owned albums and refuses changi
).total,
1,
)
+ sqlite
+ .prepare(
+ "UPDATE cloud_photos SET deleted=0 WHERE site_id=1 AND id='upload'",
+ )
+ .run()
+ sqlite
+ .prepare(
+ "UPDATE cloud_processing_jobs SET state='running',created_at=1 WHERE site_id=1 AND id='job'",
+ )
+ .run()
+ sqlite.transaction(() => {
+ for (let index = 0; index < 125; index++) {
+ const id = 'more-' + index
+ sqlite
+ .prepare(
+ "INSERT INTO cloud_photos(site_id,id,source_version,title) VALUES(1,?,'1','Photo')",
+ )
+ .run(id)
+ sqlite
+ .prepare(
+ "INSERT INTO cloud_uploads(site_id,id,owner_id,photo_id,source_version,connection_id,object_key,expected_bytes,expires_at) VALUES(1,?,1,?,'1','storage',?,10,9999999999999)",
+ )
+ .run(id, id, id)
+ sqlite
+ .prepare(
+ "INSERT INTO cloud_processing_jobs(site_id,id,upload_id,photo_id,source_version,pipeline_version,state,created_at) VALUES(1,?,?,?,'1','1',?,?)",
+ )
+ .run(
+ id,
+ id,
+ id,
+ index === 0 ? 'queued' : index === 1 ? 'retry_wait' : 'succeeded',
+ index < 2 ? index + 2 : 10000 + index,
+ )
+ }
+ })()
+ const jobs = (await api({ method: 'GET', body: {} })).jobs!
+ assert.equal(jobs.length, 103) // All three older active jobs plus 100 history rows.
+ for (const id of ['job', 'more-0', 'more-1'])
+ assert(jobs.some((job) => job.id === id))
+ assert.equal(jobs.filter((job) => job.state === 'succeeded').length, 100)
+ sqlite
+ .prepare(
+ "UPDATE cloud_photos SET deleted=1 WHERE site_id=1 AND id='upload'",
+ )
+ .run()
+ const afterDelete = (await api({ method: 'GET', body: {} })).jobs!
+ assert.equal(afterDelete.length, 102)
+ assert(!afterDelete.some((job) => job.id === 'job'))
} finally {
sqlite.close()
}
diff --git a/tests/portal-review-regressions.test.ts b/tests/portal-review-regressions.test.ts
index 5065a1ad..0d9690f8 100644
--- a/tests/portal-review-regressions.test.ts
+++ b/tests/portal-review-regressions.test.ts
@@ -748,6 +748,7 @@ test('upload sessions capture their actual storage type and move progress out of
release = resolve
})
const reservations: any[] = []
+ const cacheKeys: string[] = []
const storage = new Map<string, string>()
const module = load(
'apps/cloud/app/composables/useCloudSiteMedia.ts',
@@ -763,7 +764,10 @@ test('upload sessions capture their actual storage type and move progress out of
setInterval,
clearInterval,
localStorage: {
- getItem: (k: string) => storage.get(k),
+ getItem: (k: string) => {
+ cacheKeys.push(k)
+ return storage.get(k)
+ },
setItem: (k: string, v: string) => storage.set(k, v),
removeItem: (k: string) => storage.delete(k),
},
@@ -808,16 +812,26 @@ test('upload sessions capture their actual storage type and move progress out of
value: 'chosen',
},
})
+ media.connectionId.value = 'changed-during-preflight'
assert.equal(media.preparingUpload.value, true)
await new Promise((resolve) => setImmediate(resolve))
assert.equal(media.uploading.value, true)
assert.equal(media.uploadOpen.value, false)
assert.equal(media.uploadSession.storage, platform ? 'platform' : 'private')
+ media.connectionId.value = 'changed-during-upload'
media.selectedAlbumIds.value = [7]
media.connections.value = []
assert.equal(media.uploadSession.storage, platform ? 'platform' : 'private')
release()
await pending
+ assert.deepEqual(
+ reservations.map((body) => body.connectionId),
+ ['chosen', 'chosen'],
+ )
+ assert(
+ cacheKeys.length > 0 &&
+ cacheKeys.every((key) => key.includes(':chosen:albums:')),
+ )
assert.equal(media.uploadSession.total, 2)
assert.equal(media.uploadSession.completed, 2)
assert.equal(media.uploadSession.failed, false)