photo-watermark-cli
Version:
A modern TypeScript CLI tool to add timestamp watermarks to photos with intelligent size scaling
52 lines • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SUPPORTED_FORMATS = void 0;
exports.scanPhotos = scanPhotos;
exports.isSupportedImage = isSupportedImage;
const fs_1 = require("fs");
const path_1 = require("path");
const glob_1 = require("glob");
// 支持的图片格式
exports.SUPPORTED_FORMATS = ['.jpg', '.jpeg', '.png', '.tiff', '.webp', '.bmp'];
/**
* 扫描目录下的所有支持的图片文件
* @param directory - 要扫描的目录路径
* @returns 图片文件路径数组
*/
async function scanPhotos(directory) {
try {
// 验证目录是否存在
await fs_1.promises.access(directory);
// 使用glob模式匹配所有支持的图片文件
const patterns = [
...exports.SUPPORTED_FORMATS.map(ext => `**/*${ext}`),
...exports.SUPPORTED_FORMATS.map(ext => `**/*${ext.toUpperCase()}`)
];
const allFiles = [];
for (const pattern of patterns) {
const files = await (0, glob_1.glob)(pattern, {
cwd: directory,
absolute: true,
nodir: true
});
allFiles.push(...files);
}
// 去重并排序
const uniqueFiles = [...new Set(allFiles)];
uniqueFiles.sort();
return uniqueFiles;
}
catch (error) {
throw new Error(`扫描目录失败: ${error.message}`);
}
}
/**
* 检查文件是否为支持的图片格式
* @param filePath - 文件路径
* @returns 是否为支持的图片格式
*/
function isSupportedImage(filePath) {
const ext = (0, path_1.extname)(filePath).toLowerCase();
return exports.SUPPORTED_FORMATS.includes(ext);
}
//# sourceMappingURL=scanner.js.map