Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
fix: preserve upload resumes and restore failed-photo cleanup
Timothy Yinf172d71b6fe7
diff --git a/apps/cloud/app/components/CloudTransferPanel.vue b/apps/cloud/app/components/CloudTransferPanel.vueindex b40d7182..8d4b4c13 100644--- a/apps/cloud/app/components/CloudTransferPanel.vue+++ b/apps/cloud/app/components/CloudTransferPanel.vue@@ -1,6 +1,8 @@<script setup lang="ts">const {jobs,+ ready,+ removePhoto,photos,uploading,progress,@@ -20,9 +22,17 @@ const tracked = ref<Record<string, Job>>({})const dismissed = new Set<string>()const active = (job: Job) =>['queued', 'running', 'retry_wait'].includes(job.state)+let initialSnapshot = truewatch(- jobs,- (items) => {+ [jobs, ready],+ ([items, loaded]) => {+ if (!loaded) return+ if (initialSnapshot) {+ // Keep historical failures reachable for cleanup without opening the panel on every visit.+ collapsed.value =+ !items.some(active) && items.some((job) => job.state === 'failed')+ initialSnapshot = false+ }const next = { ...tracked.value }for (const job of items) {if (active(job)) dismissed.delete(job.id)@@ -58,6 +68,10 @@ function dismiss() {uploadSession.total = 0uploadSession.message = ''}+function removeFailed(job: Job) {+ if (admin.value && !busy.value && job.state === 'failed')+ return removePhoto(job.photo_id)+}function photoName(job: Job) {return (job.file_name ||@@ -234,7 +248,7 @@ function photoName(job: Job) {</p></div><CfButton- v-if="admin && job.state === 'failed'"+ v-if="admin && job.state === 'failed' && job.attempts <= 6"variant="secondary"size="sm":disabled="busy"@@ -250,6 +264,14 @@ function photoName(job: Job) {">{{ t('cloud.messages.retry') }}</CfButton>+ <CfButton+ v-if="admin && job.state === 'failed'"+ variant="destructive"+ size="sm"+ :disabled="busy"+ @click="removeFailed(job)"+ >{{ t('cloud.messages.remove') }}</CfButton+ ></li></ul></div>diff --git a/apps/cloud/app/components/CloudUploadDrawer.vue b/apps/cloud/app/components/CloudUploadDrawer.vueindex d94acf2d..be5397ec 100644--- a/apps/cloud/app/components/CloudUploadDrawer.vue+++ b/apps/cloud/app/components/CloudUploadDrawer.vue@@ -10,6 +10,7 @@ const {connectionId,upload,uploading,+ preparingUpload,usage,allowance,fileLimit,@@ -37,6 +38,7 @@ async function dropFiles(event: DragEvent) {if (!input.value ||uploading.value ||+ preparingUpload.value ||!uploadAllowed.value ||!admin.value ||!event.dataTransfer?.files.length@@ -85,7 +87,7 @@ async function dropFiles(event: DragEvent) {}))"class="w-full"- :disabled="uploading"+ :disabled="uploading || preparingUpload"/></UFormField><i18n-tv-if="!connections.length && allowance?.byosLimits.byos"@@ -130,7 +132,7 @@ async function dropFiles(event: DragEvent) {}))":placeholder="$t('cloud.transfer.selectAlbums')"- :disabled="uploading || !admin"+ :disabled="uploading || preparingUpload || !admin"class="w-full"/></UFormField>@@ -141,13 +143,13 @@ async function dropFiles(event: DragEvent) {multipleaccept="image/*,.heic,.heif,.hif,.bmp,.tif,.tiff,.mov":aria-label="$t('cloud.messages.choosePhotographs')"- :disabled="uploading || !uploadAllowed || !admin"+ :disabled="uploading || preparingUpload || !uploadAllowed || !admin"@change="upload"/><buttonclass="cf-upload-dropzone"@dragover.prevent@drop.prevent="dropFiles"- :disabled="uploading || !uploadAllowed || !admin"+ :disabled="uploading || preparingUpload || !uploadAllowed || !admin"@click="input?.click()"><UIcondiff --git a/apps/cloud/app/composables/useCloudSiteMedia.ts b/apps/cloud/app/composables/useCloudSiteMedia.tsindex c7e6199d..83c7985c 100644--- a/apps/cloud/app/composables/useCloudSiteMedia.ts+++ b/apps/cloud/app/composables/useCloudSiteMedia.ts@@ -72,6 +72,7 @@ export function useCloudMediaWorkspace(props: {failed = ref(false),busy = ref(false),uploading = ref(false),+ preparingUpload = ref(false),progress = ref(0),currentFile = ref('')const uploadSession = reactive({@@ -105,6 +106,7 @@ export function useCloudMediaWorkspace(props: {const loadError = ref('')const loading = ref(true),uploadOpen = ref(false)+ let disposed = falselet storageInitialized = falselet reloadGeneration = 0async function reload() {@@ -151,6 +153,7 @@ export function useCloudMediaWorkspace(props: {}loadError.value = ''canPoll.value = true+ if (uploading.value || preparingUpload.value) returnconst uploadTargets = s.connections.filter((c) => c.uploads_enabled !== 0 && (c.pool_id || u.byosLimits?.byos),)@@ -424,7 +427,13 @@ export function useCloudMediaWorkspace(props: {async function upload(event: Event) {const input = event.target as HTMLInputElement,files = Array.from(input.files ?? [])- if (!files.length || uploading.value || !connectionId.value) return+ if (+ !files.length ||+ uploading.value ||+ preparingUpload.value ||+ !connectionId.value+ )+ returnconst isVideo = (file: File) => file.name.toLowerCase().endsWith('.mov')const companionFor = (photo: File) =>files.find(@@ -449,7 +458,38 @@ export function useCloudMediaWorkspace(props: {input.value = ''return}- const albumIds = [...selectedAlbumIds.value].sort((a, b) => a - b)+ const chosenAlbums = [...selectedAlbumIds.value].sort((a, b) => a - b)+ preparingUpload.value = true+ try {+ if (chosenAlbums.length) {+ const result = await api<{ albums: typeof albums.value }>(+ base.value + '/albums',+ )+ if (disposed) return+ albums.value = result.albums+ const unavailable = chosenAlbums.some(+ (id) => !result.albums.some((album) => Number(album.id) === id),+ )+ // 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)))+ if (unavailable && hasNewFiles) {+ failed.value = true+ message.value = t('cloud.transfer.albumsUnavailable')+ input.value = ''+ return+ }+ }+ } catch {+ failed.value = true+ message.value = t('cloud.transfer.albumsCheckFailed')+ input.value = ''+ return+ } finally {+ preparingUpload.value = false+ }+ const albumIds = chosenAlbumsuploadSession.total = files.filter((f) => !isVideo(f)).lengthuploadSession.completed = 0uploadSession.storage = connections.value.find(@@ -478,11 +518,26 @@ export function useCloudMediaWorkspace(props: {uploadSession.message = t('cloud.messages.uploadVerifiedProcessingContinuesInTheBackground',)- } catch {+ } catch (error) {+ const albumUnavailable =+ (error as { data?: { data?: { code?: string } } }).data?.data?.code ===+ 'upload_albums_unavailable'+ if (albumUnavailable) {+ await api<{ albums: typeof albums.value }>(base.value + '/albums')+ .then((result) => {+ albums.value = result.albums+ })+ .catch(() => {})+ failed.value = true+ message.value = t('cloud.transfer.albumsUnavailable')+ uploadOpen.value = true+ }uploadSession.failed = true- uploadSession.message = controller.signal.aborted- ? t('cloud.messages.uploadCancelled')- : t('cloud.messages.uploadInterruptedSelectTheSameFileToResumeIts')+ uploadSession.message = albumUnavailable+ ? t('cloud.transfer.albumsUnavailable')+ : controller.signal.aborted+ ? t('cloud.messages.uploadCancelled')+ : t('cloud.messages.uploadInterruptedSelectTheSameFileToResumeIts')} finally {uploading.value = falseinput.value = ''@@ -545,13 +600,14 @@ export function useCloudMediaWorkspace(props: {onMounted(() => {void reload()timer = setInterval(() => {- if (canPoll.value && !busy.value) {+ if (canPoll.value && !busy.value && !preparingUpload.value) {if (uploading.value) void refreshProcessing()else void reload()}}, 5000)})onBeforeUnmount(() => {+ disposed = truereloadGeneration++if (timer) clearInterval(timer)controller?.abort()@@ -572,6 +628,7 @@ export function useCloudMediaWorkspace(props: {failed,busy,uploading,+ preparingUpload,progress,currentFile,uploadSession,diff --git a/apps/cloud/i18n/locales/en.json b/apps/cloud/i18n/locales/en.jsonindex 959f4afc..e0dbf157 100644--- a/apps/cloud/i18n/locales/en.json+++ b/apps/cloud/i18n/locales/en.json@@ -850,7 +850,9 @@"verifying": "Upload complete. Verifying files…","albums": "Add to albums","selectAlbums": "Select albums (optional, multiple allowed)",- "albumHelp": "Uploaded photographs are automatically added to the selected albums. Leave empty to add only to the library."+ "albumHelp": "Uploaded photographs are automatically added to the selected albums. Leave empty to add only to the library.",+ "albumsUnavailable": "A selected album is no longer available. Update your album selection and try again. Previously uploaded photographs are unaffected.",+ "albumsCheckFailed": "Unable to verify albums. Check your connection and try again. Upload has not started."}}}diff --git a/apps/cloud/i18n/locales/zh-Hans.json b/apps/cloud/i18n/locales/zh-Hans.jsonindex 690c4c74..5995f2bf 100644--- a/apps/cloud/i18n/locales/zh-Hans.json+++ b/apps/cloud/i18n/locales/zh-Hans.json@@ -850,7 +850,9 @@"verifying": "上传完成,正在校验文件…","albums": "添加到相簿","selectAlbums": "选择相簿(可选,可多选)",- "albumHelp": "上传的照片将自动加入所选相簿;留空则仅加入照片库。"+ "albumHelp": "上传的照片将自动加入所选相簿;留空则仅加入照片库。",+ "albumsUnavailable": "所选相簿已不可用,请更新相簿选择后重新上传。已上传的照片不受影响。",+ "albumsCheckFailed": "无法核对相簿,请检查网络后重试。尚未开始上传。"}}}diff --git a/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts b/apps/cloud/server/api/sites/[siteId]/media/[...operation].tsindex c4d390f8..fe8279ec 100644--- a/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts+++ b/apps/cloud/server/api/sites/[siteId]/media/[...operation].ts@@ -1,4 +1,7 @@-import { validateUploadAlbums } from '../../../../utils/cloud-upload-albums'+import {+ uploadAlbumIds,+ validateUploadAlbums,+} from '../../../../utils/cloud-upload-albums'import { cloudLiveAttachment } from '../../../../utils/cloud-live-attachment'import { queuePhotoProcessing } from '../../../../utils/cloud-photo-processing'import {@@ -328,11 +331,7 @@ export default definePlatformHandler(async (event) => {}}if (kind === 'uploads' && !id && event.method === 'POST') {- const albumIdsJson = await validateUploadAlbums(- raw as unknown as MediaDatabase,- siteId,- body.albumIds,- )+ const albumIdsJson = JSON.stringify(uploadAlbumIds(body.albumIds))const uploadId = ID(body.requestId),connectionId = ID(body.connectionId),bytes = number(body.bytes),@@ -381,6 +380,11 @@ export default definePlatformHandler(async (event) => {throw createError({ statusCode: 400 })let upload = await repository.upload(uploadId, userId)if (!upload) {+ await validateUploadAlbums(+ raw as unknown as MediaDatabase,+ siteId,+ body.albumIds,+ )const stored = storedConnectionconst expression = workspaceUsageSQL(siteId)const objectKey = `chronoframe/${siteId}/originals/${uploadId}/1/source.${ext}`diff --git a/apps/cloud/server/utils/cloud-upload-albums.ts b/apps/cloud/server/utils/cloud-upload-albums.tsindex 6a0d5b41..c4a53b48 100644--- a/apps/cloud/server/utils/cloud-upload-albums.ts+++ b/apps/cloud/server/utils/cloud-upload-albums.ts@@ -31,6 +31,7 @@ export async function validateUploadAlbums(throw createError({statusCode: 400,statusMessage: 'Selected upload albums are unavailable',+ data: { code: 'upload_albums_unavailable' },})}return JSON.stringify(ids)diff --git a/tests/cloud-upload-albums.test.ts b/tests/cloud-upload-albums.test.tsindex caf03523..7f359ea1 100644--- a/tests/cloud-upload-albums.test.ts+++ b/tests/cloud-upload-albums.test.ts@@ -3,9 +3,16 @@ import assert from 'node:assert/strict'import { readFile, readdir } from 'node:fs/promises'import Database from 'better-sqlite3'import ts from 'typescript'-import { createCloudMediaRepository } from '../packages/database/src/cloud-media.ts'+import {+ createCloudMediaRepository,+ type MediaDatabase,+ type MediaStatement,+} from '../packages/database/src/cloud-media.ts'import { workspaceUsageSQL } from '../packages/platform/src/entitlements.ts'-import { validateUploadAlbums } from '../apps/cloud/server/utils/cloud-upload-albums.ts'+import {+ uploadAlbumIds,+ validateUploadAlbums,+} from '../apps/cloud/server/utils/cloud-upload-albums.ts'test('upload reservation persists canonical site-owned albums and refuses changing choices on resume', async () => {const sqlite = new Database(':memory:')@@ -20,13 +27,13 @@ test('upload reservation persists canonical site-owned albums and refuses changisqlite.exec("INSERT INTO platform_users(id,email,created_at) VALUES(1,'[email protected]',1); INSERT INTO platform_workspaces(id,name) VALUES(1,'Workspace'); INSERT INTO platform_sites(id,workspace_id,name) VALUES(1,1,'One'),(2,1,'Two'); INSERT INTO cloud_albums(site_id,id,title) VALUES(1,'1','First'),(1,'2','Second'),(2,'3','Foreign')",)- class Statement {+ class Statement implements MediaStatement {sql: string- values: any[] = []+ values: (string | number | null)[] = []constructor(sql: string) {this.sql = sql}- bind(...values: any[]) {+ bind(...values: (string | number | null)[]) {this.values = valuesreturn this}@@ -39,12 +46,17 @@ test('upload reservation persists canonical site-owned albums and refuses changi}const db = {prepare: (sql: string) => new Statement(sql),- async batch(statements: Statement[]) {+ async batch(statements: MediaStatement[]) {sqlite.transaction(() => {- for (const s of statements) sqlite.prepare(s.sql).run(...s.values)+ for (const s of statements) {+ assert(s instanceof Statement)+ sqlite.prepare(s.sql).run(...s.values)+ }})()},}+ db satisfies MediaDatabase+ type RequestEvent = { method: string; body: Record<string, unknown> }const repository = createCloudMediaRepository(db, { id: 1 })const source = (await readFile(@@ -59,7 +71,7 @@ test('upload reservation persists canonical site-owned albums and refuses changi},}).outputTextconst deps = {- definePlatformHandler: (f: any) => f,+ definePlatformHandler: <T>(f: T) => f,cloudMedia: async () => ({siteId: 1,userId: 1,@@ -73,7 +85,8 @@ test('upload reservation persists canonical site-owned albums and refuses changi},}),getRouterParam: () => 'uploads',- siteManagementBody: async (event: any) => event.body,+ siteManagementBody: async (event: RequestEvent) => event.body,+ uploadAlbumIds,validateUploadAlbums,workspaceUsageSQL,workspaceEntitlements: async () => ({@@ -85,14 +98,19 @@ test('upload reservation persists canonical site-owned albums and refuses changi},quotaPeriod: { start: 1 },}),- createError: (value: any) =>- Object.assign(new Error(value.statusMessage), value),+ createError: (value: {+ statusCode: number+ statusMessage?: string+ data?: Record<string, unknown>+ }) => Object.assign(new Error(value.statusMessage), value),}const api = new Function('exports',...Object.keys(deps),code + '; return exports.default',- )({}, ...Object.values(deps))+ )({}, ...Object.values(deps)) as (+ event: RequestEvent,+ ) => Promise<{ id: string }>const body = {requestId: 'upload',connectionId: 'storage',@@ -113,6 +131,12 @@ test('upload reservation persists canonical site-owned albums and refuses changistatusCode: 400,})assert.equal(await repository.upload('foreign', 1), null)+ sqlite.prepare("DELETE FROM cloud_albums WHERE site_id=1 AND id='2'").run()+ await post({ albumIds: [1, 2] })+ await assert.rejects(post({ requestId: 'new-after-deletion' }), {+ statusCode: 400,+ data: { code: 'upload_albums_unavailable' },+ })await repository.completeUpload({id: 'upload',ownerId: 1,@@ -123,12 +147,12 @@ test('upload reservation persists canonical site-owned albums and refuses changi})assert.deepEqual(sqlite- .prepare(+ .prepare<[], { album_id: string }>('SELECT album_id FROM cloud_album_photos WHERE site_id=1 ORDER BY album_id',).all()- .map((r: any) => r.album_id),- ['1', '2'],+ .map((r) => r.album_id),+ ['1'],)} finally {sqlite.close()diff --git a/tests/portal-review-regressions.test.ts b/tests/portal-review-regressions.test.tsindex 2f4a2dbb..1cac05ab 100644--- a/tests/portal-review-regressions.test.ts+++ b/tests/portal-review-regressions.test.ts@@ -775,7 +775,8 @@ test('upload sessions capture their actual storage type and move progress out ofif (path.endsWith('/plan')) return { state: 'completed' }if (path.endsWith('/storage')) return { connections: [] }if (path.endsWith('/photos')) return { photos: [], jobs: [] }- if (path.endsWith('/albums')) return { albums: [] }+ if (path.endsWith('/albums'))+ return { albums: [{ id: 1 }, { id: 2 }] }if (path.endsWith('/usage'))return { usage: { photos: 0 }, limits: {}, byosLimits: {} }return {}@@ -807,6 +808,8 @@ test('upload sessions capture their actual storage type and move progress out ofvalue: 'chosen',},})+ 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')@@ -830,8 +833,13 @@ test('upload sessions capture their actual storage type and move progress out of})test('floating queue follows task completion and retry without showing historical successful jobs', async () => {+ const removed: string[] = []const media = {jobs: ref<any[]>([{ id: 'old', state: 'succeeded' }]),+ ready: ref(true),+ removePhoto: (id: string) => {+ removed.push(id)+ },photos: ref([]),uploading: ref(false),progress: ref(0),@@ -852,7 +860,7 @@ test('floating queue follows task completion and retry without showing historica{ useCloudSiteMedia: () => media, useId: () => 'queue' },{},script +- '\nexport { visible, collapsed, processingJobs, processingActive, processingCompleted, dismiss }',+ '\nexport { visible, collapsed, processingJobs, processingActive, processingCompleted, dismiss, removeFailed }',)assert.equal(state.visible.value, false)media.jobs.value = [{ id: 'j', state: 'queued', attempts: 0 }]@@ -874,4 +882,118 @@ test('floating queue follows task completion and retry without showing historicamedia.jobs.value = [{ id: 'j', state: 'failed', attempts: 3 }]await nextTick()assert.equal(state.processingJobs.value[0].state, 'failed')+ const failedJob = {+ id: 'failed',+ photo_id: 'never-ready',+ state: 'failed',+ attempts: 7,+ }+ media.admin.value = false+ state.removeFailed(failedJob)+ assert.deepEqual(removed, [])+ media.admin.value = true+ media.busy.value = true+ state.removeFailed(failedJob)+ assert.deepEqual(removed, [])+ media.busy.value = false+ state.removeFailed(failedJob)+ assert.deepEqual(removed, ['never-ready'])+ // A new visit keeps historical failures accessible, but starts collapsed.+ media.ready.value = false+ media.jobs.value = [failedJob]+ const history = load(+ file,+ { useCloudSiteMedia: () => media, useId: () => 'history' },+ {},+ script + '\nexport { visible, collapsed }',+ )+ assert.equal(history.visible.value, false)+ media.ready.value = true+ await nextTick()+ assert.equal(history.visible.value, true)+ assert.equal(history.collapsed.value, true)+})++test('upload album preflight fails closed for new uploads, refreshes reservation races and preserves resumable choices', async () => {+ for (const scenario of ['removed', 'offline', 'race', 'resume']) {+ let reservations = 0,+ albumReads = 0+ const cache = new Map<string, string>()+ const module = load(+ 'apps/cloud/app/composables/useCloudSiteMedia.ts',+ {+ useEntryContext: () => ({+ value: { scope: 'site:1:user:1', siteId: 1 },+ }),+ onMounted: () => {},+ onBeforeUnmount: () => {},+ AbortController,+ crypto,+ setTimeout,+ setInterval,+ clearInterval,+ localStorage: {+ getItem: (k: string) =>+ scenario === 'resume' ? 'existing' : cache.get(k),+ setItem: (k: string, v: string) => cache.set(k, v),+ removeItem: (k: string) => cache.delete(k),+ },+ $fetch: async (path: string) => {+ if (path.endsWith('/albums')) {+ albumReads+++ if (scenario === 'offline') throw Error('offline')+ return {+ albums:+ scenario === 'race' && albumReads === 1 ? [{ id: 1 }] : [],+ }+ }+ if (path.endsWith('/uploads')) {+ reservations+++ if (scenario === 'race')+ throw { data: { data: { code: 'upload_albums_unavailable' } } }+ }+ if (path.endsWith('/plan')) return { state: 'completed' }+ if (path.endsWith('/storage')) return { connections: [] }+ if (path.endsWith('/photos')) return { photos: [], jobs: [] }+ if (path.endsWith('/usage'))+ return { usage: {}, limits: {}, byosLimits: {} }+ return {}+ },+ },+ {+ '@chronoframe/contracts/storage-diagnostics': {+ STORAGE_CONNECTION_MESSAGES: {},+ },+ },+ )+ const media = module.useCloudMediaWorkspace({ siteId: 1, role: 'admin' })+ media.connectionId.value = 'chosen'+ media.allowance.value = {+ byosLimits: { fileBytes: 100, byos: 1 },+ limits: { fileBytes: 100 },+ }+ media.selectedAlbumIds.value = [1]+ media.uploadOpen.value = true+ await media.upload({+ target: { files: [{ name: 'one.jpg', size: 10 }], value: 'chosen' },+ })+ assert.deepEqual(Array.from(media.selectedAlbumIds.value), [1])+ assert.equal(media.preparingUpload.value, false)+ assert.equal(+ reservations,+ ['removed', 'offline'].includes(scenario) ? 0 : 1,+ )+ if (scenario === 'resume') assert.equal(media.uploadSession.completed, 1)+ else {+ assert.equal(media.failed.value, true)+ assert.match(+ media.message.value,+ scenario === 'offline'+ ? /Unable to verify albums/+ : /selected album is no longer available/,+ )+ assert.equal(media.uploadOpen.value, true)+ }+ if (scenario === 'race') assert(albumReads >= 2)+ }})