From f85e360ea858f2d4ae15200f74c6f39be1ebaa28 Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 14 Feb 2024 19:51:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=89=8D,=20=E9=A2=84=E5=A4=84=E7=90=86=E8=AE=A2=E9=98=85,=20?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E5=90=8C=E6=97=B6=E8=AF=B7=E6=B1=82=E8=BF=87?= =?UTF-8?q?=E5=A4=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/products/cron-sync-artifacts.js | 94 ++++++++++++++++++--- backend/src/restful/sync.js | 40 +++++++++ backend/src/utils/download.js | 1 + 4 files changed, 122 insertions(+), 15 deletions(-) diff --git a/backend/package.json b/backend/package.json index 0d484c2..14c7299 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.217", + "version": "2.14.218", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/products/cron-sync-artifacts.js b/backend/src/products/cron-sync-artifacts.js index c1dc261..f4f8c0d 100644 --- a/backend/src/products/cron-sync-artifacts.js +++ b/backend/src/products/cron-sync-artifacts.js @@ -1,8 +1,14 @@ import { version } from '../../package.json'; -import { SETTINGS_KEY, ARTIFACTS_KEY } from '@/constants'; +import { + SETTINGS_KEY, + ARTIFACTS_KEY, + SUBS_KEY, + COLLECTIONS_KEY, +} from '@/constants'; import $ from '@/core/app'; import { produceArtifact } from '@/restful/sync'; import { syncToGist } from '@/restful/artifacts'; +import { findByName } from '@/utils/database'; !(async function () { const settings = $.read(SETTINGS_KEY); @@ -30,23 +36,83 @@ async function doSync() { const files = {}; try { + const invalid = []; + const allSubs = $.read(SUBS_KEY); + const allCols = $.read(COLLECTIONS_KEY); + const subNames = []; + allArtifacts.map((artifact) => { + if (artifact.sync && artifact.source) { + if (artifact.type === 'subscription') { + const subName = artifact.source; + const sub = findByName(allSubs, subName); + if (sub && sub.url && !subNames.includes(subName)) { + subNames.push(subName); + } + } else if (artifact.type === 'collection') { + const collection = findByName(allCols, artifact.source); + if (collection && Array.isArray(collection.subscriptions)) { + collection.subscriptions.map((subName) => { + const sub = findByName(allSubs, subName); + if (sub && sub.url && !subNames.includes(subName)) { + subNames.push(subName); + } + }); + } + } + } + }); + + if (subNames.length > 0) { + await Promise.all( + subNames.map(async (subName) => { + try { + await produceArtifact({ + type: 'subscription', + name: subName, + }); + } catch (e) { + // $.error(`${e.message ?? e}`); + } + }), + ); + } await Promise.all( allArtifacts.map(async (artifact) => { - if (artifact.sync) { - $.info(`正在同步云配置:${artifact.name}...`); - const output = await produceArtifact({ - type: artifact.type, - name: artifact.source, - platform: artifact.platform, - }); + try { + if (artifact.sync && artifact.source) { + $.info(`正在同步云配置:${artifact.name}...`); + const output = await produceArtifact({ + type: artifact.type, + name: artifact.source, + platform: artifact.platform, + produceOpts: { + 'include-unsupported-proxy': + artifact.includeUnsupportedProxy, + }, + }); - files[encodeURIComponent(artifact.name)] = { - content: output, - }; + // if (!output || output.length === 0) + // throw new Error('该配置的结果为空 不进行上传'); + + files[encodeURIComponent(artifact.name)] = { + content: output, + }; + } + } catch (e) { + $.error( + `同步配置 ${artifact.name} 发生错误: ${e.message ?? e}`, + ); + invalid.push(artifact.name); } }), ); + if (invalid.length > 0) { + throw new Error( + `同步配置 ${invalid.join(', ')} 发生错误 详情请查看日志`, + ); + } + const resp = await syncToGist(files); const body = JSON.parse(resp.body); @@ -71,8 +137,8 @@ async function doSync() { $.write(allArtifacts, ARTIFACTS_KEY); $.notify('🌍 Sub-Store', '全部订阅同步成功!'); - } catch (err) { - $.notify('🌍 Sub-Store', '同步订阅失败', `原因:${err}`); - $.error(`无法同步订阅配置到 Gist,原因:${err}`); + } catch (e) { + $.notify('🌍 Sub-Store', '同步订阅失败', `原因:${e.message ?? e}`); + $.error(`无法同步订阅配置到 Gist,原因:${e}`); } } diff --git a/backend/src/restful/sync.js b/backend/src/restful/sync.js index 1256d95..534168a 100644 --- a/backend/src/restful/sync.js +++ b/backend/src/restful/sync.js @@ -448,6 +448,46 @@ async function syncArtifacts() { try { const invalid = []; + const allSubs = $.read(SUBS_KEY); + const allCols = $.read(COLLECTIONS_KEY); + const subNames = []; + allArtifacts.map((artifact) => { + if (artifact.sync && artifact.source) { + if (artifact.type === 'subscription') { + const subName = artifact.source; + const sub = findByName(allSubs, subName); + if (sub && sub.url && !subNames.includes(subName)) { + subNames.push(subName); + } + } else if (artifact.type === 'collection') { + const collection = findByName(allCols, artifact.source); + if (collection && Array.isArray(collection.subscriptions)) { + collection.subscriptions.map((subName) => { + const sub = findByName(allSubs, subName); + if (sub && sub.url && !subNames.includes(subName)) { + subNames.push(subName); + } + }); + } + } + } + }); + + if (subNames.length > 0) { + await Promise.all( + subNames.map(async (subName) => { + try { + await produceArtifact({ + type: 'subscription', + name: subName, + }); + } catch (e) { + // $.error(`${e.message ?? e}`); + } + }), + ); + } + await Promise.all( allArtifacts.map(async (artifact) => { try { diff --git a/backend/src/utils/download.js b/backend/src/utils/download.js index f1f7c80..b4d8fa9 100644 --- a/backend/src/utils/download.js +++ b/backend/src/utils/download.js @@ -74,6 +74,7 @@ export default async function download(rawUrl, ua, timeout) { // try to find in app cache const cached = resourceCache.get(id); if (!$arguments?.noCache && cached) { + $.info(`使用缓存: ${url}`); result = cached; } else { $.info(