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.

refactor: 重新组织 API 端点

Timothy Yindf68d4ce254e
diff --git a/app/pages/dashboard/location.vue b/app/pages/dashboard/location.vue
index bcc13905..57616d16 100644
--- a/app/pages/dashboard/location.vue
+++ b/app/pages/dashboard/location.vue
@@ -37,7 +37,7 @@ const isReindexing = ref(false)
const reindexResult = ref<ReindexResult | null>(null)
const { data: locationStats, refresh: refreshStats } =
- await useFetch<LocationStats>('/api/photos/reindex-city', {
+ await useFetch<LocationStats>('/api/photos/locations/geocode', {
server: false,
})
@@ -57,7 +57,7 @@ const startReindex = async (force = false) => {
reindexResult.value = null
try {
- const data = await $fetch<ReindexResult>('/api/photos/reindex-city', {
+ const data = await $fetch<ReindexResult>('/api/photos/locations/geocode', {
method: 'POST',
body: { force },
})
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index c76c49a7..b5c00f61 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -587,7 +587,7 @@ const handleUpdateLivePhoto = async (photoId: string) => {
color: 'info',
})
- const result = (await $fetch('/api/photos/livephoto-manage', {
+ const result = (await $fetch('/api/photos/livephoto/manage', {
method: 'POST',
body: {
action: 'update-photo',
@@ -691,10 +691,10 @@ const handleBatchDetectLivePhoto = async () => {
const photoIds = selectedPhotos.map((photo: any) => photo.id)
- const result = (await $fetch('/api/photos/livephoto-detect', {
+ const result = (await $fetch('/api/photos/livephoto/manage', {
method: 'POST',
body: {
- action: 'batch-detect',
+ action: 'detect',
photoIds: photoIds, // 传递选中的照片ID列表
},
})) as any
@@ -748,7 +748,7 @@ const handleBatchReindexExif = async () => {
const photoIds = selectedPhotos.map((photo: any) => photo.id)
- const result = (await $fetch('/api/photos/reindex-exif', {
+ const result = (await $fetch('/api/photos/exif/reindex', {
method: 'POST',
body: {
action: 'batch-reindex',
@@ -788,7 +788,7 @@ const handleReindexSingleExif = async (photoId: string) => {
color: 'info',
})
- const result = (await $fetch('/api/photos/reindex-exif', {
+ const result = (await $fetch('/api/photos/exif/reindex', {
method: 'POST',
body: {
action: 'single-reindex',
diff --git a/server/api/photos/[photoId].delete.ts b/server/api/photos/[photoId]/index.delete.ts
similarity index 100%
rename from server/api/photos/[photoId].delete.ts
rename to server/api/photos/[photoId]/index.delete.ts
diff --git a/server/api/photos/reindex-exif.post.ts b/server/api/photos/exif/reindex.post.ts
similarity index 100%
rename from server/api/photos/reindex-exif.post.ts
rename to server/api/photos/exif/reindex.post.ts
diff --git a/server/api/photos/index.put.ts b/server/api/photos/index.put.ts
deleted file mode 100644
index 0d4b4f92..00000000
--- a/server/api/photos/index.put.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import { execPhotoPipelineAsync } from '~~/server/services/photo/pipeline-async'
-import { useStorageProvider } from '~~/server/utils/useStorageProvider'
-
-export default eventHandler(async (event) => {
- await requireUserSession(event)
- const { storageProvider } = useStorageProvider(event)
- const formData = await readFormData(event)
-
- // 尝试从两个可能的字段名获取文件
- const file = (formData.get('file') || formData.get('files')) as File | null
-
- if (!file || Array.isArray(file)) {
- throw createError({
- statusCode: 400,
- statusMessage: 'File field is required',
- })
- }
-
- const originalObject = await storageProvider.create(
- `${file.name}`,
- Buffer.from(await file.arrayBuffer()),
- )
-
- // 立即返回响应,不等待处理完成
- processPhotoInBackground(originalObject.key, originalObject)
-
- // 返回处理中的响应
- return {
- message: 'Photo upload successful, processing in background',
- fileKey: originalObject.key,
- fileName: file.name,
- status: 'processing'
- }
-})
-
-// 后台处理函数
-async function processPhotoInBackground(s3key: string, originalObject: any) {
- try {
- logger.chrono.info('Starting background photo processing for', s3key)
-
- const photo = await execPhotoPipelineAsync(s3key, originalObject)
-
- if (!photo) {
- logger.chrono.error('Photo processing failed for', s3key)
- return
- }
-
- await useDB().insert(tables.photos).values(photo).execute()
- logger.chrono.success('Background photo processing completed for', photo.title || photo.storageKey)
- } catch (error) {
- logger.chrono.error('Background photo processing error for', s3key, error)
- }
-}
diff --git a/server/api/photos/livephoto-detect.post.ts b/server/api/photos/livephoto-detect.post.ts
deleted file mode 100644
index f3b59326..00000000
--- a/server/api/photos/livephoto-detect.post.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import { batchTestLivePhotoDetection } from '~~/server/services/video/test-utils'
-
-export default eventHandler(async (event) => {
- await requireUserSession(event)
-
- const body = await readBody(event)
- const { action, photoIds } = body
-
- if (!action) {
- throw createError({
- statusCode: 400,
- statusMessage: 'Action is required',
- })
- }
-
- try {
- switch (action) {
- case 'batch-detect': // 批量检测现有照片的 LivePhoto 视频
- {
- const results = await batchTestLivePhotoDetection(photoIds)
- 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
similarity index 87%
rename from server/api/photos/livephoto-manage.post.ts
rename to server/api/photos/livephoto/manage.post.ts
index 3ced8971..faea2849 100644
--- a/server/api/photos/livephoto-manage.post.ts
+++ b/server/api/photos/livephoto/manage.post.ts
@@ -5,12 +5,13 @@ import {
import { eq } from 'drizzle-orm'
import { findLivePhotoVideoForImage } from '~~/server/services/video/livephoto'
import { getStorageManager } from '~~/server/plugins/storage'
+import { batchTestLivePhotoDetection } from '~~/server/services/video/test-utils'
export default eventHandler(async (event) => {
await requireUserSession(event)
const body = await readBody(event)
- const { action, videoKey, photoId } = body
+ const { action, videoKey, photoId, photoIds } = body
if (!action) {
throw createError({
@@ -29,6 +30,14 @@ export default eventHandler(async (event) => {
}
}
+ case 'detect': { // 批量检测现有照片的 LivePhoto 视频
+ const results = await batchTestLivePhotoDetection(photoIds)
+ return {
+ message: 'Batch LivePhoto detection completed',
+ results,
+ }
+ }
+
case 'process': { // 处理特定文件
if (!videoKey) {
throw createError({
@@ -108,7 +117,7 @@ export default eventHandler(async (event) => {
throw createError({
statusCode: 400,
statusMessage:
- 'Invalid action. Use "scan", "process", or "update-photo"',
+ 'Invalid action. Use "scan", "detect", "process", or "update-photo"',
})
}
} catch (error) {
diff --git a/server/api/photos/reindex-city.get.ts b/server/api/photos/locations/geocode.get.ts
similarity index 100%
rename from server/api/photos/reindex-city.get.ts
rename to server/api/photos/locations/geocode.get.ts
diff --git a/server/api/photos/reindex-city.post.ts b/server/api/photos/locations/geocode.post.ts
similarity index 100%
rename from server/api/photos/reindex-city.post.ts
rename to server/api/photos/locations/geocode.post.ts
diff --git a/server/api/photos/locations.get.ts b/server/api/photos/locations/index.get.ts
similarity index 100%
rename from server/api/photos/locations.get.ts
rename to server/api/photos/locations/index.get.ts
diff --git a/server/api/photos/location-stats.get.ts b/server/api/photos/locations/stats.get.ts
similarity index 100%
rename from server/api/photos/location-stats.get.ts
rename to server/api/photos/locations/stats.get.ts
diff --git a/server/api/photos/status.ts b/server/api/photos/status.get.ts
similarity index 100%
rename from server/api/photos/status.ts
rename to server/api/photos/status.get.ts