Skip to content

Commit

Permalink
Allow multiple file upload (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 committed Dec 11, 2019
1 parent a80ddab commit fc8af99
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ValidationError,
} from '../framework/types';
import { MulterError } from 'multer';

const multer = require('multer');

export function multipart(
Expand All @@ -32,13 +33,31 @@ export function multipart(
//
// This is a bit complex because the schema may be defined inline (easy) or via a $ref (complex) in which
// case we must follow the $ref to check the type.

if (req.files) {
// add files to body
(<Express.Multer.File[]>req.files).forEach(
(f: Express.Multer.File) => {
req.body[f.fieldname] = '';
},
);

// to handle single and multiple file upload at the same time, let us this initialize this count variable
// for example { "files": 5 }
const count_by_fieldname = (<Express.Multer.File[]>req.files)
.map(file => file.fieldname)
.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});

// add file(s) to body
Object
.entries(count_by_fieldname)
.forEach(
([fieldname, count]: [string, number]) => {
// TODO maybe also check in the api doc if it is a single upload or multiple
const is_multiple = count > 1;
req.body[fieldname] = (is_multiple)
? new Array(count).fill('')
: '';
},
);

}
next();
}
Expand Down

0 comments on commit fc8af99

Please sign in to comment.