diff --git a/backend/package.json b/backend/package.json index fa6cfcb..e0aae64 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.19.82", + "version": "2.19.83", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/restful/miscs.js b/backend/src/restful/miscs.js index 48258cf..4a07008 100644 --- a/backend/src/restful/miscs.js +++ b/backend/src/restful/miscs.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import $ from '@/core/app'; import { ENV } from '@/vendor/open-api'; import { failed, success } from '@/restful/response'; @@ -104,7 +105,7 @@ async function refresh(_, res) { success(res); } -async function gistBackupAction(action) { +async function gistBackupAction(action, keep) { // read token const { gistToken, syncPlatform } = $.read(SETTINGS_KEY); if (!gistToken) throw new Error('GitHub Token is required for backup!'); @@ -114,6 +115,9 @@ async function gistBackupAction(action) { key: GIST_BACKUP_KEY, syncPlatform, }); + let currentContent = $.read('#sub-store'); + currentContent = currentContent ? JSON.parse(currentContent) : {}; + if ($.env.isNode) currentContent = JSON.parse(JSON.stringify($.cache)); let content; const settings = $.read(SETTINGS_KEY); const updated = settings.syncTime; @@ -162,7 +166,8 @@ async function gistBackupAction(action) { $.info(`还原备份中...`); content = await gist.download(GIST_BACKUP_FILE_NAME); try { - if (Object.keys(JSON.parse(content).settings).length === 0) { + content = JSON.parse(content); + if (Object.keys(content.settings).length === 0) { throw new Error('备份文件应该至少包含 settings 字段'); } } catch (err) { @@ -173,10 +178,15 @@ async function gistBackupAction(action) { ); throw new Error('Gist 备份文件校验失败, 无法还原'); } + if (keep) { + $.info(`保留原有设置 ${keep}`); + keep.split(',').forEach((path) => { + _.set(content, path, _.get(currentContent, path)); + }); + } // restore settings - $.write(content, '#sub-store'); + $.write(JSON.stringify(content, null, ` `), '#sub-store'); if ($.env.isNode) { - content = JSON.parse(content); $.cache = content; $.persistCache(); } @@ -188,7 +198,7 @@ async function gistBackupAction(action) { } } async function gistBackup(req, res) { - const { action } = req.query; + const { action, keep } = req.query; // read token const { gistToken } = $.read(SETTINGS_KEY); if (!gistToken) { @@ -201,7 +211,7 @@ async function gistBackup(req, res) { ); } else { try { - await gistBackupAction(action); + await gistBackupAction(action, keep); success(res); } catch (err) { $.error(