Skip to content

Commit e81245a

Browse files
committed
feat: Gist 备份恢复增加 Base64 编码方式
1 parent 217fdae commit e81245a

3 files changed

Lines changed: 52 additions & 22 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.84",
3+
"version": "2.19.85",
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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Base64 } from 'js-base64';
12
import _ from 'lodash';
23
import express from '@/vendor/express';
34
import $ from '@/core/app';
@@ -428,19 +429,28 @@ export default function serve() {
428429
download(data_url)
429430
.then(async (content) => {
430431
try {
431-
content = JSON.parse(content);
432+
content = JSON.parse(Base64.decode(content));
432433
if (Object.keys(content.settings).length === 0) {
433434
throw new Error(
434435
'备份文件应该至少包含 settings 字段',
435436
);
436437
}
437438
} catch (err) {
438-
$.error(
439-
`Gist 备份文件校验失败, 无法还原\nReason: ${
440-
err.message ?? err
441-
}`,
442-
);
443-
throw new Error('Gist 备份文件校验失败, 无法还原');
439+
try {
440+
content = JSON.parse(content);
441+
if (Object.keys(content.settings).length === 0) {
442+
throw new Error(
443+
'备份文件应该至少包含 settings 字段',
444+
);
445+
}
446+
} catch (err) {
447+
$.error(
448+
`Gist 备份文件校验失败, 无法还原\nReason: ${
449+
err.message ?? err
450+
}`,
451+
);
452+
throw new Error('Gist 备份文件校验失败, 无法还原');
453+
}
444454
}
445455
if (data_url_post) {
446456
$.info('[BACKEND] executing post-processing script');

backend/src/restful/miscs.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Base64 } from 'js-base64';
12
import _ from 'lodash';
23
import $ from '@/core/app';
34
import { ENV } from '@/vendor/open-api';
@@ -105,7 +106,7 @@ async function refresh(_, res) {
105106
success(res);
106107
}
107108

108-
async function gistBackupAction(action, keep) {
109+
async function gistBackupAction(action, keep, encode) {
109110
// read token
110111
const { gistToken, syncPlatform } = $.read(SETTINGS_KEY);
111112
if (!gistToken) throw new Error('GitHub Token is required for backup!');
@@ -127,8 +128,16 @@ async function gistBackupAction(action, keep) {
127128
content = $.read('#sub-store');
128129
content = content ? JSON.parse(content) : {};
129130
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
130-
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
131-
content = JSON.stringify(content, null, ` `);
131+
if (encode === 'base64') {
132+
content = Base64.encode(
133+
JSON.stringify(content, null, ` `),
134+
);
135+
} else {
136+
content.settings.gistToken =
137+
'恢复后请重新设置 GitHub Token';
138+
content = JSON.stringify(content, null, ` `);
139+
}
140+
132141
$.info(`下载备份, 与本地内容对比...`);
133142
const onlineContent = await gist.download(
134143
GIST_BACKUP_FILE_NAME,
@@ -147,8 +156,12 @@ async function gistBackupAction(action, keep) {
147156
content = $.read('#sub-store');
148157
content = content ? JSON.parse(content) : {};
149158
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
150-
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
151-
content = JSON.stringify(content, null, ` `);
159+
if (encode) {
160+
content = Base64.encode(JSON.stringify(content, null, ` `));
161+
} else {
162+
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
163+
content = JSON.stringify(content, null, ` `);
164+
}
152165
$.info(`上传备份中...`);
153166
try {
154167
await gist.upload({
@@ -166,17 +179,24 @@ async function gistBackupAction(action, keep) {
166179
$.info(`还原备份中...`);
167180
content = await gist.download(GIST_BACKUP_FILE_NAME);
168181
try {
169-
content = JSON.parse(content);
182+
content = JSON.parse(Base64.decode(content));
170183
if (Object.keys(content.settings).length === 0) {
171184
throw new Error('备份文件应该至少包含 settings 字段');
172185
}
173186
} catch (err) {
174-
$.error(
175-
`Gist 备份文件校验失败, 无法还原\nReason: ${
176-
err.message ?? err
177-
}`,
178-
);
179-
throw new Error('Gist 备份文件校验失败, 无法还原');
187+
try {
188+
content = JSON.parse(content);
189+
if (Object.keys(content.settings).length === 0) {
190+
throw new Error('备份文件应该至少包含 settings 字段');
191+
}
192+
} catch (err) {
193+
$.error(
194+
`Gist 备份文件校验失败, 无法还原\nReason: ${
195+
err.message ?? err
196+
}`,
197+
);
198+
throw new Error('Gist 备份文件校验失败, 无法还原');
199+
}
180200
}
181201
if (keep) {
182202
$.info(`保留原有设置 ${keep}`);
@@ -198,7 +218,7 @@ async function gistBackupAction(action, keep) {
198218
}
199219
}
200220
async function gistBackup(req, res) {
201-
const { action, keep } = req.query;
221+
const { action, keep, encode } = req.query;
202222
// read token
203223
const { gistToken } = $.read(SETTINGS_KEY);
204224
if (!gistToken) {
@@ -211,7 +231,7 @@ async function gistBackup(req, res) {
211231
);
212232
} else {
213233
try {
214-
await gistBackupAction(action, keep);
234+
await gistBackupAction(action, keep, encode);
215235
success(res);
216236
} catch (err) {
217237
$.error(

0 commit comments

Comments
 (0)