Skip to content

Commit

Permalink
feat: add handle multi upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Feb 7, 2021
1 parent 39eb6f8 commit 992c438
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/helpers/Multer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@ function pickSingleFieldMulter(req: Request, fields: string[]) {
)
}

/**
*
* @param req
* @param fields
*/
function pickMultiFieldMulter(req: Request, fields: string[]) {
return pickBy(
fields.reduce<any>((acc, field) => {
acc[field] = req.getMultiArrayFile(field)
return acc
}, {}),
(value) => {
return value !== undefined
}
)
}

const Multers = {
pickSingleFieldMulter,
pickMultiFieldMulter,
}

export default Multers
15 changes: 14 additions & 1 deletion src/helpers/withState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class withState {
this.req.getCookies = this.getCookies.bind(this)
this.req.getBody = this.getBody.bind(this)
this.req.setBody = this.setBody.bind(this)
this.req.getSingleArrayFile = this.getSingleArrayFile.bind(this)
this.req.getTransaction = this.getTransaction.bind(this)
this.req.rollbackTransactions = this.rollbackTransactions.bind(this)
this.req.getSingleArrayFile = this.getSingleArrayFile.bind(this)
this.req.pickSingleFieldMulter = this.pickSingleFieldMulter.bind(this)
this.req.getMultiArrayFile = this.getMultiArrayFile.bind(this)
this.req.pickMultiFieldMulter = this.pickMultiFieldMulter.bind(this)
this.req._transaction = {}
}

Expand Down Expand Up @@ -79,6 +81,13 @@ class withState {
}
}

getMultiArrayFile(name: string) {
const data = get(this.req.files, name, []) as Express.Multer.File
if (data) {
return data
}
}

async getTransaction(id?: any): Promise<Transaction> {
id = id === undefined ? 1 : id
const txn = this.req._transaction[id]
Expand Down Expand Up @@ -106,6 +115,10 @@ class withState {
pickSingleFieldMulter(fields: string[]) {
return Multers.pickSingleFieldMulter(this.req, fields)
}

pickMultiFieldMulter(fields: string[]) {
return Multers.pickMultiFieldMulter(this.req, fields)
}
}

export default withState

0 comments on commit 992c438

Please sign in to comment.