Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Wrap whole upload route in try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed May 20, 2020
1 parent 772ed1c commit 48a8b55
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions api/routes/image/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const imageSizes = [ 80, 160, 240, 320, 400, 480, "default" ];
router.use(requireAuth());

router.post("/", async (req, res) => {
const { file } = req.files;
let files = {};

const error = (message, data = {}) => {
res.status(415);

Expand All @@ -46,38 +47,38 @@ router.post("/", async (req, res) => {
});
};

if (!file) {
return error("No file provided");
}
try {
const { file } = req.files;

const ext = extensionMap[file.mimetype];
if (!file) {
return error("No file provided");
}

if (!ext) {
return error(`Invalid file type. Only ${ Object.values(extensionMap).map((ext) => `.${ ext }`).join(", ") } supported.`, {
mimeType: file.mimetype,
});
}
const ext = extensionMap[file.mimetype];

const id = `image-${ i++ }`;
const dir = joinPath(process.cwd(), "uploads", id);
if (!ext) {
return error(`Invalid file type. Only ${ Object.values(extensionMap).map((ext) => `.${ ext }`).join(", ") } supported.`, {
mimeType: file.mimetype,
});
}

await mkdir(dir, { recursive: true });
const id = `image-${ i++ }`;
const dir = joinPath(process.cwd(), "uploads", id);

const fileUploadName =
(name) =>
Number.isInteger(name)
? `w${ name }.${ ext }`
: `${ name }.${ ext }`
;
await mkdir(dir, { recursive: true });

const fileUploadPath =
(name) =>
joinPath(dir, fileUploadName(name))
;
const fileUploadName =
(name) =>
Number.isInteger(name)
? `w${ name }.${ ext }`
: `${ name }.${ ext }`
;

let files = {};
const fileUploadPath =
(name) =>
joinPath(dir, fileUploadName(name))
;

try {
if ("gif" === ext) {
const filePath = fileUploadPath("default");

Expand Down

0 comments on commit 48a8b55

Please sign in to comment.