mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-10 09:19:00 +08:00
feat: 文件支持脚本操作
This commit is contained in:
parent
9ae70eca09
commit
c059296224
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.14.145",
|
||||
"version": "2.14.146",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
||||
"main": "src/main.js",
|
||||
"scripts": {
|
||||
|
@ -316,11 +316,20 @@ function ScriptOperator(script, targetPlatform, $arguments, source) {
|
||||
await (async function () {
|
||||
const operator = createDynamicFunction(
|
||||
'operator',
|
||||
`async function operator(proxies = []) {
|
||||
return proxies.map(($server = {}) => {
|
||||
${script}
|
||||
return $server
|
||||
})
|
||||
`async function operator(input = []) {
|
||||
let proxies
|
||||
if (Array.isArray(input)) {
|
||||
proxies = input
|
||||
return proxies.map(($server = {}) => {
|
||||
${script}
|
||||
return $server
|
||||
})
|
||||
} else {
|
||||
let $content = input
|
||||
${script}
|
||||
return $content
|
||||
}
|
||||
|
||||
}`,
|
||||
$arguments,
|
||||
);
|
||||
@ -689,7 +698,10 @@ async function ApplyOperator(operator, objs) {
|
||||
);
|
||||
let funcErr = '';
|
||||
let funcErrMsg = `${err.message ?? err}`;
|
||||
if (funcErrMsg.includes('$server is not defined')) {
|
||||
if (
|
||||
funcErrMsg.includes('$server is not defined') ||
|
||||
funcErrMsg.includes('$content is not defined')
|
||||
) {
|
||||
funcErr = '';
|
||||
} else {
|
||||
funcErr = `执行 function operator 失败 ${funcErrMsg}; `;
|
||||
|
@ -3,6 +3,7 @@ import { FILES_KEY } from '@/constants';
|
||||
import { failed, success } from '@/restful/response';
|
||||
import $ from '@/core/app';
|
||||
import { RequestInvalidError, ResourceNotFoundError } from '@/restful/errors';
|
||||
import { ProxyUtils } from '@/core/proxy-utils';
|
||||
|
||||
export default function register($app) {
|
||||
if (!$.read(FILES_KEY)) $.write([], FILES_KEY);
|
||||
@ -40,13 +41,17 @@ function createFile(req, res) {
|
||||
success(res, file, 201);
|
||||
}
|
||||
|
||||
function getFile(req, res) {
|
||||
async function getFile(req, res) {
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
const allFiles = $.read(FILES_KEY);
|
||||
const file = findByName(allFiles, name);
|
||||
if (file) {
|
||||
res.set('Content-Type', 'text/plain; charset=utf-8').send(file.content);
|
||||
let content = file.content ?? '';
|
||||
content = await ProxyUtils.process(content, file.process || []);
|
||||
res.set('Content-Type', 'text/plain; charset=utf-8').send(
|
||||
content ?? '',
|
||||
);
|
||||
} else {
|
||||
failed(
|
||||
res,
|
||||
|
@ -9,6 +9,28 @@ import $ from '@/core/app';
|
||||
export default function register($app) {
|
||||
$app.post('/api/preview/sub', compareSub);
|
||||
$app.post('/api/preview/collection', compareCollection);
|
||||
$app.post('/api/preview/file', previewFile);
|
||||
}
|
||||
|
||||
async function previewFile(req, res) {
|
||||
try {
|
||||
let { content = '', process = [] } = req.body;
|
||||
|
||||
const processed = await ProxyUtils.process(content, process || []);
|
||||
|
||||
// produce
|
||||
success(res, { original: content, processed });
|
||||
} catch (err) {
|
||||
$.error(err.message ?? err);
|
||||
failed(
|
||||
res,
|
||||
new InternalServerError(
|
||||
`INTERNAL_SERVER_ERROR`,
|
||||
`Failed to preview file`,
|
||||
`Reason: ${err.message ?? err}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function compareSub(req, res) {
|
||||
|
@ -331,7 +331,10 @@ async function produceArtifact({
|
||||
} else if (type === 'file') {
|
||||
const allFiles = $.read(FILES_KEY);
|
||||
const file = findByName(allFiles, name);
|
||||
return file?.content ?? '';
|
||||
if (!file) throw new Error(`找不到文件 ${name}`);
|
||||
let content = file.content ?? '';
|
||||
content = await ProxyUtils.process(content, file.process || []);
|
||||
return content ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user