(req, res, next)
| 25 | ); |
| 26 | |
| 27 | const schemaMultipartHandler: RequestHandler = (req, res, next) => { |
| 28 | if (req.is('application/json')) { |
| 29 | if (Object.keys(req.body).length === 0) { |
| 30 | throw new InvalidPayloadError({ reason: `No data was included in the body` }); |
| 31 | } |
| 32 | |
| 33 | res.locals['upload'] = req.body; |
| 34 | return next(); |
| 35 | } |
| 36 | |
| 37 | if (!req.is('multipart/form-data')) { |
| 38 | throw new UnsupportedMediaTypeError({ mediaType: req.headers['content-type']!, where: 'Content-Type header' }); |
| 39 | } |
| 40 | |
| 41 | const headers = req.headers['content-type'] |
| 42 | ? req.headers |
| 43 | : { |
| 44 | ...req.headers, |
| 45 | 'content-type': 'application/octet-stream', |
| 46 | }; |
| 47 | |
| 48 | const busboy = Busboy({ headers }); |
| 49 | |
| 50 | let isFileIncluded = false; |
| 51 | let upload: any | null = null; |
| 52 | |
| 53 | busboy.on('file', async (_, fileStream, { mimeType }) => { |
| 54 | const logger = useLogger(); |
| 55 | |
| 56 | if (isFileIncluded) return next(new InvalidPayloadError({ reason: `More than one file was included in the body` })); |
| 57 | |
| 58 | isFileIncluded = true; |
| 59 | |
| 60 | const { readableStreamToString } = await import('@directus/utils/node'); |
| 61 | |
| 62 | try { |
| 63 | const uploadedString = await readableStreamToString(fileStream); |
| 64 | |
| 65 | if (mimeType === 'application/json') { |
| 66 | try { |
| 67 | upload = parseJSON(uploadedString); |
| 68 | } catch (err: any) { |
| 69 | logger.warn(err); |
| 70 | throw new InvalidPayloadError({ reason: 'The provided JSON is invalid' }); |
| 71 | } |
| 72 | } else { |
| 73 | try { |
| 74 | upload = await loadYaml(uploadedString); |
| 75 | } catch (err: any) { |
| 76 | logger.warn(err); |
| 77 | throw new InvalidPayloadError({ reason: 'The provided YAML is invalid' }); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (!upload) { |
| 82 | throw new InvalidPayloadError({ reason: `No file was included in the body` }); |
| 83 | } |
| 84 |
nothing calls this directly
no test coverage detected