Compare commits

...

3 commits

Author SHA1 Message Date
Johan le stickman 276780bc82
Merge 25f2a015c5 into 709d14ee9e 2024-04-30 00:56:20 +01:00
Johan le stickman 25f2a015c5 Add .mp3 extension to audio without extension, include filename in response 2024-04-01 20:15:56 +02:00
Johan le stickman b1adbd659e Accept header 'application/json' with utf8 charset 2024-04-01 20:13:08 +02:00
2 changed files with 14 additions and 5 deletions

View file

@ -67,7 +67,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
app.use('/api/json', express.json({
verify: (req, res, buf) => {
let acceptCon = String(req.header('Accept')) === "application/json";
let acceptCon = String(req.header('Accept')) === "application/json" || String(req.header('Accept')) === "application/json; charset=utf-8";
if (acceptCon) {
if (buf.length > 720) throw new Error();
JSON.parse(buf);
@ -80,7 +80,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
// handle express.json errors properly (https://github.com/expressjs/express/issues/4065)
app.use('/api/json', (err, req, res, next) => {
let errorText = "invalid json body";
let acceptCon = String(req.header('Accept')) !== "application/json";
let acceptCon = String(req.header('Accept')) !== "application/json" && String(req.header('Accept')) !== "application/json; charset=utf-8";
if (err || acceptCon) {
if (acceptCon) errorText = "invalid accept header";
@ -98,7 +98,7 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
let lang = languageCode(req);
let j = apiJSON(0, { t: "bad request" });
try {
let contentCon = String(req.header('Content-Type')) === "application/json";
let contentCon = String(req.header('Accept')) === "application/json" || String(req.header('Accept')) === "application/json; charset=utf-8";
let request = req.body;
if (contentCon && request.url) {
request.dubLang = request.dubLang ? lang : false;

View file

@ -23,13 +23,22 @@ const forbiddenCharsString = ['}', '{', '%', '>', '<', '^', ';', '`', '$', '"',
export function apiJSON(type, obj) {
try {
try {
var filename = obj.filename;
if (obj.isAudioOnly == true && typeof filename == 'string' && !filename.endsWith(".mp3") && !filename.endsWith(".ogg") && !filename.endsWith(".wav") && !filename.endsWith(".opus")) {
obj.filename = filename + '.mp3';
}
} catch (e) {
return { status: 500, body: { status: "error", text: "Internal Server Error", critical: true } };
}
switch (type) {
case 0:
return { status: 400, body: { status: "error", text: obj.t } };
case 1:
return { status: 200, body: { status: "redirect", url: obj.u } };
return { status: 200, body: { status: "redirect", url: obj.u, filename: obj.filename } };
case 2:
return { status: 200, body: { status: "stream", url: createStream(obj) } };
return { status: 200, body: { status: "stream", url: createStream(obj), filename: obj.filename } };
case 3:
return { status: 200, body: { status: "success", text: obj.t } };
case 4: