Commit d85b45dc by 曹玉玺

添加测试脚本

parent 976c0c21
const system = require("../../system");
const path = require("path");
const fs = require("fs");
const gm = require("gm");
const imageMagick = gm.subClass({ imageMagick: true });
const { execSync } = require('child_process');
// 检查 Docker 环境
async function checkDockerEnv() {
console.log("=== Docker 环境检查 ===\n");
// 检查是否在 Docker 容器中
try {
const cgroup = fs.readFileSync('/proc/1/cgroup', 'utf8');
if (cgroup.includes('docker') || cgroup.includes('kubepods')) {
console.log("✓ 当前运行在 Docker/K8s 容器中\n");
} else {
console.log("✗ 当前不在 Docker 容器中运行\n");
}
} catch (e) {
console.log("? 无法判断是否在 Docker 容器中(可能是本地环境)\n");
}
// 检查 ImageMagick
console.log("1. 检查 ImageMagick:");
try {
const version = execSync('magick -version', { encoding: 'utf8' });
console.log(" ✓ ImageMagick 已安装");
console.log(" 版本:", version.split('\n')[0]);
// 列出支持的字体
console.log("\n2. 检查中文字体支持:");
try {
const fonts = execSync('magick list font | grep -i "han\\|song\\|hei\\|chinese"', {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore']
});
if (fonts.trim()) {
console.log(" 找到以下中文字体:");
fonts.split('\n').forEach(f => {
if (f.trim()) console.log(" -", f.trim());
});
} else {
console.log(" ✗ 未找到中文字体");
}
} catch (e) {
console.log(" ? 无法列出字体,可能需要手动检查");
}
} catch (e1) {
// 尝试 GraphicsMagick
try {
const version = execSync('gm -version', { encoding: 'utf8' });
console.log(" ✓ GraphicsMagick 已安装");
console.log(" 版本:", version.split('\n')[0]);
} catch (e2) {
console.log(" ✗ ImageMagick 和 GraphicsMagick 都未安装");
}
}
console.log("\n=== 检查完成 ===\n");
}
checkDockerEnv().catch(console.error);
const system = require("../../system");
const path = require("path");
const fs = require("fs");
const gm = require("gm");
const imageMagick = gm.subClass({ imageMagick: true });
// Docker 环境适配测试脚本
async function testFontFiles() {
console.log("=== Docker 环境字体测试 ===\n");
// Docker 环境路径适配
const fontDir = path.join(__dirname, "fonts");
// Docker 中使用 /tmp 目录
const tmpDir = process.platform === 'linux' ? '/tmp' : path.join(__dirname, "../../../tmp");
// 确保 tmp 目录存在
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}
const fonts = [
{ name: "SourceHanSerif-Regular.ttc", path: path.join(fontDir, "SourceHanSerif-Regular.ttc") },
{ name: "heiti.otf", path: path.join(fontDir, "heiti.otf") },
{ name: "songti.TTC", path: path.join(fontDir, "songti.TTC") }
];
// 检查字体文件是否存在
console.log("1. 检查字体文件:");
fonts.forEach(font => {
const exists = fs.existsSync(font.path);
console.log(` - ${font.name}: ${exists ? '✓ 存在' : '✗ 不存在'}`);
if (exists) {
const stats = fs.statSync(font.path);
console.log(` 大小:${(stats.size / 1024).toFixed(2)} KB`);
// Docker 中检查文件权限
if (process.platform === 'linux') {
console.log(` 权限:${(stats.mode & 0o777).toString(8)}`);
}
}
});
console.log("\n2. 检查 ImageMagick/ GraphicsMagick 环境:\n");
try {
const { execSync } = require('child_process');
// 先检查 ImageMagick
try {
const version = execSync('magick -version', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] });
console.log(" ✓ ImageMagick 已安装");
console.log(" 版本信息:", version.split('\n')[0]);
// Docker 中尝试列出字体
if (process.platform === 'linux') {
console.log("\n3. 检查可用字体:");
try {
const fontList = execSync('magick list font', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] });
const lines = fontList.split('\n');
let foundFonts = [];
// 查找中文字体
for (let i = 0; i < lines.length; i++) {
const line = lines[i].toLowerCase();
if (line.includes('han') || line.includes('song') || line.includes('hei') ||
line.includes('chinese') || line.includes('noto')) {
foundFonts.push(lines[i].trim());
}
}
if (foundFonts.length > 0) {
console.log(" 找到以下相关字体:");
foundFonts.forEach(f => console.log(` - ${f}`));
} else {
console.log(" ⚠ 未找到中文字体(这可能是汉字乱码的原因)");
}
} catch (e) {
console.log(" ? 无法列出字体列表");
}
}
} catch (e1) {
// 再检查 GraphicsMagick
try {
const version = execSync('gm -version', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] });
console.log(" ✓ GraphicsMagick 已安装");
console.log(" 版本信息:", version.split('\n')[0]);
} catch (e2) {
console.log(" ✗ ImageMagick 和 GraphicsMagick 都未安装");
return;
}
}
} catch (error) {
console.log(" ✗ 检查失败:", error.message);
}
// 测试不同的字体
const testCases = [
{ fontName: "SourceHanSerif-Regular.ttc", fontPath: path.join(fontDir, "SourceHanSerif-Regular.ttc") },
{ fontName: "heiti.otf", fontPath: path.join(fontDir, "heiti.otf") },
{ fontName: "songti.TTC", fontPath: path.join(fontDir, "songti.TTC") }
];
for (const testCase of testCases) {
try {
const outputFile = path.join(tmpDir, `test_font_${testCase.fontName.replace(/\./g, '_')}.jpg`);
await new Promise((resolve, reject) => {
imageMagick(500, 500, "#fff")
.font(testCase.fontPath, 96)
.drawText(0, 0, "开心", "center")
.write(outputFile, function (err) {
if (err) {
reject(err);
} else {
resolve(outputFile);
}
});
});
console.log(` ✓ ${testCase.fontName} - 生成成功`);
console.log(` 输出文件:${outputFile}`);
// 检查生成的文件
if (fs.existsSync(outputFile)) {
const stats = fs.statSync(outputFile);
console.log(` 文件大小:${(stats.size / 1024).toFixed(2)} KB`);
}
} catch (error) {
console.log(` ✗ ${testCase.fontName} - 生成失败`);
console.log(` 错误:${error.message}`);
// Docker 中的特殊提示
if (process.platform === 'linux') {
console.log(`\n Docker 环境建议:`);
console.log(` 1. 检查字体文件是否复制到镜像中`);
console.log(` 2. 检查字体文件权限:chmod 644 fonts/*`);
console.log(` 3. 在 Dockerfile 中添加中文字体支持`);
console.log(` 4. 运行 fc-cache -fv 刷新字体缓存`);
}
}
}
console.log("\n5. Docker 环境配置建议:\n");
console.log(" 如果在 Docker 中出现汉字乱码,请在 Dockerfile 中添加:");
console.log("\n # 安装中文字体");
console.log(" RUN apt-get update && apt-get install -y \\");
console.log(" fonts-noto-cjk \\");
console.log(" fontconfig \\");
console.log(" && fc-cache -fv \\");
console.log(" && rm -rf /var/lib/apt/lists/*");
console.log("\n # 或者手动复制字体文件");
console.log(" COPY fonts/ /usr/share/fonts/");
console.log(" RUN fc-cache -fv");
console.log("\n=== 测试完成 ===\n");
}
// 运行测试
testFontFiles().catch(console.error);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment