增加图像缓存功能

This commit is contained in:
yoimiya-kokomi 2022-04-04 06:47:06 +08:00
parent 7183b63a4d
commit 0ba905744e
4 changed files with 91 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import { Character } from "../components/models.js";
export async function wiki(e, { render }) { export async function wiki(e, { render }) {
const ret = /^#*(.*)(素材|技能|材料|信息|天赋|wiki|资料|命座)$/.exec(e.msg); const ret = /^#*(.*)(缓存)$/.exec(e.msg);
if (!ret || !ret[1]) { if (!ret || !ret[1]) {
return; return;

View File

@ -1,5 +1,6 @@
import lodash from "lodash"; import lodash from "lodash";
import fs from "fs"; import fs from "fs";
import request from "request";
let Data = { let Data = {
@ -91,9 +92,82 @@ let Data = {
return lodash.get(target, keyFrom, defaultValue); return lodash.get(target, keyFrom, defaultValue);
}, },
getUrlPath(url) {
let reg = /^https*:\/\/(.*)\/(\w+\.(png|jpg|jpeg|webp))(\?.*)?$/;
let ret = reg.exec(url);
if (!ret) {
return false;
}
return {
path: ret[1],
filename: ret[2],
type: ret[3],
url
}
},
pathExists(root, path) {
if (fs.existsSync(root + "/" + path)) {
return true;
}
path = path.replace("\\", "/");
const dirList = path.split("/");
let currentDir = root;
for (let dir of dirList) {
currentDir = currentDir + "/" + dir;
if (!fs.existsSync(currentDir)) {
fs.mkdirSync(currentDir);
}
}
return true;
},
async asyncPool(poolLimit, array, iteratorFn) {
const ret = []; // 存储所有的异步任务
const executing = []; // 存储正在执行的异步任务
for (const item of array) {
// 调用iteratorFn函数创建异步任务
const p = Promise.resolve().then(() => iteratorFn(item, array));
// 保存新的异步任务
ret.push(p);
// 当poolLimit值小于或等于总任务个数时进行并发控制
if (poolLimit <= array.length) {
// 当任务完成后,从正在执行的任务数组中移除已完成的任务
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e); // 保存正在执行的异步任务
if (executing.length >= poolLimit) {
// 等待较快的任务执行完成
await Promise.race(executing);
}
}
}
return Promise.all(ret);
},
async cacheFile(fileList, cacheRoot) {
let ret = {};
let cacheFn = async function (url) {
let path = Data.getUrlPath(url);
if (fs.existsSync(`${cacheRoot}/${path.path}/${path.filename}`)) {
console.log("已存在,跳过 " + path.path + "/" + path.filename);
ret[url] = `${path.path}/${path.filename}`;
return true;
}
Data.pathExists(cacheRoot, path.path);
await request(url).pipe(fs.createWriteStream(`${cacheRoot}/${path.path}/` + path.filename));
console.log("下载成功: " + path.path + "/" + path.filname);
ret[url] = `${path.path}/${path.filename}`;
return true;
}; };
await Data.asyncPool(10, fileList, cacheFn);
return ret;
}
}
export default Data; export default Data;

View File

@ -65,6 +65,19 @@ class Character extends Base {
fileList = lodash.uniq(fileList); fileList = lodash.uniq(fileList);
let fileMap = await Data.cacheFile(fileList, `${_path}/resources/`, `cache`);
let path = Data.getUrlPath()
lodash.forEach(this.img, (url, key) => {
if (path[url]) {
this.img[key] = path[url]
}
});
console.log(this);
} }

View File

@ -18,7 +18,7 @@ export const rule = {
describe: "【#老婆,#老公,#女儿】角色详情", describe: "【#老婆,#老公,#女儿】角色详情",
}, },
wiki: { wiki: {
reg: "^#*.*(天赋|技能|命座|命之座|详情)$", reg: "^#*.*(缓存)$",
priority: 300 priority: 300
} }
}; };