Skip to content

Commit 217fdae

Browse files
committed
feat: Node 环境使用 SUB_STORE_DATA_URL 恢复备份后, 支持 SUB_STORE_DATA_URL_POST 执行自定义命令
1 parent ef4d0a2 commit 217fdae

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.19.83",
3+
"version": "2.19.84",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/restful/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import express from '@/vendor/express';
23
import $ from '@/core/app';
34
import migrate from '@/utils/migration';
@@ -304,6 +305,7 @@ export default function serve() {
304305
const path = eval(`require("path")`);
305306
const fs = eval(`require("fs")`);
306307
const data_url = eval('process.env.SUB_STORE_DATA_URL');
308+
const data_url_post = eval('process.env.SUB_STORE_DATA_URL_POST');
307309
const fe_be_path = eval('process.env.SUB_STORE_FRONTEND_BACKEND_PATH');
308310
const fe_port = eval('process.env.SUB_STORE_FRONTEND_PORT') || 3001;
309311
const fe_host =
@@ -424,10 +426,30 @@ export default function serve() {
424426
if (data_url) {
425427
$.info(`[BACKEND] downloading data from ${data_url}`);
426428
download(data_url)
427-
.then((content) => {
428-
$.write(content, '#sub-store');
429+
.then(async (content) => {
430+
try {
431+
content = JSON.parse(content);
432+
if (Object.keys(content.settings).length === 0) {
433+
throw new Error(
434+
'备份文件应该至少包含 settings 字段',
435+
);
436+
}
437+
} catch (err) {
438+
$.error(
439+
`Gist 备份文件校验失败, 无法还原\nReason: ${
440+
err.message ?? err
441+
}`,
442+
);
443+
throw new Error('Gist 备份文件校验失败, 无法还原');
444+
}
445+
if (data_url_post) {
446+
$.info('[BACKEND] executing post-processing script');
447+
eval(data_url_post);
448+
}
449+
450+
$.write(JSON.stringify(content, null, ` `), '#sub-store');
429451

430-
$.cache = JSON.parse(content);
452+
$.cache = content;
431453
$.persistCache();
432454

433455
migrate();

0 commit comments

Comments
 (0)