From 340387c18a273bec333e24c7fda063f4a10e5b0f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 4 Dec 2020 14:32:57 -0500 Subject: [PATCH] fix: #9055, non-standard API response from addThumbs route Also removed old thumb upload router handler, and updated uploadPost handling in composer to match new response schema --- public/openapi/read/post/upload.yaml | 58 ++++++---------------------- src/controllers/uploads.js | 8 ++-- src/controllers/write/topics.js | 2 +- src/routes/api.js | 1 - 4 files changed, 18 insertions(+), 51 deletions(-) diff --git a/public/openapi/read/post/upload.yaml b/public/openapi/read/post/upload.yaml index 877160d6ad..5ffdc7afbb 100644 --- a/public/openapi/read/post/upload.yaml +++ b/public/openapi/read/post/upload.yaml @@ -8,52 +8,18 @@ post: description: "" content: application/json: - schema: - type: array - items: - type: object - properties: - name: - type: string - url: - type: string - text/plain: - schema: - type: array - items: - type: object - properties: - name: - type: string - url: - type: string - "403": - description: "" - content: - application/json: - schema: - type: string - example: Forbidden - text/plain: - schema: - type: string - example: Forbidden - "500": - description: "" - content: - application/json: - schema: - type: object - properties: - path: - type: string - error: - type: string - text/plain: schema: type: object properties: - path: - type: string - error: - type: string \ No newline at end of file + status: + $ref: ../components/schemas/Status.yaml#/Status + response: + type: object + properties: + images: + type: array + items: + type: object + properties: + url: + type: string \ No newline at end of file diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index e4dd6f73e3..be1ff5529e 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -11,6 +11,8 @@ const plugins = require('../plugins'); const image = require('../image'); const privileges = require('../privileges'); +const helpers = require('./helpers'); + const uploadsController = module.exports; uploadsController.upload = async function (req, res, filesIterator) { @@ -18,7 +20,7 @@ uploadsController.upload = async function (req, res, filesIterator) { // These checks added because of odd behaviour by request: https://github.com/request/request/issues/2445 if (!Array.isArray(files)) { - return res.status(500).json('invalid files'); + return helpers.formatApiResponse(500, res, new Error('[[error:invalid-file]]')); } if (Array.isArray(files[0])) { files = files[0]; @@ -30,10 +32,10 @@ uploadsController.upload = async function (req, res, filesIterator) { /* eslint-disable no-await-in-loop */ images.push(await filesIterator(fileObj)); } - res.status(200).json(images); + helpers.formatApiResponse(200, res, { images }); return images; } catch (err) { - res.status(500).json({ path: req.path, error: err.message }); + return helpers.formatApiResponse(500, res, err); } finally { deleteTempFiles(files); } diff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.js index 7c08574699..53cfbcc037 100644 --- a/src/controllers/write/topics.js +++ b/src/controllers/write/topics.js @@ -100,7 +100,7 @@ Topics.addThumb = async (req, res, next) => { return; } - const files = await uploadsController.uploadThumb(req, res, next); // response is handled here, fix this? + const files = await uploadsController.uploadThumb(req, res, next); // response is handled here // Add uploaded files to topic zset if (files && files.length) { diff --git a/src/routes/api.js b/src/routes/api.js index ba6e9b31e3..3280246626 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -33,7 +33,6 @@ module.exports = function (app, middleware, controllers) { var multipartMiddleware = multipart(); var middlewares = [middleware.maintenanceMode, multipartMiddleware, middleware.validateFiles, middleware.applyCSRF]; router.post('/post/upload', middlewares, uploadsController.uploadPost); - router.post('/topic/thumb/upload', middlewares, uploadsController.uploadThumb); router.post('/user/:userslug/uploadpicture', middlewares.concat([middleware.exposeUid, middleware.authenticate, middleware.canViewUsers, middleware.checkAccountPermissions]), controllers.accounts.edit.uploadPicture); };