Improve file handling

master
Joe Biellik 4 years ago
parent db780c024d
commit ccd7798996

@ -41,12 +41,12 @@ module.exports = {
async create(ctx) { async create(ctx) {
ctx.set('Cache-Control', 'no-cache'); ctx.set('Cache-Control', 'no-cache');
// Request body // File body
if (!ctx.request.body.paste && ctx.request.files) { if (ctx.request.files && Object.keys(ctx.request.files).length > 0) {
try { // Request body, xxx
// Request body, xxx let path = Object.values(ctx.request.files)[0].path;
let path = Object.values(ctx.request.files)[0].path;
try {
// Request body, paste=xxx // Request body, paste=xxx
if (ctx.request.files.paste) { if (ctx.request.files.paste) {
path = ctx.request.files.paste.path; path = ctx.request.files.paste.path;
@ -55,30 +55,21 @@ module.exports = {
const data = await fs.readFile(path); const data = await fs.readFile(path);
const stat = await fs.lstat(path); const stat = await fs.lstat(path);
if (isBinaryFile(data, stat.size)) ctx.throw(); if (await isBinaryFile(data, stat.size)) ctx.throw('Binary file');
ctx.request.body.paste = data;
ctx.request.body.paste = data.toString();
} catch {
ctx.throw(400, 'Bad Paste Body');
} finally {
try { try {
await fs.unlink(path); await fs.unlink(path);
} catch { } catch {
// Ignore // Ignore
} }
} catch {
ctx.throw(400, 'Bad Paste Body');
} }
} }
// Expiry multiplier // Raw body
try {
if (ctx.request.body.expire && ctx.request.body.multiplier) {
ctx.request.body.expire *= ctx.request.body.multiplier;
}
} catch {
ctx.throw(400, 'Bad Paste Expiry');
}
// Raw request body
if (typeof ctx.request.body === 'string') { if (typeof ctx.request.body === 'string') {
ctx.request.body = { ctx.request.body = {
paste: ctx.request.body paste: ctx.request.body
@ -89,6 +80,15 @@ module.exports = {
ctx.throw(400, 'No Paste Provided'); ctx.throw(400, 'No Paste Provided');
} }
// Expiry multiplier
try {
if (ctx.request.body.expire && ctx.request.body.multiplier) {
ctx.request.body.expire *= ctx.request.body.multiplier;
}
} catch {
ctx.throw(400, 'Bad Paste Expiry');
}
// /?expire=xxx // /?expire=xxx
if (!ctx.request.body.expire && ctx.query.expire) { if (!ctx.request.body.expire && ctx.query.expire) {
ctx.request.body.expire = ctx.query.expire; ctx.request.body.expire = ctx.query.expire;

Loading…
Cancel
Save