Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
fix(location): 修复位置信息重建索引的依据错误
Timothy Yin672a993e6de5
diff --git a/app/pages/dashboard/location.vue b/app/pages/dashboard/location.vueindex dcb32763..7b02901a 100644--- a/app/pages/dashboard/location.vue+++ b/app/pages/dashboard/location.vue@@ -12,6 +12,8 @@ interface LocationStats {totalPhotosWithGps: numberphotosWithCity: numberphotosNeedingReindex: number+ photosNeedingGpsReindex: number+ photosNeedingLocationReindex: numbercoveragePercentage: string}message: string@@ -21,6 +23,8 @@ interface ReindexResult {message: stringprocessed: numberupdated: number+ gpsUpdated: number+ locationUpdated: numbererrors?: Array<{ photoId: string; error: string }>statistics: {totalFound: number@@ -32,9 +36,10 @@ const isLoading = ref(false)const isReindexing = ref(false)const reindexResult = ref<ReindexResult | null>(null)-const { data: locationStats, refresh: refreshStats } = await useFetch<LocationStats>('/api/photos/reindex-city', {- server: false,-})+const { data: locationStats, refresh: refreshStats } =+ await useFetch<LocationStats>('/api/photos/reindex-city', {+ server: false,+ })const refreshData = async () => {isLoading.value = true@@ -47,31 +52,31 @@ const refreshData = async () => {const startReindex = async (force = false) => {if (isReindexing.value) return-+isReindexing.value = truereindexResult.value = null-+try {const data = await $fetch<ReindexResult>('/api/photos/reindex-city', {method: 'POST',- body: { force }+ body: { force },})-+reindexResult.value = dataawait refreshStats()-+// 显示成功通知if (data.updated > 0) {useToast().add({title: '重新索引完成',description: `成功更新了 ${data.updated} 张照片的城市信息`,- color: 'success'+ color: 'success',})} else {useToast().add({title: '重新索引完成',description: '没有照片需要更新',- color: 'info'+ color: 'info',})}} catch (error: any) {@@ -79,7 +84,7 @@ const startReindex = async (force = false) => {useToast().add({title: '重新索引失败',description: error?.message || '未知错误',- color: 'error'+ color: 'error',})} finally {isReindexing.value = false@@ -107,15 +112,17 @@ const formatPercentage = (value: string) => {</div><!-- 统计信息 -->- <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">+ <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4"><!-- 总GPS照片数 --><UCard><div class="flex items-center justify-between"><div><p class="text-sm text-neutral-500 dark:text-neutral-400">- 有GPS坐标的照片+ EXIF中有GPS的照片+ </p>+ <p class="text-2xl font-bold">+ {{ locationStats?.statistics.totalPhotosWithGps || 0 }}</p>- <p class="text-2xl font-bold">{{ locationStats?.statistics.totalPhotosWithGps || 0 }}</p></div><UIconname="i-tabler-map-pin"@@ -131,7 +138,9 @@ const formatPercentage = (value: string) => {<p class="text-sm text-neutral-500 dark:text-neutral-400">已有城市信息</p>- <p class="text-2xl font-bold">{{ locationStats?.statistics.photosWithCity || 0 }}</p>+ <p class="text-2xl font-bold">+ {{ locationStats?.statistics.photosWithCity || 0 }}+ </p></div><UIconname="i-tabler-building"@@ -140,17 +149,37 @@ const formatPercentage = (value: string) => {</div></UCard>- <!-- 需要重新索引的照片 -->+ <!-- 需要GPS重新索引的照片 -->+ <UCard>+ <div class="flex items-center justify-between">+ <div>+ <p class="text-sm text-neutral-500 dark:text-neutral-400">+ 需要GPS重新提取+ </p>+ <p class="text-2xl font-bold text-orange-500">+ {{ locationStats?.statistics.photosNeedingGpsReindex || 0 }}+ </p>+ </div>+ <UIcon+ name="i-tabler-gps"+ class="w-8 h-8 text-orange-500"+ />+ </div>+ </UCard>++ <!-- 需要城市信息重新索引的照片 --><UCard><div class="flex items-center justify-between"><div><p class="text-sm text-neutral-500 dark:text-neutral-400">- 需要重新索引+ 需要城市重新索引+ </p>+ <p class="text-2xl font-bold text-warning-500">+ {{ locationStats?.statistics.photosNeedingLocationReindex || 0 }}</p>- <p class="text-2xl font-bold text-warning-500">{{ locationStats?.statistics.photosNeedingReindex || 0 }}</p></div><UIcon- name="i-tabler-alert-triangle"+ name="i-tabler-building-skyscraper"class="w-8 h-8 text-warning-500"/></div>@@ -164,7 +193,11 @@ const formatPercentage = (value: string) => {城市信息覆盖率</p><p class="text-2xl font-bold">- {{ formatPercentage(locationStats?.statistics.coveragePercentage || '0') }}+ {{+ formatPercentage(+ locationStats?.statistics.coveragePercentage || '0',+ )+ }}</p></div><UIcon@@ -186,22 +219,26 @@ const formatPercentage = (value: string) => {<p class="text-sm text-neutral-600 dark:text-neutral-300 mb-3">{{ locationStats?.message }}</p>-+<div class="flex gap-3"><UButtonicon="i-tabler-refresh":loading="isReindexing"- :disabled="(locationStats?.statistics.photosNeedingReindex || 0) === 0"+ :disabled="+ (locationStats?.statistics.photosNeedingReindex || 0) === 0+ "@click="startReindex(false)">- 重新索引缺失的城市信息+ 重新索引缺失的位置信息</UButton>-+<UButtonicon="i-tabler-reload"variant="outline":loading="isReindexing"- :disabled="(locationStats?.statistics.totalPhotosWithGps || 0) === 0"+ :disabled="+ (locationStats?.statistics.totalPhotosWithGps || 0) === 0+ "@click="startReindex(true)">强制重新索引所有@@ -215,7 +252,7 @@ const formatPercentage = (value: string) => {color="info"variant="soft"title="操作说明"- description="重新索引操作将为有GPS坐标但缺少城市信息的照片获取地理位置信息。由于API限制,处理速度较慢,请耐心等待。"+ description="重新索引操作将从EXIF中重新提取GPS坐标,并为有GPS坐标但缺少城市信息的照片获取地理位置信息。由于API限制,处理速度较慢,请耐心等待。"/></div></UCard>@@ -227,41 +264,73 @@ const formatPercentage = (value: string) => {</template><div class="space-y-4">- <div class="grid grid-cols-1 md:grid-cols-3 gap-4">+ <div class="grid grid-cols-1 md:grid-cols-5 gap-4"><div class="bg-blue-50 dark:bg-blue-950 p-3 rounded-lg"><p class="text-sm text-blue-600 dark:text-blue-400">处理总数</p>- <p class="text-xl font-bold text-blue-700 dark:text-blue-300">{{ reindexResult.processed }}</p>+ <p class="text-xl font-bold text-blue-700 dark:text-blue-300">+ {{ reindexResult.processed }}+ </p></div>-+<div class="bg-green-50 dark:bg-green-950 p-3 rounded-lg"><p class="text-sm text-green-600 dark:text-green-400">更新数量</p>- <p class="text-xl font-bold text-green-700 dark:text-green-300">{{ reindexResult.updated }}</p>+ <p class="text-xl font-bold text-green-700 dark:text-green-300">+ {{ reindexResult.updated }}+ </p></div>-++ <div class="bg-orange-50 dark:bg-orange-950 p-3 rounded-lg">+ <p class="text-sm text-orange-600 dark:text-orange-400">GPS更新</p>+ <p class="text-xl font-bold text-orange-700 dark:text-orange-300">+ {{ reindexResult.gpsUpdated }}+ </p>+ </div>++ <div class="bg-yellow-50 dark:bg-yellow-950 p-3 rounded-lg">+ <p class="text-sm text-yellow-600 dark:text-yellow-400">位置更新</p>+ <p class="text-xl font-bold text-yellow-700 dark:text-yellow-300">+ {{ reindexResult.locationUpdated }}+ </p>+ </div>+<div class="bg-purple-50 dark:bg-purple-950 p-3 rounded-lg"><p class="text-sm text-purple-600 dark:text-purple-400">成功率</p>- <p class="text-xl font-bold text-purple-700 dark:text-purple-300">{{ reindexResult.statistics.successRate }}</p>+ <p class="text-xl font-bold text-purple-700 dark:text-purple-300">+ {{ reindexResult.statistics.successRate }}+ </p></div></div><div class="bg-gray-50 dark:bg-gray-800 p-4 rounded-lg"><p class="font-medium mb-2">{{ reindexResult.message }}</p><p class="text-sm text-gray-600 dark:text-gray-400">- 在 {{ reindexResult.statistics.totalFound }} 张照片中,成功处理了 {{ reindexResult.processed }} 张,- 更新了 {{ reindexResult.updated }} 张照片的城市信息。+ 在 {{ reindexResult.statistics.totalFound }} 张照片中,成功处理了+ {{ reindexResult.processed }} 张, 更新了+ {{ reindexResult.updated }} 张照片的位置信息 ({{+ reindexResult.gpsUpdated+ }}+ 张GPS坐标,{{ reindexResult.locationUpdated }} 张城市信息)。</p></div><!-- 错误信息 -->- <div v-if="reindexResult.errors && reindexResult.errors.length > 0" class="bg-red-50 dark:bg-red-950 p-4 rounded-lg">- <h3 class="font-medium text-red-700 dark:text-red-300 mb-2">处理错误 ({{ reindexResult.errors.length }})</h3>+ <div+ v-if="reindexResult.errors && reindexResult.errors.length > 0"+ class="bg-red-50 dark:bg-red-950 p-4 rounded-lg"+ >+ <h3 class="font-medium text-red-700 dark:text-red-300 mb-2">+ 处理错误 ({{ reindexResult.errors.length }})+ </h3><div class="space-y-2 max-h-48 overflow-y-auto">- <div- v-for="error in reindexResult.errors"+ <div+ v-for="error in reindexResult.errors":key="error.photoId"class="text-sm bg-white dark:bg-red-900 p-2 rounded">- <span class="font-mono text-red-600 dark:text-red-400">{{ error.photoId }}</span>: {{ error.error }}+ <span class="font-mono text-red-600 dark:text-red-400">{{+ error.photoId+ }}</span+ >: {{ error.error }}</div></div></div>@@ -271,7 +340,10 @@ const formatPercentage = (value: string) => {<!-- 进度提示 --><UCard v-if="isReindexing"><div class="flex items-center gap-3 p-4">- <UIcon name="i-tabler-loader-2" class="w-6 h-6 animate-spin text-primary" />+ <UIcon+ name="i-tabler-loader-2"+ class="w-6 h-6 animate-spin text-primary"+ /><div><p class="font-medium">正在重新索引城市信息...</p><p class="text-sm text-neutral-500 dark:text-neutral-400">diff --git a/server/api/photos/reindex-city.get.ts b/server/api/photos/reindex-city.get.tsindex 11d1f302..885cebe3 100644--- a/server/api/photos/reindex-city.get.ts+++ b/server/api/photos/reindex-city.get.ts@@ -1,70 +1,82 @@import { isNull, and, isNotNull, sql, eq } from 'drizzle-orm'-import { extractLocationFromGPS } from '~~/server/services/location/geocoding'+import {+ extractLocationFromGPS,+ parseGPSCoordinates,+} from '~~/server/services/location/geocoding'export default eventHandler(async (event) => {await requireUserSession(event)-+try {- // 获取统计信息:有GPS坐标但缺少城市信息的照片- const statsResult = await useDB()+ // 获取统计信息:EXIF中有GPS坐标但数据库中缺少位置信息的照片+ const photosWithExif = await useDB().select({- total: sql<number>`count(*)`+ id: tables.photos.id,+ exif: tables.photos.exif,+ latitude: tables.photos.latitude,+ longitude: tables.photos.longitude,+ city: tables.photos.city,+ country: tables.photos.country,}).from(tables.photos)- .where(- and(- isNotNull(tables.photos.latitude),- isNotNull(tables.photos.longitude),- sql`(city IS NULL OR city = '' OR country IS NULL OR country = '')`- )- )+ .where(isNotNull(tables.photos.exif))- const needsReindex = statsResult[0]?.total || 0+ // 分析照片的GPS和位置信息状态+ let totalWithGpsInExif = 0+ let needsLocationReindex = 0+ let needsGpsReindex = 0+ let photosWithCity = 0- // 获取总的有GPS坐标的照片数- const totalWithGpsResult = await useDB()- .select({- total: sql<number>`count(*)`- })- .from(tables.photos)- .where(- and(- isNotNull(tables.photos.latitude),- isNotNull(tables.photos.longitude)- )- )+ for (const photo of photosWithExif) {+ const exif = photo.exif as any+ if (!exif) continue- const totalWithGps = totalWithGpsResult[0]?.total || 0+ // 检查EXIF中是否有GPS数据+ const hasGpsInExif =+ (exif.GPSLatitude && exif.GPSLongitude) || exif.GPSCoordinates- // 获取已有城市信息的照片数- const withCityResult = await useDB()- .select({- total: sql<number>`count(*)`- })- .from(tables.photos)- .where(- and(- isNotNull(tables.photos.latitude),- isNotNull(tables.photos.longitude),- isNotNull(tables.photos.city),- sql`city != ''`- )- )+ if (hasGpsInExif) {+ totalWithGpsInExif++- const withCity = withCityResult[0]?.total || 0+ // 检查数据库中是否缺少GPS坐标(需要从EXIF重新提取)+ if (!photo.latitude || !photo.longitude) {+ needsGpsReindex+++ }+ // 检查是否缺少城市信息(需要地理编码)+ else if (+ !photo.city ||+ !photo.country ||+ photo.city === '' ||+ photo.country === ''+ ) {+ needsLocationReindex+++ }+ // 已有完整信息+ else {+ photosWithCity+++ }+ }+ }++ const needsReindex = needsGpsReindex + needsLocationReindexreturn {statistics: {- totalPhotosWithGps: totalWithGps,- photosWithCity: withCity,+ totalPhotosWithGps: totalWithGpsInExif,+ photosWithCity: photosWithCity,photosNeedingReindex: needsReindex,- coveragePercentage: totalWithGps > 0 ? ((withCity / totalWithGps) * 100).toFixed(1) : '0',+ photosNeedingGpsReindex: needsGpsReindex,+ photosNeedingLocationReindex: needsLocationReindex,+ coveragePercentage:+ totalWithGpsInExif > 0+ ? ((photosWithCity / totalWithGpsInExif) * 100).toFixed(1)+ : '0',},- message: needsReindex > 0- ? `发现 ${needsReindex} 张照片需要重新索引城市信息`- : '所有有GPS坐标的照片都已有城市信息'+ message:+ needsReindex > 0+ ? `发现 ${needsReindex} 张照片需要重新索引 (${needsGpsReindex} 张需要GPS重新提取, ${needsLocationReindex} 张需要城市信息重新索引)`+ : '所有有GPS信息的照片都已完成索引',}-} catch (error) {logger.chrono.error('Failed to get reindex city statistics:', error)throw createError({diff --git a/server/api/photos/reindex-city.post.ts b/server/api/photos/reindex-city.post.tsindex 19805855..ba997396 100644--- a/server/api/photos/reindex-city.post.ts+++ b/server/api/photos/reindex-city.post.ts@@ -1,29 +1,21 @@import { isNull, and, isNotNull, sql, eq } from 'drizzle-orm'-import { extractLocationFromGPS } from '~~/server/services/location/geocoding'+import {+ extractLocationFromGPS,+ parseGPSCoordinates,+} from '~~/server/services/location/geocoding'export default eventHandler(async (event) => {await requireUserSession(event)-+const body = await readBody(event)const { force = false } = bodytry {- // 查找有GPS坐标但缺少城市信息的照片- let whereConditions = [- isNotNull(tables.photos.latitude),- isNotNull(tables.photos.longitude),- ]-- // 如果不是强制重新索引,则只处理没有城市信息的照片- if (!force) {- whereConditions.push(- sql`(city IS NULL OR city = '' OR country IS NULL OR country = '')`- )- }-- const photosToUpdate = await useDB()+ // 查找EXIF中有GPS坐标但数据库中缺少完整位置信息的照片+ const photosWithExif = await useDB().select({id: tables.photos.id,+ exif: tables.photos.exif,latitude: tables.photos.latitude,longitude: tables.photos.longitude,city: tables.photos.city,@@ -31,7 +23,42 @@ export default eventHandler(async (event) => {locationName: tables.photos.locationName,}).from(tables.photos)- .where(and(...whereConditions))+ .where(isNotNull(tables.photos.exif))++ // 筛选需要处理的照片+ const photosToUpdate: typeof photosWithExif = []++ for (const photo of photosWithExif) {+ const exif = photo.exif as any+ if (!exif) continue++ // 检查EXIF中是否有GPS数据+ const hasGpsInExif =+ (exif.GPSLatitude && exif.GPSLongitude) || exif.GPSCoordinates++ if (!hasGpsInExif) continue++ // 根据force参数决定处理条件+ let shouldProcess = false++ if (force) {+ // 强制重新索引所有有GPS的照片+ shouldProcess = true+ } else {+ // 只处理缺少GPS坐标或城市信息的照片+ const needsGpsReindex = !photo.latitude || !photo.longitude+ const needsLocationReindex =+ !photo.city ||+ !photo.country ||+ photo.city === '' ||+ photo.country === ''+ shouldProcess = needsGpsReindex || needsLocationReindex+ }++ if (shouldProcess) {+ photosToUpdate.push(photo)+ }+ }if (photosToUpdate.length === 0) {return {@@ -41,7 +68,9 @@ export default eventHandler(async (event) => {}}- logger.chrono.info(`Found ${photosToUpdate.length} photos to reindex city information`)+ logger.chrono.info(+ `Found ${photosToUpdate.length} photos to reindex city information`,+ )let processed = 0let updated = 0@@ -51,27 +80,53 @@ export default eventHandler(async (event) => {for (const photo of photosToUpdate) {try {processed++-- logger.chrono.info(`Processing photo ${photo.id} (${processed}/${photosToUpdate.length})`)- // 重新获取地理位置信息- const locationInfo = await extractLocationFromGPS(- photo.latitude!,- photo.longitude!+ logger.chrono.info(+ `Processing photo ${photo.id} (${processed}/${photosToUpdate.length})`,)+ const exif = photo.exif as any+ let latitude = photo.latitude+ let longitude = photo.longitude++ // 如果数据库中没有GPS坐标,从EXIF重新提取+ if (!latitude || !longitude) {+ const gpsCoords = parseGPSCoordinates(exif)+ latitude = gpsCoords.latitude || null+ longitude = gpsCoords.longitude || null++ if (!latitude || !longitude) {+ logger.chrono.warn(+ `No valid GPS coordinates found in EXIF for photo ${photo.id}`,+ )+ continue+ }++ logger.chrono.info(+ `Extracted GPS coordinates from EXIF: ${latitude}, ${longitude}`,+ )+ }++ // 重新获取地理位置信息+ const locationInfo = await extractLocationFromGPS(latitude, longitude)+if (locationInfo && (locationInfo.city || locationInfo.country)) {// 只有当获取到新的位置信息时才更新- const shouldUpdate = force ||- !photo.city ||- !photo.country ||+ const shouldUpdate =+ force ||+ !photo.city ||+ !photo.country ||photo.city !== locationInfo.city ||- photo.country !== locationInfo.country+ photo.country !== locationInfo.country ||+ !photo.latitude ||+ !photo.longitudeif (shouldUpdate) {await useDB().update(tables.photos).set({+ latitude: latitude,+ longitude: longitude,city: locationInfo.city || photo.city,country: locationInfo.country || photo.country,locationName: locationInfo.locationName || photo.locationName,@@ -79,52 +134,60 @@ export default eventHandler(async (event) => {.where(eq(tables.photos.id, photo.id))updated++-+logger.chrono.success(- `Updated photo ${photo.id}: ${locationInfo.city}, ${locationInfo.country}`+ `Updated photo ${photo.id}: ${latitude}, ${longitude} -> ${locationInfo.city}, ${locationInfo.country}`,)} else {- logger.chrono.info(`Photo ${photo.id} already has up-to-date location info`)+ logger.chrono.info(+ `Photo ${photo.id} already has up-to-date location info`,+ )}} else {- logger.chrono.warn(`No location info found for photo ${photo.id} at ${photo.latitude}, ${photo.longitude}`)+ logger.chrono.warn(+ `No location info found for photo ${photo.id} at ${latitude}, ${longitude}`,+ )}// 添加延迟以遵守API速率限制(Nominatim要求至少1秒间隔)if (processed < photosToUpdate.length) {- await new Promise(resolve => setTimeout(resolve, 1100))+ await new Promise((resolve) => setTimeout(resolve, 1100))}-} catch (error) {- const errorMessage = error instanceof Error ? error.message : 'Unknown error'+ const errorMessage =+ error instanceof Error ? error.message : 'Unknown error'logger.chrono.error(`Failed to process photo ${photo.id}:`, error)errors.push({photoId: photo.id,- error: errorMessage+ error: errorMessage,})}}const result = {- message: `城市信息重新索引完成`,+ message: `位置信息重新索引完成`,processed,updated,errors: errors.length > 0 ? errors : undefined,statistics: {totalFound: photosToUpdate.length,- successRate: processed > 0 ? ((processed - errors.length) / processed * 100).toFixed(1) + '%' : '0%'- }+ successRate:+ processed > 0+ ? (((processed - errors.length) / processed) * 100).toFixed(1) + '%'+ : '0%',+ },}- logger.chrono.success(`City reindexing completed: ${updated} photos updated out of ${processed} processed`)+ logger.chrono.success(+ `Location reindexing completed: ${updated} photos updated out of ${processed} processed`,+ )return result-} catch (error) {- logger.chrono.error('Failed to reindex city information:', error)+ logger.chrono.error('Failed to reindex location information:', error)throw createError({statusCode: 500,- statusMessage: 'Failed to reindex city information',+ statusMessage: 'Failed to reindex location information',})}})