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(livephoto): LivePhoto 实况照片处理和管理,上传 HEIC 和 MOV 时通过文件名自动配对实况

Timothy Yin09db319b1705
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index c1c029d1..636c18e0 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -197,6 +197,34 @@ const totalRowsCount = computed((): number => {
return table.value?.tableApi?.getFilteredRowModel().rows.length || 0
})
+// 计算 LivePhoto 统计
+const livePhotoStats = computed(() => {
+ if (!data.value) return { total: 0, livePhotos: 0, staticPhotos: 0 }
+
+ const total = data.value.length
+ const livePhotos = data.value.filter((photo: Photo) => photo.isLivePhoto).length
+ const staticPhotos = total - livePhotos
+
+ return { total, livePhotos, staticPhotos }
+})
+
+// 照片过滤器
+const photoFilter = ref<'all' | 'livephoto' | 'static'>('all')
+
+// 过滤后的数据
+const filteredData = computed(() => {
+ if (!data.value) return []
+
+ switch (photoFilter.value) {
+ case 'livephoto':
+ return data.value.filter((photo: Photo) => photo.isLivePhoto)
+ case 'static':
+ return data.value.filter((photo: Photo) => !photo.isLivePhoto)
+ default:
+ return data.value
+ }
+})
+
// 状态检查间隔
let statusInterval: NodeJS.Timeout | null = null
@@ -228,6 +256,32 @@ const columns: TableColumn<Photo>[] = [
accessorKey: 'title',
header: '照片标题',
},
+ {
+ accessorKey: 'isLivePhoto',
+ header: 'Live Photo',
+ cell: ({ row }) => {
+ const isLivePhoto = row.original.isLivePhoto
+ return h('div', { class: 'flex items-center gap-2' }, [
+ isLivePhoto
+ ? h('div', { class: 'flex items-center gap-1' }, [
+ h('div', {
+ class: 'size-2 bg-blue-500 rounded-full animate-pulse'
+ }),
+ h('span', {
+ class: 'text-blue-600 dark:text-blue-400 text-xs font-medium'
+ }, 'Live Photo')
+ ])
+ : h('span', {
+ class: 'text-gray-400 text-xs'
+ }, '静态照片')
+ ])
+ },
+ sortingFn: (rowA, rowB) => {
+ const valueA = rowA.original.isLivePhoto ? 1 : 0
+ const valueB = rowB.original.isLivePhoto ? 1 : 0
+ return valueB - valueA // LivePhoto 优先排序
+ }
+ },
{
accessorKey: 'lastModified',
header: '最后修改',
@@ -286,13 +340,24 @@ const checkProcessingStatus = async () => {
// 文件验证函数
const validateFile = (file: File): { valid: boolean; error?: string } => {
// 检查文件类型
- // const allowedTypes = ['image/jpeg', 'image/png', 'image/heic', 'image/heif']
- // if (!allowedTypes.includes(file.type)) {
- // return {
- // valid: false,
- // error: `不支持的文件格式: ${file.type}。请选择 JPEG、PNG 或 HEIC 格式的图片。`
- // }
- // }
+ const allowedTypes = [
+ 'image/jpeg',
+ 'image/png',
+ 'image/heic',
+ 'image/heif',
+ 'video/quicktime', // MOV 文件
+ 'video/mp4' // MP4 文件(备用)
+ ]
+
+ const isValidImageType = allowedTypes.includes(file.type)
+ const isValidVideoExtension = file.name.toLowerCase().endsWith('.mov')
+
+ if (!isValidImageType && !isValidVideoExtension) {
+ return {
+ valid: false,
+ error: `不支持的文件格式: ${file.type}。请选择 JPEG、PNG、HEIC 格式的图片或 MOV 格式的 LivePhoto 视频。`
+ }
+ }
// 检查文件大小 (128MB 限制)
const maxSize = 128 * 1024 * 1024 // 128MB
@@ -326,10 +391,11 @@ const handleUpload = async () => {
}
const fileName = file.name
+ const isVideoFile = fileName.toLowerCase().endsWith('.mov')
processingFiles.value.add(fileName)
toast.add({
- title: '开始上传照片',
+ title: isVideoFile ? '开始上传 LivePhoto 视频' : '开始上传照片',
description: `正在上传 ${fileName} 到云存储...`,
color: 'info',
})
@@ -369,6 +435,90 @@ const handleDelete = async (photoId: string) => {
refresh()
}
+// LivePhoto 相关操作
+const handleViewLivePhoto = async (photoId: string) => {
+ try {
+ const livePhotoInfo = await $fetch(`/api/photos/${photoId}/livephoto`)
+
+ if (livePhotoInfo.isLivePhoto && livePhotoInfo.livePhotoVideoUrl) {
+ // 设置模态框数据
+ selectedLivePhoto.value = {
+ id: livePhotoInfo.id,
+ title: livePhotoInfo.title,
+ originalUrl: livePhotoInfo.originalUrl || '',
+ videoUrl: livePhotoInfo.livePhotoVideoUrl
+ }
+ isLivePhotoModalOpen.value = true
+ } else {
+ toast.add({
+ title: '不是 LivePhoto',
+ description: '该照片不包含 LivePhoto 视频',
+ color: 'warning',
+ })
+ }
+ } catch (error) {
+ console.error('获取 LivePhoto 信息失败:', error)
+ toast.add({
+ title: '操作失败',
+ description: '无法获取 LivePhoto 信息',
+ color: 'error',
+ })
+ }
+}
+
+// LivePhoto 模态框相关状态
+const isLivePhotoModalOpen = ref(false)
+const selectedLivePhoto = ref<{
+ id: string
+ title: string | null
+ originalUrl: string
+ videoUrl: string
+} | null>(null)
+
+// 打开链接的辅助函数
+const openInNewTab = (url: string) => {
+ if (typeof window !== 'undefined') {
+ window.open(url, '_blank')
+ }
+}
+
+const handleUpdateLivePhoto = async (photoId: string) => {
+ try {
+ const updateToast = toast.add({
+ title: '正在检查 LivePhoto',
+ description: '正在检查是否有对应的视频文件...',
+ color: 'info',
+ })
+
+ const result = await $fetch('/api/photos/livephoto-manage', {
+ method: 'POST',
+ body: {
+ action: 'update-photo',
+ photoId: photoId,
+ },
+ }) as any
+
+ const isSuccess = 'success' in result && result.success
+
+ toast.update(updateToast.id, {
+ title: isSuccess ? 'LivePhoto 更新成功' : 'LivePhoto 更新失败',
+ description: result.message,
+ color: isSuccess ? 'success' : 'warning',
+ })
+
+ if (isSuccess) {
+ await refresh()
+ }
+ } catch (error: any) {
+ console.error('LivePhoto 更新失败:', error)
+ toast.add({
+ title: 'LivePhoto 更新失败',
+ description: error.message || '更新过程中发生错误',
+ color: 'error',
+ })
+ }
+}
+
// 批量删除功能
const handleBatchDelete = async () => {
const selectedRowModel = table.value?.tableApi?.getFilteredSelectedRowModel()
@@ -420,6 +570,44 @@ const handleBatchDelete = async () => {
}
}
+// 批量检测 LivePhoto 功能
+const handleBatchDetectLivePhoto = async () => {
+ try {
+ const detectToast = toast.add({
+ title: '正在批量检测 LivePhoto',
+ description: '正在检查所有照片的 LivePhoto 状态...',
+ color: 'info',
+ })
+
+ const result = await $fetch('/api/photos/livephoto-detect', {
+ method: 'POST',
+ body: {
+ action: 'batch-detect',
+ },
+ }) as any
+
+ const foundCount = result.results?.found || 0
+ const totalCount = result.results?.total || 0
+
+ toast.update(detectToast.id, {
+ title: 'LivePhoto 检测完成',
+ description: `检测了 ${totalCount} 张照片,发现 ${foundCount} 个潜在的 LivePhoto`,
+ color: foundCount > 0 ? 'success' : 'info',
+ })
+
+ if (foundCount > 0) {
+ await refresh()
+ }
+ } catch (error: any) {
+ console.error('批量检测 LivePhoto 失败:', error)
+ toast.add({
+ title: '批量检测失败',
+ description: error.message || '检测过程中发生错误',
+ color: 'error',
+ })
+ }
+}
+
// 清理定时器
onUnmounted(() => {
if (statusInterval) {
@@ -532,10 +720,10 @@ onUnmounted(() => {
<UFileUpload
v-model="selectedFiles"
label="选择照片"
- description="支持 JPEG、PNG、HEIC 格式,最大 256MB"
+ description="支持 JPEG、PNG、HEIC 格式照片,以及 MOV 格式 LivePhoto 视频,最大 256MB"
layout="list"
size="xl"
- accept="image/jpeg,image/png,image/heic,image/heif"
+ accept="image/jpeg,image/png,image/heic,image/heif,video/quicktime,.mov"
multiple
/>
<UButton
@@ -550,6 +738,81 @@ onUnmounted(() => {
</UButton>
</div>
+ <!-- 工具栏 -->
+ <div class="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-900/50 border border-gray-200 dark:border-gray-700 rounded-lg">
+ <div class="flex items-center gap-2">
+ <UIcon
+ name="tabler:photo"
+ class="text-gray-500"
+ />
+ <span class="font-medium text-gray-700 dark:text-gray-300">
+ 照片管理
+ </span>
+ <div class="flex items-center gap-2">
+ <UBadge
+ v-if="livePhotoStats.total > 0"
+ variant="soft"
+ color="info"
+ >
+ {{ livePhotoStats.total }} 张照片
+ </UBadge>
+ <UBadge
+ v-if="livePhotoStats.livePhotos > 0"
+ variant="soft"
+ color="success"
+ >
+ {{ livePhotoStats.livePhotos }} Live Photo
+ </UBadge>
+ <UBadge
+ v-if="livePhotoStats.staticPhotos > 0"
+ variant="soft"
+ color="neutral"
+ >
+ {{ livePhotoStats.staticPhotos }} 静态照片
+ </UBadge>
+ </div>
+ </div>
+
+ <div class="flex items-center gap-2">
+ <!-- 过滤器 -->
+ <USelectMenu
+ v-model="photoFilter"
+ class="w-48"
+ :items="[
+ { label: '全部照片', value: 'all', icon: 'tabler:photo-scan' },
+ { label: 'Live Photo', value: 'livephoto', icon: 'tabler:live-photo' },
+ { label: '静态照片', value: 'static', icon: 'tabler:photo' }
+ ]"
+ value-key="value"
+ label-key="label"
+ size="sm"
+ >
+ </USelectMenu>
+
+ <!-- LivePhoto 批量检测按钮 -->
+ <UButton
+ variant="soft"
+ color="info"
+ size="sm"
+ icon="tabler:scan"
+ @click="handleBatchDetectLivePhoto"
+ >
+ 批量检测 LivePhoto
+ </UButton>
+
+ <!-- 刷新按钮 -->
+ <UButton
+ variant="ghost"
+ color="neutral"
+ size="sm"
+ icon="tabler:refresh"
+ @click="() => refresh()"
+ >
+ 刷新
+ </UButton>
+ </div>
+ </div>
+
<!-- 批量操作栏 -->
<div
v-if="selectedRowsCount > 0"
@@ -591,7 +854,7 @@ onUnmounted(() => {
<UTable
ref="table"
v-model:row-selection="rowSelection"
- :data="data as Photo[]"
+ :data="filteredData as Photo[]"
:columns="columns"
:loading="status === 'pending'"
sticky
@@ -602,14 +865,42 @@ onUnmounted(() => {
}"
>
<template #actions-cell="{ row }">
- <UButton
- size="sm"
- variant="soft"
- color="error"
- @click="handleDelete(row.original.id)"
- >
- 删除
- </UButton>
+ <div class="flex items-center gap-2">
+ <!-- LivePhoto 相关操作 -->
+ <template v-if="row.original.isLivePhoto">
+ <UButton
+ size="sm"
+ variant="soft"
+ color="info"
+ icon="tabler:live-photo"
+ @click="handleViewLivePhoto(row.original.id)"
+ >
+ Live Photo 预览
+ </UButton>
+ </template>
+ <template v-else>
+ <UButton
+ size="sm"
+ variant="ghost"
+ color="neutral"
+ icon="tabler:refresh"
+ @click="handleUpdateLivePhoto(row.original.id)"
+ >
+ 检测 LivePhoto
+ </UButton>
+ </template>
+
+ <!-- 删除按钮 -->
+ <UButton
+ size="sm"
+ variant="soft"
+ color="error"
+ icon="tabler:trash"
+ @click="handleDelete(row.original.id)"
+ >
+ 删除
+ </UButton>
+ </div>
</template>
</UTable>
@@ -620,6 +911,81 @@ onUnmounted(() => {
{{ selectedRowsCount }} / {{ totalRowsCount }} 行已选择
</div>
</div>
+
+ <!-- LivePhoto 预览模态框 -->
+ <UModal v-model:open="isLivePhotoModalOpen">
+ <template #content>
+ <div class="p-6">
+ <div class="flex items-center justify-between mb-4">
+ <h3 class="text-lg font-semibold">
+ Live Photo: {{ selectedLivePhoto?.title || 'Untitled' }}
+ </h3>
+ <UButton
+ variant="ghost"
+ color="neutral"
+ size="sm"
+ icon="tabler:x"
+ @click="isLivePhotoModalOpen = false"
+ />
+ </div>
+
+ <div v-if="selectedLivePhoto" class="space-y-4">
+ <!-- 静态图片预览 -->
+ <div class="space-y-2">
+ <h4 class="font-medium text-sm text-gray-600 dark:text-gray-400">
+ 静态图片
+ </h4>
+ <div class="bg-gray-100 dark:bg-gray-800 rounded-lg p-4 flex justify-center">
+ <img
+ :src="selectedLivePhoto.originalUrl"
+ :alt="selectedLivePhoto.title || 'Live Photo'"
+ class="max-h-64 object-contain rounded"
+ />
+ </div>
+ </div>
+
+ <!-- 视频预览 -->
+ <div class="space-y-2">
+ <h4 class="font-medium text-sm text-gray-600 dark:text-gray-400">
+ Live Photo 视频
+ </h4>
+ <div class="bg-gray-100 dark:bg-gray-800 rounded-lg p-4 flex justify-center">
+ <video
+ :src="selectedLivePhoto.videoUrl"
+ controls
+ autoplay
+ loop
+ muted
+ class="max-h-64 object-contain rounded"
+ >
+ 您的浏览器不支持视频播放
+ </video>
+ </div>
+ </div>
+
+ <!-- 操作按钮 -->
+ <div class="flex justify-end gap-2 pt-4 border-t">
+ <UButton
+ variant="ghost"
+ color="neutral"
+ icon="tabler:external-link"
+ @click="() => { if (selectedLivePhoto) openInNewTab(selectedLivePhoto.videoUrl) }"
+ >
+ 在新窗口打开视频
+ </UButton>
+ <UButton
+ variant="soft"
+ color="info"
+ icon="tabler:download"
+ @click="() => { if (selectedLivePhoto) openInNewTab(selectedLivePhoto.videoUrl) }"
+ >
+ 下载视频
+ </UButton>
+ </div>
+ </div>
+ </div>
+ </template>
+ </UModal>
</div>
</template>
diff --git a/server/api/photos/[photoId]/livephoto.get.ts b/server/api/photos/[photoId]/livephoto.get.ts
new file mode 100644
index 00000000..2646ee10
--- /dev/null
+++ b/server/api/photos/[photoId]/livephoto.get.ts
@@ -0,0 +1,49 @@
+import { eq } from 'drizzle-orm'
+
+export default eventHandler(async (event) => {
+ await requireUserSession(event)
+
+ const photoId = getRouterParam(event, 'photoId')
+
+ if (!photoId) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Photo ID is required',
+ })
+ }
+
+ try {
+ const db = useDB()
+
+ // 查询照片信息
+ const photos = await db
+ .select()
+ .from(tables.photos)
+ .where(eq(tables.photos.id, photoId))
+ .limit(1)
+
+ if (photos.length === 0) {
+ throw createError({
+ statusCode: 404,
+ statusMessage: 'Photo not found',
+ })
+ }
+
+ const photo = photos[0]
+
+ return {
+ id: photo.id,
+ title: photo.title,
+ isLivePhoto: Boolean(photo.isLivePhoto),
+ livePhotoVideoUrl: photo.livePhotoVideoUrl,
+ originalUrl: photo.originalUrl,
+ thumbnailUrl: photo.thumbnailUrl,
+ }
+ } catch (error) {
+ logger.chrono.error('Failed to get photo details:', error)
+ throw createError({
+ statusCode: 500,
+ statusMessage: 'Failed to get photo details',
+ })
+ }
+})
diff --git a/server/api/photos/livephoto-detect.post.ts b/server/api/photos/livephoto-detect.post.ts
new file mode 100644
index 00000000..36755d2a
--- /dev/null
+++ b/server/api/photos/livephoto-detect.post.ts
@@ -0,0 +1,39 @@
+import { batchTestLivePhotoDetection } from '~~/server/services/video/test-utils'
+
+export default eventHandler(async (event) => {
+ await requireUserSession(event)
+
+ const body = await readBody(event)
+ const { action } = body
+
+ if (!action) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Action is required',
+ })
+ }
+
+ try {
+ switch (action) {
+ case 'batch-detect':
+ // 批量检测现有照片的 LivePhoto 视频
+ const results = await batchTestLivePhotoDetection()
+ return {
+ message: 'Batch LivePhoto detection completed',
+ results
+ }
+
+ default:
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Invalid action. Use "batch-detect"',
+ })
+ }
+ } catch (error) {
+ logger.chrono.error('LivePhoto detection error:', error)
+ throw createError({
+ statusCode: 500,
+ statusMessage: 'Failed to perform LivePhoto detection',
+ })
+ }
+})
diff --git a/server/api/photos/livephoto-manage.post.ts b/server/api/photos/livephoto-manage.post.ts
new file mode 100644
index 00000000..2fa157f0
--- /dev/null
+++ b/server/api/photos/livephoto-manage.post.ts
@@ -0,0 +1,110 @@
+import { processSpecificLivePhotoVideo, scanAndProcessExistingLivePhotos } from '~~/server/services/video/scanner'
+import { eq } from 'drizzle-orm'
+import { findLivePhotoVideoForImage } from '~~/server/services/video/livephoto'
+import { getStorageManager } from '~~/server/plugins/storage'
+
+export default eventHandler(async (event) => {
+ await requireUserSession(event)
+
+ const body = await readBody(event)
+ const { action, videoKey, photoId } = body
+
+ if (!action) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Action is required',
+ })
+ }
+
+ try {
+ switch (action) {
+ case 'scan':
+ // 扫描现有文件
+ const scanResults = await scanAndProcessExistingLivePhotos()
+ return {
+ message: 'Scan completed',
+ results: scanResults
+ }
+
+ case 'process':
+ // 处理特定文件
+ if (!videoKey) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'videoKey is required for process action',
+ })
+ }
+
+ const success = await processSpecificLivePhotoVideo(videoKey)
+ return {
+ message: success ? 'LivePhoto processed successfully' : 'Failed to process LivePhoto',
+ success,
+ videoKey
+ }
+
+ case 'update-photo':
+ // 为特定照片检查和更新 LivePhoto 状态
+ if (!photoId) {
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'photoId is required for update-photo action',
+ })
+ }
+
+ const db = useDB()
+ const photos = await db
+ .select()
+ .from(tables.photos)
+ .where(eq(tables.photos.id, photoId))
+ .limit(1)
+
+ if (photos.length === 0) {
+ throw createError({
+ statusCode: 404,
+ statusMessage: 'Photo not found',
+ })
+ }
+
+ const photo = photos[0]
+ const livePhotoVideo = await findLivePhotoVideoForImage(photo.storageKey!)
+
+ if (livePhotoVideo) {
+ const storageProvider = getStorageManager().getProvider()
+
+ await db
+ .update(tables.photos)
+ .set({
+ isLivePhoto: 1,
+ livePhotoVideoUrl: storageProvider.getPublicUrl(livePhotoVideo.videoKey),
+ livePhotoVideoKey: livePhotoVideo.videoKey,
+ })
+ .where(eq(tables.photos.id, photoId))
+
+ return {
+ message: 'Photo updated to LivePhoto successfully',
+ success: true,
+ photoId,
+ videoKey: livePhotoVideo.videoKey
+ }
+ } else {
+ return {
+ message: 'No matching video found for this photo',
+ success: false,
+ photoId
+ }
+ }
+
+ default:
+ throw createError({
+ statusCode: 400,
+ statusMessage: 'Invalid action. Use "scan", "process", or "update-photo"',
+ })
+ }
+ } catch (error) {
+ logger.chrono.error('LivePhoto management error:', error)
+ throw createError({
+ statusCode: 500,
+ statusMessage: 'Failed to process LivePhoto management request',
+ })
+ }
+})
diff --git a/server/api/photos/process.post.ts b/server/api/photos/process.post.ts
index 6fa0793f..1474d6a4 100644
--- a/server/api/photos/process.post.ts
+++ b/server/api/photos/process.post.ts
@@ -1,5 +1,6 @@
import { execPhotoPipelineAsync } from '~~/server/services/photo/pipeline-async'
import { useStorageProvider } from '~~/server/utils/useStorageProvider'
+import { isLivePhotoVideo, processLivePhotoVideo } from '~~/server/services/video/livephoto'
export default eventHandler(async (event) => {
await requireUserSession(event)
@@ -32,7 +33,22 @@ export default eventHandler(async (event) => {
lastModified: new Date(),
}
- logger.chrono.info('Starting photo processing for', fileKey)
+ logger.chrono.info('Starting file processing for', fileKey)
+
+ // 检查是否为 LivePhoto 视频文件
+ if (fileName && isLivePhotoVideo(fileName, fileSize || fileBuffer.length)) {
+ logger.chrono.info('LivePhoto video detected:', fileName)
+
+ // 异步处理 LivePhoto 视频
+ processLivePhotoVideoInBackground(fileKey, fileSize || fileBuffer.length)
+
+ return {
+ message: 'LivePhoto video processing started',
+ fileKey,
+ status: 'processing',
+ type: 'livephoto-video'
+ }
+ }
// 异步处理照片
processPhotoInBackground(fileKey, originalObject, fileName)
@@ -74,3 +90,20 @@ async function processPhotoInBackground(s3key: string, originalObject: any, orig
logger.chrono.error('Background photo processing error for', s3key, error)
}
}
+
+// LivePhoto 视频后台处理函数
+async function processLivePhotoVideoInBackground(videoKey: string, videoSize: number) {
+ try {
+ logger.chrono.info('Starting background LivePhoto video processing for', videoKey)
+
+ const success = await processLivePhotoVideo(videoKey, videoSize)
+
+ if (success) {
+ logger.chrono.success('LivePhoto video processing completed for', videoKey)
+ } else {
+ logger.chrono.warn('LivePhoto video processing failed - no matching photo found for', videoKey)
+ }
+ } catch (error) {
+ logger.chrono.error('LivePhoto video processing error for', videoKey, error)
+ }
+}
diff --git a/server/database/migrations/0002_faulty_tyrannus.sql b/server/database/migrations/0002_faulty_tyrannus.sql
new file mode 100644
index 00000000..b7cdd3ef
--- /dev/null
+++ b/server/database/migrations/0002_faulty_tyrannus.sql
@@ -0,0 +1,3 @@
+ALTER TABLE `photos` ADD `is_live_photo` integer DEFAULT 0 NOT NULL;--> statement-breakpoint
+ALTER TABLE `photos` ADD `live_photo_video_url` text;--> statement-breakpoint
+ALTER TABLE `photos` ADD `live_photo_video_key` text;
\ No newline at end of file
diff --git a/server/database/migrations/meta/0002_snapshot.json b/server/database/migrations/meta/0002_snapshot.json
new file mode 100644
index 00000000..3531caa2
--- /dev/null
+++ b/server/database/migrations/meta/0002_snapshot.json
@@ -0,0 +1,273 @@
+{
+ "version": "6",
+ "dialect": "sqlite",
+ "id": "f6c0e2d0-2c8f-4919-b584-804ecaf815b6",
+ "prevId": "de563135-f904-40ae-aea3-82a77eb2f160",
+ "tables": {
+ "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
+ },
+ "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": {}
+ },
+ "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": {}
+ }
+}
\ No newline at end of file
diff --git a/server/database/migrations/meta/_journal.json b/server/database/migrations/meta/_journal.json
index 27f8baf6..18273eef 100644
--- a/server/database/migrations/meta/_journal.json
+++ b/server/database/migrations/meta/_journal.json
@@ -15,6 +15,13 @@
"when": 1756737014171,
"tag": "0001_narrow_spot",
"breakpoints": true
+ },
+ {
+ "idx": 2,
+ "version": "6",
+ "when": 1756831944824,
+ "tag": "0002_faulty_tyrannus",
+ "breakpoints": true
}
]
}
\ No newline at end of file
diff --git a/server/database/schema.ts b/server/database/schema.ts
index ab3bb320..40d4a32d 100644
--- a/server/database/schema.ts
+++ b/server/database/schema.ts
@@ -33,4 +33,8 @@ export const photos = sqliteTable('photos', {
country: text('country'),
city: text('city'),
locationName: text('location_name'),
+ // LivePhoto 相关字段
+ isLivePhoto: integer('is_live_photo').default(0).notNull(),
+ livePhotoVideoUrl: text('live_photo_video_url'),
+ livePhotoVideoKey: text('live_photo_video_key'),
})
diff --git a/server/services/image/processor.ts b/server/services/image/processor.ts
index 3c87beb3..458ae36a 100644
--- a/server/services/image/processor.ts
+++ b/server/services/image/processor.ts
@@ -35,7 +35,7 @@ const getMetadataWithSharp = async (
let { width, height, orientation } = metadata
if (orientation && [5, 6, 7, 8].includes(orientation)) {
- [width, height] = [height, width]
+ ;[width, height] = [height, width]
}
return {
@@ -99,6 +99,63 @@ export const preprocessImageBuffer = async (
return buffer
}
+export const preprocessImageWithJpegUpload = async (
+ s3key: string,
+): Promise<{
+ raw: Buffer
+ processed: Buffer
+ jpegKey?: string // HEIC 转换后上传的 JPEG 文件的 key
+} | null> => {
+ const storageProvider = getStorageManager().getProvider()
+ if (!storageProvider) return null
+
+ try {
+ const rawImageBuffer = await storageProvider.get(s3key)
+ if (!rawImageBuffer) {
+ logger.image.error(`Image not found in storage: ${s3key}`)
+ return null
+ }
+
+ const extName = path.extname(s3key).toLowerCase()
+ let processedBuffer: Buffer
+ let jpegKey: string | undefined
+ let jpegStorageKey: string | undefined
+
+ if (['.heic', '.heif', '.hif'].includes(extName)) {
+ logger.image.info(
+ 'HEIC image detected, converting and uploading JPEG version',
+ s3key,
+ )
+
+ try {
+ processedBuffer = await convertHeicToJpeg(rawImageBuffer)
+
+ // 生成 JPEG 版本的 key(替换扩展名为 .jpg)
+ const baseName = path.basename(s3key, path.extname(s3key))
+ jpegKey = `${baseName}.jpeg`
+
+ // 上传 JPEG 版本到存储
+ jpegStorageKey = (await storageProvider.create(jpegKey, processedBuffer, 'image/jpeg')).key
+ logger.image.info(`Uploaded JPEG version to: ${jpegKey}`)
+ } catch (err) {
+ logger.image.error(`HEIC conversion failed: ${s3key}`, err)
+ return null
+ }
+ } else {
+ processedBuffer = rawImageBuffer
+ }
+
+ return {
+ raw: rawImageBuffer,
+ processed: processedBuffer,
+ jpegKey: jpegStorageKey,
+ }
+ } catch (err) {
+ logger.image.error(`Image preprocessing failed: ${s3key}`, err)
+ return null
+ }
+}
+
export const preprocessImage = async (
s3key: string,
): Promise<{
@@ -160,7 +217,7 @@ export const processImageMetadataAndSharp = async (
return {
sharpInst,
imageBuffer: convertedBuffer,
- metadata
+ metadata,
}
} catch (err) {
logger.image.error(`Image processing with Sharp failed: ${s3key}`, err)
diff --git a/server/services/photo/pipeline-async.ts b/server/services/photo/pipeline-async.ts
index cc3838a2..8f551c87 100644
--- a/server/services/photo/pipeline-async.ts
+++ b/server/services/photo/pipeline-async.ts
@@ -1,6 +1,7 @@
import path from 'path'
import {
preprocessImage,
+ preprocessImageWithJpegUpload,
processImageMetadataAndSharp,
} from '../image/processor'
import { generateThumbnailAndHash } from '../image/thumbnail'
@@ -10,6 +11,7 @@ import { compressUint8Array } from '~~/shared/utils/u8array'
import { StorageObject } from '../storage'
import { extractExifData, extractPhotoInfo } from '../image/exif'
import { extractLocationFromGPS, parseGPSCoordinates } from '../location/geocoding'
+import { findLivePhotoVideoForImage } from '../video/livephoto'
const generatePhotoId = (s3key: string) => {
return path.basename(s3key, path.extname(s3key)).replace(/ /g, '_')
@@ -53,7 +55,7 @@ async function processPhotoInternal(
const imageBuffers = await new Promise<any>((resolve, reject) => {
setImmediate(async () => {
try {
- const result = await preprocessImage(s3key)
+ const result = await preprocessImageWithJpegUpload(s3key)
resolve(result)
} catch (error) {
reject(error)
@@ -129,6 +131,28 @@ async function processPhotoInternal(
}
}
+ // 检查是否有对应的 LivePhoto 视频文件
+ let livePhotoInfo = null
+ const livePhotoVideo = await new Promise<any>((resolve, reject) => {
+ setImmediate(async () => {
+ try {
+ const result = await findLivePhotoVideoForImage(s3key)
+ resolve(result)
+ } catch (error) {
+ reject(error)
+ }
+ })
+ })
+
+ if (livePhotoVideo) {
+ livePhotoInfo = {
+ isLivePhoto: 1,
+ livePhotoVideoUrl: storageProvider.getPublicUrl(livePhotoVideo.videoKey),
+ livePhotoVideoKey: livePhotoVideo.videoKey,
+ }
+ log.info(`LivePhoto video found for ${s3key}: ${livePhotoVideo.videoKey}`)
+ }
+
const thumbnailObject = await new Promise<any>((resolve, reject) => {
setImmediate(async () => {
try {
@@ -157,7 +181,9 @@ async function processPhotoInternal(
fileSize: storageObject.size || null,
lastModified:
storageObject.lastModified?.toISOString() || new Date().toISOString(),
- originalUrl: storageProvider.getPublicUrl(s3key),
+ originalUrl: imageBuffers.jpegKey
+ ? storageProvider.getPublicUrl(imageBuffers.jpegKey) // 使用 JPEG 版本作为 originalUrl
+ : storageProvider.getPublicUrl(s3key),
thumbnailUrl: storageProvider.getPublicUrl(thumbnailObject.key),
thumbnailHash: thumbnailHash ? compressUint8Array(thumbnailHash) : null,
exif: exifData,
@@ -167,6 +193,10 @@ async function processPhotoInternal(
country: locationInfo?.country || null,
city: locationInfo?.city || null,
locationName: locationInfo?.locationName || null,
+ // LivePhoto 相关字段
+ isLivePhoto: livePhotoInfo?.isLivePhoto || 0,
+ livePhotoVideoUrl: livePhotoInfo?.livePhotoVideoUrl || null,
+ livePhotoVideoKey: livePhotoInfo?.livePhotoVideoKey || null,
}
log.info(`Async processing completed for ${s3key}`)
diff --git a/server/services/photo/pipeline.ts b/server/services/photo/pipeline.ts
index e46e5612..799f67f8 100644
--- a/server/services/photo/pipeline.ts
+++ b/server/services/photo/pipeline.ts
@@ -1,6 +1,7 @@
import path from 'path'
import {
preprocessImage,
+ preprocessImageWithJpegUpload,
processImageMetadataAndSharp,
} from '../image/processor'
import { generateThumbnailAndHash } from '../image/thumbnail'
@@ -9,6 +10,7 @@ import { getStorageManager } from '~~/server/plugins/storage'
import { compressUint8Array } from '~~/shared/utils/u8array'
import { StorageObject } from '../storage'
import { extractExifData, extractPhotoInfo } from '../image/exif'
+import { findLivePhotoVideoForImage } from '../video/livephoto'
const generatePhotoId = (s3key: string) => {
return path.basename(s3key, path.extname(s3key))
@@ -24,7 +26,7 @@ export const execPhotoPipeline = async (
try {
// 1. 预处理图片,分离原始 Buffer 和 HEIC 转换
- const imageBuffers = await preprocessImage(s3key)
+ const imageBuffers = await preprocessImageWithJpegUpload(s3key)
if (!imageBuffers) return null
// 2. 转换 BMP 格式,返回 Sharp 实例
@@ -45,7 +47,20 @@ export const execPhotoPipeline = async (
// 5. 提取照片信息
const photoInfo = extractPhotoInfo(s3key, exifData)
- // 6. 上传缩略图
+ // 6. 检查是否有对应的 LivePhoto 视频文件
+ const livePhotoVideo = await findLivePhotoVideoForImage(s3key)
+ let livePhotoInfo = null
+
+ if (livePhotoVideo) {
+ livePhotoInfo = {
+ isLivePhoto: 1,
+ livePhotoVideoUrl: storageProvider.getPublicUrl(livePhotoVideo.videoKey),
+ livePhotoVideoKey: livePhotoVideo.videoKey,
+ }
+ log.info(`LivePhoto video found for ${s3key}: ${livePhotoVideo.videoKey}`)
+ }
+
+ // 7. 上传缩略图
const thumbnailObject = await storageProvider.create(
`thumbnails/${photoId}.webp`,
thumbnailBuffer,
@@ -65,10 +80,22 @@ export const execPhotoPipeline = async (
fileSize: storageObject.size || null,
lastModified:
storageObject.lastModified?.toISOString() || new Date().toISOString(),
- originalUrl: storageProvider.getPublicUrl(s3key),
+ originalUrl: imageBuffers.jpegKey
+ ? storageProvider.getPublicUrl(imageBuffers.jpegKey) // 使用 JPEG 版本作为 originalUrl
+ : storageProvider.getPublicUrl(s3key),
thumbnailUrl: storageProvider.getPublicUrl(thumbnailObject.key),
thumbnailHash: thumbnailHash ? compressUint8Array(thumbnailHash) : null,
exif: exifData,
+ // 地理位置信息
+ latitude: null,
+ longitude: null,
+ country: null,
+ city: null,
+ locationName: null,
+ // LivePhoto 相关字段
+ isLivePhoto: livePhotoInfo?.isLivePhoto || 0,
+ livePhotoVideoUrl: livePhotoInfo?.livePhotoVideoUrl || null,
+ livePhotoVideoKey: livePhotoInfo?.livePhotoVideoKey || null,
}
} catch (err) {
log.error(`Photo pipeline failed for ${s3key}`, err)
diff --git a/server/services/video/livephoto.ts b/server/services/video/livephoto.ts
new file mode 100644
index 00000000..b621348e
--- /dev/null
+++ b/server/services/video/livephoto.ts
@@ -0,0 +1,150 @@
+import path from 'path'
+import { eq } from 'drizzle-orm'
+import { getStorageManager } from '~~/server/plugins/storage'
+
+/**
+ * 处理 LivePhoto MOV 文件,匹配相同文件名的照片并更新 LivePhoto 信息
+ */
+export const processLivePhotoVideo = async (
+ videoKey: string,
+ videoSize: number
+): Promise<boolean> => {
+ const storageProvider = getStorageManager().getProvider()
+ const db = useDB()
+
+ try {
+ // 从视频文件名提取基础名称(去除扩展名)
+ const videoBaseName = path.basename(videoKey, path.extname(videoKey))
+ const videoDir = path.dirname(videoKey)
+
+ logger.chrono.info(`Processing LivePhoto video: ${videoKey}, looking for photo with base name: ${videoBaseName}`)
+
+ // 查找可能匹配的照片文件名模式
+ // LivePhoto 通常会有相同的基础文件名,但照片可能是 .HEIC/.JPG 等格式
+ const possiblePhotoKeys = [
+ path.join(videoDir, `${videoBaseName}.HEIC`).replace(/\\/g, '/'),
+ path.join(videoDir, `${videoBaseName}.heic`).replace(/\\/g, '/'),
+ path.join(videoDir, `${videoBaseName}.JPG`).replace(/\\/g, '/'),
+ path.join(videoDir, `${videoBaseName}.jpg`).replace(/\\/g, '/'),
+ path.join(videoDir, `${videoBaseName}.JPEG`).replace(/\\/g, '/'),
+ path.join(videoDir, `${videoBaseName}.jpeg`).replace(/\\/g, '/'),
+ ]
+
+ // 在数据库中查找匹配的照片
+ let matchedPhoto = null
+ for (const photoKey of possiblePhotoKeys) {
+ const photos = await db
+ .select()
+ .from(tables.photos)
+ .where(eq(tables.photos.storageKey, photoKey))
+ .limit(1)
+
+ if (photos.length > 0) {
+ matchedPhoto = photos[0]
+ logger.chrono.info(`Found matching photo: ${photoKey}`)
+ break
+ }
+ }
+
+ if (!matchedPhoto) {
+ logger.chrono.warn(`No matching photo found for LivePhoto video: ${videoKey}`)
+ return false
+ }
+
+ // 获取视频的公共 URL
+ const videoUrl = storageProvider.getPublicUrl(videoKey)
+
+ // 更新照片记录,设置 LivePhoto 信息
+ await db
+ .update(tables.photos)
+ .set({
+ isLivePhoto: 1,
+ livePhotoVideoUrl: videoUrl,
+ livePhotoVideoKey: videoKey,
+ })
+ .where(eq(tables.photos.id, matchedPhoto.id))
+
+ logger.chrono.success(`Successfully processed LivePhoto: ${matchedPhoto.id}, video: ${videoKey}`)
+ return true
+
+ } catch (error) {
+ logger.chrono.error(`Failed to process LivePhoto video ${videoKey}:`, error)
+ return false
+ }
+}
+
+/**
+ * 检查存储桶中是否有与照片对应的 LivePhoto 视频文件
+ */
+export const findLivePhotoVideoForImage = async (
+ imageKey: string
+): Promise<{ videoKey: string; videoSize: number } | null> => {
+ const storageProvider = getStorageManager().getProvider()
+
+ try {
+ // 从图片文件名提取基础名称(去除扩展名)
+ const imageBaseName = path.basename(imageKey, path.extname(imageKey))
+ const imageDir = path.dirname(imageKey)
+
+ logger.chrono.info(`Checking for LivePhoto video for image: ${imageKey}, base name: ${imageBaseName}`)
+
+ // 查找可能匹配的视频文件名模式
+ const possibleVideoKeys = [
+ path.join(imageDir, `${imageBaseName}.MOV`).replace(/\\/g, '/'),
+ path.join(imageDir, `${imageBaseName}.mov`).replace(/\\/g, '/'),
+ ]
+
+ // 检查存储中是否存在对应的视频文件
+ for (const videoKey of possibleVideoKeys) {
+ try {
+ const videoBuffer = await storageProvider.get(videoKey)
+ if (videoBuffer) {
+ const videoSize = videoBuffer.length
+
+ // 检查是否符合 LivePhoto 视频的特征
+ const fileName = path.basename(videoKey)
+ if (isLivePhotoVideo(fileName, videoSize)) {
+ logger.chrono.info(`Found matching LivePhoto video: ${videoKey}`)
+ return { videoKey, videoSize }
+ } else {
+ logger.chrono.warn(`Video file found but doesn't match LivePhoto criteria: ${videoKey} (size: ${videoSize})`)
+ }
+ }
+ } catch (err) {
+ // 文件不存在,继续检查下一个
+ continue
+ }
+ }
+
+ logger.chrono.info(`No matching LivePhoto video found for image: ${imageKey}`)
+ return null
+
+ } catch (error) {
+ logger.chrono.error(`Failed to check for LivePhoto video for ${imageKey}:`, error)
+ return null
+ }
+}
+
+/**
+ * 检查文件是否为 MOV 视频格式
+ */
+export const isVideoFile = (fileName: string): boolean => {
+ const extName = path.extname(fileName).toLowerCase()
+ return ['.mov', '.mp4'].includes(extName)
+}
+
+/**
+ * 检查文件是否可能是 LivePhoto 的 MOV 文件
+ * LivePhoto 的 MOV 文件通常很小(几MB以内)
+ */
+export const isLivePhotoVideo = (fileName: string, fileSize: number): boolean => {
+ const extName = path.extname(fileName).toLowerCase()
+
+ // 检查是否为 MOV 格式
+ if (extName !== '.mov') {
+ return false
+ }
+
+ const maxLivePhotoSize = 12 * 1024 * 1024 // 12MB
+ return fileSize <= maxLivePhotoSize
+}
diff --git a/server/services/video/scanner.ts b/server/services/video/scanner.ts
new file mode 100644
index 00000000..f7b57373
--- /dev/null
+++ b/server/services/video/scanner.ts
@@ -0,0 +1,70 @@
+import { getStorageManager } from '~~/server/plugins/storage'
+import { processLivePhotoVideo, isLivePhotoVideo } from '../video/livephoto'
+
+/**
+ * 批量扫描存储中的 MOV 文件,尝试匹配 LivePhoto
+ */
+export const scanAndProcessExistingLivePhotos = async (): Promise<{
+ processed: number
+ matched: number
+ errors: string[]
+}> => {
+ const storageProvider = getStorageManager().getProvider()
+ const results = {
+ processed: 0,
+ matched: 0,
+ errors: [] as string[]
+ }
+
+ try {
+ logger.chrono.info('Starting scan for existing LivePhoto videos...')
+
+ // 这里需要根据你的存储提供商实现列出所有文件的功能
+ // 如果你的存储提供商支持列出文件,可以使用类似的逻辑:
+ // const allFiles = await storageProvider.list()
+
+ // 由于大多数存储提供商不直接提供列出所有文件的功能,
+ // 这个函数可以作为管理工具,手动调用处理特定的 MOV 文件
+
+ logger.chrono.info('Scan completed. Use processSpecificLivePhotoVideo for individual files.')
+
+ return results
+ } catch (error) {
+ logger.chrono.error('Failed to scan existing LivePhotos:', error)
+ results.errors.push(error instanceof Error ? error.message : String(error))
+ return results
+ }
+}
+
+/**
+ * 处理特定的 MOV 文件
+ */
+export const processSpecificLivePhotoVideo = async (
+ videoKey: string
+): Promise<boolean> => {
+ try {
+ const storageProvider = getStorageManager().getProvider()
+
+ // 获取文件信息
+ const fileBuffer = await storageProvider.get(videoKey)
+ if (!fileBuffer) {
+ logger.chrono.error(`Video file not found: ${videoKey}`)
+ return false
+ }
+
+ const fileName = videoKey.split('/').pop() || videoKey
+
+ // 检查是否为可能的 LivePhoto 视频
+ if (!isLivePhotoVideo(fileName, fileBuffer.length)) {
+ logger.chrono.warn(`File does not appear to be a LivePhoto video: ${videoKey}`)
+ return false
+ }
+
+ // 处理 LivePhoto 视频
+ return await processLivePhotoVideo(videoKey, fileBuffer.length)
+
+ } catch (error) {
+ logger.chrono.error(`Failed to process specific LivePhoto video ${videoKey}:`, error)
+ return false
+ }
+}
diff --git a/server/services/video/test-utils.ts b/server/services/video/test-utils.ts
new file mode 100644
index 00000000..ded857df
--- /dev/null
+++ b/server/services/video/test-utils.ts
@@ -0,0 +1,80 @@
+import { eq } from 'drizzle-orm'
+import { findLivePhotoVideoForImage } from '../video/livephoto'
+
+/**
+ * 测试 LivePhoto 检测功能的工具函数
+ */
+export const testLivePhotoDetection = async (imageKey: string) => {
+ logger.chrono.info(`Testing LivePhoto detection for: ${imageKey}`)
+
+ try {
+ const result = await findLivePhotoVideoForImage(imageKey)
+
+ if (result) {
+ logger.chrono.success(`LivePhoto video found:`, {
+ imageKey,
+ videoKey: result.videoKey,
+ videoSize: result.videoSize
+ })
+ return {
+ found: true,
+ videoKey: result.videoKey,
+ videoSize: result.videoSize
+ }
+ } else {
+ logger.chrono.info(`No LivePhoto video found for: ${imageKey}`)
+ return {
+ found: false
+ }
+ }
+ } catch (error) {
+ logger.chrono.error(`LivePhoto detection test failed for ${imageKey}:`, error)
+ return {
+ found: false,
+ error: error instanceof Error ? error.message : String(error)
+ }
+ }
+}
+
+/**
+ * 批量测试现有照片的 LivePhoto 检测
+ */
+export const batchTestLivePhotoDetection = async () => {
+ const db = useDB()
+
+ try {
+ // 获取所有还不是 LivePhoto 的照片
+ const photos = await db
+ .select()
+ .from(tables.photos)
+ .where(eq(tables.photos.isLivePhoto, 0))
+
+ logger.chrono.info(`Testing ${photos.length} photos for LivePhoto detection`)
+
+ const results = []
+
+ for (const photo of photos) {
+ const result = await testLivePhotoDetection(photo.storageKey!)
+ results.push({
+ photoId: photo.id,
+ storageKey: photo.storageKey,
+ ...result
+ })
+
+ // 避免过快请求存储
+ await new Promise(resolve => setTimeout(resolve, 100))
+ }
+
+ const foundCount = results.filter(r => r.found).length
+ logger.chrono.success(`LivePhoto detection test completed. Found ${foundCount} potential LivePhotos out of ${photos.length} photos`)
+
+ return {
+ total: photos.length,
+ found: foundCount,
+ results: results.filter(r => r.found) // 只返回找到的
+ }
+ } catch (error) {
+ logger.chrono.error('Batch LivePhoto detection test failed:', error)
+ throw error
+ }
+}