优化 拖入文件上传速度
This commit is contained in:
1 parent
aeb403f3c0
commit
a71230bce4
4 files changed
+134
-41
No files matched your search
@@ -237,6 +237,9 @@ async function getFolderStructure(libraryPath) {
|
||||
* @param {boolean} dryRun - If true, return data instead of inserting into DB (for batch write)
|
||||
*/
|
||||
async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
const processStartTime = Date.now();
|
||||
const stepTimes = {}; // 记录每个步骤的耗时
|
||||
|
||||
try {
|
||||
const relativePath = path.relative(libraryPath, imagePath);
|
||||
const filename = path.basename(imagePath);
|
||||
@@ -244,8 +247,13 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
const folder = folderRaw === '.' ? '' : folderRaw.replace(/\\/g, '/');
|
||||
|
||||
// Check if image already exists in database
|
||||
let stepStart = Date.now();
|
||||
const existing = db.getImageByPath(relativePath.replace(/\\/g, '/'));
|
||||
stepTimes.dbCheck = Date.now() - stepStart;
|
||||
|
||||
stepStart = Date.now();
|
||||
const currentHash = calculateFileHash(imagePath);
|
||||
stepTimes.hashCalc = Date.now() - stepStart;
|
||||
|
||||
// For unchanged files, check whether thumbnails need upgrade/regeneration
|
||||
let needRegenThumbs = false;
|
||||
@@ -270,17 +278,26 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
|
||||
// Skip only if unchanged and thumbnails are up-to-date
|
||||
if (existing && existing.file_hash === currentHash && !needRegenThumbs) {
|
||||
const totalTime = Date.now() - processStartTime;
|
||||
// logger.info(`⏭️ 跳过 (未变化): ${filename} (${totalTime}ms)`);
|
||||
return { status: 'skipped', path: relativePath };
|
||||
}
|
||||
|
||||
// Get image metadata
|
||||
stepStart = Date.now();
|
||||
const metadata = await getImageMetadata(imagePath);
|
||||
stepTimes.metadata = Date.now() - stepStart;
|
||||
if (!metadata) {
|
||||
const totalTime = Date.now() - processStartTime;
|
||||
logger.warn(`❌ 元数据读取失败: ${filename} (${totalTime}ms)`);
|
||||
return { status: 'error', path: relativePath, error: 'Failed to read metadata' };
|
||||
}
|
||||
|
||||
// Generate thumbnails (also for unchanged files when thumbnails missing/outdated)
|
||||
stepStart = Date.now();
|
||||
const thumbnails = await generateImageThumbnails(imagePath, libraryPath);
|
||||
stepTimes.thumbnail = Date.now() - stepStart;
|
||||
|
||||
const fileType = getFileType(imagePath);
|
||||
|
||||
// 使用缩略图的实际尺寸(对于视频/PSD,这是提取后的真实尺寸)
|
||||
@@ -308,12 +325,30 @@ async function processImage(imagePath, libraryPath, db, dryRun = false) {
|
||||
}
|
||||
|
||||
// Insert/update in database
|
||||
stepStart = Date.now();
|
||||
db.insertImage(imageData);
|
||||
|
||||
return { status: 'processed', path: relativePath };
|
||||
stepTimes.dbInsert = Date.now() - stepStart;
|
||||
|
||||
const totalTime = Date.now() - processStartTime;
|
||||
|
||||
// 输出详细的性能日志
|
||||
const logParts = [
|
||||
`总计${totalTime}ms`,
|
||||
`哈希${stepTimes.hashCalc}ms`,
|
||||
`元数据${stepTimes.metadata}ms`,
|
||||
`缩略图${stepTimes.thumbnail}ms`
|
||||
];
|
||||
|
||||
// 只有当总耗时超过 500ms 时才输出警告
|
||||
if (totalTime > 500) {
|
||||
logger.warn(`⚠️ 处理较慢: ${filename} (${logParts.join(', ')})`);
|
||||
}
|
||||
|
||||
return { status: 'processed', path: relativePath, timing: stepTimes, totalTime };
|
||||
} catch (error) {
|
||||
logger.error('处理图片失败:', path.basename(imagePath), error.message);
|
||||
return { status: 'error', path: imagePath, error: error.message };
|
||||
const totalTime = Date.now() - processStartTime;
|
||||
logger.error(`❌ 处理图片失败: ${path.basename(imagePath)} (${totalTime}ms)`, error.message);
|
||||
return { status: 'error', path: imagePath, error: error.message, totalTime };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -183,6 +183,9 @@ function getThumbnailConfig(originalWidth, originalHeight, targetHeight = 200) {
|
||||
* 4. 不修改色彩(饱和度/亮度),保持原图风格
|
||||
*/
|
||||
async function generateThumbnail(inputPath, outputPath, targetHeight = 200) {
|
||||
const startTime = Date.now();
|
||||
const filename = path.basename(inputPath);
|
||||
|
||||
try {
|
||||
// 先读取文件到 Buffer,避免 Sharp 锁定文件句柄
|
||||
let inputBuffer = fs.readFileSync(inputPath);
|
||||
@@ -220,6 +223,7 @@ async function generateThumbnail(inputPath, outputPath, targetHeight = 200) {
|
||||
const effort = constants.THUMBNAIL_GENERATION.EFFORT;
|
||||
|
||||
// 生成缩略图(使用 buffer,不锁定原文件)
|
||||
const processStart = Date.now();
|
||||
await sharp(inputBuffer)
|
||||
.rotate() // 按EXIF旋转
|
||||
.resize(config.width, config.height, {
|
||||
@@ -247,6 +251,7 @@ async function generateThumbnail(inputPath, outputPath, targetHeight = 200) {
|
||||
alphaQuality: hasAlpha ? 100 : undefined
|
||||
})
|
||||
.toFile(outputPath);
|
||||
const processTime = Date.now() - processStart;
|
||||
|
||||
// 显式释放 Buffer 内存
|
||||
inputBuffer = null;
|
||||
@@ -261,13 +266,21 @@ async function generateThumbnail(inputPath, outputPath, targetHeight = 200) {
|
||||
const stats = fs.statSync(outputPath);
|
||||
const finalSize = stats.size;
|
||||
|
||||
const totalTime = Date.now() - startTime;
|
||||
|
||||
// 只有当耗时超过 200ms 时才输出警告
|
||||
if (totalTime > 200) {
|
||||
console.log(`⚠️ Sharp处理较慢: ${filename} (总计${totalTime}ms, 处理${processTime}ms)`);
|
||||
}
|
||||
|
||||
return {
|
||||
width: config.width,
|
||||
height: config.height,
|
||||
size: finalSize,
|
||||
quality: quality,
|
||||
originalPixels: config.originalPixels,
|
||||
path: outputPath
|
||||
path: outputPath,
|
||||
timing: { total: totalTime, process: processTime }
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error generating thumbnail:', error);
|
||||
@@ -325,6 +338,10 @@ async function getImageMetadata(imagePath) {
|
||||
* 使用 480px 高度(与 Billfish 一致)
|
||||
*/
|
||||
async function generateImageThumbnails(imagePath, libraryPath) {
|
||||
const startTime = Date.now();
|
||||
const filename = path.basename(imagePath);
|
||||
const stepTimes = {};
|
||||
|
||||
const flypicDir = path.join(libraryPath, '.flypic');
|
||||
const relativePath = path.relative(libraryPath, imagePath);
|
||||
const hash = crypto.createHash('md5').update(relativePath).digest('hex');
|
||||
@@ -339,45 +356,70 @@ async function generateImageThumbnails(imagePath, libraryPath) {
|
||||
fs.mkdirSync(path.dirname(out480), { recursive: true });
|
||||
|
||||
let thumbnailResult;
|
||||
let stepStart = Date.now();
|
||||
|
||||
// 根据文件类型生成不同的缩略图
|
||||
const ext = path.extname(imagePath).slice(1).toUpperCase();
|
||||
|
||||
if (fileType === 'image' && canGenerateThumbnail(imagePath)) {
|
||||
// 图片:使用 Sharp 生成真实缩略图
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await generateThumbnail(imagePath, out480, targetHeight);
|
||||
stepTimes.generate = Date.now() - stepStart;
|
||||
} else if (fileType === 'video') {
|
||||
// 视频:尝试提取封面
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await extractVideoThumbnail(imagePath, out480);
|
||||
stepTimes.videoExtract = Date.now() - stepStart;
|
||||
|
||||
// 如果提取失败,生成占位图
|
||||
if (!thumbnailResult) {
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, 'video', ext);
|
||||
stepTimes.placeholder = Date.now() - stepStart;
|
||||
}
|
||||
} else if (fileType === 'design') {
|
||||
// 设计文件:尝试提取嵌入缩略图(仅 PSD)
|
||||
if (ext.toLowerCase() === 'psd') {
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await extractPSDThumbnail(imagePath, out480);
|
||||
stepTimes.psdExtract = Date.now() - stepStart;
|
||||
}
|
||||
|
||||
// 如果提取失败或不是 PSD,生成占位图
|
||||
if (!thumbnailResult) {
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, 'design', ext);
|
||||
stepTimes.placeholder = Date.now() - stepStart;
|
||||
}
|
||||
} else {
|
||||
// 其他类型(音频/文档/未知):生成占位图
|
||||
stepStart = Date.now();
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, fileType, ext);
|
||||
stepTimes.placeholder = Date.now() - stepStart;
|
||||
}
|
||||
|
||||
// 返回相对于 libraryPath 的路径(包含 .flypic 前缀)
|
||||
const thumbnailPath = path.relative(libraryPath, out480).replace(/\\/g, '/');
|
||||
|
||||
const totalTime = Date.now() - startTime;
|
||||
|
||||
// 只有当总耗时超过 300ms 时才输出警告
|
||||
if (totalTime > 300) {
|
||||
const details = Object.entries(stepTimes)
|
||||
.map(([key, time]) => `${key}:${time}ms`)
|
||||
.join(', ');
|
||||
console.log(`⚠️ 缩略图较慢: ${filename} (总计${totalTime}ms, ${details})`);
|
||||
}
|
||||
|
||||
return {
|
||||
thumbnail_path: thumbnailPath,
|
||||
thumbnail_size: thumbnailResult.size,
|
||||
width: thumbnailResult.width,
|
||||
height: thumbnailResult.height,
|
||||
file_type: fileType
|
||||
file_type: fileType,
|
||||
timing: stepTimes,
|
||||
totalTime
|
||||
};
|
||||
}
|
||||
|
||||
@@ -456,6 +498,7 @@ async function extractPSDThumbnail(psdPath, outputPath) {
|
||||
const targetWidth = Math.round(targetHeight * aspectRatio);
|
||||
|
||||
// 简化处理策略
|
||||
const processStart = Date.now();
|
||||
await sharp(jpegData)
|
||||
.resize(targetWidth, targetHeight, {
|
||||
fit: 'inside',
|
||||
@@ -469,6 +512,7 @@ async function extractPSDThumbnail(psdPath, outputPath) {
|
||||
smartSubsample: false
|
||||
})
|
||||
.toFile(outputPath);
|
||||
const processTime = Date.now() - processStart;
|
||||
|
||||
const stats = fs.statSync(outputPath);
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user