Skip to content

Commit

Permalink
fix: #9055, non-standard API response from addThumbs route
Browse files Browse the repository at this point in the history
Also removed old thumb upload router handler, and updated uploadPost handling in composer to match new response schema
  • Loading branch information
julianlam committed Dec 9, 2020
1 parent c09c238 commit 340387c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 51 deletions.
58 changes: 12 additions & 46 deletions public/openapi/read/post/upload.yaml
Expand Up @@ -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
status:
$ref: ../components/schemas/Status.yaml#/Status
response:
type: object
properties:
images:
type: array
items:
type: object
properties:
url:
type: string
8 changes: 5 additions & 3 deletions src/controllers/uploads.js
Expand Up @@ -11,14 +11,16 @@ 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) {
let files = req.files.files;

// 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];
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/write/topics.js
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/routes/api.js
Expand Up @@ -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);
};

0 comments on commit 340387c

Please sign in to comment.