refactor: 重构lib代码(无实质性功能更新,可不更新)

This commit is contained in:
bietiaop 2024-08-22 13:34:08 +08:00
parent a7f06d404b
commit aa3b7928ec
26 changed files with 547 additions and 483 deletions

44
lib/download/core.js Normal file
View file

@ -0,0 +1,44 @@
import path from 'path';
import fs from 'fs';
import request from '../../utils/request.js';
/**
* 下载文件
* @param {string} url 下载地址
* @param {string} savePath 保存路径
* @returns {Promise<string | null>} 保存路径
*/
export const downloadFile = async (url, savePath) => {
// 下载文件
try {
const download = await request(url, {}, 5);
const arrayBuffer = await download.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// 保存文件
if (!fs.existsSync(path.dirname(savePath))) {
fs.mkdirSync(path.dirname(savePath), { recursive: true });
}
fs.writeFileSync(savePath, buffer);
// 返回保存路径
return savePath;
} catch (error) {
return null;
}
};
/**
* 查看文件是否存在如果存在则返回路径否则下载文件
* @param {string} url 下载地址
* @param {string} savePath 保存路径
* @returns {Promise<string | null>} 保存路径
*/
export const checkFile = async (url, savePath) => {
if (fs.existsSync(savePath)) {
const stats = fs.statSync(savePath);
if (stats.size > 0) {
return savePath;
}
}
const download = await downloadFile(url, savePath);
return download;
};