Skip to content

Commit

Permalink
feat: 支持从 gist 获取不在同步配置中的 gist 文件
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jan 16, 2024
1 parent 14d9885 commit 16c79ac
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.170",
"version": "2.14.171",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
60 changes: 58 additions & 2 deletions backend/src/restful/artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default function register($app) {
if (!$.read(ARTIFACTS_KEY)) $.write({}, ARTIFACTS_KEY);

// RESTful APIs
$app.get('/api/artifacts/restore', restoreArtifacts);

$app.route('/api/artifacts')
.get(getAllArtifacts)
.post(createArtifact)
Expand All @@ -30,6 +32,60 @@ export default function register($app) {
.delete(deleteArtifact);
}

async function restoreArtifacts(_, res) {
$.info('开始恢复远程配置...');
try {
const { gistToken } = $.read(SETTINGS_KEY);
if (!gistToken) {
return Promise.reject('未设置 GitHub Token!');
}
const manager = new Gist({
token: gistToken,
key: ARTIFACT_REPOSITORY_KEY,
});

try {
const gist = await manager.locate();
if (!gist?.files) {
throw new Error(`找不到 Sub-Store Gist 文件列表`);
}
const allArtifacts = $.read(ARTIFACTS_KEY);
Object.keys(gist.files).map((key) => {
const filename = gist.files[key]?.filename;
if (filename) {
const artifact = findByName(allArtifacts, filename);
if (artifact) {
updateByName(allArtifacts, filename, {
...artifact,
url: gist.files[key]?.raw_url,
});
} else {
allArtifacts.push({
name: `${filename}`,
url: gist.files[key]?.raw_url,
});
}
}
});
$.write(allArtifacts, ARTIFACTS_KEY);
} catch (err) {
$.error(`查找 Sub-Store Gist 时发生错误: ${err.message ?? err}`);
throw err;
}
success(res);
} catch (e) {
$.error(`恢复远程配置失败,原因:${e.message ?? e}`);
failed(
res,
new InternalServerError(
`FAILED_TO_RESTORE_ARTIFACTS`,
`Failed to restore artifacts`,
`Reason: ${e.message ?? e}`,
),
);
}
}

function getAllArtifacts(req, res) {
const allArtifacts = $.read(ARTIFACTS_KEY);
success(res, allArtifacts);
Expand Down Expand Up @@ -137,7 +193,7 @@ async function deleteArtifact(req, res) {
if (artifact.updated) {
// delete gist
const files = {};
files[encodeURIComponent(artifact.name)] = {
files[artifact.name] = {
content: '',
};
// 当别的Sub 删了同步订阅 或 gist里面删了 当前设备没有删除 时 无法删除的bug
Expand Down Expand Up @@ -171,7 +227,7 @@ function validateArtifactName(name) {
async function syncToGist(files) {
const { gistToken } = $.read(SETTINGS_KEY);
if (!gistToken) {
return Promise.reject('未设置Gist Token!');
return Promise.reject('未设置 GitHub Token!');
}
const manager = new Gist({
token: gistToken,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/restful/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export async function updateGitHubAvatar() {
export async function updateArtifactStore() {
$.log('Updating artifact store');
const settings = $.read(SETTINGS_KEY);
const { githubUser, gistToken } = settings;
if (githubUser && gistToken) {
const { gistToken } = settings;
if (gistToken) {
const manager = new Gist({
token: gistToken,
key: ARTIFACT_REPOSITORY_KEY,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/restful/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,16 @@ async function syncAllArtifacts(_, res) {
try {
await syncArtifacts();
success(res);
} catch (err) {
} catch (e) {
$.error(`同步订阅失败,原因:${e.message ?? e}`);
failed(
res,
new InternalServerError(
`FAILED_TO_SYNC_ARTIFACTS`,
`Failed to sync all artifacts`,
`Reason: ${err}`,
`Reason: ${e.message ?? e}`,
),
);
$.info(`同步订阅失败,原因:${err}`);
}
}

Expand Down

0 comments on commit 16c79ac

Please sign in to comment.