-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e1ee08
commit 187da28
Showing
11 changed files
with
197 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
7 changes: 7 additions & 0 deletions
7
src/backend/lib/request/validations/implementations/raw-request.ts
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,7 @@ | ||
import { ValidationImplType } from "./types"; | ||
|
||
export const rawRequestValidationImpl: ValidationImplType< | ||
Record<string, unknown> | ||
> = async (req) => { | ||
return req as unknown as Record<string, unknown>; | ||
}; |
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,21 @@ | ||
export const FORMIDABLE_ERRORS = { | ||
aborted: 1002, | ||
biggerThanMaxFileSize: 1016, | ||
biggerThanTotalMaxFileSize: 1009, | ||
cannotCreateDir: 1018, | ||
filenameNotString: 1005, | ||
malformedMultipart: 1012, | ||
maxFieldsExceeded: 1007, | ||
maxFieldsSizeExceeded: 1006, | ||
maxFilesExceeded: 1015, | ||
missingContentType: 1011, | ||
missingMultipartBoundary: 1013, | ||
missingPlugin: 1000, | ||
noEmptyFiles: 1010, | ||
noParser: 1003, | ||
pluginFailed: 1017, | ||
pluginFunction: 1001, | ||
smallerThanMinFileSize: 1008, | ||
uninitializedParser: 1004, | ||
unknownTransferEncoding: 1014, | ||
}; |
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,52 @@ | ||
import formidable from "formidable"; | ||
import { mkdir, stat } from "fs/promises"; | ||
import { join } from "path"; | ||
import { sluggify } from "shared/lib/strings"; | ||
import { format } from "date-fns"; | ||
import { NextApiRequest } from "next"; | ||
import { nanoid } from "nanoid"; | ||
|
||
export async function parseForm( | ||
req: NextApiRequest | ||
): Promise<{ fields: formidable.Fields; files: formidable.Files }> { | ||
const UPLOAD_CONFIG = { | ||
entity: sluggify("posts"), | ||
maxFileSize: 1024 * 1024 * 5, | ||
fileType: "image", | ||
rootDir: process.cwd(), | ||
}; | ||
|
||
const uploadDir = join( | ||
UPLOAD_CONFIG.rootDir || process.cwd(), | ||
`/uploads/${UPLOAD_CONFIG.entity}/${format(Date.now(), "dd-MM-Y")}` | ||
); | ||
|
||
try { | ||
await stat(uploadDir); | ||
} catch (e: any) { | ||
if (e.code === "ENOENT") { | ||
await mkdir(uploadDir, { recursive: true }); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
const form = formidable({ | ||
maxFiles: 1, | ||
maxFileSize: UPLOAD_CONFIG.maxFileSize, | ||
uploadDir, | ||
filename: (_name, _ext, part) => { | ||
return `${nanoid()}.${part.originalFilename.split(".").pop()}`; | ||
}, | ||
filter: (part) => { | ||
return ( | ||
part.name === "file" && | ||
(part.mimetype?.includes(UPLOAD_CONFIG.fileType) || false) | ||
); | ||
}, | ||
}); | ||
|
||
const [fields, files] = await form.parse(req); | ||
|
||
return { fields, files }; | ||
} |
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
Oops, something went wrong.