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

18
lib/download/const.js Normal file
View file

@ -0,0 +1,18 @@
import path from 'path';
import { imageResourcesPath } from '../path.js';
export const ZZZ_SQUARE_AVATAR_PATH = path.join(
imageResourcesPath,
'square_avatar'
),
ZZZ_SMALL_SQUARE_AVATAR_PATH = path.join(imageResourcesPath, 'role_general'),
ZZZ_SQUARE_BANGBOO_PATH = path.join(
imageResourcesPath,
'bangboo_square_avatar'
),
ZZZ_WEAPON_PATH = path.join(imageResourcesPath, 'weapon'),
ZZZ_ROLE_PATH = path.join(imageResourcesPath, 'role'),
ZZZ_ROLE_CIRCLE_PATH = path.join(imageResourcesPath, 'role_circle'),
ZZZ_SUIT_3D_PATH = path.join(imageResourcesPath, 'suit_3d'),
ZZZ_SUIT_PATH = path.join(imageResourcesPath, 'suit');
// const ZZZ_GUIDES_PATH = path.join(imageResourcesPath, 'guides');

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;
};

57
lib/download/download.js Normal file
View file

@ -0,0 +1,57 @@
import path from 'path';
import { checkFile } from './core.js';
import { getResourceRemotePath } from '../assets.js';
import * as MysURL from '../mysapi/api.js';
import * as LocalURI from './const.js';
/**
* 下载米游社图片
* @param {keyof MysURL} base 远程地址
* @param {keyof LocalURI} localBase 本地地址
* @param {string} filename 文件名
* @param {keyof MysURL} newBase 新远程地址
*/
export const downloadMysImage = async (
base,
localBase,
filename,
newBase = ''
) => {
base = MysURL[base];
localBase = LocalURI[localBase];
if (!!newBase) {
newBase = MysURL[newBase];
}
const finalPath = path.join(localBase, filename);
let url = `${base}/${filename}`;
let result = await checkFile(url, finalPath);
if (!result && !!newBase) {
url = `${newBase}/${filename}`;
result = await checkFile(url, finalPath);
}
return result;
};
/**
* 下载资源库图片
* @param {Parameters<getResourceRemotePath>[0]} remoteLabel 远程地址
* @param {keyof LocalURI} localBase 本地地址
* @param {string} filename 文件名
* @param {string} replaceFilename 替换文件名(如果资源不存在)
*/
export const downloadResourceImage = async (
remoteLabel,
localBase,
filename,
replaceFilename = ''
) => {
localBase = LocalURI[localBase];
const finalPath = path.join(localBase, filename);
const url = await getResourceRemotePath(remoteLabel, filename);
let result = await checkFile(url, finalPath);
if (!result && !!replaceFilename) {
const finalPath = path.join(localBase, replaceFilename);
const url = await getResourceRemotePath(remoteLabel, replaceFilename);
result = await checkFile(url, finalPath);
}
return result;
};