mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2026-04-30 05:08:05 +08:00
Added support for non-ascii characters in subscriptions, collections and artifacts
This commit is contained in:
@@ -30,7 +30,8 @@ export default function register($app) {
|
||||
}
|
||||
|
||||
async function getArtifact(req, res) {
|
||||
const name = req.params.name;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
const action = req.query.action;
|
||||
const allArtifacts = $.read(ARTIFACTS_KEY);
|
||||
const artifact = allArtifacts[name];
|
||||
@@ -61,14 +62,15 @@ async function getArtifact(req, res) {
|
||||
console.log(JSON.stringify(artifact, null, 2));
|
||||
try {
|
||||
const resp = await syncArtifact({
|
||||
[artifact.name]: { content: output },
|
||||
[encodeURIComponent(artifact.name)]: {
|
||||
content: output,
|
||||
},
|
||||
});
|
||||
artifact.updated = new Date().getTime();
|
||||
const body = JSON.parse(resp.body);
|
||||
artifact.url = body.files[artifact.name].raw_url.replace(
|
||||
/\/raw\/[^/]*\/(.*)/,
|
||||
'/raw/$1',
|
||||
);
|
||||
artifact.url = body.files[
|
||||
encodeURIComponent(artifact.name)
|
||||
].raw_url.replace(/\/raw\/[^/]*\/(.*)/, '/raw/$1');
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
res.json({
|
||||
status: 'success',
|
||||
@@ -104,25 +106,19 @@ function createArtifact(req, res) {
|
||||
message: `远程配置${artifact.name}已存在!`,
|
||||
});
|
||||
} else {
|
||||
if (/^[\w-_.]*$/.test(artifact.name)) {
|
||||
allArtifacts[artifact.name] = artifact;
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: artifact,
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
status: 'failed',
|
||||
message: `远程配置名称 ${artifact.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`,
|
||||
});
|
||||
}
|
||||
allArtifacts[artifact.name] = artifact;
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: artifact,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function updateArtifact(req, res) {
|
||||
const allArtifacts = $.read(ARTIFACTS_KEY);
|
||||
const oldName = req.params.name;
|
||||
let oldName = req.params.name;
|
||||
oldName = decodeURIComponent(oldName);
|
||||
const artifact = allArtifacts[oldName];
|
||||
if (artifact) {
|
||||
$.info(`正在更新远程配置:${artifact.name}`);
|
||||
@@ -215,7 +211,8 @@ async function cronSyncArtifacts(_, res) {
|
||||
}
|
||||
|
||||
async function deleteArtifact(req, res) {
|
||||
const name = req.params.name;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
$.info(`正在删除远程配置:${name}`);
|
||||
const allArtifacts = $.read(ARTIFACTS_KEY);
|
||||
try {
|
||||
@@ -223,10 +220,11 @@ async function deleteArtifact(req, res) {
|
||||
if (!artifact) throw new Error(`远程配置:${name}不存在!`);
|
||||
if (artifact.updated) {
|
||||
// delete gist
|
||||
await syncArtifact({
|
||||
filename: name,
|
||||
const files = {};
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: '',
|
||||
});
|
||||
};
|
||||
await syncArtifact(files);
|
||||
}
|
||||
// delete local cache
|
||||
delete allArtifacts[name];
|
||||
@@ -235,9 +233,7 @@ async function deleteArtifact(req, res) {
|
||||
status: 'success',
|
||||
});
|
||||
} catch (err) {
|
||||
// delete local cache
|
||||
delete allArtifacts[name];
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
$.error(`无法删除远程配置:${name},原因:${err}`);
|
||||
res.status(500).json({
|
||||
status: 'failed',
|
||||
message: `无法删除远程配置:${name}, 原因:${err}`,
|
||||
|
||||
@@ -20,7 +20,9 @@ export default function register($app) {
|
||||
|
||||
// collection API
|
||||
async function downloadCollection(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
|
||||
const { raw } = req.query || 'false';
|
||||
const platform =
|
||||
req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
|
||||
@@ -90,24 +92,17 @@ function createCollection(req, res) {
|
||||
message: `订阅集${collection.name}已存在!`,
|
||||
});
|
||||
}
|
||||
// validate name
|
||||
if (/^[\w-_]*$/.test(collection.name)) {
|
||||
allCol[collection.name] = collection;
|
||||
$.write(allCol, COLLECTIONS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: collection,
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
status: 'failed',
|
||||
message: `订阅集名称 ${collection.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`,
|
||||
});
|
||||
}
|
||||
allCol[collection.name] = collection;
|
||||
$.write(allCol, COLLECTIONS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: collection,
|
||||
});
|
||||
}
|
||||
|
||||
function getCollection(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
const collection = $.read(COLLECTIONS_KEY)[name];
|
||||
if (collection) {
|
||||
res.json({
|
||||
@@ -123,7 +118,8 @@ function getCollection(req, res) {
|
||||
}
|
||||
|
||||
function updateCollection(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
let collection = req.body;
|
||||
const allCol = $.read(COLLECTIONS_KEY);
|
||||
if (allCol[name]) {
|
||||
@@ -149,7 +145,8 @@ function updateCollection(req, res) {
|
||||
}
|
||||
|
||||
function deleteCollection(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
$.info(`正在删除组合订阅:${name}`);
|
||||
let allCol = $.read(COLLECTIONS_KEY);
|
||||
delete allCol[name];
|
||||
|
||||
@@ -17,7 +17,9 @@ export default function register($app) {
|
||||
|
||||
// subscriptions API
|
||||
async function downloadSubscription(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
|
||||
const { raw } = req.query || 'false';
|
||||
const platform =
|
||||
req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
|
||||
@@ -80,24 +82,17 @@ function createSubscription(req, res) {
|
||||
message: `订阅${sub.name}已存在!`,
|
||||
});
|
||||
}
|
||||
// validate name
|
||||
if (/^[\w-_]*$/.test(sub.name)) {
|
||||
allSubs[sub.name] = sub;
|
||||
$.write(allSubs, SUBS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: sub,
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
status: 'failed',
|
||||
message: `订阅名称 ${sub.name} 中含有非法字符!名称中只能包含英文字母、数字、下划线、横杠。`,
|
||||
});
|
||||
}
|
||||
allSubs[sub.name] = sub;
|
||||
$.write(allSubs, SUBS_KEY);
|
||||
res.status(201).json({
|
||||
status: 'success',
|
||||
data: sub,
|
||||
});
|
||||
}
|
||||
|
||||
function getSubscription(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
const sub = $.read(SUBS_KEY)[name];
|
||||
if (sub) {
|
||||
res.json({
|
||||
@@ -113,7 +108,8 @@ function getSubscription(req, res) {
|
||||
}
|
||||
|
||||
function updateSubscription(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
let sub = req.body;
|
||||
const allSubs = $.read(SUBS_KEY);
|
||||
if (allSubs[name]) {
|
||||
@@ -152,7 +148,8 @@ function updateSubscription(req, res) {
|
||||
}
|
||||
|
||||
function deleteSubscription(req, res) {
|
||||
const { name } = req.params;
|
||||
let { name } = req.params;
|
||||
name = decodeURIComponent(name);
|
||||
$.info(`删除订阅:${name}...`);
|
||||
// delete from subscriptions
|
||||
let allSubs = $.read(SUBS_KEY);
|
||||
|
||||
Reference in New Issue
Block a user