Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include file name in response, and support "Accept" header that contain charset property #415

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions src/modules/sub/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,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:
Expand Down