mirror of
https://github.com/yoimiya-kokomi/miao-plugin.git
synced 2024-11-16 04:35:42 +00:00
增加图像缓存功能
This commit is contained in:
parent
7183b63a4d
commit
0ba905744e
@ -6,7 +6,7 @@ import { Character } from "../components/models.js";
|
||||
|
||||
export async function wiki(e, { render }) {
|
||||
|
||||
const ret = /^#*(.*)(素材|技能|材料|信息|天赋|wiki|资料|命座)$/.exec(e.msg);
|
||||
const ret = /^#*(.*)(缓存)$/.exec(e.msg);
|
||||
|
||||
if (!ret || !ret[1]) {
|
||||
return;
|
||||
|
@ -1,5 +1,6 @@
|
||||
import lodash from "lodash";
|
||||
import fs from "fs";
|
||||
import request from "request";
|
||||
|
||||
let Data = {
|
||||
|
||||
@ -91,9 +92,82 @@ let Data = {
|
||||
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;
|
||||
|
@ -65,6 +65,19 @@ class Character extends Base {
|
||||
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user