feat: 模块接口; 脚本参数支持 JSON 和 URL编码

This commit is contained in:
xream
2023-09-14 15:16:12 +08:00
parent 33a17c2d66
commit 3211fbf357
6 changed files with 143 additions and 17 deletions

View File

@@ -1,4 +1,4 @@
import { FILES_KEY } from '@/constants';
import { FILES_KEY, MODULES_KEY } from '@/constants';
import { findByName } from '@/utils/database';
import { HTTP, ENV } from '@/vendor/open-api';
import { hex_md5 } from '@/vendor/md5';
@@ -8,19 +8,21 @@ import $ from '@/core/app';
const tasks = new Map();
export default async function download(url, ua) {
const downloadUrlMatch = url.match(/^\/api\/file\/(.+)/);
const downloadUrlMatch = url.match(/^\/api\/(file|module)\/(.+)/);
if (downloadUrlMatch) {
let fileName = downloadUrlMatch?.[1];
if (fileName == null) {
throw new Error(`本地文件 URL 无效: ${url}`);
let type = downloadUrlMatch?.[1];
let name = downloadUrlMatch?.[2];
if (name == null) {
throw new Error(`本地 ${type} URL 无效: ${url}`);
}
fileName = decodeURIComponent(fileName);
const allFiles = $.read(FILES_KEY);
const file = findByName(allFiles, fileName);
if (!file) {
throw new Error(`找不到本地文件: ${fileName}`);
name = decodeURIComponent(name);
const key = type === 'module' ? MODULES_KEY : FILES_KEY;
const item = findByName($.read(key), name);
if (!item) {
throw new Error(`找不到本地 ${type}: ${name}`);
}
return file.content;
return item.content;
}
const { isNode } = ENV();