-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(website): Added an endpoint for uploading files that acts like …
…a /dev/null Hopefully...
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* 5 MB | ||
*/ | ||
export const MAX_UPLOAD_SIZE = 5 * 1024 * 1024; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { promises as fs } from "fs"; | ||
import { IncomingForm, File } from "formidable"; | ||
|
||
import { MAX_UPLOAD_SIZE } from "constants/constraints"; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
function parse(req: NextApiRequest): Promise<readonly File[]> { | ||
return new Promise((resolve, reject) => { | ||
const form = new IncomingForm({ | ||
multiples: true, | ||
maxFields: 1, | ||
maxFileSize: MAX_UPLOAD_SIZE, | ||
}); | ||
form.parse(req, (error, _fields, files) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
const list = Object.values(files).reduce<File[]>((list, fileOrFiles) => { | ||
if ("path" in fileOrFiles) { | ||
list.push(fileOrFiles); | ||
} else { | ||
list.push(...fileOrFiles); | ||
} | ||
|
||
return list; | ||
}, []); | ||
resolve(list); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* This endpoint will upload files to a temp directory with formidable and then | ||
* immediately remove them just so that an upload progress demo can be used. | ||
*/ | ||
export default async function devnull( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
): Promise<void> { | ||
if (req.method?.toLowerCase() !== "post") { | ||
res.status(400).end(); | ||
return; | ||
} | ||
|
||
const files = await parse(req); | ||
const errors: NodeJS.ErrnoException[] = []; | ||
await Promise.all( | ||
files.map((file) => | ||
fs.unlink(file.path).catch((error) => { | ||
errors.push(error); | ||
}) | ||
) | ||
); | ||
|
||
// TODO: Actually do something with possible errors? | ||
if (errors.length) { | ||
res.status(500); | ||
} | ||
|
||
res.end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters