Skip to content

Commit ef4d0a2

Browse files
committed
feat: 支持从 Gist 恢复时保留当前 GitHub Token
1 parent e816e5b commit ef4d0a2

2 files changed

Lines changed: 17 additions & 7 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.82",
3+
"version": "2.19.83",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/restful/miscs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import $ from '@/core/app';
23
import { ENV } from '@/vendor/open-api';
34
import { failed, success } from '@/restful/response';
@@ -104,7 +105,7 @@ async function refresh(_, res) {
104105
success(res);
105106
}
106107

107-
async function gistBackupAction(action) {
108+
async function gistBackupAction(action, keep) {
108109
// read token
109110
const { gistToken, syncPlatform } = $.read(SETTINGS_KEY);
110111
if (!gistToken) throw new Error('GitHub Token is required for backup!');
@@ -114,6 +115,9 @@ async function gistBackupAction(action) {
114115
key: GIST_BACKUP_KEY,
115116
syncPlatform,
116117
});
118+
let currentContent = $.read('#sub-store');
119+
currentContent = currentContent ? JSON.parse(currentContent) : {};
120+
if ($.env.isNode) currentContent = JSON.parse(JSON.stringify($.cache));
117121
let content;
118122
const settings = $.read(SETTINGS_KEY);
119123
const updated = settings.syncTime;
@@ -162,7 +166,8 @@ async function gistBackupAction(action) {
162166
$.info(`还原备份中...`);
163167
content = await gist.download(GIST_BACKUP_FILE_NAME);
164168
try {
165-
if (Object.keys(JSON.parse(content).settings).length === 0) {
169+
content = JSON.parse(content);
170+
if (Object.keys(content.settings).length === 0) {
166171
throw new Error('备份文件应该至少包含 settings 字段');
167172
}
168173
} catch (err) {
@@ -173,10 +178,15 @@ async function gistBackupAction(action) {
173178
);
174179
throw new Error('Gist 备份文件校验失败, 无法还原');
175180
}
181+
if (keep) {
182+
$.info(`保留原有设置 ${keep}`);
183+
keep.split(',').forEach((path) => {
184+
_.set(content, path, _.get(currentContent, path));
185+
});
186+
}
176187
// restore settings
177-
$.write(content, '#sub-store');
188+
$.write(JSON.stringify(content, null, ` `), '#sub-store');
178189
if ($.env.isNode) {
179-
content = JSON.parse(content);
180190
$.cache = content;
181191
$.persistCache();
182192
}
@@ -188,7 +198,7 @@ async function gistBackupAction(action) {
188198
}
189199
}
190200
async function gistBackup(req, res) {
191-
const { action } = req.query;
201+
const { action, keep } = req.query;
192202
// read token
193203
const { gistToken } = $.read(SETTINGS_KEY);
194204
if (!gistToken) {
@@ -201,7 +211,7 @@ async function gistBackup(req, res) {
201211
);
202212
} else {
203213
try {
204-
await gistBackupAction(action);
214+
await gistBackupAction(action, keep);
205215
success(res);
206216
} catch (err) {
207217
$.error(

0 commit comments

Comments
 (0)