From 02d844a771bf68308f9d64b76b9d911df15900e7 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 17 Jun 2025 17:15:51 +0800 Subject: [PATCH] refactor: simplify photo deletion handling and manifest saving - Streamlined the handling of deleted photos by directly passing the manifest to the `handleDeletedPhotos` function. - Removed unnecessary options checks and logging parameters from the `saveManifest` function, enhancing clarity and reducing complexity. - Updated thumbnail deletion logic to ensure thumbnails not present in the manifest are removed efficiently. Signed-off-by: Innei --- packages/builder/src/builder/builder.ts | 16 ++------- packages/builder/src/manifest/manager.ts | 45 +++++++++--------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/packages/builder/src/builder/builder.ts b/packages/builder/src/builder/builder.ts index 39bc4f26..47f626a3 100644 --- a/packages/builder/src/builder/builder.ts +++ b/packages/builder/src/builder/builder.ts @@ -261,21 +261,9 @@ export class PhotoGalleryBuilder { } // 检测并处理已删除的图片 - if ( - !options.isForceMode && - !options.isForceManifest && - existingManifestItems.length > 0 - ) { - deletedCount = await handleDeletedPhotos( - existingManifestItems, - s3ImageKeys, - logger.main, - logger.fs, - ) - } - + deletedCount = await handleDeletedPhotos(manifest) // 保存 manifest - await saveManifest(manifest, logger.fs) + await saveManifest(manifest) // 显示构建结果 if (this.config.options.showDetailedStats) { diff --git a/packages/builder/src/manifest/manager.ts b/packages/builder/src/manifest/manager.ts index 1551f6fc..ab3f139c 100644 --- a/packages/builder/src/manifest/manager.ts +++ b/packages/builder/src/manifest/manager.ts @@ -1,10 +1,9 @@ import fs from 'node:fs/promises' -import path from 'node:path' +import path, { basename } from 'node:path' import { workdir } from '@afilmory/builder/path.js' import type { _Object } from '@aws-sdk/client-s3' -import type { Logger } from '../logger/index.js' import { logger } from '../logger/index.js' import type { AfilmoryManifest } from '../types/manifest.js' import type { PhotoManifestItem } from '../types/photo.js' @@ -45,10 +44,7 @@ export function needsUpdate( } // 保存 manifest -export async function saveManifest( - items: PhotoManifestItem[], - fsLogger?: Logger['fs'], -): Promise { +export async function saveManifest(items: PhotoManifestItem[]): Promise { // 按日期排序(最新的在前) const sortedManifest = [...items].sort( (a, b) => new Date(b.dateTaken).getTime() - new Date(a.dateTaken).getTime(), @@ -67,42 +63,33 @@ export async function saveManifest( ), ) - fsLogger?.info(`📁 Manifest 保存至:${manifestPath}`) + logger.fs.info(`📁 Manifest 保存至:${manifestPath}`) } // 检测并处理已删除的图片 export async function handleDeletedPhotos( items: PhotoManifestItem[], - s3ImageKeys: Set, - mainLogger?: Logger['main'], - fsLogger?: Logger['fs'], ): Promise { + logger.main.info('🔍 检查已删除的图片...') if (items.length === 0) { + // Clear all thumbnails + await fs.rm(path.join(workdir, 'public/thumbnails'), { recursive: true }) + logger.main.info('🔍 没有图片,清空缩略图...') return 0 } - mainLogger?.info('🔍 检查已删除的图片...') let deletedCount = 0 + const allThumbnails = await fs.readdir( + path.join(workdir, 'public/thumbnails'), + ) - for (const existingItem of items) { - // 如果现有 manifest 中的图片在 S3 中不存在了 - if (!s3ImageKeys.has(existingItem.s3Key)) { - mainLogger?.info(`🗑️ 检测到已删除的图片:${existingItem.s3Key}`) + // If thumbnails not in manifest, delete it + const manifestKeySet = new Set(items.map((item) => item.id)) + + for (const thumbnail of allThumbnails) { + if (!manifestKeySet.has(basename(thumbnail, '.webp'))) { + await fs.unlink(path.join(workdir, 'public/thumbnails', thumbnail)) deletedCount++ - - // 删除对应的缩略图文件 - try { - const thumbnailPath = path.join( - workdir, - 'public/thumbnails', - `${existingItem.id}.webp`, - ) - await fs.unlink(thumbnailPath) - fsLogger?.info(`🗑️ 已删除缩略图:${existingItem.id}.webp`) - } catch (error) { - // 缩略图可能已经不存在,忽略错误 - fsLogger?.warn(`删除缩略图失败:${existingItem.id}.webp`, error) - } } }