UNPKG

@nzz/q-server

Version:

__Q__ is a system that lets journalists create visual elements for stories. It is developed by [NZZ Storytelling](https://www.nzz.ch/storytelling) and used in the [NZZ](https://www.nzz.ch) newsroom.

62 lines (55 loc) 1.37 kB
const Boom = require('boom'); const Joi = require('joi'); const uuidv4 = require('uuid/v4'); function getKey(options, id) { let filename = ''; if (options.filename && options.filename.prepend) { filename += options.filename.prepend; } filename += uuidv4(); } function getRoutes(s3, options) { return [ { path: '/assets/upload/{id?}', method: 'POST', config: { validate: { auth: 'q-auth', params: { id: Joi.string().optional() }, payload: { output: 'stream', parse: true, allow: 'multipart/form-data', maxBytes: options.maxBytes || 10485760 // 10MB } }, tags: ['api'] }, handler: (request, reply) => { const file = request.payload.file; if (!file) { return reply(Boom.badData('Failed to read file')); } const key = getKey(options, request.params.id); s3.upload({ Bucket: options.s3.bucket, Key: key, Body: file, ContentType: contentType }, (err, data) => { if (err) { request.log(['error', 'upload'], err); return reply(err); } return reply(data); }); } } ]; } module.exports = { getRoutes: getRoutes }