Skip to content

Commit

Permalink
chore(website): Added an endpoint for uploading files that acts like …
Browse files Browse the repository at this point in the history
…a /dev/null

Hopefully...
  • Loading branch information
mlaursen committed Jul 16, 2021
1 parent 3a6ab33 commit 9663ae8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"classnames": "^2.3.1",
"codesandbox": "^2.2.3",
"date-fns": "^2.22.1",
"formidable": "^1.2.2",
"fuse.js": "6.4.6",
"js-cookie": "^2.2.1",
"lodash": "^4.17.21",
Expand All @@ -75,6 +76,7 @@
"devDependencies": {
"@babel/core": "^7.14.6",
"@react-md/dev-utils": "^2.8.5",
"@types/formidable": "^1.2.2",
"@types/gtag.js": "^0.0.6",
"@types/js-cookie": "^2.2.6",
"@types/marked": "^2.0.3",
Expand Down
4 changes: 4 additions & 0 deletions packages/documentation/src/constants/constraints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* 5 MB
*/
export const MAX_UPLOAD_SIZE = 5 * 1024 * 1024;
68 changes: 68 additions & 0 deletions packages/documentation/src/pages/api/devnull.ts
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();
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,13 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/formidable@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/formidable/-/formidable-1.2.2.tgz#e690d60732ee9d3f0a441bc572c17409785b283c"
integrity sha512-8RDAMnMHOh7QrY1xuQ7s6/Xre9pMvJ2zT2VgATiz5cIE71Q/6N3+P8sr3z/dNWNmvX5/aX9x8uJlG0MZiMZXoA==
dependencies:
"@types/node" "*"

"@types/fs-extra@^9.0.11":
version "9.0.11"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.11.tgz#8cc99e103499eab9f347dbc6ca4e99fb8d2c2b87"
Expand Down Expand Up @@ -6331,6 +6338,11 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

formidable@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.2.2.tgz#bf69aea2972982675f00865342b982986f6b8dd9"
integrity sha512-V8gLm+41I/8kguQ4/o1D3RIHRmhYFG4pnNyonvua+40rqcEmT4+V71yaZ3B457xbbgCsCfjSPi65u/W6vK1U5Q==

fragment-cache@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19"
Expand Down

0 comments on commit 9663ae8

Please sign in to comment.