article-writer-cn
Version:
AI 驱动的智能写作系统 - 专注公众号/自媒体文章创作
110 lines • 2.96 kB
TypeScript
/**
* 图片下载工具模块
*
* 功能:
* - 下载单个/批量图片
* - 图片格式验证
* - 并发控制和进度显示
* - 错误重试机制
*/
/**
* 下载选项
*/
export interface DownloadOptions {
/** 图片URL */
url: string;
/** 保存路径(绝对路径) */
savePath: string;
/** 最大重试次数 */
maxRetries?: number;
/** 超时时间(毫秒) */
timeout?: number;
/** 用户代理 */
userAgent?: string;
}
/**
* 下载结果
*/
export interface DownloadResult {
/** 图片URL */
url: string;
/** 保存路径 */
savePath: string;
/** 是否成功 */
success: boolean;
/** 文件大小(字节) */
size?: number;
/** 图片格式 */
format?: string;
/** 错误信息 */
error?: string;
}
/**
* 图片元数据
*/
export interface ImageInfo {
/** 图片格式(png/jpg/webp/svg等) */
format: string;
/** 文件大小(字节) */
size: number;
/** Content-Type */
contentType?: string;
}
/**
* 验证文件是否为有效图片
* 通过检查文件签名(Magic Bytes)
*/
export declare function validateImage(buffer: Buffer): {
valid: boolean;
format?: string;
};
/**
* 获取远程图片信息(不下载完整文件)
*/
export declare function getRemoteImageInfo(url: string): Promise<ImageInfo | null>;
/**
* 下载单个图片
*
* 下载策略:
* 1. 首先尝试 axios 直接下载 (快速)
* 2. 如果失败且检测到 Playwright MCP, 使用浏览器下载 (处理反爬虫)
* 3. 都失败则返回错误
*/
export declare function downloadImage(options: DownloadOptions, usePlaywright?: boolean): Promise<DownloadResult>;
/**
* 批量下载图片
*/
export declare function downloadImages(tasks: DownloadOptions[], concurrency?: number, showProgress?: boolean): Promise<DownloadResult[]>;
/**
* 从URL推断文件扩展名
*/
export declare function inferFileExtension(url: string, contentType?: string): string;
/**
* 生成下载日志
*/
export declare function saveDownloadLog(results: DownloadResult[], logPath: string): Promise<void>;
/**
* 将本地图片文件转换为 Base64 Data URI
* 适用于微信公众号一键复制功能
*
* @param imagePath - 图片文件的绝对路径
* @returns Base64 Data URI 字符串 (如: data:image/png;base64,...)
* @throws 如果文件不存在或读取失败
*
* @example
* ```typescript
* const dataUri = await imageToBase64('/path/to/image.png');
* // 返回: "data:image/png;base64,iVBORw0KGgoAAAANS..."
* ```
*/
export declare function imageToBase64(imagePath: string): Promise<string>;
/**
* 判断路径是否为本地文件路径
* (非 http/https URL)
*/
export declare function isLocalPath(urlOrPath: string): boolean;
/**
* 获取图片文件的大小(字节)和格式信息
*/
export declare function getImageInfo(imagePath: string): Promise<ImageInfo | null>;
//# sourceMappingURL=image-downloader.d.ts.map