mirror of
https://github.com/wukko/cobalt.git
synced 2025-01-12 11:52:12 +01:00
api: refactor /api/json POST handler
This commit is contained in:
parent
0a7cdfbbfe
commit
c10012130b
2 changed files with 36 additions and 30 deletions
|
@ -95,37 +95,42 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const acceptRegex = /^application\/json(; charset=utf-8)?$/;
|
||||||
app.post('/api/json', async (req, res) => {
|
app.post('/api/json', async (req, res) => {
|
||||||
|
const request = req.body;
|
||||||
|
const lang = languageCode(req);
|
||||||
|
const fail = (t) => {
|
||||||
|
const { status, body } = apiJSON(0, { t: loc(lang, t) });
|
||||||
|
res.status(status).json(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!acceptRegex.test(req.header('Content-Type'))) {
|
||||||
|
return fail('ErrorInvalidContentType');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request.url) {
|
||||||
|
return fail('ErrorNoLink');
|
||||||
|
}
|
||||||
|
|
||||||
|
request.dubLang = request.dubLang ? lang : false;
|
||||||
|
const normalizedRequest = checkJSONPost(request);
|
||||||
|
if (!normalizedRequest) {
|
||||||
|
return fail('ErrorCantProcess');
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsed = extract(normalizedRequest.url);
|
||||||
|
if (parsed === null) {
|
||||||
|
return fail('ErrorUnsupported');
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let lang = languageCode(req);
|
const result = await match(
|
||||||
let j = apiJSON(0, { t: "bad request" });
|
parsed.host, parsed.patternMatch, lang, normalizedRequest
|
||||||
try {
|
);
|
||||||
let contentCon = String(req.header('Content-Type')) === "application/json";
|
|
||||||
let request = req.body;
|
|
||||||
if (contentCon && request.url) {
|
|
||||||
request.dubLang = request.dubLang ? lang : false;
|
|
||||||
|
|
||||||
let chck = checkJSONPost(request);
|
res.status(result.status).json(result.body);
|
||||||
if (!chck) throw new Error();
|
} catch {
|
||||||
|
fail('ErrorSomethingWentWrong');
|
||||||
const parsed = extract(chck.url);
|
|
||||||
if (parsed === null) {
|
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) })
|
|
||||||
}
|
|
||||||
|
|
||||||
j = await match(parsed.host, parsed.patternMatch, lang, chck)
|
|
||||||
.catch(() => apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') }))
|
|
||||||
} else {
|
|
||||||
j = apiJSON(0, {
|
|
||||||
t: !contentCon ? "invalid content type header" : loc(lang, 'ErrorNoLink')
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
j = apiJSON(0, { t: loc(lang, 'ErrorCantProcess') });
|
|
||||||
}
|
|
||||||
return res.status(j.status).json(j.body);
|
|
||||||
} catch (e) {
|
|
||||||
return res.destroy();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -155,6 +155,7 @@
|
||||||
"SettingsTikTokH265Description": "download 1080p videos from tiktok in h265/hevc format when available.",
|
"SettingsTikTokH265Description": "download 1080p videos from tiktok in h265/hevc format when available.",
|
||||||
"SettingsYoutubeDub": "use browser language",
|
"SettingsYoutubeDub": "use browser language",
|
||||||
"SettingsYoutubeDubDescription": "uses your browser's default language for youtube dubbed audio tracks. works even if cobalt ui isn't translated to your language.",
|
"SettingsYoutubeDubDescription": "uses your browser's default language for youtube dubbed audio tracks. works even if cobalt ui isn't translated to your language.",
|
||||||
"UpdateIstream": "better service support and ux"
|
"UpdateIstream": "better service support and ux",
|
||||||
|
"ErrorInvalidContentType": "invalid content type header"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue