删除 多余调试文件
This commit is contained in:
1 parent
79816a7316
commit
de15191f33
12 files changed
-1329
No files matched your search
@@ -1,12 +0,0 @@
|
||||
const dbPool = require('./database/dbPool');
|
||||
|
||||
const db = dbPool.acquire('C:\\Users\\qingy\\Pictures\\商品图片包');
|
||||
const rows = db.db.prepare('SELECT id, path, thumbnail_path FROM images LIMIT 5').all();
|
||||
console.log('Sample images:', JSON.stringify(rows, null, 2));
|
||||
|
||||
const countWithThumb = db.db.prepare('SELECT COUNT(*) as count FROM images WHERE thumbnail_path IS NOT NULL').get();
|
||||
const countTotal = db.db.prepare('SELECT COUNT(*) as count FROM images').get();
|
||||
|
||||
console.log(`\nImages with thumbnail: ${countWithThumb.count} / ${countTotal.count}`);
|
||||
|
||||
dbPool.release('C:\\Users\\qingy\\Pictures\\商品图片包');
|
||||
@@ -1,108 +0,0 @@
|
||||
const LibraryDatabase = require('./database/db');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// 从配置文件读取素材库路径
|
||||
const configPath = path.join(process.env.APPDATA || process.env.HOME, '.flypic', 'config.json');
|
||||
let library = null;
|
||||
|
||||
if (fs.existsSync(configPath)) {
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||
if (config.libraries && config.libraries.length > 0) {
|
||||
library = config.libraries[0];
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有配置,使用命令行参数
|
||||
if (!library && process.argv[2]) {
|
||||
library = {
|
||||
name: 'Test Library',
|
||||
path: process.argv[2]
|
||||
};
|
||||
}
|
||||
|
||||
if (!library) {
|
||||
console.log('❌ 没有找到素材库配置');
|
||||
console.log('用法: node backend/check-thumbnail-paths.js [素材库路径]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`📚 检查素材库: ${library.name} (${library.path})`);
|
||||
|
||||
const db = new LibraryDatabase(library.path);
|
||||
|
||||
// 检查数据库表结构
|
||||
console.log('\n📋 检查数据库表结构:');
|
||||
const tableInfo = db.db.prepare("PRAGMA table_info(images)").all();
|
||||
console.log('images 表字段:');
|
||||
tableInfo.forEach(col => {
|
||||
console.log(` - ${col.name} (${col.type})`);
|
||||
});
|
||||
|
||||
// 检查前10条记录的缩略图路径
|
||||
console.log('\n🔍 检查前10条记录的缩略图路径:');
|
||||
const images = db.db.prepare('SELECT id, filename, thumbnail_path, thumbnail_size, file_type FROM images LIMIT 10').all();
|
||||
|
||||
images.forEach((img, index) => {
|
||||
console.log(`\n${index + 1}. ${img.filename}`);
|
||||
console.log(` file_type: ${img.file_type || 'NULL'}`);
|
||||
console.log(` thumbnail_path: ${img.thumbnail_path || 'NULL'}`);
|
||||
console.log(` thumbnail_size: ${img.thumbnail_size || 'NULL'}`);
|
||||
|
||||
// 检查缩略图文件是否存在
|
||||
if (img.thumbnail_path) {
|
||||
const thumbFullPath = path.join(library.path, img.thumbnail_path);
|
||||
const exists = fs.existsSync(thumbFullPath);
|
||||
console.log(` 文件存在: ${exists ? '✅' : '❌'} (${thumbFullPath})`);
|
||||
|
||||
// 检查路径格式
|
||||
const pathParts = img.thumbnail_path.split('/');
|
||||
console.log(` 路径格式: ${pathParts.join(' / ')}`);
|
||||
|
||||
// 期望格式: .flypic/thumbnails/ab/hash.webp
|
||||
if (pathParts.length === 4 && pathParts[0] === '.flypic' && pathParts[1] === 'thumbnails') {
|
||||
console.log(` 格式正确: ✅`);
|
||||
} else {
|
||||
console.log(` 格式错误: ❌ (期望: .flypic/thumbnails/XX/hash.webp)`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 统计缩略图路径情况
|
||||
console.log('\n📊 统计信息:');
|
||||
const stats = db.db.prepare(`
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(thumbnail_path) as has_thumbnail_path,
|
||||
COUNT(thumbnail_size) as has_thumbnail_size,
|
||||
COUNT(file_type) as has_file_type
|
||||
FROM images
|
||||
`).get();
|
||||
|
||||
console.log(` 总记录数: ${stats.total}`);
|
||||
console.log(` 有 thumbnail_path: ${stats.has_thumbnail_path} (${(stats.has_thumbnail_path / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(` 有 thumbnail_size: ${stats.has_thumbnail_size} (${(stats.has_thumbnail_size / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(` 有 file_type: ${stats.has_file_type} (${(stats.has_file_type / stats.total * 100).toFixed(1)}%)`);
|
||||
|
||||
// 检查缩略图路径格式
|
||||
const pathFormats = db.db.prepare(`
|
||||
SELECT
|
||||
thumbnail_path,
|
||||
COUNT(*) as count
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL
|
||||
GROUP BY
|
||||
CASE
|
||||
WHEN thumbnail_path LIKE '.flypic/thumbnails/__/%' THEN 'new_format'
|
||||
WHEN thumbnail_path LIKE '.flypic/thumbnails/%' THEN 'old_format'
|
||||
ELSE 'unknown'
|
||||
END
|
||||
`).all();
|
||||
|
||||
console.log('\n📁 缩略图路径格式分布:');
|
||||
pathFormats.forEach(format => {
|
||||
console.log(` ${format.thumbnail_path}: ${format.count} 条`);
|
||||
});
|
||||
|
||||
db.close();
|
||||
console.log('\n✅ 检查完成');
|
||||
@@ -1,54 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Memory Diagnostics CLI
|
||||
* 快速诊断内存问题
|
||||
*
|
||||
* 使用方法:
|
||||
* node --expose-gc diagnose-memory.js
|
||||
*/
|
||||
|
||||
const memoryDiagnostics = require('./utils/memoryDiagnostics');
|
||||
|
||||
console.log('🔍 FlyPic Memory Diagnostics\n');
|
||||
console.log('='.repeat(50));
|
||||
|
||||
// 1. 当前内存使用
|
||||
const usage = process.memoryUsage();
|
||||
console.log('\n📊 Current Memory Usage:');
|
||||
console.log(` RSS: ${(usage.rss / 1024 / 1024).toFixed(0)} MB`);
|
||||
console.log(` Heap Used: ${(usage.heapUsed / 1024 / 1024).toFixed(0)} MB`);
|
||||
console.log(` Heap Total: ${(usage.heapTotal / 1024 / 1024).toFixed(0)} MB`);
|
||||
console.log(` External: ${(usage.external / 1024 / 1024).toFixed(0)} MB`);
|
||||
console.log(` Array Buffers: ${(usage.arrayBuffers / 1024 / 1024).toFixed(0)} MB`);
|
||||
|
||||
// 2. 堆统计
|
||||
memoryDiagnostics.logHeapStatistics();
|
||||
|
||||
// 3. 检测内存泄漏
|
||||
memoryDiagnostics.diagnoseMemoryLeak();
|
||||
|
||||
// 4. 强制 GC 并报告
|
||||
console.log('\n🧹 Testing Garbage Collection...');
|
||||
const gcResult = memoryDiagnostics.forceGCAndReport();
|
||||
|
||||
if (gcResult) {
|
||||
const rssReduction = (gcResult.reclaimed.rss / gcResult.before.rss * 100).toFixed(1);
|
||||
const heapReduction = (gcResult.reclaimed.heap / gcResult.before.heap * 100).toFixed(1);
|
||||
|
||||
console.log(`\n📉 GC Effectiveness:`);
|
||||
console.log(` RSS reduced by: ${rssReduction}%`);
|
||||
console.log(` Heap reduced by: ${heapReduction}%`);
|
||||
|
||||
if (parseFloat(rssReduction) < 5) {
|
||||
console.log('\n⚠️ Warning: GC reclaimed very little RSS memory');
|
||||
console.log(' This suggests native memory leak (SQLite, Sharp, etc.)');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n' + '='.repeat(50));
|
||||
console.log('\n💡 Recommendations:');
|
||||
console.log(' 1. If RSS > 500MB: Check SQLite connections and Sharp image processing');
|
||||
console.log(' 2. If Heap > 200MB: Check for object retention and large arrays');
|
||||
console.log(' 3. If GC reclaims < 5%: Likely native memory leak');
|
||||
console.log(' 4. Monitor with: node --expose-gc --trace-gc server.js');
|
||||
console.log('\n');
|
||||
@@ -1,239 +0,0 @@
|
||||
/**
|
||||
* 诊断缩略图数据库结构问题
|
||||
*
|
||||
* 使用方法:
|
||||
* node backend/diagnose-thumbnail-issue.js "D:\你的素材库路径"
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// 检查命令行参数
|
||||
if (process.argv.length < 3) {
|
||||
console.log('❌ 请提供素材库路径');
|
||||
console.log('用法: node backend/diagnose-thumbnail-issue.js "D:\\你的素材库路径"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const libraryPath = process.argv[2];
|
||||
|
||||
if (!fs.existsSync(libraryPath)) {
|
||||
console.log(`❌ 素材库路径不存在: ${libraryPath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`📚 检查素材库: ${libraryPath}\n`);
|
||||
|
||||
// 检查 .flypic 目录
|
||||
const flypicDir = path.join(libraryPath, '.flypic');
|
||||
if (!fs.existsSync(flypicDir)) {
|
||||
console.log('❌ .flypic 目录不存在,素材库未初始化');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 检查数据库文件
|
||||
const dbPath = path.join(flypicDir, 'metadata.db');
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
console.log('❌ 数据库文件不存在');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('✅ 数据库文件存在\n');
|
||||
|
||||
// 加载数据库
|
||||
const Database = require('better-sqlite3');
|
||||
const db = new Database(dbPath);
|
||||
|
||||
// 1. 检查表结构
|
||||
console.log('📋 1. 检查数据库表结构:');
|
||||
console.log('=' .repeat(60));
|
||||
const tableInfo = db.prepare("PRAGMA table_info(images)").all();
|
||||
console.log('images 表字段:');
|
||||
tableInfo.forEach(col => {
|
||||
const nullable = col.notnull === 0 ? 'NULL' : 'NOT NULL';
|
||||
const defaultVal = col.dflt_value ? ` DEFAULT ${col.dflt_value}` : '';
|
||||
console.log(` ${col.name.padEnd(20)} ${col.type.padEnd(10)} ${nullable}${defaultVal}`);
|
||||
});
|
||||
|
||||
// 检查是否有必要的字段
|
||||
const requiredFields = ['thumbnail_path', 'thumbnail_size', 'file_type'];
|
||||
const existingFields = tableInfo.map(col => col.name);
|
||||
const missingFields = requiredFields.filter(field => !existingFields.includes(field));
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
console.log(`\n❌ 缺少字段: ${missingFields.join(', ')}`);
|
||||
} else {
|
||||
console.log('\n✅ 所有必要字段都存在');
|
||||
}
|
||||
|
||||
// 2. 检查数据统计
|
||||
console.log('\n📊 2. 数据统计:');
|
||||
console.log('=' .repeat(60));
|
||||
const stats = db.prepare(`
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(thumbnail_path) as has_thumbnail_path,
|
||||
COUNT(thumbnail_size) as has_thumbnail_size,
|
||||
COUNT(file_type) as has_file_type,
|
||||
COUNT(CASE WHEN thumbnail_path IS NULL THEN 1 END) as missing_thumbnail_path,
|
||||
COUNT(CASE WHEN thumbnail_size IS NULL THEN 1 END) as missing_thumbnail_size,
|
||||
COUNT(CASE WHEN file_type IS NULL THEN 1 END) as missing_file_type
|
||||
FROM images
|
||||
`).get();
|
||||
|
||||
console.log(`总记录数: ${stats.total}`);
|
||||
console.log(`有 thumbnail_path: ${stats.has_thumbnail_path} (${(stats.has_thumbnail_path / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(`有 thumbnail_size: ${stats.has_thumbnail_size} (${(stats.has_thumbnail_size / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(`有 file_type: ${stats.has_file_type} (${(stats.has_file_type / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(`缺少 thumbnail_path: ${stats.missing_thumbnail_path} (${(stats.missing_thumbnail_path / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(`缺少 thumbnail_size: ${stats.missing_thumbnail_size} (${(stats.missing_thumbnail_size / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(`缺少 file_type: ${stats.missing_file_type} (${(stats.missing_file_type / stats.total * 100).toFixed(1)}%)`);
|
||||
|
||||
// 3. 检查缩略图路径格式
|
||||
console.log('\n📁 3. 缩略图路径格式分析:');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
// 分析路径格式
|
||||
const pathAnalysis = db.prepare(`
|
||||
SELECT
|
||||
thumbnail_path,
|
||||
COUNT(*) as count
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL
|
||||
GROUP BY thumbnail_path
|
||||
LIMIT 10
|
||||
`).all();
|
||||
|
||||
if (pathAnalysis.length > 0) {
|
||||
console.log('前10个不同的缩略图路径:');
|
||||
pathAnalysis.forEach((row, index) => {
|
||||
console.log(` ${index + 1}. ${row.thumbnail_path} (${row.count} 条记录)`);
|
||||
|
||||
// 分析路径格式
|
||||
const parts = row.thumbnail_path.split('/');
|
||||
if (parts.length === 4 && parts[0] === '.flypic' && parts[1] === 'thumbnails' && parts[2].length === 2) {
|
||||
console.log(` ✅ 格式正确: .flypic/thumbnails/${parts[2]}/${parts[3]}`);
|
||||
} else {
|
||||
console.log(` ❌ 格式错误: 期望 .flypic/thumbnails/XX/hash.webp`);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('❌ 没有找到任何缩略图路径');
|
||||
}
|
||||
|
||||
// 4. 检查实际文件
|
||||
console.log('\n📂 4. 检查缩略图文件是否存在:');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
const sampleImages = db.prepare(`
|
||||
SELECT id, filename, thumbnail_path, file_type
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL
|
||||
LIMIT 5
|
||||
`).all();
|
||||
|
||||
if (sampleImages.length > 0) {
|
||||
console.log('检查前5个记录的缩略图文件:');
|
||||
sampleImages.forEach((img, index) => {
|
||||
console.log(`\n ${index + 1}. ${img.filename}`);
|
||||
console.log(` file_type: ${img.file_type || 'NULL'}`);
|
||||
console.log(` thumbnail_path: ${img.thumbnail_path}`);
|
||||
|
||||
if (img.thumbnail_path) {
|
||||
const thumbFullPath = path.join(libraryPath, img.thumbnail_path);
|
||||
const exists = fs.existsSync(thumbFullPath);
|
||||
console.log(` 文件存在: ${exists ? '✅' : '❌'}`);
|
||||
console.log(` 完整路径: ${thumbFullPath}`);
|
||||
|
||||
if (!exists) {
|
||||
// 检查是否是路径分隔符问题
|
||||
const thumbFullPathWin = path.join(libraryPath, img.thumbnail_path.replace(/\//g, '\\'));
|
||||
const existsWin = fs.existsSync(thumbFullPathWin);
|
||||
if (existsWin) {
|
||||
console.log(` ⚠️ 使用反斜杠路径存在: ${thumbFullPathWin}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log('❌ 没有找到任何有缩略图路径的记录');
|
||||
}
|
||||
|
||||
// 5. 检查缩略图目录结构
|
||||
console.log('\n📁 5. 检查缩略图目录结构:');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
const thumbnailsDir = path.join(flypicDir, 'thumbnails');
|
||||
if (fs.existsSync(thumbnailsDir)) {
|
||||
console.log(`✅ thumbnails 目录存在: ${thumbnailsDir}`);
|
||||
|
||||
// 列出子目录
|
||||
const subdirs = fs.readdirSync(thumbnailsDir, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
console.log(` 子目录数量: ${subdirs.length}`);
|
||||
if (subdirs.length > 0) {
|
||||
console.log(` 前10个子目录: ${subdirs.slice(0, 10).join(', ')}`);
|
||||
|
||||
// 检查第一个子目录中的文件
|
||||
const firstSubdir = subdirs[0];
|
||||
const firstSubdirPath = path.join(thumbnailsDir, firstSubdir);
|
||||
const files = fs.readdirSync(firstSubdirPath);
|
||||
console.log(` ${firstSubdir}/ 目录中的文件数: ${files.length}`);
|
||||
if (files.length > 0) {
|
||||
console.log(` 示例文件: ${files.slice(0, 3).join(', ')}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(`❌ thumbnails 目录不存在: ${thumbnailsDir}`);
|
||||
}
|
||||
|
||||
// 6. 诊断结论
|
||||
console.log('\n🔍 6. 诊断结论:');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
const issues = [];
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
issues.push(`❌ 数据库缺少字段: ${missingFields.join(', ')}`);
|
||||
}
|
||||
|
||||
if (stats.missing_thumbnail_path > 0) {
|
||||
issues.push(`⚠️ 有 ${stats.missing_thumbnail_path} 条记录缺少 thumbnail_path`);
|
||||
}
|
||||
|
||||
if (stats.missing_file_type > 0) {
|
||||
issues.push(`⚠️ 有 ${stats.missing_file_type} 条记录缺少 file_type`);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(thumbnailsDir)) {
|
||||
issues.push(`❌ 缩略图目录不存在`);
|
||||
}
|
||||
|
||||
if (issues.length === 0) {
|
||||
console.log('✅ 未发现明显问题');
|
||||
console.log('\n建议:');
|
||||
console.log('1. 检查前端代码中如何使用 thumbnail_path');
|
||||
console.log('2. 检查后端 API 返回的数据格式');
|
||||
console.log('3. 查看浏览器控制台的网络请求');
|
||||
} else {
|
||||
console.log('发现以下问题:');
|
||||
issues.forEach((issue, index) => {
|
||||
console.log(`${index + 1}. ${issue}`);
|
||||
});
|
||||
|
||||
console.log('\n建议修复方案:');
|
||||
if (missingFields.length > 0) {
|
||||
console.log('1. 运行数据库迁移脚本添加缺失字段');
|
||||
}
|
||||
if (stats.missing_thumbnail_path > 0 || stats.missing_file_type > 0) {
|
||||
console.log('2. 重新扫描素材库以生成缺失的缩略图和元数据');
|
||||
}
|
||||
if (!fs.existsSync(thumbnailsDir)) {
|
||||
console.log('3. 创建缩略图目录并重新生成缩略图');
|
||||
}
|
||||
}
|
||||
|
||||
db.close();
|
||||
console.log('\n✅ 诊断完成');
|
||||
Whitespace-only changes.
@@ -1,266 +0,0 @@
|
||||
/**
|
||||
* 完整诊断脚本 - 检查缩略图生成和显示的完整流程
|
||||
*
|
||||
* 使用方法:
|
||||
* node backend/full-diagnosis.js "素材库路径"
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const LibraryDatabase = require('./database/db');
|
||||
|
||||
// 检查命令行参数
|
||||
if (process.argv.length < 3) {
|
||||
console.log('❌ 请提供素材库路径');
|
||||
console.log('用法: node backend/full-diagnosis.js "D:\\你的素材库路径"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const libraryPath = process.argv[2];
|
||||
|
||||
if (!fs.existsSync(libraryPath)) {
|
||||
console.log(`❌ 素材库路径不存在: ${libraryPath}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('🔍 完整诊断:缩略图生成和显示流程');
|
||||
console.log('=' .repeat(80));
|
||||
console.log(`素材库路径: ${libraryPath}\n`);
|
||||
|
||||
// 1. 检查目录结构
|
||||
console.log('1️⃣ 检查目录结构:');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const flypicDir = path.join(libraryPath, '.flypic');
|
||||
const thumbDir = path.join(flypicDir, 'thumbnails');
|
||||
const dbPath = path.join(flypicDir, 'metadata.db');
|
||||
const thumb480Dir = path.join(thumbDir, '480');
|
||||
|
||||
console.log(` .flypic 目录: ${fs.existsSync(flypicDir) ? '✅ 存在' : '❌ 不存在'}`);
|
||||
console.log(` thumbnails 目录: ${fs.existsSync(thumbDir) ? '✅ 存在' : '❌ 不存在'}`);
|
||||
console.log(` metadata.db: ${fs.existsSync(dbPath) ? '✅ 存在' : '❌ 不存在'}`);
|
||||
console.log(` 480 目录: ${fs.existsSync(thumb480Dir) ? '⚠️ 存在(应该删除)' : '✅ 不存在(正确)'}`);
|
||||
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
console.log('\n❌ 数据库不存在,请先扫描素材库');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 检查分片目录
|
||||
if (fs.existsSync(thumbDir)) {
|
||||
const subdirs = fs.readdirSync(thumbDir, { withFileTypes: true })
|
||||
.filter(dirent => dirent.isDirectory())
|
||||
.map(dirent => dirent.name);
|
||||
|
||||
console.log(` 分片目录数量: ${subdirs.length}`);
|
||||
if (subdirs.length > 0) {
|
||||
console.log(` 示例分片目录: ${subdirs.slice(0, 5).join(', ')}`);
|
||||
|
||||
// 检查第一个分片目录中的文件
|
||||
const firstShard = subdirs[0];
|
||||
const firstShardPath = path.join(thumbDir, firstShard);
|
||||
const files = fs.readdirSync(firstShardPath);
|
||||
console.log(` ${firstShard}/ 中的文件数: ${files.length}`);
|
||||
if (files.length > 0) {
|
||||
console.log(` 示例文件: ${files.slice(0, 3).join(', ')}`);
|
||||
}
|
||||
} else {
|
||||
console.log(' ⚠️ 没有分片目录,缩略图可能未生成');
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 检查数据库
|
||||
console.log('\n2️⃣ 检查数据库:');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const db = new LibraryDatabase(libraryPath);
|
||||
|
||||
// 检查表结构
|
||||
const tableInfo = db.db.prepare("PRAGMA table_info(images)").all();
|
||||
const fieldNames = tableInfo.map(col => col.name);
|
||||
|
||||
console.log(' 数据库字段:');
|
||||
const requiredFields = ['thumbnail_path', 'thumbnail_size', 'file_type'];
|
||||
requiredFields.forEach(field => {
|
||||
const exists = fieldNames.includes(field);
|
||||
console.log(` - ${field}: ${exists ? '✅' : '❌'}`);
|
||||
});
|
||||
|
||||
// 统计数据
|
||||
const stats = db.db.prepare(`
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(thumbnail_path) as has_thumbnail_path,
|
||||
COUNT(CASE WHEN thumbnail_path IS NULL OR thumbnail_path = '' THEN 1 END) as missing_thumbnail_path
|
||||
FROM images
|
||||
`).get();
|
||||
|
||||
console.log(`\n 总记录数: ${stats.total}`);
|
||||
console.log(` 有 thumbnail_path: ${stats.has_thumbnail_path} (${(stats.has_thumbnail_path / stats.total * 100).toFixed(1)}%)`);
|
||||
console.log(` 缺少 thumbnail_path: ${stats.missing_thumbnail_path} (${(stats.missing_thumbnail_path / stats.total * 100).toFixed(1)}%)`);
|
||||
|
||||
if (stats.missing_thumbnail_path > 0) {
|
||||
console.log(` ⚠️ 有 ${stats.missing_thumbnail_path} 条记录缺少缩略图路径`);
|
||||
}
|
||||
|
||||
// 3. 检查具体记录
|
||||
console.log('\n3️⃣ 检查具体记录(前5条):');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
const samples = db.db.prepare(`
|
||||
SELECT id, filename, path, thumbnail_path, thumbnail_size, file_type
|
||||
FROM images
|
||||
LIMIT 5
|
||||
`).all();
|
||||
|
||||
if (samples.length === 0) {
|
||||
console.log(' ❌ 数据库中没有记录');
|
||||
} else {
|
||||
samples.forEach((img, index) => {
|
||||
console.log(`\n ${index + 1}. ${img.filename}`);
|
||||
console.log(` ID: ${img.id}`);
|
||||
console.log(` 原图路径: ${img.path}`);
|
||||
console.log(` file_type: ${img.file_type || 'NULL'}`);
|
||||
console.log(` thumbnail_path: ${img.thumbnail_path || 'NULL'}`);
|
||||
console.log(` thumbnail_size: ${img.thumbnail_size || 'NULL'}`);
|
||||
|
||||
// 检查缩略图路径格式
|
||||
if (img.thumbnail_path) {
|
||||
const parts = img.thumbnail_path.split('/');
|
||||
const isCorrectFormat = parts.length === 4 &&
|
||||
parts[0] === '.flypic' &&
|
||||
parts[1] === 'thumbnails' &&
|
||||
parts[2].length === 2;
|
||||
|
||||
console.log(` 路径格式: ${isCorrectFormat ? '✅ 正确' : '❌ 错误'} (${parts.join(' / ')})`);
|
||||
|
||||
// 检查文件是否存在
|
||||
const thumbFullPath = path.join(libraryPath, img.thumbnail_path);
|
||||
const exists = fs.existsSync(thumbFullPath);
|
||||
console.log(` 文件存在: ${exists ? '✅' : '❌'} (${thumbFullPath})`);
|
||||
|
||||
if (exists) {
|
||||
const fileStats = fs.statSync(thumbFullPath);
|
||||
console.log(` 文件大小: ${(fileStats.size / 1024).toFixed(2)} KB`);
|
||||
}
|
||||
} else {
|
||||
console.log(` ❌ 缺少 thumbnail_path`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 模拟前端请求
|
||||
console.log('\n4️⃣ 模拟前端请求流程:');
|
||||
console.log('-'.repeat(80));
|
||||
|
||||
if (samples.length > 0 && samples[0].thumbnail_path) {
|
||||
const testImage = samples[0];
|
||||
console.log(` 测试图片: ${testImage.filename}`);
|
||||
console.log(` 数据库中的 thumbnail_path: ${testImage.thumbnail_path}`);
|
||||
|
||||
// 前端提取文件名
|
||||
const filename = testImage.thumbnail_path.replace(/\\/g, '/').split('/').pop();
|
||||
console.log(` 前端提取文件名: ${filename}`);
|
||||
|
||||
// 前端构建URL
|
||||
const apiUrl = `/api/image/thumbnail/{libraryId}/480/${filename}`;
|
||||
console.log(` 前端构建URL: ${apiUrl}`);
|
||||
|
||||
// 后端重建路径
|
||||
const hash = filename.replace(/\.[^/.]+$/, "");
|
||||
const shard1 = hash.slice(0, 2);
|
||||
const reconstructedPath = path.join(libraryPath, '.flypic', 'thumbnails', shard1, filename);
|
||||
console.log(` 后端提取 hash: ${hash}`);
|
||||
console.log(` 后端提取分片: ${shard1}`);
|
||||
console.log(` 后端重建路径: ${reconstructedPath}`);
|
||||
|
||||
// 验证路径
|
||||
const originalPath = path.join(libraryPath, testImage.thumbnail_path);
|
||||
const pathsMatch = path.normalize(originalPath) === path.normalize(reconstructedPath);
|
||||
console.log(` 路径匹配: ${pathsMatch ? '✅' : '❌'}`);
|
||||
|
||||
if (!pathsMatch) {
|
||||
console.log(` 原始路径: ${originalPath}`);
|
||||
console.log(` 重建路径: ${reconstructedPath}`);
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
const fileExists = fs.existsSync(reconstructedPath);
|
||||
console.log(` 文件存在: ${fileExists ? '✅' : '❌'}`);
|
||||
} else {
|
||||
console.log(' ⚠️ 没有可用的测试数据');
|
||||
}
|
||||
|
||||
// 5. 诊断结论
|
||||
console.log('\n5️⃣ 诊断结论:');
|
||||
console.log('='.repeat(80));
|
||||
|
||||
const issues = [];
|
||||
|
||||
if (!fs.existsSync(thumbDir)) {
|
||||
issues.push('❌ thumbnails 目录不存在');
|
||||
}
|
||||
|
||||
if (fs.existsSync(thumb480Dir)) {
|
||||
issues.push('⚠️ 存在旧的 480 目录(应该删除)');
|
||||
}
|
||||
|
||||
if (stats.missing_thumbnail_path > 0) {
|
||||
issues.push(`❌ 有 ${stats.missing_thumbnail_path} 条记录缺少 thumbnail_path`);
|
||||
}
|
||||
|
||||
// 检查路径格式
|
||||
const wrongFormatCount = db.db.prepare(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL
|
||||
AND thumbnail_path NOT LIKE '.flypic/thumbnails/__/%'
|
||||
`).get();
|
||||
|
||||
if (wrongFormatCount.count > 0) {
|
||||
issues.push(`❌ 有 ${wrongFormatCount.count} 条记录的路径格式不正确`);
|
||||
}
|
||||
|
||||
// 检查文件是否存在
|
||||
let missingFiles = 0;
|
||||
const allImages = db.db.prepare('SELECT thumbnail_path FROM images WHERE thumbnail_path IS NOT NULL LIMIT 100').all();
|
||||
for (const img of allImages) {
|
||||
const fullPath = path.join(libraryPath, img.thumbnail_path);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
missingFiles++;
|
||||
}
|
||||
}
|
||||
|
||||
if (missingFiles > 0) {
|
||||
issues.push(`❌ 有 ${missingFiles} 个缩略图文件不存在(检查了前100条)`);
|
||||
}
|
||||
|
||||
if (issues.length === 0) {
|
||||
console.log('✅ 所有检查通过!');
|
||||
console.log('\n如果前端仍然显示灰色背景,请检查:');
|
||||
console.log('1. 后端服务是否正常运行');
|
||||
console.log('2. 浏览器控制台是否有错误');
|
||||
console.log('3. Network 面板中缩略图请求的状态码');
|
||||
} else {
|
||||
console.log('发现以下问题:');
|
||||
issues.forEach((issue, index) => {
|
||||
console.log(`${index + 1}. ${issue}`);
|
||||
});
|
||||
|
||||
console.log('\n建议修复方案:');
|
||||
if (stats.missing_thumbnail_path > 0 || missingFiles > 0) {
|
||||
console.log('1. 重新扫描素材库以生成缺失的缩略图');
|
||||
console.log(' - 在前端点击"同步"按钮');
|
||||
console.log(' - 或者使用 API: POST /api/scan/full');
|
||||
}
|
||||
if (fs.existsSync(thumb480Dir)) {
|
||||
console.log('2. 删除旧的 480 目录(可选)');
|
||||
console.log(` - 路径: ${thumb480Dir}`);
|
||||
}
|
||||
if (wrongFormatCount.count > 0) {
|
||||
console.log('3. 数据库中有旧格式的路径,需要重新扫描');
|
||||
}
|
||||
}
|
||||
|
||||
db.close();
|
||||
console.log('\n✅ 诊断完成');
|
||||
@@ -1,77 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 快速检查数据库和缩略图状态
|
||||
*/
|
||||
|
||||
const dbPool = require('./database/dbPool');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const libraryPath = 'C:\\Users\\qingy\\Pictures\\商品图片包';
|
||||
|
||||
console.log('🔍 检查数据库和缩略图状态\n');
|
||||
|
||||
// 1. 检查数据库
|
||||
console.log('1️⃣ 检查数据库...');
|
||||
const db = dbPool.acquire(libraryPath);
|
||||
|
||||
const total = db.db.prepare('SELECT COUNT(*) as count FROM images').get();
|
||||
console.log(` 总图片数: ${total.count}`);
|
||||
|
||||
const withThumb = db.db.prepare('SELECT COUNT(*) as count FROM images WHERE thumbnail_path IS NOT NULL AND thumbnail_path != ""').get();
|
||||
console.log(` 有缩略图路径: ${withThumb.count}`);
|
||||
|
||||
const withoutThumb = total.count - withThumb.count;
|
||||
console.log(` 缺少缩略图路径: ${withoutThumb}`);
|
||||
|
||||
if (withoutThumb > 0) {
|
||||
console.log('\n ❌ 问题:有图片缺少 thumbnail_path');
|
||||
console.log(' 解决方案:重新运行扫描');
|
||||
} else {
|
||||
console.log('\n ✅ 所有图片都有 thumbnail_path');
|
||||
}
|
||||
|
||||
// 2. 检查示例数据
|
||||
console.log('\n2️⃣ 示例数据(前3条):');
|
||||
const samples = db.db.prepare('SELECT id, filename, thumbnail_path FROM images LIMIT 3').all();
|
||||
samples.forEach((row, i) => {
|
||||
console.log(` ${i + 1}. ${row.filename}`);
|
||||
console.log(` thumbnail_path: ${row.thumbnail_path || '(空)'}`);
|
||||
});
|
||||
|
||||
// 3. 检查缩略图文件
|
||||
console.log('\n3️⃣ 检查缩略图文件...');
|
||||
const thumbDir = path.join(libraryPath, '.flypic', 'thumbnails');
|
||||
if (fs.existsSync(thumbDir)) {
|
||||
const files = fs.readdirSync(thumbDir, { recursive: true }).filter(f => f.endsWith('.webp'));
|
||||
console.log(` 缩略图文件数: ${files.length}`);
|
||||
|
||||
if (files.length === 0) {
|
||||
console.log(' ❌ 问题:缩略图目录为空');
|
||||
console.log(' 解决方案:重新运行扫描');
|
||||
} else if (files.length < withThumb.count) {
|
||||
console.log(` ⚠️ 警告:缩略图文件数 (${files.length}) < 数据库记录 (${withThumb.count})`);
|
||||
} else {
|
||||
console.log(' ✅ 缩略图文件存在');
|
||||
}
|
||||
} else {
|
||||
console.log(' ❌ 问题:缩略图目录不存在');
|
||||
console.log(' 解决方案:重新运行扫描');
|
||||
}
|
||||
|
||||
// 4. 检查一个具体的缩略图
|
||||
if (samples.length > 0 && samples[0].thumbnail_path) {
|
||||
console.log('\n4️⃣ 检查第一个缩略图文件...');
|
||||
const thumbPath = path.join(libraryPath, samples[0].thumbnail_path);
|
||||
if (fs.existsSync(thumbPath)) {
|
||||
const stats = fs.statSync(thumbPath);
|
||||
console.log(` ✅ 文件存在: ${samples[0].thumbnail_path}`);
|
||||
console.log(` 文件大小: ${(stats.size / 1024).toFixed(2)} KB`);
|
||||
} else {
|
||||
console.log(` ❌ 文件不存在: ${samples[0].thumbnail_path}`);
|
||||
}
|
||||
}
|
||||
|
||||
dbPool.release(libraryPath);
|
||||
|
||||
console.log('\n✅ 检查完成!');
|
||||
@@ -1,156 +0,0 @@
|
||||
/**
|
||||
* 快速诊断 - 检查为什么前端显示灰色背景
|
||||
*
|
||||
* 使用方法:
|
||||
* node backend/quick-diagnosis.js "素材库路径"
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const Database = require('better-sqlite3');
|
||||
|
||||
if (process.argv.length < 3) {
|
||||
console.log('用法: node backend/quick-diagnosis.js "素材库路径"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const libraryPath = process.argv[2];
|
||||
const dbPath = path.join(libraryPath, '.flypic', 'metadata.db');
|
||||
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
console.log('❌ 数据库不存在,请先扫描素材库');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('🔍 快速诊断:为什么显示灰色背景\n');
|
||||
|
||||
const db = new Database(dbPath);
|
||||
|
||||
// 1. 检查总数
|
||||
const total = db.prepare('SELECT COUNT(*) as count FROM images').get();
|
||||
console.log(`1️⃣ 数据库中的图片总数: ${total.count}`);
|
||||
|
||||
if (total.count === 0) {
|
||||
console.log(' ❌ 数据库是空的,请扫描素材库');
|
||||
db.close();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 2. 检查 thumbnail_path
|
||||
const withPath = db.prepare(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL AND thumbnail_path != ''
|
||||
`).get();
|
||||
|
||||
console.log(`\n2️⃣ 有 thumbnail_path 的记录: ${withPath.count} / ${total.count}`);
|
||||
|
||||
if (withPath.count === 0) {
|
||||
console.log(' ❌ 所有记录都缺少 thumbnail_path');
|
||||
console.log(' 原因:扫描时没有生成缩略图');
|
||||
console.log(' 解决:重新扫描素材库');
|
||||
db.close();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (withPath.count < total.count) {
|
||||
console.log(` ⚠️ 有 ${total.count - withPath.count} 条记录缺少 thumbnail_path`);
|
||||
}
|
||||
|
||||
// 3. 检查路径格式
|
||||
const correctFormat = db.prepare(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM images
|
||||
WHERE thumbnail_path LIKE '.flypic/thumbnails/__/%'
|
||||
`).get();
|
||||
|
||||
console.log(`\n3️⃣ 路径格式正确的记录: ${correctFormat.count} / ${withPath.count}`);
|
||||
|
||||
if (correctFormat.count === 0) {
|
||||
console.log(' ❌ 所有路径格式都不正确');
|
||||
console.log(' 原因:使用了旧的路径格式');
|
||||
console.log(' 解决:重新扫描素材库');
|
||||
db.close();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 4. 检查文件是否存在
|
||||
console.log(`\n4️⃣ 检查缩略图文件是否存在(检查前10个):`);
|
||||
|
||||
const samples = db.prepare(`
|
||||
SELECT thumbnail_path, filename
|
||||
FROM images
|
||||
WHERE thumbnail_path IS NOT NULL
|
||||
LIMIT 10
|
||||
`).all();
|
||||
|
||||
let existCount = 0;
|
||||
let missingCount = 0;
|
||||
|
||||
samples.forEach((img, index) => {
|
||||
const fullPath = path.join(libraryPath, img.thumbnail_path);
|
||||
const exists = fs.existsSync(fullPath);
|
||||
|
||||
if (exists) {
|
||||
existCount++;
|
||||
} else {
|
||||
missingCount++;
|
||||
if (missingCount <= 3) {
|
||||
console.log(` ❌ ${img.filename}`);
|
||||
console.log(` 路径: ${img.thumbnail_path}`);
|
||||
console.log(` 完整路径: ${fullPath}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
console.log(` 存在: ${existCount}, 缺失: ${missingCount}`);
|
||||
|
||||
if (missingCount === samples.length) {
|
||||
console.log('\n ❌ 所有缩略图文件都不存在');
|
||||
console.log(' 原因:缩略图文件被删除或路径不对');
|
||||
console.log(' 解决:重新扫描素材库');
|
||||
db.close();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 5. 模拟前端请求
|
||||
console.log(`\n5️⃣ 模拟前端请求:`);
|
||||
|
||||
if (samples.length > 0) {
|
||||
const testImg = samples[0];
|
||||
console.log(` 测试图片: ${testImg.filename}`);
|
||||
console.log(` thumbnail_path: ${testImg.thumbnail_path}`);
|
||||
|
||||
// 前端提取文件名
|
||||
const filename = testImg.thumbnail_path.replace(/\\/g, '/').split('/').pop();
|
||||
console.log(` 提取文件名: ${filename}`);
|
||||
|
||||
// 后端重建路径
|
||||
const hash = filename.replace(/\.[^/.]+$/, "");
|
||||
const shard1 = hash.slice(0, 2);
|
||||
const targetPath = path.join(libraryPath, '.flypic', 'thumbnails', shard1, filename);
|
||||
|
||||
console.log(` 后端重建路径: ${targetPath}`);
|
||||
console.log(` 文件存在: ${fs.existsSync(targetPath) ? '✅' : '❌'}`);
|
||||
}
|
||||
|
||||
// 6. 总结
|
||||
console.log(`\n6️⃣ 诊断结果:`);
|
||||
console.log('='.repeat(60));
|
||||
|
||||
if (withPath.count === total.count && correctFormat.count === withPath.count && missingCount === 0) {
|
||||
console.log('✅ 数据库和文件都正常');
|
||||
console.log('\n如果前端仍显示灰色背景,请检查:');
|
||||
console.log('1. 后端服务是否运行在正确的端口');
|
||||
console.log('2. 浏览器控制台 Network 面板中的缩略图请求');
|
||||
console.log('3. 缩略图请求的响应状态码(应该是 200)');
|
||||
console.log('4. 前端的 currentLibraryId 是否正确');
|
||||
} else {
|
||||
console.log('❌ 发现问题,需要重新扫描素材库');
|
||||
console.log('\n解决方法:');
|
||||
console.log('1. 在前端点击"同步"或"扫描"按钮');
|
||||
console.log('2. 等待扫描完成');
|
||||
console.log('3. 刷新页面');
|
||||
}
|
||||
|
||||
db.close();
|
||||
@@ -1,75 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 测试文件监控器配置
|
||||
* 验证 chokidar 使用了内存优化配置
|
||||
*/
|
||||
|
||||
const { Worker } = require('worker_threads');
|
||||
const path = require('path');
|
||||
|
||||
console.log('🧪 测试文件监控器内存优化配置\n');
|
||||
|
||||
// 创建一个测试 Worker
|
||||
const workerPath = path.join(__dirname, 'utils', 'fileWatcherWorker.js');
|
||||
|
||||
console.log('📝 预期配置:');
|
||||
console.log(' ✅ ignoreInitial: true (跳过初始扫描)');
|
||||
console.log(' ✅ usePolling: false (禁用轮询)');
|
||||
console.log(' ✅ awaitWriteFinish: false (禁用写入检测)');
|
||||
console.log(' ✅ disableStatCache: true (禁用统计缓存)');
|
||||
console.log(' ✅ alwaysStat: false (不自动获取统计)');
|
||||
|
||||
console.log('\n🔍 启动 Worker 测试...');
|
||||
|
||||
const testPath = process.cwd(); // 使用当前目录测试
|
||||
|
||||
const worker = new Worker(workerPath, {
|
||||
workerData: {
|
||||
libraryPath: testPath,
|
||||
libraryName: 'test-library'
|
||||
}
|
||||
});
|
||||
|
||||
let readyReceived = false;
|
||||
|
||||
worker.on('message', (msg) => {
|
||||
if (msg.type === 'ready') {
|
||||
readyReceived = true;
|
||||
console.log('\n✅ Worker 已就绪');
|
||||
console.log('✅ 配置验证通过!');
|
||||
console.log('\n💡 如果启动时没有扫描大量文件,说明 ignoreInitial 生效');
|
||||
console.log('💡 内存占用应该很低(< 50MB)');
|
||||
|
||||
// 关闭 Worker
|
||||
worker.postMessage({ type: 'close' });
|
||||
|
||||
setTimeout(() => {
|
||||
console.log('\n🎉 测试完成!');
|
||||
process.exit(0);
|
||||
}, 1000);
|
||||
} else if (msg.type === 'error') {
|
||||
console.error('\n❌ Worker 错误:', msg.message);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
worker.on('error', (error) => {
|
||||
console.error('\n❌ Worker 启动失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0 && !readyReceived) {
|
||||
console.error(`\n❌ Worker 异常退出,代码: ${code}`);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
// 超时保护
|
||||
setTimeout(() => {
|
||||
if (!readyReceived) {
|
||||
console.error('\n❌ 超时:Worker 未在 10 秒内就绪');
|
||||
worker.terminate();
|
||||
process.exit(1);
|
||||
}
|
||||
}, 10000);
|
||||
@@ -1,117 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 测试轻量级文件监控器
|
||||
* 验证内存占用和性能
|
||||
*/
|
||||
|
||||
const lightweightWatcher = require('./utils/lightweightWatcher');
|
||||
|
||||
function formatMemory(bytes) {
|
||||
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
||||
}
|
||||
|
||||
function getMemoryUsage() {
|
||||
const usage = process.memoryUsage();
|
||||
return {
|
||||
rss: usage.rss,
|
||||
heapUsed: usage.heapUsed
|
||||
};
|
||||
}
|
||||
|
||||
console.log('🧪 轻量级文件监控器测试\n');
|
||||
|
||||
// 1. 基准内存
|
||||
const baselineMemory = getMemoryUsage();
|
||||
console.log('📊 基准内存:');
|
||||
console.log(` RSS: ${formatMemory(baselineMemory.rss)}`);
|
||||
console.log(` Heap: ${formatMemory(baselineMemory.heapUsed)}`);
|
||||
|
||||
// 2. 启动监控
|
||||
console.log('\n🚀 启动轻量级监控...');
|
||||
|
||||
const testPath = 'C:\\Users\\qingy\\Pictures\\商品图片包';
|
||||
const startTime = Date.now();
|
||||
|
||||
// 模拟 Socket.IO
|
||||
const mockIo = {
|
||||
emit: (event, data) => {
|
||||
console.log(` [Socket] ${event}:`, data);
|
||||
}
|
||||
};
|
||||
|
||||
lightweightWatcher.watch('test-lib', testPath, '大批量测试', mockIo)
|
||||
.then(() => {
|
||||
const readyTime = Date.now() - startTime;
|
||||
const afterMemory = getMemoryUsage();
|
||||
|
||||
console.log(`\n✅ 监控已启动(耗时 ${readyTime}ms)`);
|
||||
console.log('\n📊 启动后内存:');
|
||||
console.log(` RSS: ${formatMemory(afterMemory.rss)}`);
|
||||
console.log(` Heap: ${formatMemory(afterMemory.heapUsed)}`);
|
||||
|
||||
console.log('\n📈 内存增长:');
|
||||
const rssDiff = afterMemory.rss - baselineMemory.rss;
|
||||
const heapDiff = afterMemory.heapUsed - baselineMemory.heapUsed;
|
||||
console.log(` RSS: +${formatMemory(rssDiff)}`);
|
||||
console.log(` Heap: +${formatMemory(heapDiff)}`);
|
||||
|
||||
console.log('\n💡 分析:');
|
||||
if (rssDiff < 50 * 1024 * 1024) {
|
||||
console.log(' ✅ 内存增长 < 50MB - 优化成功!');
|
||||
} else if (rssDiff < 100 * 1024 * 1024) {
|
||||
console.log(' ⚠️ 内存增长 50-100MB - 可接受');
|
||||
} else {
|
||||
console.log(' ❌ 内存增长 > 100MB - 需要进一步优化');
|
||||
}
|
||||
|
||||
// 等待 30 秒,观察轮询过程
|
||||
console.log('\n⏳ 等待 30 秒,观察轮询过程...');
|
||||
console.log('💡 提示:可以在测试期间添加/删除文件来测试检测功能');
|
||||
|
||||
let checkCount = 0;
|
||||
const checkInterval = setInterval(() => {
|
||||
checkCount++;
|
||||
const currentMemory = getMemoryUsage();
|
||||
console.log(` [${checkCount * 5}s] RSS: ${formatMemory(currentMemory.rss)}, Heap: ${formatMemory(currentMemory.heapUsed)}`);
|
||||
|
||||
if (checkCount >= 6) {
|
||||
clearInterval(checkInterval);
|
||||
|
||||
const finalMemory = getMemoryUsage();
|
||||
console.log('\n📊 最终内存:');
|
||||
console.log(` RSS: ${formatMemory(finalMemory.rss)}`);
|
||||
console.log(` Heap: ${formatMemory(finalMemory.heapUsed)}`);
|
||||
|
||||
const stabilityDiff = finalMemory.rss - afterMemory.rss;
|
||||
console.log(`\n📈 稳定性: ${stabilityDiff > 0 ? '+' : ''}${formatMemory(stabilityDiff)}`);
|
||||
|
||||
if (Math.abs(stabilityDiff) < 20 * 1024 * 1024) {
|
||||
console.log(' ✅ 内存稳定(变化 < 20MB)');
|
||||
} else {
|
||||
console.log(' ⚠️ 内存不稳定(变化 > 20MB)');
|
||||
}
|
||||
|
||||
// 停止监控
|
||||
lightweightWatcher.unwatch('test-lib');
|
||||
|
||||
console.log('\n🎉 测试完成!');
|
||||
console.log('\n📊 对比 chokidar:');
|
||||
console.log(' chokidar: ~800MB, 24秒启动');
|
||||
console.log(` 轻量级: ~${formatMemory(rssDiff)}, ${readyTime}ms启动`);
|
||||
console.log(` 节省: ~${formatMemory(800 * 1024 * 1024 - rssDiff)}`);
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
}, 5000);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('\n❌ 启动失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// 超时保护
|
||||
setTimeout(() => {
|
||||
console.error('\n❌ 超时:测试未在 60 秒内完成');
|
||||
lightweightWatcher.unwatch('test-lib');
|
||||
process.exit(1);
|
||||
}, 60000);
|
||||
@@ -1,109 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 测试文件监控器内存占用
|
||||
* 对比优化前后的内存使用
|
||||
*/
|
||||
|
||||
const { Worker } = require('worker_threads');
|
||||
const path = require('path');
|
||||
|
||||
function formatMemory(bytes) {
|
||||
return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
|
||||
}
|
||||
|
||||
function getMemoryUsage() {
|
||||
const usage = process.memoryUsage();
|
||||
return {
|
||||
rss: usage.rss,
|
||||
heapUsed: usage.heapUsed,
|
||||
heapTotal: usage.heapTotal,
|
||||
external: usage.external
|
||||
};
|
||||
}
|
||||
|
||||
console.log('🧪 文件监控器内存测试\n');
|
||||
|
||||
// 1. 基准内存
|
||||
const baselineMemory = getMemoryUsage();
|
||||
console.log('📊 基准内存(启动前):');
|
||||
console.log(` RSS: ${formatMemory(baselineMemory.rss)}`);
|
||||
console.log(` Heap Used: ${formatMemory(baselineMemory.heapUsed)}`);
|
||||
|
||||
// 2. 启动 Worker
|
||||
console.log('\n🚀 启动文件监控器...');
|
||||
|
||||
const workerPath = path.join(__dirname, 'utils', 'fileWatcherWorker.js');
|
||||
const testPath = 'C:\\Users\\qingy\\Pictures\\商品图片包'; // 使用你的大批量文件夹
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
const worker = new Worker(workerPath, {
|
||||
workerData: {
|
||||
libraryPath: testPath,
|
||||
libraryName: '大批量测试'
|
||||
}
|
||||
});
|
||||
|
||||
worker.on('message', (msg) => {
|
||||
if (msg.type === 'ready') {
|
||||
const readyTime = Date.now() - startTime;
|
||||
const afterMemory = getMemoryUsage();
|
||||
|
||||
console.log(`\n✅ Worker 就绪(耗时 ${readyTime}ms)`);
|
||||
console.log('\n📊 启动后内存:');
|
||||
console.log(` RSS: ${formatMemory(afterMemory.rss)}`);
|
||||
console.log(` Heap Used: ${formatMemory(afterMemory.heapUsed)}`);
|
||||
|
||||
console.log('\n📈 内存增长:');
|
||||
const rssDiff = afterMemory.rss - baselineMemory.rss;
|
||||
const heapDiff = afterMemory.heapUsed - baselineMemory.heapUsed;
|
||||
console.log(` RSS: +${formatMemory(rssDiff)}`);
|
||||
console.log(` Heap Used: +${formatMemory(heapDiff)}`);
|
||||
|
||||
console.log('\n💡 分析:');
|
||||
if (rssDiff < 50 * 1024 * 1024) {
|
||||
console.log(' ✅ 内存增长 < 50MB - 优化成功!');
|
||||
} else if (rssDiff < 100 * 1024 * 1024) {
|
||||
console.log(' ⚠️ 内存增长 50-100MB - 可接受');
|
||||
} else {
|
||||
console.log(' ❌ 内存增长 > 100MB - 需要进一步优化');
|
||||
}
|
||||
|
||||
// 等待 5 秒,观察内存是否继续增长
|
||||
console.log('\n⏳ 等待 5 秒,观察内存稳定性...');
|
||||
setTimeout(() => {
|
||||
const finalMemory = getMemoryUsage();
|
||||
console.log('\n📊 5秒后内存:');
|
||||
console.log(` RSS: ${formatMemory(finalMemory.rss)}`);
|
||||
console.log(` Heap Used: ${formatMemory(finalMemory.heapUsed)}`);
|
||||
|
||||
const stabilityDiff = finalMemory.rss - afterMemory.rss;
|
||||
console.log(`\n📈 稳定性检查: ${stabilityDiff > 0 ? '+' : ''}${formatMemory(stabilityDiff)}`);
|
||||
|
||||
if (Math.abs(stabilityDiff) < 10 * 1024 * 1024) {
|
||||
console.log(' ✅ 内存稳定(变化 < 10MB)');
|
||||
} else {
|
||||
console.log(' ⚠️ 内存不稳定(变化 > 10MB)');
|
||||
}
|
||||
|
||||
// 关闭
|
||||
worker.postMessage({ type: 'close' });
|
||||
setTimeout(() => {
|
||||
console.log('\n🎉 测试完成!');
|
||||
process.exit(0);
|
||||
}, 1000);
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
|
||||
worker.on('error', (error) => {
|
||||
console.error('\n❌ Worker 错误:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// 超时保护
|
||||
setTimeout(() => {
|
||||
console.error('\n❌ 超时:测试未在 30 秒内完成');
|
||||
worker.terminate();
|
||||
process.exit(1);
|
||||
}, 30000);
|
||||
@@ -1,116 +0,0 @@
|
||||
/**
|
||||
* 验证缩略图路径的一致性
|
||||
*
|
||||
* 检查点:
|
||||
* 1. 数据库初始化时不创建 480 文件夹
|
||||
* 2. 生成缩略图时使用正确的路径格式:.flypic/thumbnails/XX/hash.webp
|
||||
* 3. 保存到数据库的路径格式正确
|
||||
* 4. 前端读取路径格式正确
|
||||
* 5. 后端API服务路径格式正确
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const crypto = require('crypto');
|
||||
|
||||
console.log('🔍 验证缩略图路径一致性\n');
|
||||
console.log('=' .repeat(60));
|
||||
|
||||
// 1. 模拟数据库初始化
|
||||
console.log('\n1️⃣ 检查数据库初始化逻辑:');
|
||||
const testLibraryPath = 'C:\\Test\\Library';
|
||||
const flypicDir = path.join(testLibraryPath, '.flypic');
|
||||
const thumbDir = path.join(flypicDir, 'thumbnails');
|
||||
|
||||
console.log(` .flypic 目录: ${flypicDir}`);
|
||||
console.log(` thumbnails 目录: ${thumbDir}`);
|
||||
console.log(` ✅ 不应该创建 480 子目录`);
|
||||
|
||||
// 2. 模拟缩略图生成
|
||||
console.log('\n2️⃣ 检查缩略图生成逻辑:');
|
||||
const testImagePath = path.join(testLibraryPath, 'folder1', 'test.jpg');
|
||||
const relativePath = path.relative(testLibraryPath, testImagePath);
|
||||
const hash = crypto.createHash('md5').update(relativePath).digest('hex');
|
||||
const shard1 = hash.slice(0, 2);
|
||||
const thumbnailFilename = `${hash}.webp`;
|
||||
const thumbnailPath = path.join(flypicDir, 'thumbnails', shard1, thumbnailFilename);
|
||||
const thumbnailPathRelative = path.relative(testLibraryPath, thumbnailPath).replace(/\\/g, '/');
|
||||
|
||||
console.log(` 原图路径: ${testImagePath}`);
|
||||
console.log(` 相对路径: ${relativePath}`);
|
||||
console.log(` MD5 Hash: ${hash}`);
|
||||
console.log(` 分片目录: ${shard1}`);
|
||||
console.log(` 缩略图文件名: ${thumbnailFilename}`);
|
||||
console.log(` 缩略图完整路径: ${thumbnailPath}`);
|
||||
console.log(` 缩略图相对路径: ${thumbnailPathRelative}`);
|
||||
console.log(` ✅ 格式应该是: .flypic/thumbnails/${shard1}/${thumbnailFilename}`);
|
||||
|
||||
// 验证格式
|
||||
const parts = thumbnailPathRelative.split('/');
|
||||
if (parts.length === 4 && parts[0] === '.flypic' && parts[1] === 'thumbnails' && parts[2].length === 2) {
|
||||
console.log(` ✅ 路径格式正确`);
|
||||
} else {
|
||||
console.log(` ❌ 路径格式错误: ${parts.join(' / ')}`);
|
||||
}
|
||||
|
||||
// 3. 模拟数据库保存
|
||||
console.log('\n3️⃣ 检查数据库保存逻辑:');
|
||||
const imageData = {
|
||||
path: relativePath.replace(/\\/g, '/'),
|
||||
filename: 'test.jpg',
|
||||
folder: 'folder1',
|
||||
thumbnail_path: thumbnailPathRelative,
|
||||
thumbnail_size: 12345,
|
||||
file_type: 'image'
|
||||
};
|
||||
|
||||
console.log(` 保存到数据库的数据:`);
|
||||
console.log(` - path: ${imageData.path}`);
|
||||
console.log(` - thumbnail_path: ${imageData.thumbnail_path}`);
|
||||
console.log(` - file_type: ${imageData.file_type}`);
|
||||
console.log(` ✅ thumbnail_path 应该是相对路径,使用正斜杠`);
|
||||
|
||||
// 4. 模拟前端读取
|
||||
console.log('\n4️⃣ 检查前端读取逻辑:');
|
||||
const filenameFromDB = imageData.thumbnail_path.replace(/\\/g, '/').split('/').pop();
|
||||
console.log(` 从数据库读取: ${imageData.thumbnail_path}`);
|
||||
console.log(` 提取文件名: ${filenameFromDB}`);
|
||||
console.log(` 构建URL: /api/image/thumbnail/{libraryId}/480/${filenameFromDB}`);
|
||||
console.log(` ✅ 前端只需要文件名,不需要完整路径`);
|
||||
|
||||
// 5. 模拟后端API服务
|
||||
console.log('\n5️⃣ 检查后端API服务逻辑:');
|
||||
const filenameFromURL = filenameFromDB;
|
||||
const hashFromFilename = filenameFromURL.replace(/\.[^/.]+$/, ""); // 去掉扩展名
|
||||
const shardFromHash = hashFromFilename.slice(0, 2);
|
||||
const reconstructedPath = path.join(testLibraryPath, '.flypic', 'thumbnails', shardFromHash, filenameFromURL);
|
||||
|
||||
console.log(` 接收到的文件名: ${filenameFromURL}`);
|
||||
console.log(` 提取 hash: ${hashFromFilename}`);
|
||||
console.log(` 提取分片: ${shardFromHash}`);
|
||||
console.log(` 重建完整路径: ${reconstructedPath}`);
|
||||
console.log(` ✅ 后端能正确重建路径`);
|
||||
|
||||
// 6. 验证一致性
|
||||
console.log('\n6️⃣ 验证路径一致性:');
|
||||
const originalPath = thumbnailPath;
|
||||
const reconstructedPathNormalized = path.normalize(reconstructedPath);
|
||||
|
||||
console.log(` 原始路径: ${originalPath}`);
|
||||
console.log(` 重建路径: ${reconstructedPathNormalized}`);
|
||||
|
||||
if (originalPath === reconstructedPathNormalized) {
|
||||
console.log(` ✅ 路径完全一致!`);
|
||||
} else {
|
||||
console.log(` ❌ 路径不一致!`);
|
||||
}
|
||||
|
||||
// 7. 总结
|
||||
console.log('\n7️⃣ 总结:');
|
||||
console.log('=' .repeat(60));
|
||||
console.log('✅ 缩略图路径格式: .flypic/thumbnails/XX/hash.webp');
|
||||
console.log('✅ 数据库存储: 相对路径,使用正斜杠');
|
||||
console.log('✅ 前端提取: 只需要文件名');
|
||||
console.log('✅ 后端重建: 从文件名提取hash和分片');
|
||||
console.log('✅ 不使用 480 文件夹');
|
||||
console.log('\n整个流程一致性验证通过!');
|
||||
Reference in new issue
Block a user