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(upload): assign uploaded photos to an existing album

Ahmet Ömer54c6c5e69bb5
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index 00a29022..0ea08b44 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -1,7 +1,8 @@
<script lang="ts" setup>
import type { FormSubmitEvent, TableColumn } from '@nuxt/ui'
-import type { Photo } from '~~/server/utils/db'
+import type { Album, Photo } from '~~/server/utils/db'
import type { UploadingFile } from '~~/shared/types/uploading-file'
+import { QueueWarningCode } from '~~/shared/utils/queue-warning'
import {
UploadQueueStatus,
UploadTransportStatus,
@@ -243,6 +244,7 @@ const uploadImage = async (
file: File,
existingFileId?: string,
eraseLocationOnUpload?: boolean,
+ albumId?: number,
) => {
const fileName = file.name
const fileId = existingFileId || `${Date.now()}-${fileName}`
@@ -323,100 +325,130 @@ const uploadImage = async (
uploadingFiles.value = new Map(uploadingFiles.value)
// 第二步:使用 composable 上传文件到存储
- await uploadManager.uploadFile(file, signedUrlResponse.upload || signedUrlResponse.signedUrl, {
- onProgress: (progress: UploadProgress) => {
- uploadingFile.progress = progress.percentage
- uploadingFile.uploadProgress = {
- loaded: progress.loaded,
- total: progress.total,
- percentage: progress.percentage,
- speed: progress.speed,
- timeRemaining: progress.timeRemaining,
- speedText: progress.speed ? `${formatBytes(progress.speed)}/s` : '',
- timeRemainingText: progress.timeRemaining !== undefined
- ? uploadManager.formatTime(progress.timeRemaining)
- : '',
- }
- uploadingFiles.value = new Map(uploadingFiles.value)
- },
- onStatusChange: (status: UploadTransportStatus) => {
- uploadingFile.canAbort = isInFlightTransportStatus(status)
- if (status === UploadTransportStatus.Finalizing)
- uploadingFile.status = UploadQueueStatus.Finalizing
- uploadingFiles.value = new Map(uploadingFiles.value)
- },
- onSuccess: async (_xhr: XMLHttpRequest) => {
- // 第三步:上传完成,提交到队列任务
- uploadingFile.status = UploadQueueStatus.Processing
- uploadingFile.progress = 100
- uploadingFile.canAbort = false
- uploadingFile.stage = null // 重置 stage,准备显示任务状态
- uploadingFiles.value = new Map(uploadingFiles.value)
+ await uploadManager.uploadFile(
+ file,
+ signedUrlResponse.upload || signedUrlResponse.signedUrl,
+ {
+ onProgress: (progress: UploadProgress) => {
+ uploadingFile.progress = progress.percentage
+ uploadingFile.uploadProgress = {
+ loaded: progress.loaded,
+ total: progress.total,
+ percentage: progress.percentage,
+ speed: progress.speed,
+ timeRemaining: progress.timeRemaining,
+ speedText: progress.speed ? `${formatBytes(progress.speed)}/s` : '',
+ timeRemainingText:
+ progress.timeRemaining !== undefined
+ ? uploadManager.formatTime(progress.timeRemaining)
+ : '',
+ }
+ uploadingFiles.value = new Map(uploadingFiles.value)
+ },
+ onStatusChange: (status: UploadTransportStatus) => {
+ uploadingFile.canAbort = isInFlightTransportStatus(status)
+ if (status === UploadTransportStatus.Finalizing)
+ uploadingFile.status = UploadQueueStatus.Finalizing
+ uploadingFiles.value = new Map(uploadingFiles.value)
+ },
+ onSuccess: async (_xhr: XMLHttpRequest) => {
+ // 第三步:上传完成,提交到队列任务
+ uploadingFile.status = UploadQueueStatus.Processing
+ uploadingFile.progress = 100
+ uploadingFile.canAbort = false
+ uploadingFile.stage = null // 重置 stage,准备显示任务状态
+ uploadingFiles.value = new Map(uploadingFiles.value)
- try {
- // 检查是否为MOV视频文件(通过MIME类型或文件扩展名)
- const isMovFile =
- file.type === 'video/quicktime' ||
- file.type === 'video/mp4' ||
- file.name.toLowerCase().endsWith('.mov')
-
- const resp = await $fetch('/api/queue/add-task', {
- method: 'POST',
- body: {
- payload: {
- type: isMovFile ? 'live-photo-video' : 'photo',
- storageKey: signedUrlResponse.fileKey,
- ...(isMovFile
- ? {}
- : {
- eraseLocation:
- eraseLocationOnUpload ??
- systemUploadEraseLocationDefault.value,
- }),
- },
- priority: isMovFile ? 0 : 1, // Live Photo 视频优先级更低,确保图片优先处理
- maxAttempts: 3,
- },
- })
-
- if (resp.success) {
- uploadingFile.taskId = resp.taskId
- uploadingFile.status = UploadQueueStatus.Processing
+ try {
+ // 检查是否为MOV视频文件(通过MIME类型或文件扩展名)
+ const isMovFile =
+ file.type === 'video/quicktime' ||
+ file.type === 'video/mp4' ||
+ file.name.toLowerCase().endsWith('.mov')
+
+ const enqueueTask = (targetAlbumId?: number) =>
+ $fetch('/api/queue/add-task', {
+ method: 'POST',
+ body: {
+ payload: {
+ type: isMovFile ? 'live-photo-video' : 'photo',
+ storageKey: signedUrlResponse.fileKey,
+ ...(isMovFile
+ ? {}
+ : {
+ eraseLocation:
+ eraseLocationOnUpload ??
+ systemUploadEraseLocationDefault.value,
+ ...(targetAlbumId !== undefined
+ ? { albumId: targetAlbumId }
+ : {}),
+ }),
+ },
+ priority: isMovFile ? 0 : 1, // Live Photo 视频优先级更低,确保图片优先处理
+ maxAttempts: 3,
+ },
+ })
+
+ let resp
+ let albumAssignmentUnavailable = false
+ try {
+ resp = await enqueueTask(albumId)
+ } catch (error: any) {
+ const statusCode = error.statusCode ?? error.response?.status
+ if (isMovFile || albumId === undefined || statusCode !== 404) {
+ throw error
+ }
+
+ // The image is already stored, so still process it if its album was deleted.
+ resp = await enqueueTask()
+ albumAssignmentUnavailable = true
+ }
+
+ if (resp.success) {
+ if (albumAssignmentUnavailable) {
+ const warning = $t('common.albumAssignment.missing')
+ uploadingFile.warning = uploadingFile.warning
+ ? `${uploadingFile.warning} ${warning}`
+ : warning
+ }
+ uploadingFile.taskId = resp.taskId
+ uploadingFile.status = UploadQueueStatus.Processing
+ uploadingFiles.value = new Map(uploadingFiles.value)
+
+ // 开始任务状态检查
+ startTaskStatusCheck(resp.taskId, fileId)
+ } else {
+ uploadingFile.status = UploadQueueStatus.Error
+ uploadingFile.error = $t(
+ 'dashboard.photos.messages.taskSubmitFailed',
+ )
+ uploadingFiles.value = new Map(uploadingFiles.value)
+ }
+ } catch (processError: any) {
+ uploadingFile.status = UploadQueueStatus.Error
+ uploadingFile.error = `${$t('dashboard.photos.messages.taskSubmitFailed')}: ${processError.message}`
+ uploadingFile.canAbort = false
uploadingFiles.value = new Map(uploadingFiles.value)
+ }
+ },
+ onError: (error: string) => {
+ const isConflict = /\b409\b|Conflict/i.test(error)
- // 开始任务状态检查
- startTaskStatusCheck(resp.taskId, fileId)
+ if (isConflict) {
+ uploadingFile.status = UploadQueueStatus.Blocked
+ uploadingFile.error = $t('upload.duplicate.block.message', {
+ fileName,
+ })
} else {
uploadingFile.status = UploadQueueStatus.Error
- uploadingFile.error = $t(
- 'dashboard.photos.messages.taskSubmitFailed',
- )
- uploadingFiles.value = new Map(uploadingFiles.value)
+ uploadingFile.error = error
}
- } catch (processError: any) {
- uploadingFile.status = UploadQueueStatus.Error
- uploadingFile.error = `${$t('dashboard.photos.messages.taskSubmitFailed')}: ${processError.message}`
+
uploadingFile.canAbort = false
uploadingFiles.value = new Map(uploadingFiles.value)
- }
- },
- onError: (error: string) => {
- const isConflict = /\b409\b|Conflict/i.test(error)
-
- if (isConflict) {
- uploadingFile.status = UploadQueueStatus.Blocked
- uploadingFile.error = $t('upload.duplicate.block.message', {
- fileName,
- })
- } else {
- uploadingFile.status = UploadQueueStatus.Error
- uploadingFile.error = error
- }
-
- uploadingFile.canAbort = false
- uploadingFiles.value = new Map(uploadingFiles.value)
+ },
},
- })
+ )
} catch (error: any) {
uploadingFile.status = UploadQueueStatus.Error
uploadingFile.canAbort = false
@@ -470,6 +502,63 @@ const toast = useToast()
const selectedFiles = ref<File[]>([])
const isUploadSlideoverOpen = ref(false)
const uploadEraseLocationEnabled = ref(systemUploadEraseLocationDefault.value)
+type UploadAlbumOption = Pick<Album, 'id' | 'title' | 'isHidden'> & {
+ photoCount: number
+ hasPassword: boolean
+}
+
+const uploadAlbums = ref<UploadAlbumOption[]>([])
+const selectedUploadAlbumId = ref(0)
+const isLoadingUploadAlbums = ref(false)
+const uploadAlbumsLoadFailed = ref(false)
+let uploadAlbumsRequestId = 0
+
+const uploadAlbumItems = computed(() => [
+ {
+ label: $t('dashboard.photos.slideover.album.none'),
+ icon: 'tabler:album-off',
+ value: 0,
+ },
+ ...uploadAlbums.value.map((album) => ({
+ label: album.title,
+ description: [
+ $t('plural.photo', { count: album.photoCount }, album.photoCount),
+ album.isHidden
+ ? $t('dashboard.photos.slideover.album.hidden')
+ : undefined,
+ album.hasPassword
+ ? $t('dashboard.photos.slideover.album.passwordProtected')
+ : undefined,
+ ]
+ .filter(Boolean)
+ .join(' · '),
+ icon: album.hasPassword
+ ? 'tabler:lock'
+ : album.isHidden
+ ? 'tabler:eye-off'
+ : 'tabler:album',
+ value: album.id,
+ })),
+])
+
+const loadUploadAlbums = async () => {
+ const requestId = ++uploadAlbumsRequestId
+ isLoadingUploadAlbums.value = true
+ uploadAlbumsLoadFailed.value = false
+ try {
+ const albums = await $fetch('/api/albums/upload-options')
+ if (requestId === uploadAlbumsRequestId) uploadAlbums.value = albums
+ } catch (error) {
+ if (requestId !== uploadAlbumsRequestId) return
+ console.error('Failed to load upload albums:', error)
+ uploadAlbums.value = []
+ uploadAlbumsLoadFailed.value = true
+ } finally {
+ if (requestId === uploadAlbumsRequestId) {
+ isLoadingUploadAlbums.value = false
+ }
+ }
+}
const hasSelectedFiles = computed(() => selectedFiles.value.length > 0)
@@ -500,14 +589,18 @@ const clearSelectedFiles = () => {
watch(isUploadSlideoverOpen, (open) => {
if (!open) {
+ uploadAlbumsRequestId++
+ isLoadingUploadAlbums.value = false
clearSelectedFiles()
uploadEraseLocationEnabled.value = systemUploadEraseLocationDefault.value
+ selectedUploadAlbumId.value = 0
}
})
const openUploadSlideover = () => {
uploadEraseLocationEnabled.value = systemUploadEraseLocationDefault.value
isUploadSlideoverOpen.value = true
+ void loadUploadAlbums()
}
watch(isEditModalOpen, (open) => {
@@ -617,6 +710,12 @@ const startTaskStatusCheck = (taskId: number, fileId: string) => {
// 任务完成
uploadingFile.status = UploadQueueStatus.Completed
uploadingFile.stage = null
+ if (response.warningCode === QueueWarningCode.AlbumMissing) {
+ const warning = $t('common.albumAssignment.missing')
+ uploadingFile.warning = uploadingFile.warning
+ ? `${uploadingFile.warning} ${warning}`
+ : warning
+ }
uploadingFiles.value = new Map(uploadingFiles.value)
// 停止状态检查
@@ -1096,6 +1195,7 @@ const validateFile = (
const handleUpload = async () => {
const fileList = [...selectedFiles.value]
const eraseLocation = uploadEraseLocationEnabled.value
+ const albumId = selectedUploadAlbumId.value || undefined
if (fileList.length === 0) {
return
@@ -1201,7 +1301,7 @@ const handleUpload = async () => {
const startUpload = async (file: File): Promise<void> => {
const fileId = fileIdMapping.get(file)!
try {
- await uploadImage(file, fileId, eraseLocation)
+ await uploadImage(file, fileId, eraseLocation, albumId)
} catch (error: any) {
errors.push(`${file.name}: ${error.message || '上传失败'}`)
console.error('上传错误:', error)
@@ -2137,6 +2237,55 @@ onUnmounted(() => {
}"
/>
+ <UCard
+ variant="soft"
+ class="border border-neutral-200/80 dark:border-neutral-800/80"
+ >
+ <UFormField
+ :label="$t('dashboard.photos.slideover.album.label')"
+ :description="
+ $t('dashboard.photos.slideover.album.description')
+ "
+ >
+ <USelectMenu
+ v-model="selectedUploadAlbumId"
+ :items="uploadAlbumItems"
+ value-key="value"
+ label-key="label"
+ :loading="isLoadingUploadAlbums"
+ :disabled="isLoadingUploadAlbums || uploadAlbumsLoadFailed"
+ :aria-label="$t('dashboard.photos.slideover.album.label')"
+ class="w-full"
+ />
+ </UFormField>
+ <p
+ v-if="
+ !isLoadingUploadAlbums &&
+ !uploadAlbumsLoadFailed &&
+ uploadAlbums.length === 0
+ "
+ class="mt-2 text-xs text-neutral-500 dark:text-neutral-400"
+ >
+ {{ $t('dashboard.photos.slideover.album.empty') }}
+ </p>
+ <div
+ v-if="uploadAlbumsLoadFailed"
+ role="alert"
+ class="mt-2 flex items-center justify-between gap-2 text-sm text-error-600 dark:text-error-400"
+ >
+ <span>{{
+ $t('dashboard.photos.slideover.album.loadFailed')
+ }}</span>
+ <UButton
+ size="xs"
+ variant="soft"
+ @click="loadUploadAlbums"
+ >
+ {{ $t('dashboard.photos.slideover.album.retry') }}
+ </UButton>
+ </div>
+ </UCard>
+
<UCard
variant="soft"
class="border border-neutral-200/80 dark:border-neutral-800/80"
diff --git a/app/pages/dashboard/queue.vue b/app/pages/dashboard/queue.vue
index f88de7a3..07e4a7f7 100644
--- a/app/pages/dashboard/queue.vue
+++ b/app/pages/dashboard/queue.vue
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { h, resolveComponent } from 'vue'
import type { TableColumn } from '@nuxt/ui'
+import { QueueWarningCode } from '~~/shared/utils/queue-warning'
const UButton = resolveComponent('UButton')
@@ -394,12 +395,27 @@ useIntervalFn(refreshData, 10000) // 每10秒刷新一次
<!-- 状态 -->
<template #status-cell="{ row }">
- <UBadge
- :label="$t(`dashboard.queue.status.${row.original.status}`)"
- variant="soft"
- :color="getStatusColor(row.original.status)"
- size="sm"
- />
+ <div class="flex flex-wrap items-center gap-1">
+ <UBadge
+ :label="$t(`dashboard.queue.status.${row.original.status}`)"
+ variant="soft"
+ :color="getStatusColor(row.original.status)"
+ size="sm"
+ />
+ <UBadge
+ v-if="
+ row.original.status === 'completed' &&
+ row.original.warningCode === QueueWarningCode.AlbumMissing
+ "
+ :label="$t('common.albumAssignment.warning')"
+ :aria-label="$t('common.albumAssignment.missing')"
+ :title="$t('common.albumAssignment.missing')"
+ icon="tabler:alert-triangle"
+ color="warning"
+ variant="soft"
+ size="sm"
+ />
+ </div>
</template>
<!-- 尝试次数 -->
@@ -494,6 +510,19 @@ useIntervalFn(refreshData, 10000) // 每10秒刷新一次
</div>
</div>
+ <UAlert
+ v-if="
+ row.original.status === 'completed' &&
+ row.original.warningCode ===
+ QueueWarningCode.AlbumMissing
+ "
+ class="mt-3"
+ color="warning"
+ variant="soft"
+ icon="tabler:alert-triangle"
+ :description="$t('common.albumAssignment.missing')"
+ />
+
<!-- 错误信息(仅在失败状态显示) -->
<div
v-if="
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index c49799fd..0c61ff71 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -514,6 +514,10 @@
"delete": "Delete",
"edit": "Edit"
},
+ "albumAssignment": {
+ "warning": "Album not added",
+ "missing": "The photo was uploaded, but the selected album was deleted before it could be added."
+ },
"unknownError": "Unknown error"
},
"upload": {
@@ -672,6 +676,16 @@
"slideover": {
"title": "Upload Photos",
"description": "Drag and drop or select files. Photos will be automatically processed and added to the library.",
+ "album": {
+ "label": "Add to album (optional)",
+ "description": "Every successfully uploaded photo in this batch will be added to the selected album.",
+ "none": "No album",
+ "empty": "No albums available yet.",
+ "hidden": "Hidden",
+ "passwordProtected": "Password protected",
+ "loadFailed": "Could not load albums. You can retry or upload without an album.",
+ "retry": "Retry"
+ },
"options": {
"eraseLocation": {
"label": "Erase location info on upload",
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index 1b04e186..65f7bdb6 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -514,6 +514,10 @@
"delete": "削除",
"edit": "編集"
},
+ "albumAssignment": {
+ "warning": "アルバムに追加できませんでした",
+ "missing": "写真はアップロードされましたが、追加前に選択したアルバムが削除されました。"
+ },
"unknownError": "不明なエラー"
},
"dashboard": {
@@ -597,6 +601,16 @@
"slideover": {
"title": "写真をアップロード",
"description": "ドラッグアンドドロップするか、ファイルを選択します。写真は自動的に処理され、ライブラリに追加されます。",
+ "album": {
+ "label": "アルバムに追加(任意)",
+ "description": "この一括アップロードで正常に処理された写真を、選択したアルバムに追加します。",
+ "none": "アルバムなし",
+ "empty": "利用できるアルバムがまだありません。",
+ "hidden": "非表示",
+ "passwordProtected": "パスワード保護",
+ "loadFailed": "アルバムを読み込めませんでした。再試行するか、アルバムなしでアップロードできます。",
+ "retry": "再試行"
+ },
"options": {
"eraseLocation": {
"label": "アップロード時に位置情報を抹消",
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index 08dceacd..e70cf384 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -514,6 +514,10 @@
"delete": "Удалить",
"edit": "Редактировать"
},
+ "albumAssignment": {
+ "warning": "Не добавлено в альбом",
+ "missing": "Фотография загружена, но выбранный альбом был удалён до её добавления."
+ },
"unknownError": "Неизвестная ошибка"
},
"upload": {
@@ -672,6 +676,16 @@
"slideover": {
"title": "Загрузка фотографий",
"description": "Перетащите или выберите файлы. Фотографии будут автоматически обработаны и добавлены в библиотеку.",
+ "album": {
+ "label": "Добавить в альбом (необязательно)",
+ "description": "Все успешно обработанные фотографии из этой загрузки будут добавлены в выбранный альбом.",
+ "none": "Без альбома",
+ "empty": "Пока нет доступных альбомов.",
+ "hidden": "Скрытый",
+ "passwordProtected": "Защищён паролем",
+ "loadFailed": "Не удалось загрузить альбомы. Повторите попытку или загрузите без альбома.",
+ "retry": "Повторить"
+ },
"options": {
"eraseLocation": {
"label": "Удалять данные о местоположении при загрузке",
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index fb0c293d..e968cf5f 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -514,6 +514,10 @@
"delete": "删除",
"edit": "编辑"
},
+ "albumAssignment": {
+ "warning": "未添加到相簿",
+ "missing": "照片已上传,但所选相簿在添加照片前已被删除。"
+ },
"unknownError": "未知错误"
},
"upload": {
@@ -672,6 +676,16 @@
"slideover": {
"title": "上传照片",
"description": "拖拽或选择文件。上传后照片会自动处理并添加到库中。",
+ "album": {
+ "label": "添加到相簿(可选)",
+ "description": "此批次中成功处理的照片都会添加到所选相簿。",
+ "none": "不添加到相簿",
+ "empty": "暂无可用相簿。",
+ "hidden": "已隐藏",
+ "passwordProtected": "密码保护",
+ "loadFailed": "无法加载相簿。你可以重试,或不选择相簿直接上传。",
+ "retry": "重试"
+ },
"options": {
"eraseLocation": {
"label": "上传时抹除位置信息",
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index 149a3bfb..f47f4588 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -514,6 +514,10 @@
"delete": "刪除",
"edit": "編輯"
},
+ "albumAssignment": {
+ "warning": "未加入相簿",
+ "missing": "相片已上傳,但所選相簿在新增相片前已被刪除。"
+ },
"unknownError": "未知錯誤"
},
"dashboard": {
@@ -597,6 +601,16 @@
"slideover": {
"title": "上傳相片",
"description": "拖曳或選擇檔案。相片會自動處理並新增到圖庫。",
+ "album": {
+ "label": "新增至相簿(可選)",
+ "description": "此批次中成功處理的相片都會新增至所選相簿。",
+ "none": "不加入相簿",
+ "empty": "暫無可用相簿。",
+ "hidden": "已隱藏",
+ "passwordProtected": "密碼保護",
+ "loadFailed": "無法載入相簿。你可以重試,或不選相簿直接上傳。",
+ "retry": "重試"
+ },
"options": {
"eraseLocation": {
"label": "上傳時抹除位置資訊",
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index 0867eeeb..1d9adb56 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -514,6 +514,10 @@
"delete": "刪除",
"edit": "編輯"
},
+ "albumAssignment": {
+ "warning": "未加入相簿",
+ "missing": "照片已上傳,但所選相簿在加入照片前已被刪除。"
+ },
"unknownError": "未知錯誤"
},
"dashboard": {
@@ -597,6 +601,16 @@
"slideover": {
"title": "上傳照片",
"description": "拖曳或選擇檔案。照片會自動處理並新增到圖庫。",
+ "album": {
+ "label": "加入相簿(可選)",
+ "description": "此批次中成功處理的照片都會加入所選相簿。",
+ "none": "不加入相簿",
+ "empty": "目前沒有可用相簿。",
+ "hidden": "已隱藏",
+ "passwordProtected": "密碼保護",
+ "loadFailed": "無法載入相簿。你可以重試,或不選相簿直接上傳。",
+ "retry": "重試"
+ },
"options": {
"eraseLocation": {
"label": "上傳時抹除位置資訊",
diff --git a/server/api/albums/upload-options.get.ts b/server/api/albums/upload-options.get.ts
new file mode 100644
index 00000000..64a61817
--- /dev/null
+++ b/server/api/albums/upload-options.get.ts
@@ -0,0 +1,6 @@
+import { listUploadAlbumOptions } from '../../utils/upload-album-options'
+
+export default defineEventHandler(async (event) => {
+ await requireUserSession(event)
+ return listUploadAlbumOptions(useDB())
+})
diff --git a/server/api/queue/add-task.post.ts b/server/api/queue/add-task.post.ts
index c1cff048..cc19faeb 100644
--- a/server/api/queue/add-task.post.ts
+++ b/server/api/queue/add-task.post.ts
@@ -1,40 +1,26 @@
import { z } from 'zod'
+import {
+ photoTaskAlbumExists,
+ queuePayloadSchema,
+} from '../../utils/queue-payload'
export default defineEventHandler(async (event) => {
await requireUserSession(event)
try {
- const payloadSchema = z.discriminatedUnion('type', [
- z.object({
- type: z.literal('photo'),
- storageKey: z.string().nonempty(),
- eraseLocation: z.boolean().optional(),
- }),
- z.object({
- type: z.literal('live-photo-video'),
- storageKey: z.string().nonempty(),
- }),
- z.object({
- type: z.literal('photo-reverse-geocoding'),
- photoId: z.string().min(1),
- latitude: z.number().min(-90).max(90).optional(),
- longitude: z.number().min(-180).max(180).optional(),
- }),
- z.object({
- type: z.literal('photo-erase-location'),
- photoId: z.string().min(1),
- }),
- ])
-
const { payload, priority, maxAttempts } = await readValidatedBody(
event,
z.object({
- payload: payloadSchema,
+ payload: queuePayloadSchema,
priority: z.number().min(0).max(9).optional().default(0),
maxAttempts: z.number().min(1).max(5).optional().default(3),
}).parse,
)
+ if (!photoTaskAlbumExists(payload)) {
+ throw createError({ statusCode: 404, statusMessage: 'Album not found' })
+ }
+
const workerPool = globalThis.__workerPool
if (!workerPool) {
diff --git a/server/api/queue/add-tasks.post.ts b/server/api/queue/add-tasks.post.ts
index 1b0155ec..2ee91b16 100644
--- a/server/api/queue/add-tasks.post.ts
+++ b/server/api/queue/add-tasks.post.ts
@@ -1,31 +1,13 @@
import { z } from 'zod'
+import {
+ createPhotoTaskAlbumChecker,
+ queuePayloadSchema,
+} from '../../utils/queue-payload'
export default defineEventHandler(async (event) => {
await requireUserSession(event)
try {
- const payloadSchema = z.discriminatedUnion('type', [
- z.object({
- type: z.literal('photo'),
- storageKey: z.string().nonempty(),
- eraseLocation: z.boolean().optional(),
- }),
- z.object({
- type: z.literal('live-photo-video'),
- storageKey: z.string().nonempty(),
- }),
- z.object({
- type: z.literal('photo-reverse-geocoding'),
- photoId: z.string().min(1),
- latitude: z.number().min(-90).max(90).optional(),
- longitude: z.number().min(-180).max(180).optional(),
- }),
- z.object({
- type: z.literal('photo-erase-location'),
- photoId: z.string().min(1),
- }),
- ])
-
const { tasks, defaultPriority, defaultMaxAttempts } =
await readValidatedBody(
event,
@@ -33,7 +15,7 @@ export default defineEventHandler(async (event) => {
tasks: z
.array(
z.object({
- payload: payloadSchema,
+ payload: queuePayloadSchema,
priority: z.number().min(0).max(9).optional(),
maxAttempts: z.number().min(1).max(5).optional(),
}),
@@ -56,11 +38,16 @@ export default defineEventHandler(async (event) => {
const results = []
const errors = []
+ const albumExists = createPhotoTaskAlbumChecker()
- for (let i = 0; i < tasks.length; i++) {
- const task = tasks[i]
-
+ for (const [i, task] of tasks.entries()) {
try {
+ if (!albumExists(task.payload)) {
+ throw createError({
+ statusCode: 404,
+ statusMessage: 'Album not found',
+ })
+ }
const taskId = await workerPool.addTask(task.payload, {
priority: task.priority ?? defaultPriority,
maxAttempts: task.maxAttempts ?? defaultMaxAttempts,
diff --git a/server/api/queue/recent.get.ts b/server/api/queue/recent.get.ts
index ab5ecf98..70b682da 100644
--- a/server/api/queue/recent.get.ts
+++ b/server/api/queue/recent.get.ts
@@ -42,6 +42,7 @@ export default defineEventHandler(async (event) => {
status: pipelineQueue.status,
statusStage: pipelineQueue.statusStage,
errorMessage: pipelineQueue.errorMessage,
+ warningCode: pipelineQueue.warningCode,
createdAt: pipelineQueue.createdAt,
completedAt: pipelineQueue.completedAt,
})
@@ -109,6 +110,7 @@ export default defineEventHandler(async (event) => {
status: task.status,
statusStage: task.statusStage,
errorMessage: task.errorMessage,
+ warningCode: task.warningCode,
createdAt: task.createdAt,
completedAt: task.completedAt,
fileName: getFileName(storageKey),
diff --git a/server/api/queue/task/list.get.ts b/server/api/queue/task/list.get.ts
index b60b1ffa..d04f3825 100644
--- a/server/api/queue/task/list.get.ts
+++ b/server/api/queue/task/list.get.ts
@@ -54,6 +54,7 @@ export default defineEventHandler(async (event) => {
status: tables.pipelineQueue.status,
statusStage: tables.pipelineQueue.statusStage,
errorMessage: tables.pipelineQueue.errorMessage,
+ warningCode: tables.pipelineQueue.warningCode,
createdAt: tables.pipelineQueue.createdAt,
completedAt: tables.pipelineQueue.completedAt,
})
diff --git a/server/api/queue/task/retry-batch.post.ts b/server/api/queue/task/retry-batch.post.ts
index 34fc956a..e45b61f5 100644
--- a/server/api/queue/task/retry-batch.post.ts
+++ b/server/api/queue/task/retry-batch.post.ts
@@ -62,6 +62,7 @@ export default defineEventHandler(async (event) => {
status: 'pending',
statusStage: null,
errorMessage: null,
+ warningCode: null,
attempts: 0, // 重置尝试次数
availableAt: new Date(),
})
diff --git a/server/api/queue/task/retry.post.ts b/server/api/queue/task/retry.post.ts
index 67e6e0a9..b20b668b 100644
--- a/server/api/queue/task/retry.post.ts
+++ b/server/api/queue/task/retry.post.ts
@@ -45,6 +45,7 @@ export default defineEventHandler(async (event) => {
status: 'pending',
statusStage: null,
errorMessage: null,
+ warningCode: null,
attempts: 0, // 重置尝试次数
availableAt: new Date(),
})
diff --git a/server/database/migrations/0016_upload_album_membership.sql b/server/database/migrations/0016_upload_album_membership.sql
new file mode 100644
index 00000000..8a13e149
--- /dev/null
+++ b/server/database/migrations/0016_upload_album_membership.sql
@@ -0,0 +1,12 @@
+ALTER TABLE `pipeline_queue` ADD `warning_code` text;--> statement-breakpoint
+DELETE FROM `album_photos`
+WHERE `id` IN (
+ SELECT `id` FROM (
+ SELECT `id`, ROW_NUMBER() OVER (
+ PARTITION BY `album_id`, `photo_id`
+ ORDER BY `position`, `id`
+ ) AS `rank`
+ FROM `album_photos`
+ ) WHERE `rank` > 1
+);--> statement-breakpoint
+CREATE UNIQUE INDEX `album_photos_album_photo_unique` ON `album_photos` (`album_id`,`photo_id`);
diff --git a/server/database/migrations/meta/0016_snapshot.json b/server/database/migrations/meta/0016_snapshot.json
new file mode 100644
index 00000000..9af6fbc7
--- /dev/null
+++ b/server/database/migrations/meta/0016_snapshot.json
@@ -0,0 +1,900 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "1e0252aa-1354-4133-a0de-6e8f2928afae",
+ "prevId": "606e2e14-6684-430a-bdeb-ef753e664f47",
+ "tables": {
+ "album_photos": {
+ "name": "album_photos",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "album_id": {
+ "name": "album_id",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "position": {
+ "name": "position",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 1000000
+ },
+ "added_at": {
+ "name": "added_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {
+ "album_photos_album_photo_unique": {
+ "name": "album_photos_album_photo_unique",
+ "columns": [
+ "album_id",
+ "photo_id"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "album_photos_album_id_albums_id_fk": {
+ "name": "album_photos_album_id_albums_id_fk",
+ "tableFrom": "album_photos",
+ "tableTo": "albums",
+ "columnsFrom": [
+ "album_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ },
+ "album_photos_photo_id_photos_id_fk": {
+ "name": "album_photos_photo_id_photos_id_fk",
+ "tableFrom": "album_photos",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "albums": {
+ "name": "albums",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "title": {
+ "name": "title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "cover_photo_id": {
+ "name": "cover_photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_hidden": {
+ "name": "is_hidden",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "password_hash": {
+ "name": "password_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "position": {
+ "name": "position",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "albums_cover_photo_id_photos_id_fk": {
+ "name": "albums_cover_photo_id_photos_id_fk",
+ "tableFrom": "albums",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "cover_photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photo_access_keys": {
+ "name": "photo_access_keys",
+ "columns": {
+ "access_key": {
+ "name": "access_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "idx_photo_access_keys_photo_id": {
+ "name": "idx_photo_access_keys_photo_id",
+ "columns": [
+ "photo_id"
+ ],
+ "isUnique": false
+ }
+ },
+ "foreignKeys": {
+ "photo_access_keys_photo_id_photos_id_fk": {
+ "name": "photo_access_keys_photo_id_photos_id_fk",
+ "tableFrom": "photo_access_keys",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {
+ "photo_access_keys_access_key_photo_id_pk": {
+ "columns": [
+ "access_key",
+ "photo_id"
+ ],
+ "name": "photo_access_keys_access_key_photo_id_pk"
+ }
+ },
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photo_reactions": {
+ "name": "photo_reactions",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "photo_id": {
+ "name": "photo_id",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "reaction_type": {
+ "name": "reaction_type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "fingerprint": {
+ "name": "fingerprint",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "ip_address": {
+ "name": "ip_address",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "user_agent": {
+ "name": "user_agent",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {
+ "photo_reactions_photo_id_photos_id_fk": {
+ "name": "photo_reactions_photo_id_photos_id_fk",
+ "tableFrom": "photo_reactions",
+ "tableTo": "photos",
+ "columnsFrom": [
+ "photo_id"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "cascade",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "photos": {
+ "name": "photos",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "text",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "title": {
+ "name": "title",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "width": {
+ "name": "width",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "height": {
+ "name": "height",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "aspect_ratio": {
+ "name": "aspect_ratio",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "date_taken": {
+ "name": "date_taken",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "storage_key": {
+ "name": "storage_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_key": {
+ "name": "thumbnail_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "file_size": {
+ "name": "file_size",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "last_modified": {
+ "name": "last_modified",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "original_url": {
+ "name": "original_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_url": {
+ "name": "thumbnail_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "thumbnail_hash": {
+ "name": "thumbnail_hash",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "tags": {
+ "name": "tags",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "exif": {
+ "name": "exif",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "latitude": {
+ "name": "latitude",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "longitude": {
+ "name": "longitude",
+ "type": "real",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "country": {
+ "name": "country",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "city": {
+ "name": "city",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "location_name": {
+ "name": "location_name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_live_photo": {
+ "name": "is_live_photo",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "live_photo_video_url": {
+ "name": "live_photo_video_url",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "live_photo_video_key": {
+ "name": "live_photo_video_key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "photos_id_unique": {
+ "name": "photos_id_unique",
+ "columns": [
+ "id"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "pipeline_queue": {
+ "name": "pipeline_queue",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "payload": {
+ "name": "payload",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'{\"type\":\"photo\",\"storageKey\":\"\"}'"
+ },
+ "priority": {
+ "name": "priority",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "available_at": {
+ "name": "available_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "0"
+ },
+ "attempts": {
+ "name": "attempts",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ },
+ "max_attempts": {
+ "name": "max_attempts",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 3
+ },
+ "status": {
+ "name": "status",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'pending'"
+ },
+ "status_stage": {
+ "name": "status_stage",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "error_message": {
+ "name": "error_message",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "warning_code": {
+ "name": "warning_code",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "completed_at": {
+ "name": "completed_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "idx_pipeline_queue_pending": {
+ "name": "idx_pipeline_queue_pending",
+ "columns": [
+ "\"priority\" DESC",
+ "created_at",
+ "id"
+ ],
+ "isUnique": false,
+ "where": "\"pipeline_queue\".\"status\" = 'pending'"
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings": {
+ "name": "settings",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "namespace": {
+ "name": "namespace",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "'common'"
+ },
+ "key": {
+ "name": "key",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "type": {
+ "name": "type",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "value": {
+ "name": "value",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "default_value": {
+ "name": "default_value",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "label": {
+ "name": "label",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "description": {
+ "name": "description",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "is_public": {
+ "name": "is_public",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_readonly": {
+ "name": "is_readonly",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "is_secret": {
+ "name": "is_secret",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": false
+ },
+ "enum": {
+ "name": "enum",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_by": {
+ "name": "updated_by",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ }
+ },
+ "indexes": {
+ "idx_namespace_key": {
+ "name": "idx_namespace_key",
+ "columns": [
+ "namespace",
+ "key"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {
+ "settings_updated_by_users_id_fk": {
+ "name": "settings_updated_by_users_id_fk",
+ "tableFrom": "settings",
+ "tableTo": "users",
+ "columnsFrom": [
+ "updated_by"
+ ],
+ "columnsTo": [
+ "id"
+ ],
+ "onDelete": "set null",
+ "onUpdate": "no action"
+ }
+ },
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "settings_storage_providers": {
+ "name": "settings_storage_providers",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "provider": {
+ "name": "provider",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "config": {
+ "name": "config",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ },
+ "updated_at": {
+ "name": "updated_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": "(unixepoch())"
+ }
+ },
+ "indexes": {},
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ },
+ "users": {
+ "name": "users",
+ "columns": {
+ "id": {
+ "name": "id",
+ "type": "integer",
+ "primaryKey": true,
+ "notNull": true,
+ "autoincrement": true
+ },
+ "name": {
+ "name": "name",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "email": {
+ "name": "email",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "password": {
+ "name": "password",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "avatar": {
+ "name": "avatar",
+ "type": "text",
+ "primaryKey": false,
+ "notNull": false,
+ "autoincrement": false
+ },
+ "created_at": {
+ "name": "created_at",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false
+ },
+ "is_admin": {
+ "name": "is_admin",
+ "type": "integer",
+ "primaryKey": false,
+ "notNull": true,
+ "autoincrement": false,
+ "default": 0
+ }
+ },
+ "indexes": {
+ "users_name_unique": {
+ "name": "users_name_unique",
+ "columns": [
+ "name"
+ ],
+ "isUnique": true
+ },
+ "users_email_unique": {
+ "name": "users_email_unique",
+ "columns": [
+ "email"
+ ],
+ "isUnique": true
+ }
+ },
+ "foreignKeys": {},
+ "compositePrimaryKeys": {},
+ "uniqueConstraints": {},
+ "checkConstraints": {}
+ }
+ },
+ "views": {},
+ "enums": {},
+ "_meta": {
+ "schemas": {},
+ "tables": {},
+ "columns": {}
+ },
+ "internal": {
+ "indexes": {
+ "idx_pipeline_queue_pending": {
+ "columns": {
+ "\"priority\" DESC": {
+ "isExpression": true
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/server/database/migrations/meta/_journal.json b/server/database/migrations/meta/_journal.json
index 7103d90c..30410abb 100644
--- a/server/database/migrations/meta/_journal.json
+++ b/server/database/migrations/meta/_journal.json
@@ -113,6 +113,13 @@
"when": 1791717320179,
"tag": "0015_smart_titanium_man",
"breakpoints": true
+ },
+ {
+ "idx": 16,
+ "version": "6",
+ "when": 1792244529968,
+ "tag": "0016_upload_album_membership",
+ "breakpoints": true
}
]
}
\ No newline at end of file
diff --git a/server/database/schema.ts b/server/database/schema.ts
index 41b3a652..feee41e4 100644
--- a/server/database/schema.ts
+++ b/server/database/schema.ts
@@ -8,6 +8,7 @@ import {
text,
uniqueIndex,
} from 'drizzle-orm/sqlite-core'
+import { QUEUE_WARNING_CODES } from '../../shared/utils/queue-warning.ts'
import type { NeededExif } from '~~/shared/types/photo'
import type { StorageConfig } from '../services/storage'
@@ -16,6 +17,7 @@ type PipelineQueuePayload =
type: 'photo'
storageKey: string
eraseLocation?: boolean
+ albumId?: number
}
| {
type: 'live-photo-video'
@@ -129,6 +131,7 @@ export const pipelineQueue = sqliteTable(
],
}),
errorMessage: text('error_message'),
+ warningCode: text('warning_code', { enum: QUEUE_WARNING_CODES }),
createdAt: integer('created_at', { mode: 'timestamp' })
.notNull()
.default(sql`(unixepoch())`),
@@ -183,19 +186,25 @@ export const albums = sqliteTable('albums', {
})
// 相簿-照片 多对多关系表
-export const albumPhotos = sqliteTable('album_photos', {
- id: integer('id').primaryKey({ autoIncrement: true }),
- albumId: integer('album_id')
- .notNull()
- .references(() => albums.id, { onDelete: 'cascade' }),
- photoId: text('photo_id')
- .notNull()
- .references(() => photos.id, { onDelete: 'cascade' }),
- position: real('position').notNull().default(1000000),
- addedAt: integer('added_at', { mode: 'timestamp' })
- .notNull()
- .default(sql`(unixepoch())`),
-})
+export const albumPhotos = sqliteTable(
+ 'album_photos',
+ {
+ id: integer('id').primaryKey({ autoIncrement: true }),
+ albumId: integer('album_id')
+ .notNull()
+ .references(() => albums.id, { onDelete: 'cascade' }),
+ photoId: text('photo_id')
+ .notNull()
+ .references(() => photos.id, { onDelete: 'cascade' }),
+ position: real('position').notNull().default(1000000),
+ addedAt: integer('added_at', { mode: 'timestamp' })
+ .notNull()
+ .default(sql`(unixepoch())`),
+ },
+ (t) => [
+ uniqueIndex('album_photos_album_photo_unique').on(t.albumId, t.photoId),
+ ],
+)
export const settings = sqliteTable(
'settings',
diff --git a/server/services/pipeline-queue/manager.ts b/server/services/pipeline-queue/manager.ts
index 4ccc08f5..e9ed317c 100644
--- a/server/services/pipeline-queue/manager.ts
+++ b/server/services/pipeline-queue/manager.ts
@@ -9,6 +9,7 @@ import type {
PipelineQueueItem,
Photo,
} from '~~/server/utils/db'
+import { QueueWarningCode } from '~~/shared/utils/queue-warning'
import { compressUint8Array } from '~~/shared/utils/u8array'
import {
preprocessImageWithJpegUpload,
@@ -24,6 +25,7 @@ import { settingsManager } from '../settings/settingsManager'
import { findLivePhotoVideoForImage } from '../video/livephoto'
import { processMotionPhotoFromXmp } from '../video/motion-photo'
import { getStorageManager } from '~~/server/plugins/3.storage'
+import { persistPhotoWithAlbum } from '~~/server/utils/photo-album'
const EXIF_LOCATION_KEYS = [
'GPSAltitude',
@@ -211,13 +213,17 @@ export class QueueManager {
* 标记任务为已完成
* @param taskId 任务ID
*/
- async markTaskCompleted(taskId: number): Promise<void> {
+ async markTaskCompleted(
+ taskId: number,
+ warningCode: PipelineQueueItem['warningCode'] = null,
+ ): Promise<void> {
const db = useDB()
await db
.update(tables.pipelineQueue)
.set({
status: 'completed',
completedAt: sql`(unixepoch())`,
+ warningCode,
})
.where(eq(tables.pipelineQueue.id, taskId))
}
@@ -255,6 +261,7 @@ export class QueueManager {
status: shouldRetry ? 'pending' : 'failed',
attempts: newAttempts,
errorMessage: errorMessage || 'Unknown error',
+ warningCode: null,
// 如果重试,设置延迟重试时间
statusStage: null,
...(shouldRetry
@@ -485,11 +492,16 @@ export class QueueManager {
null,
}
- const db = useDB()
- await db.insert(tables.photos).values(result).onConflictDoUpdate({
- target: tables.photos.id,
- set: result,
- })
+ const assignment = persistPhotoWithAlbum(
+ useDB(),
+ result,
+ payload.albumId,
+ )
+ if (assignment === QueueWarningCode.AlbumMissing) {
+ this.logger.warn(
+ `Task ${taskId}: album ${payload.albumId} was deleted before photo ${photoId} could be added`,
+ )
+ }
if (shouldAutoEraseLocationOnUpload) {
try {
@@ -512,7 +524,9 @@ export class QueueManager {
}
this.logger.success(`Task ${taskId} processed successfully`)
- return result
+ return assignment === QueueWarningCode.AlbumMissing
+ ? QueueWarningCode.AlbumMissing
+ : null
} catch (error) {
this.logger.error(`Task ${taskId} processing failed`, error)
throw error
@@ -838,12 +852,13 @@ export class QueueManager {
try {
const { type } = task.payload
+ let warningCode: PipelineQueueItem['warningCode'] = null
switch (type) {
case 'live-photo-video':
await this.processors.livePhotoDetect(task)
break
case 'photo':
- await this.processors.photo(task)
+ warningCode = await this.processors.photo(task)
break
case 'photo-reverse-geocoding':
await this.processors.reverseGeocoding(task)
@@ -855,23 +870,11 @@ export class QueueManager {
throw new PermanentTaskError(`Unknown task type: ${type}`)
}
- await this.markTaskCompleted(task.id)
+ await this.markTaskCompleted(task.id, warningCode)
this.processedCount++
this.logger.success(
`[${this.workerId}] Task ${task.id} processed successfully (Total: ${this.processedCount})`,
)
-
- // const result = await this.processTask(task)
- // if (result) {
- // await this.markTaskCompleted(task.id)
- // this.processedCount++
- // this.logger.success(
- // `[${this.workerId}] Task ${task.id} processed successfully (Total: ${this.processedCount})`,
- // )
- // } else {
- // await this.markTaskFailed(task.id, 'Processing result is empty')
- // this.errorCount++
- // }
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error)
diff --git a/server/utils/db.ts b/server/utils/db.ts
index 4bf0dd87..d3b768c1 100644
--- a/server/utils/db.ts
+++ b/server/utils/db.ts
@@ -1,7 +1,7 @@
import { drizzle } from 'drizzle-orm/better-sqlite3'
import Database from 'better-sqlite3'
-import * as schema from '../database/schema'
+import * as schema from '../database/schema.ts'
export const tables = schema
export { eq, and, or, inArray } from 'drizzle-orm'
diff --git a/server/utils/photo-album.ts b/server/utils/photo-album.ts
new file mode 100644
index 00000000..d129c818
--- /dev/null
+++ b/server/utils/photo-album.ts
@@ -0,0 +1,61 @@
+import { and, eq, max } from 'drizzle-orm'
+import type { drizzle } from 'drizzle-orm/better-sqlite3'
+import * as tables from '../database/schema.ts'
+import { QueueWarningCode } from '../../shared/utils/queue-warning.ts'
+
+export type AlbumAssignmentResult =
+ | 'no-album'
+ | typeof QueueWarningCode.AlbumMissing
+ | 'already-added'
+ | 'added'
+
+export const persistPhotoWithAlbum = (
+ db: ReturnType<typeof drizzle>,
+ photo: typeof tables.photos.$inferInsert,
+ albumId?: number,
+): AlbumAssignmentResult =>
+ db.transaction((tx) => {
+ tx.insert(tables.photos)
+ .values(photo)
+ .onConflictDoUpdate({ target: tables.photos.id, set: photo })
+ .run()
+
+ if (albumId === undefined) return 'no-album'
+
+ const album = tx
+ .select({ id: tables.albums.id })
+ .from(tables.albums)
+ .where(eq(tables.albums.id, albumId))
+ .get()
+ if (!album) return QueueWarningCode.AlbumMissing
+
+ const existing = tx
+ .select({ id: tables.albumPhotos.id })
+ .from(tables.albumPhotos)
+ .where(
+ and(
+ eq(tables.albumPhotos.albumId, albumId),
+ eq(tables.albumPhotos.photoId, photo.id),
+ ),
+ )
+ .get()
+ if (existing) return 'already-added'
+
+ const last = tx
+ .select({ position: max(tables.albumPhotos.position) })
+ .from(tables.albumPhotos)
+ .where(eq(tables.albumPhotos.albumId, albumId))
+ .get()
+
+ const insertResult = tx
+ .insert(tables.albumPhotos)
+ .values({
+ albumId,
+ photoId: photo.id,
+ position: (last?.position ?? 1000000) + 10,
+ })
+ .onConflictDoNothing()
+ .run()
+
+ return insertResult.changes > 0 ? 'added' : 'already-added'
+ })
diff --git a/server/utils/queue-payload.ts b/server/utils/queue-payload.ts
new file mode 100644
index 00000000..7935cb09
--- /dev/null
+++ b/server/utils/queue-payload.ts
@@ -0,0 +1,61 @@
+import { z } from 'zod'
+import { eq } from 'drizzle-orm'
+import * as tables from '../database/schema.ts'
+import { useDB } from './db.ts'
+
+export const queuePayloadSchema = z.discriminatedUnion('type', [
+ z.object({
+ type: z.literal('photo'),
+ storageKey: z.string().nonempty(),
+ eraseLocation: z.boolean().optional(),
+ albumId: z.number().int().positive().optional(),
+ }),
+ z
+ .object({
+ type: z.literal('live-photo-video'),
+ storageKey: z.string().nonempty(),
+ })
+ .strict(),
+ z.object({
+ type: z.literal('photo-reverse-geocoding'),
+ photoId: z.string().min(1),
+ latitude: z.number().min(-90).max(90).optional(),
+ longitude: z.number().min(-180).max(180).optional(),
+ }),
+ z.object({
+ type: z.literal('photo-erase-location'),
+ photoId: z.string().min(1),
+ }),
+])
+
+export const photoTaskAlbumExists = (
+ payload: z.infer<typeof queuePayloadSchema>,
+ db: ReturnType<typeof useDB> = useDB(),
+): boolean => {
+ if (payload.type !== 'photo' || payload.albumId === undefined) return true
+
+ const album = db
+ .select({ id: tables.albums.id })
+ .from(tables.albums)
+ .where(eq(tables.albums.id, payload.albumId))
+ .get()
+
+ return Boolean(album)
+}
+
+export const createPhotoTaskAlbumChecker = (
+ db: ReturnType<typeof useDB> = useDB(),
+) => {
+ const cache = new Map<number, boolean>()
+
+ return (payload: z.infer<typeof queuePayloadSchema>): boolean => {
+ if (payload.type !== 'photo' || payload.albumId === undefined) return true
+
+ const cached = cache.get(payload.albumId)
+ if (cached !== undefined) return cached
+
+ const exists = photoTaskAlbumExists(payload, db)
+ cache.set(payload.albumId, exists)
+ return exists
+ }
+}
diff --git a/server/utils/upload-album-options.ts b/server/utils/upload-album-options.ts
new file mode 100644
index 00000000..0c3c49c6
--- /dev/null
+++ b/server/utils/upload-album-options.ts
@@ -0,0 +1,27 @@
+import { asc, count, desc, eq } from 'drizzle-orm'
+import type { drizzle } from 'drizzle-orm/better-sqlite3'
+import * as tables from '../database/schema.ts'
+
+export const listUploadAlbumOptions = (db: ReturnType<typeof drizzle>) => {
+ const albums = db
+ .select({
+ id: tables.albums.id,
+ title: tables.albums.title,
+ isHidden: tables.albums.isHidden,
+ passwordHash: tables.albums.passwordHash,
+ photoCount: count(tables.albumPhotos.id),
+ })
+ .from(tables.albums)
+ .leftJoin(
+ tables.albumPhotos,
+ eq(tables.albumPhotos.albumId, tables.albums.id),
+ )
+ .groupBy(tables.albums.id)
+ .orderBy(asc(tables.albums.position), desc(tables.albums.createdAt))
+ .all()
+
+ return albums.map(({ passwordHash, ...album }) => ({
+ ...album,
+ hasPassword: Boolean(passwordHash),
+ }))
+}
diff --git a/shared/utils/queue-warning.ts b/shared/utils/queue-warning.ts
new file mode 100644
index 00000000..3973a38c
--- /dev/null
+++ b/shared/utils/queue-warning.ts
@@ -0,0 +1,17 @@
+/**
+ * Warning codes attached to completed pipeline queue tasks.
+ *
+ * The values are persisted in `pipeline_queue.warning_code` and returned by
+ * the queue APIs. Keep them stable.
+ */
+
+export const QueueWarningCode = {
+ AlbumMissing: 'album-missing',
+} as const
+
+export type QueueWarningCode =
+ (typeof QueueWarningCode)[keyof typeof QueueWarningCode]
+
+export const QUEUE_WARNING_CODES: [QueueWarningCode, ...QueueWarningCode[]] = [
+ QueueWarningCode.AlbumMissing,
+]
diff --git a/tests/upload-album-batch.test.ts b/tests/upload-album-batch.test.ts
new file mode 100644
index 00000000..321db5d0
--- /dev/null
+++ b/tests/upload-album-batch.test.ts
@@ -0,0 +1,47 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import * as schema from '../server/database/schema.ts'
+import { createPhotoTaskAlbumChecker } from '../server/utils/queue-payload.ts'
+
+test('batch album checker queries once per distinct album ID', () => {
+ const queries: string[] = []
+ const sqlite = new Database(':memory:', {
+ verbose: (query) => queries.push(query),
+ })
+ try {
+ sqlite.exec('CREATE TABLE albums (id INTEGER PRIMARY KEY)')
+ sqlite.exec('INSERT INTO albums (id) VALUES (7)')
+ const db = drizzle(sqlite, { schema })
+ const albumExists = createPhotoTaskAlbumChecker(db)
+
+ assert.equal(
+ albumExists({ type: 'photo', storageKey: 'a.jpg', albumId: 7 }),
+ true,
+ )
+ assert.equal(
+ albumExists({ type: 'photo', storageKey: 'b.jpg', albumId: 7 }),
+ true,
+ )
+ assert.equal(
+ albumExists({ type: 'photo', storageKey: 'c.jpg', albumId: 8 }),
+ false,
+ )
+ assert.equal(
+ albumExists({ type: 'photo', storageKey: 'd.jpg', albumId: 8 }),
+ false,
+ )
+ assert.equal(albumExists({ type: 'photo', storageKey: 'e.jpg' }), true)
+ assert.equal(
+ albumExists({ type: 'live-photo-video', storageKey: 'f.mov' }),
+ true,
+ )
+ assert.equal(
+ queries.filter((query) => /select.*from.*albums/i.test(query)).length,
+ 2,
+ )
+ } finally {
+ sqlite.close()
+ }
+})
diff --git a/tests/upload-album-membership.test.ts b/tests/upload-album-membership.test.ts
new file mode 100644
index 00000000..84e0a694
--- /dev/null
+++ b/tests/upload-album-membership.test.ts
@@ -0,0 +1,156 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { readFileSync } from 'node:fs'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
+import * as schema from '../server/database/schema.ts'
+import { persistPhotoWithAlbum } from '../server/utils/photo-album.ts'
+
+const setup = () => {
+ const sqlite = new Database(':memory:')
+ const db = drizzle(sqlite, { schema })
+ migrate(db, { migrationsFolder: './server/database/migrations' })
+ return { sqlite, db }
+}
+
+test('successful photo processing appends once and preserves album order', () => {
+ const { sqlite, db } = setup()
+ try {
+ const album = db
+ .insert(schema.albums)
+ .values({ title: 'Trip' })
+ .returning()
+ .get()
+ db.insert(schema.photos).values({ id: 'existing' }).run()
+ db.insert(schema.albumPhotos)
+ .values({ albumId: album.id, photoId: 'existing', position: 25 })
+ .run()
+ assert.equal(persistPhotoWithAlbum(db, { id: 'first' }, album.id), 'added')
+ assert.equal(persistPhotoWithAlbum(db, { id: 'second' }, album.id), 'added')
+
+ const beforeRetry = db
+ .select()
+ .from(schema.albumPhotos)
+ .orderBy(schema.albumPhotos.position)
+ .all()
+ assert.deepEqual(
+ beforeRetry.map(({ photoId }) => photoId),
+ ['existing', 'first', 'second'],
+ )
+ assert.equal(beforeRetry[0]!.position, 25)
+ assert.ok(beforeRetry[1]!.position > beforeRetry[0]!.position)
+ assert.ok(beforeRetry[2]!.position > beforeRetry[1]!.position)
+
+ assert.equal(
+ persistPhotoWithAlbum(db, { id: 'first', title: 'Updated' }, album.id),
+ 'already-added',
+ )
+ const afterRetry = db
+ .select()
+ .from(schema.albumPhotos)
+ .orderBy(schema.albumPhotos.position)
+ .all()
+ assert.equal(afterRetry.length, 3)
+ assert.equal(afterRetry[1]!.position, beforeRetry[1]!.position)
+ assert.equal(
+ db
+ .select()
+ .from(schema.photos)
+ .all()
+ .find(({ id }) => id === 'first')!.title,
+ 'Updated',
+ )
+ } finally {
+ sqlite.close()
+ }
+})
+
+test('a race that skips the membership insert reports already added', () => {
+ const { sqlite, db } = setup()
+ try {
+ const album = db
+ .insert(schema.albums)
+ .values({ title: 'Trip' })
+ .returning()
+ .get()
+
+ // Simulate a concurrent writer winning the unique-index race after the
+ // existence check but before the insert.
+ sqlite.exec(`
+ CREATE TRIGGER skip_album_photo_insert
+ BEFORE INSERT ON album_photos
+ BEGIN
+ SELECT RAISE(IGNORE);
+ END
+ `)
+
+ assert.equal(
+ persistPhotoWithAlbum(db, { id: 'raced' }, album.id),
+ 'already-added',
+ )
+ assert.equal(db.select().from(schema.albumPhotos).all().length, 0)
+ } finally {
+ sqlite.close()
+ }
+})
+
+test('no target and deleted target both preserve the processed photo', () => {
+ const { sqlite, db } = setup()
+ try {
+ assert.equal(persistPhotoWithAlbum(db, { id: 'plain' }), 'no-album')
+ assert.equal(
+ persistPhotoWithAlbum(db, { id: 'missing-target' }, 999),
+ 'album-missing',
+ )
+ assert.deepEqual(
+ db
+ .select()
+ .from(schema.photos)
+ .all()
+ .map(({ id }) => id),
+ ['plain', 'missing-target'],
+ )
+ assert.equal(db.select().from(schema.albumPhotos).all().length, 0)
+ } finally {
+ sqlite.close()
+ }
+})
+
+test('migration keeps the earliest album position while removing legacy duplicates', () => {
+ const sqlite = new Database(':memory:')
+ try {
+ sqlite.exec('CREATE TABLE pipeline_queue (id INTEGER PRIMARY KEY)')
+ sqlite.exec(
+ 'CREATE TABLE album_photos (id INTEGER PRIMARY KEY, album_id INTEGER, photo_id TEXT, position REAL)',
+ )
+ sqlite.exec(`INSERT INTO album_photos VALUES
+ (1, 1, 'photo', 30),
+ (2, 1, 'photo', 10),
+ (3, 1, 'photo', 20),
+ (4, 2, 'photo', 15)`)
+
+ const migration = readFileSync(
+ 'server/database/migrations/0016_upload_album_membership.sql',
+ 'utf8',
+ )
+ for (const statement of migration.split('--> statement-breakpoint')) {
+ if (statement.trim()) sqlite.exec(statement)
+ }
+
+ assert.deepEqual(
+ sqlite
+ .prepare('SELECT id, position FROM album_photos ORDER BY album_id')
+ .all(),
+ [
+ { id: 2, position: 10 },
+ { id: 4, position: 15 },
+ ],
+ )
+ assert.throws(() =>
+ sqlite.exec("INSERT INTO album_photos VALUES (5, 1, 'photo', 40)"),
+ )
+ } finally {
+ sqlite.close()
+ }
+})
diff --git a/tests/upload-album-options.test.ts b/tests/upload-album-options.test.ts
new file mode 100644
index 00000000..596a2d8a
--- /dev/null
+++ b/tests/upload-album-options.test.ts
@@ -0,0 +1,53 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
+import * as schema from '../server/database/schema.ts'
+import { listUploadAlbumOptions } from '../server/utils/upload-album-options.ts'
+
+test('upload album options include metadata in album order', () => {
+ const sqlite = new Database(':memory:')
+ try {
+ const db = drizzle(sqlite, { schema })
+ migrate(db, { migrationsFolder: './server/database/migrations' })
+ const later = db
+ .insert(schema.albums)
+ .values({
+ title: 'Later',
+ description: 'Summer photos',
+ position: 20,
+ passwordHash: 'test-hash',
+ })
+ .returning()
+ .get()
+ const first = db
+ .insert(schema.albums)
+ .values({ title: 'First', position: 10, isHidden: true })
+ .returning()
+ .get()
+ db.insert(schema.photos).values({ id: 'photo' }).run()
+ db.insert(schema.albumPhotos)
+ .values({ albumId: later.id, photoId: 'photo' })
+ .run()
+
+ assert.deepEqual(listUploadAlbumOptions(db), [
+ {
+ id: first.id,
+ title: 'First',
+ photoCount: 0,
+ isHidden: true,
+ hasPassword: false,
+ },
+ {
+ id: later.id,
+ title: 'Later',
+ photoCount: 1,
+ isHidden: false,
+ hasPassword: true,
+ },
+ ])
+ } finally {
+ sqlite.close()
+ }
+})
diff --git a/tests/upload-album-queue.test.ts b/tests/upload-album-queue.test.ts
new file mode 100644
index 00000000..960abe9c
--- /dev/null
+++ b/tests/upload-album-queue.test.ts
@@ -0,0 +1,81 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import Database from 'better-sqlite3'
+import { drizzle } from 'drizzle-orm/better-sqlite3'
+import * as schema from '../server/database/schema.ts'
+import {
+ photoTaskAlbumExists,
+ queuePayloadSchema,
+} from '../server/utils/queue-payload.ts'
+
+test('photo queue payload accepts an optional positive album ID', () => {
+ assert.deepEqual(
+ queuePayloadSchema.parse({ type: 'photo', storageKey: 'image.jpg' }),
+ { type: 'photo', storageKey: 'image.jpg' },
+ )
+ assert.deepEqual(
+ queuePayloadSchema.parse({
+ type: 'photo',
+ storageKey: 'image.jpg',
+ albumId: 7,
+ }),
+ { type: 'photo', storageKey: 'image.jpg', albumId: 7 },
+ )
+})
+
+test('photo queue payload rejects invalid albums and video album IDs', () => {
+ for (const albumId of [0, -1, 1.5, '7', null]) {
+ assert.equal(
+ queuePayloadSchema.safeParse({
+ type: 'photo',
+ storageKey: 'image.jpg',
+ albumId,
+ }).success,
+ false,
+ )
+ }
+ assert.equal(
+ queuePayloadSchema.safeParse({
+ type: 'live-photo-video',
+ storageKey: 'image.mov',
+ albumId: 7,
+ }).success,
+ false,
+ )
+ assert.equal(
+ queuePayloadSchema.safeParse({
+ type: 'live-photo-video',
+ storageKey: 'image.mov',
+ }).success,
+ true,
+ )
+})
+
+test('photo tasks only target existing albums', () => {
+ const sqlite = new Database(':memory:')
+ try {
+ sqlite.exec('CREATE TABLE albums (id INTEGER PRIMARY KEY)')
+ sqlite.exec('INSERT INTO albums (id) VALUES (7)')
+ const db = drizzle(sqlite, { schema })
+ assert.equal(
+ photoTaskAlbumExists(
+ { type: 'photo', storageKey: 'image.jpg', albumId: 7 },
+ db,
+ ),
+ true,
+ )
+ assert.equal(
+ photoTaskAlbumExists({ type: 'photo', storageKey: 'image.jpg' }, db),
+ true,
+ )
+ assert.equal(
+ photoTaskAlbumExists(
+ { type: 'photo', storageKey: 'image.jpg', albumId: 8 },
+ db,
+ ),
+ false,
+ )
+ } finally {
+ sqlite.close()
+ }
+})