-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add multipart file upload #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuchem2
wants to merge
4
commits into
develop
Choose a base branch
from
CARGO-416
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e218af1
feat: add source decorator to bind multipart/form-data files
yuchem2 cda267c
feat: add multipart file upload example
yuchem2 433c105
refactor: rename @File/@Files to @UploadedFile/@UploadedFiles
yuchem2 2f9e89a
feat: reject @Transform on uploaded file fileds
yuchem2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| import express, { Router } from 'express' | ||
| import multer from 'multer' | ||
| import { bindingCargo, getCargo, Body, UploadedFile, UploadedFiles } from 'express-cargo' | ||
|
|
||
| const router: Router = express.Router() | ||
|
|
||
| const upload = multer({ storage: multer.memoryStorage() }) | ||
|
|
||
| function summarize(file?: Express.Multer.File) { | ||
| if (!file) return null | ||
| return { | ||
| fieldname: file.fieldname, | ||
| originalname: file.originalname, | ||
| mimetype: file.mimetype, | ||
| size: file.size, | ||
| } | ||
| } | ||
|
|
||
| class UploadAvatarRequest { | ||
| @Body('username') | ||
| username!: string | ||
|
|
||
| @UploadedFile() | ||
| avatar!: Express.Multer.File | ||
| } | ||
|
|
||
| router.post('/upload/avatar', upload.single('avatar'), bindingCargo(UploadAvatarRequest), (req, res) => { | ||
| const cargo = getCargo<UploadAvatarRequest>(req) | ||
| res.json({ username: cargo.username, avatar: summarize(cargo.avatar) }) | ||
| }) | ||
|
|
||
| class UploadGalleryRequest { | ||
| @UploadedFiles('photos') | ||
| photos!: Express.Multer.File[] | ||
| } | ||
|
|
||
| router.post('/upload/gallery', upload.array('photos'), bindingCargo(UploadGalleryRequest), (req, res) => { | ||
| const cargo = getCargo<UploadGalleryRequest>(req) | ||
| res.json({ count: cargo.photos.length, photos: cargo.photos.map(summarize) }) | ||
| }) | ||
|
|
||
| class UploadProfileRequest { | ||
| @Body('bio') | ||
| bio!: string | ||
|
|
||
| @UploadedFile('avatar') | ||
| avatar!: Express.Multer.File | ||
|
|
||
| @UploadedFiles('gallery') | ||
| gallery!: Express.Multer.File[] | ||
| } | ||
|
|
||
| router.post( | ||
| '/upload/profile', | ||
| upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery' }]), | ||
| bindingCargo(UploadProfileRequest), | ||
| (req, res) => { | ||
| const cargo = getCargo<UploadProfileRequest>(req) | ||
| res.json({ | ||
| bio: cargo.bio, | ||
| avatar: summarize(cargo.avatar), | ||
| gallery: cargo.gallery.map(summarize), | ||
| }) | ||
| }, | ||
| ) | ||
|
|
||
| export default router |
This file contains hidden or 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 hidden or 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 { Request } from 'express' | ||
|
|
||
| /** An uploaded file as produced by a multipart parser (e.g. multer's `Express.Multer.File`). Bound as-is. */ | ||
| export type CargoFile = any | ||
|
|
||
| /** | ||
| * Locates uploaded files on the request, keyed by form field name. | ||
| * Only the location is parser-specific; the file objects are returned untouched. | ||
| */ | ||
| export type FileLocator = (req: Request) => Record<string, CargoFile[]> | ||
|
|
||
| /** Default locator for multer's `req.file` (single) and `req.files` (array or fielded object). */ | ||
| const multerFileLocator: FileLocator = (req: Request) => { | ||
| const map: Record<string, CargoFile[]> = {} | ||
| const push = (name: string, file: CargoFile) => { | ||
| ;(map[name] ??= []).push(file) | ||
| } | ||
|
|
||
| const single = (req as any).file | ||
| if (single) push(single.fieldname, single) | ||
|
|
||
| // upload.array()/any() → File[]; upload.fields() → { field: File[] } | ||
| const files = (req as any).files | ||
| if (Array.isArray(files)) { | ||
| for (const file of files) push(file.fieldname, file) | ||
| } else if (files) { | ||
| for (const name of Object.keys(files)) { | ||
| map[name] = files[name] | ||
| } | ||
| } | ||
|
yuchem2 marked this conversation as resolved.
|
||
|
|
||
| return map | ||
| } | ||
|
|
||
| let globalFileLocator: FileLocator = multerFileLocator | ||
|
|
||
| /** | ||
| * Overrides the global file locator used during binding. | ||
| * Only needed for parsers whose request shape differs from multer's. | ||
| * @param locator - Returns uploaded files keyed by field name. | ||
| */ | ||
| export function setCargoFileLocator(locator: FileLocator): void { | ||
| globalFileLocator = locator | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the currently configured global file locator. | ||
| * @returns The current file locator (defaults to multer support). | ||
| */ | ||
| export function getCargoFileLocator(): FileLocator { | ||
| return globalFileLocator | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.