file_uploads.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const multer = require('multer')
  20. // Taken from express-openapi examples
  21. module.exports = function (req, res, next) {
  22. multer().any()(req, res, function (err) {
  23. if (err) {
  24. return next(err)
  25. }
  26. // Handle both single and multiple files
  27. const filesMap = req.files.reduce(
  28. (acc, f) =>
  29. Object.assign(acc, {
  30. [f.fieldname]: (acc[f.fieldname] || []).concat(f),
  31. }),
  32. {}
  33. )
  34. Object.keys(filesMap).forEach((fieldname) => {
  35. const files = filesMap[fieldname]
  36. req.body[fieldname] = files.length > 1 ? files.map(() => '') : ''
  37. })
  38. return next()
  39. })
  40. }