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.

feat: stage cloud photo selections before starting uploads

Timothy Yin54cad8e6fdea
diff --git a/apps/cloud/app/components/CloudPhotoLibrary.vue b/apps/cloud/app/components/CloudPhotoLibrary.vue
index f8c387e4..a9d1f920 100644
--- a/apps/cloud/app/components/CloudPhotoLibrary.vue
+++ b/apps/cloud/app/components/CloudPhotoLibrary.vue
@@ -349,7 +349,11 @@ watch(
>
</div>
<CloudQuotaNotice
- v-if="admin && (uploadAdmission.reason || quota.error.value)"
+ v-if="
+ admin &&
+ (quota.error.value ||
+ (uploadAdmission.reason && uploadAdmission.reason !== 'checking'))
+ "
:reason="uploadAdmission.reason"
:error="!!quota.error.value"
@retry="quota.refresh()"
diff --git a/apps/cloud/app/components/CloudUploadDrawer.vue b/apps/cloud/app/components/CloudUploadDrawer.vue
index be5397ec..aa58bd22 100644
--- a/apps/cloud/app/components/CloudUploadDrawer.vue
+++ b/apps/cloud/app/components/CloudUploadDrawer.vue
@@ -1,5 +1,9 @@
<script setup lang="ts">
import { uploadQuotaGate } from '../utils/quota-gate'
+import {
+ mergeUploadFiles,
+ remainingUploadFiles,
+} from '../utils/upload-selection'
const {
uploadOpen,
albums,
@@ -34,18 +38,53 @@ const uploadAllowed = computed(
selectedAdmission.value.allowed &&
!quota.error.value,
)
-async function dropFiles(event: DragEvent) {
+const selectedFiles = shallowRef<File[]>([])
+const starting = ref(false)
+const selectedPhotos = computed(
+ () =>
+ selectedFiles.value.filter(
+ (file) => !file.name.toLowerCase().endsWith('.mov'),
+ ).length,
+)
+const selectionLocked = computed(
+ () => starting.value || uploading.value || preparingUpload.value,
+)
+function addFiles(files: File[]) {
+ if (selectionLocked.value || !admin.value) return
+ selectedFiles.value = mergeUploadFiles(selectedFiles.value, files)
+}
+function selectFiles(event: Event) {
+ const target = event.target as HTMLInputElement
+ addFiles(Array.from(target.files ?? []))
+ target.value = ''
+}
+function dropFiles(event: DragEvent) {
+ if (!uploadAllowed.value) return
+ addFiles(Array.from(event.dataTransfer?.files ?? []))
+}
+function removeSelected(index: number) {
+ if (selectionLocked.value) return
+ selectedFiles.value = selectedFiles.value.filter(
+ (_, position) => position !== index,
+ )
+}
+async function startUpload() {
if (
- !input.value ||
- uploading.value ||
- preparingUpload.value ||
+ selectionLocked.value ||
+ !selectedPhotos.value ||
!uploadAllowed.value ||
- !admin.value ||
- !event.dataTransfer?.files.length
+ !admin.value
)
return
- input.value.files = event.dataTransfer.files
- await upload({ target: input.value } as unknown as Event)
+ const files = [...selectedFiles.value]
+ starting.value = true
+ try {
+ const result = await upload(files)
+ if (result)
+ selectedFiles.value = remainingUploadFiles(files, result.completed)
+ } finally {
+ starting.value = false
+ }
}
</script>
<template>
@@ -87,7 +126,7 @@ async function dropFiles(event: DragEvent) {
}))
"
class="w-full"
- :disabled="uploading || preparingUpload"
+ :disabled="selectionLocked"
/></UFormField>
<i18n-t
v-if="!connections.length && allowance?.byosLimits.byos"
@@ -132,7 +171,7 @@ async function dropFiles(event: DragEvent) {
}))
"
:placeholder="$t('cloud.transfer.selectAlbums')"
- :disabled="uploading || preparingUpload || !admin"
+ :disabled="selectionLocked || !admin"
class="w-full"
/>
</UFormField>
@@ -143,13 +182,13 @@ async function dropFiles(event: DragEvent) {
multiple
accept="image/*,.heic,.heif,.hif,.bmp,.tif,.tiff,.mov"
:aria-label="$t('cloud.messages.choosePhotographs')"
- :disabled="uploading || preparingUpload || !uploadAllowed || !admin"
- @change="upload"
+ :disabled="selectionLocked || !uploadAllowed || !admin"
+ @change="selectFiles"
/><button
class="cf-upload-dropzone"
@dragover.prevent
@drop.prevent="dropFiles"
- :disabled="uploading || preparingUpload || !uploadAllowed || !admin"
+ :disabled="selectionLocked || !uploadAllowed || !admin"
@click="input?.click()"
>
<UIcon
@@ -165,6 +204,71 @@ async function dropFiles(event: DragEvent) {
})
}}</span>
</button>
+ <section
+ v-if="selectedFiles.length"
+ class="space-y-3"
+ :aria-label="$t('cloud.transfer.selectedFiles')"
+ >
+ <div class="flex items-center justify-between gap-3">
+ <strong class="text-sm">{{
+ $t('cloud.transfer.selectedFilesCount', {
+ count: selectedFiles.length,
+ })
+ }}</strong>
+ <UButton
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ :disabled="selectionLocked"
+ @click="selectedFiles = []"
+ >{{ $t('cloud.transfer.clearSelection') }}</UButton
+ >
+ </div>
+ <ul
+ class="max-h-72 divide-y divide-default overflow-y-auto rounded-lg border border-default"
+ >
+ <li
+ v-for="(file, index) in selectedFiles"
+ :key="JSON.stringify([file.name, file.size, file.lastModified])"
+ class="flex items-center gap-3 px-3 py-2"
+ >
+ <UIcon
+ :name="
+ file.name.toLowerCase().endsWith('.mov')
+ ? 'i-lucide-film'
+ : 'i-lucide-image'
+ "
+ class="size-5 shrink-0 text-muted"
+ />
+ <div class="min-w-0 flex-1">
+ <p
+ class="truncate text-sm"
+ :title="file.name"
+ >
+ {{ file.name }}
+ </p>
+ <p class="text-xs text-muted">{{ formatBytes(file.size) }}</p>
+ </div>
+ <UButton
+ icon="i-lucide-x"
+ color="neutral"
+ variant="ghost"
+ size="sm"
+ :aria-label="
+ $t('cloud.transfer.removeSelected', { name: file.name })
+ "
+ :disabled="selectionLocked"
+ @click="removeSelected(index)"
+ />
+ </li>
+ </ul>
+ <p
+ v-if="!selectedPhotos"
+ class="text-xs text-muted"
+ >
+ {{ $t('cloud.transfer.selectPhotosForVideo') }}
+ </p>
+ </section>
<p
v-if="usage"
class="text-xs text-muted"
@@ -181,12 +285,20 @@ async function dropFiles(event: DragEvent) {
}}
</p>
</div></template
- ><template #footer
- ><CfButton
+ ><template #footer>
+ <CfButton
variant="secondary"
@click="uploadOpen = false"
- >{{ $t('cloud.messages.done') }}</CfButton
- ></template
- ></USlideover
+ >{{ $t('cloud.transfer.closeSelection') }}</CfButton
+ >
+ <CfButton
+ :disabled="
+ selectionLocked || !selectedPhotos || !uploadAllowed || !admin
+ "
+ :loading="starting"
+ @click="startUpload"
+ >{{ $t('cloud.transfer.startUpload') }}</CfButton
+ >
+ </template></USlideover
>
</template>
diff --git a/apps/cloud/app/composables/useCloudSiteMedia.ts b/apps/cloud/app/composables/useCloudSiteMedia.ts
index 6335970d..e4444a8d 100644
--- a/apps/cloud/app/composables/useCloudSiteMedia.ts
+++ b/apps/cloud/app/composables/useCloudSiteMedia.ts
@@ -425,8 +425,10 @@ export function useCloudMediaWorkspace(props: {
localStorage.removeItem(cache)
activeUpload = undefined
}
- async function upload(event: Event) {
- const input = event.target as HTMLInputElement,
+ async function upload(event: Event | File[]) {
+ const input = Array.isArray(event)
+ ? { files: event, value: '' }
+ : (event.target as HTMLInputElement),
files = Array.from(input.files ?? [])
if (
!files.length ||
@@ -551,6 +553,7 @@ export function useCloudMediaWorkspace(props: {
currentFile.value = ''
await reload()
}
+ return { completed: uploadSession.completed }
}
async function cancel() {
controller?.abort()
diff --git a/apps/cloud/app/utils/upload-selection.ts b/apps/cloud/app/utils/upload-selection.ts
new file mode 100644
index 00000000..c887b251
--- /dev/null
+++ b/apps/cloud/app/utils/upload-selection.ts
@@ -0,0 +1,34 @@
+type UploadFile = Pick<File, 'name' | 'size' | 'lastModified'>
+export function mergeUploadFiles<T extends UploadFile>(
+ current: T[],
+ incoming: T[],
+): T[] {
+ const seen = new Set<string>()
+ return [...current, ...incoming].filter((file) => {
+ const key = JSON.stringify([file.name, file.size, file.lastModified])
+ if (seen.has(key)) return false
+ seen.add(key)
+ return true
+ })
+}
+/** Remove verified photos and their paired MOV files; keep interrupted and unstarted files. */
+export function remainingUploadFiles<T extends UploadFile>(
+ files: T[],
+ completed: number,
+): T[] {
+ const isVideo = (file: T) => file.name.toLowerCase().endsWith('.mov')
+ const completedPhotos = files
+ .filter((file) => !isVideo(file))
+ .slice(0, completed)
+ const removed = new Set(completedPhotos)
+ for (const photo of completedPhotos) {
+ const companion = files.find(
+ (file) =>
+ isVideo(file) &&
+ file.name.replace(/\.[^.]+$/, '').toLowerCase() ===
+ photo.name.replace(/\.[^.]+$/, '').toLowerCase(),
+ )
+ if (companion) removed.add(companion)
+ }
+ return files.filter((file) => !removed.has(file))
+}
diff --git a/apps/cloud/i18n/locales/en.json b/apps/cloud/i18n/locales/en.json
index e0dbf157..7dee2965 100644
--- a/apps/cloud/i18n/locales/en.json
+++ b/apps/cloud/i18n/locales/en.json
@@ -852,7 +852,14 @@
"selectAlbums": "Select albums (optional, multiple allowed)",
"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."
+ "albumsCheckFailed": "Unable to verify albums. Check your connection and try again. Upload has not started.",
+ "selectedFiles": "Files to upload",
+ "selectedFilesCount": "{count} files selected",
+ "clearSelection": "Clear",
+ "removeSelected": "Remove {name}",
+ "selectPhotosForVideo": "Select photographs together with their matching MOV companion videos.",
+ "closeSelection": "Close",
+ "startUpload": "Start upload"
}
}
}
diff --git a/apps/cloud/i18n/locales/zh-Hans.json b/apps/cloud/i18n/locales/zh-Hans.json
index 5995f2bf..2e8871f3 100644
--- a/apps/cloud/i18n/locales/zh-Hans.json
+++ b/apps/cloud/i18n/locales/zh-Hans.json
@@ -852,7 +852,14 @@
"selectAlbums": "选择相簿(可选,可多选)",
"albumHelp": "上传的照片将自动加入所选相簿;留空则仅加入照片库。",
"albumsUnavailable": "所选相簿已不可用,请更新相簿选择后重新上传。已上传的照片不受影响。",
- "albumsCheckFailed": "无法核对相簿,请检查网络后重试。尚未开始上传。"
+ "albumsCheckFailed": "无法核对相簿,请检查网络后重试。尚未开始上传。",
+ "selectedFiles": "待上传文件",
+ "selectedFilesCount": "已选择 {count} 个文件",
+ "clearSelection": "清空",
+ "removeSelected": "移除 {name}",
+ "selectPhotosForVideo": "请同时选择照片及其同名 MOV 配对视频。",
+ "closeSelection": "关闭",
+ "startUpload": "开始上传"
}
}
}
diff --git a/tests/portal-review-regressions.test.ts b/tests/portal-review-regressions.test.ts
index 0d9690f8..742a416e 100644
--- a/tests/portal-review-regressions.test.ts
+++ b/tests/portal-review-regressions.test.ts
@@ -5,9 +5,14 @@ import vm from 'node:vm'
import { createServer } from 'node:http'
import { createRequire } from 'node:module'
import ts from 'typescript'
+import {
+ mergeUploadFiles,
+ remainingUploadFiles,
+} from '../apps/cloud/app/utils/upload-selection.ts'
import { isAlbumPagePath } from '../packages/gallery/shared/utils/albumPath.ts'
import {
ref,
+ shallowRef,
reactive,
computed,
readonly,
@@ -30,6 +35,7 @@ function load(
vm.runInNewContext(code, {
exports,
ref,
+ shallowRef,
reactive,
computed,
readonly,
@@ -1014,3 +1020,86 @@ test('upload album preflight fails closed for new uploads, refreshes reservation
if (scenario === 'race') assert(albumReads >= 2)
}
})
+
+test('file selection and dropping only stage files; explicit start uploads once and retains unfinished photos', async () => {
+ const batches: File[][] = []
+ let release!: () => void
+ let gate = new Promise<void>((resolve) => {
+ release = resolve
+ })
+ let completed: number | undefined = 1
+ const media = {
+ uploadOpen: ref(true),
+ albums: ref([]),
+ selectedAlbumIds: ref([]),
+ message: ref(''),
+ failed: ref(false),
+ connections: ref([]),
+ connectionId: ref('storage'),
+ uploading: ref(false),
+ preparingUpload: ref(false),
+ usage: ref(null),
+ allowance: ref(null),
+ fileLimit: ref(1024),
+ admin: ref(true),
+ upload: async (files: File[]) => {
+ batches.push(files)
+ await gate
+ return completed === undefined ? undefined : { completed }
+ },
+ }
+ const file = 'apps/cloud/app/components/CloudUploadDrawer.vue'
+ const script = readFileSync(file, 'utf8')
+ .split('<script setup lang="ts">')[1]!
+ .split('</script>')[0]!
+ const state = load(
+ file,
+ {
+ useCloudSiteMedia: () => media,
+ useEntryContext: () => ref({ siteId: 1, workspaceId: 1 }),
+ useCloudQuota: () => ({ data: ref({}), error: ref(null) }),
+ },
+ {
+ '../utils/quota-gate': { uploadQuotaGate: () => ({ allowed: true }) },
+ '../utils/upload-selection': { mergeUploadFiles, remainingUploadFiles },
+ },
+ script +
+ '\nexport { selectedFiles, selectFiles, dropFiles, removeSelected, startUpload }',
+ )
+ const photo = new File(['photo'], 'one.HEIC', { lastModified: 1 })
+ const video = new File(['video'], 'one.MOV', { lastModified: 1 })
+ const other = new File(['other'], 'two.jpg', { lastModified: 1 })
+ const input = { files: [photo, video], value: 'chosen' }
+ state.selectFiles({ target: input })
+ assert.equal(input.value, '')
+ state.dropFiles({ dataTransfer: { files: [photo, other] } })
+ assert.equal(state.selectedFiles.value.length, 3)
+ assert.equal(batches.length, 0)
+ state.removeSelected(2)
+ const pending = state.startUpload()
+ await state.startUpload()
+ assert.equal(batches.length, 1)
+ assert.deepEqual(
+ Array.from(batches[0]!).map((f) => f.name),
+ ['one.HEIC', 'one.MOV'],
+ )
+ release()
+ await pending
+ assert.equal(state.selectedFiles.value.length, 0)
+ gate = Promise.resolve()
+ state.selectFiles({
+ target: { files: [photo, video, other], value: 'chosen' },
+ })
+ await state.startUpload()
+ assert.deepEqual(
+ Array.from(state.selectedFiles.value as File[]).map((f) => f.name),
+ ['two.jpg'],
+ )
+ completed = undefined // Admission/preflight rejection must not consume staged files.
+ await state.startUpload()
+ assert.equal(state.selectedFiles.value[0].name, 'two.jpg')
+ media.admin.value = false
+ const count = batches.length
+ await state.startUpload()
+ assert.equal(batches.length, count)
+})