优化 素材库新增处理优化,提速,其余bug处理
This commit is contained in:
1 parent
ad774d8a22
commit
4bce6f5d72
67 files changed
+4861
-3111
No files matched your search
@@ -13,8 +13,6 @@ sharp.cache({
|
||||
// 设置并发限制
|
||||
sharp.concurrency(1); // 一次只处理 1 张图片
|
||||
|
||||
console.log('[Sharp] Memory-optimized configuration applied: cache=50MB, concurrency=1');
|
||||
|
||||
// 支持的文件格式(确定可以生成缩略图的)
|
||||
const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'bmp', 'tiff', 'tif', 'avif', 'heif', 'heic', 'svg'];
|
||||
|
||||
@@ -248,13 +246,6 @@ async function generateThumbnail(inputPath, outputPath, targetHeight = 200) {
|
||||
const stats = fs.statSync(outputPath);
|
||||
const finalSize = stats.size;
|
||||
|
||||
// 可选:输出详细信息(每100张输出一次,避免日志过多)
|
||||
const shouldLog = Math.random() < 0.01; // 1% 概率输出
|
||||
if (shouldLog) {
|
||||
const sizeKB = (finalSize / 1024).toFixed(1);
|
||||
console.log(`📸 Thumbnail: ${config.megaPixels}MP → ${config.width}x${config.height} (${sizeKB}KB, Q${quality}, ratio ${downscaleRatio.toFixed(1)}x)`);
|
||||
}
|
||||
|
||||
return {
|
||||
width: config.width,
|
||||
height: config.height,
|
||||
@@ -324,8 +315,6 @@ async function generateImageThumbnails(imagePath, libraryPath) {
|
||||
const hash = crypto.createHash('md5').update(relativePath).digest('hex');
|
||||
const fileType = getFileType(imagePath);
|
||||
|
||||
console.log(`📝 Processing file: ${path.basename(imagePath)}, type: ${fileType}`);
|
||||
|
||||
// Sharding: use first 2 chars of hash for subdirectories (e.g. /ab/)
|
||||
const shard1 = hash.slice(0, 2);
|
||||
// 1-level sharding: .flypic/thumbnails/ab/hash.webp
|
||||
@@ -342,34 +331,27 @@ async function generateImageThumbnails(imagePath, libraryPath) {
|
||||
thumbnailResult = await generateThumbnail(imagePath, out480, 480);
|
||||
} else if (fileType === 'video') {
|
||||
// 视频:尝试提取封面
|
||||
console.log(`🎬 Extracting video thumbnail for: ${path.basename(imagePath)}`);
|
||||
thumbnailResult = await extractVideoThumbnail(imagePath, out480);
|
||||
|
||||
// 如果提取失败,生成占位图
|
||||
if (!thumbnailResult) {
|
||||
console.log(`🎬 Generating video placeholder for: ${path.basename(imagePath)}`);
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, 'video', ext);
|
||||
}
|
||||
} else if (fileType === 'design') {
|
||||
// 设计文件:尝试提取嵌入缩略图(仅 PSD)
|
||||
if (ext.toLowerCase() === 'psd') {
|
||||
console.log(`🎨 Extracting PSD thumbnail for: ${path.basename(imagePath)}`);
|
||||
thumbnailResult = await extractPSDThumbnail(imagePath, out480);
|
||||
}
|
||||
|
||||
// 如果提取失败或不是 PSD,生成占位图
|
||||
if (!thumbnailResult) {
|
||||
console.log(`🎨 Generating design placeholder for: ${path.basename(imagePath)}`);
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, 'design', ext);
|
||||
}
|
||||
} else {
|
||||
// 其他类型(音频/文档/未知):生成占位图
|
||||
console.log(`📦 Generating ${fileType} placeholder for: ${path.basename(imagePath)}`);
|
||||
thumbnailResult = await generatePlaceholderThumbnail(out480, fileType, ext);
|
||||
}
|
||||
|
||||
console.log(`✅ Thumbnail generated: ${path.basename(out480)}, ${thumbnailResult.width}x${thumbnailResult.height}, ${(thumbnailResult.size / 1024).toFixed(1)}KB`);
|
||||
|
||||
// 返回相对于 libraryPath 的路径(包含 .flypic 前缀)
|
||||
const thumbnailPath = path.relative(libraryPath, out480).replace(/\\/g, '/');
|
||||
|
||||
@@ -427,7 +409,6 @@ async function extractPSDThumbnail(psdPath, outputPath) {
|
||||
|
||||
// 先获取原始缩略图尺寸
|
||||
const metadata = await sharp(jpegData).metadata();
|
||||
console.log(` 📐 PSD embedded thumbnail: ${metadata.width}x${metadata.height}`);
|
||||
|
||||
// 使用高质量缩放,保持宽高比
|
||||
const aspectRatio = metadata.width / metadata.height;
|
||||
@@ -477,7 +458,6 @@ async function extractPSDThumbnail(psdPath, outputPath) {
|
||||
.toFile(outputPath);
|
||||
|
||||
const stats = fs.statSync(outputPath);
|
||||
console.log(` ✅ PSD thumbnail extracted: ${(stats.size / 1024).toFixed(1)}KB`);
|
||||
return {
|
||||
width: targetWidth,
|
||||
height: targetHeight,
|
||||
@@ -524,7 +504,6 @@ async function extractVideoThumbnail(videoPath, outputPath) {
|
||||
if (fs.existsSync(tempJpg)) {
|
||||
// 先获取实际尺寸
|
||||
const metadata = await sharp(tempJpg).metadata();
|
||||
console.log(` 📐 Video frame: ${metadata.width}x${metadata.height}`);
|
||||
|
||||
// 保持宽高比缩放到 480 高度
|
||||
const aspectRatio = metadata.width / metadata.height;
|
||||
@@ -544,7 +523,6 @@ async function extractVideoThumbnail(videoPath, outputPath) {
|
||||
fs.unlinkSync(tempJpg); // 删除临时 JPG
|
||||
|
||||
const stats = fs.statSync(outputPath);
|
||||
console.log(` ✅ Video thumbnail extracted: ${targetWidth}x${targetHeight}, ${(stats.size / 1024).toFixed(1)}KB`);
|
||||
return {
|
||||
width: targetWidth,
|
||||
height: targetHeight,
|
||||
|
||||
Reference in new issue
Block a user