mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-14 21:57:55 +08:00
dce872fac8
- Bunyan logs json output, and added a stream to send our logs to cloudwatch - Replaces /all/ instances of console.log. Turned eslint rule back on, so we don't use console.log ever again. - Added npm scripts to view pretty logs
126 lines
3.1 KiB
JavaScript
126 lines
3.1 KiB
JavaScript
const Joi = require('joi');
|
|
const Serialization = require('../serialization');
|
|
|
|
|
|
module.exports = (server) => {
|
|
server.route({
|
|
method: 'GET',
|
|
path: '/files',
|
|
config: {
|
|
description: 'Returns an array of file metadata.',
|
|
notes: 'Notes go here',
|
|
tags: ['files'],
|
|
validate: {
|
|
query: {
|
|
filename: Joi.string(),
|
|
message_id: Joi.number().integer().min(0),
|
|
content_type: Joi.string(),
|
|
limit: Joi.number().integer().min(1).max(2000).default(100),
|
|
offset: Joi.number().integer().min(0).default(0),
|
|
},
|
|
},
|
|
response: {
|
|
schema: Joi.array().items(
|
|
Serialization.jsonSchema('File')
|
|
),
|
|
},
|
|
},
|
|
handler: (request, reply) => {
|
|
request.getAccountDatabase().then((db) => {
|
|
const {File} = db;
|
|
const query = request.query;
|
|
const where = {};
|
|
|
|
if (query.filename) {
|
|
where.filename = query.filename;
|
|
}
|
|
if (query.message_id) {
|
|
where.messageId = query.message_id;
|
|
}
|
|
if (query.content_type) {
|
|
where.contentType = query.content_type;
|
|
}
|
|
|
|
File.findAll({
|
|
where: where,
|
|
limit: request.query.limit,
|
|
offset: request.query.offset,
|
|
}).then((files) => {
|
|
reply(Serialization.jsonStringify(files));
|
|
})
|
|
})
|
|
},
|
|
});
|
|
|
|
server.route({
|
|
method: 'GET',
|
|
path: '/files/{id}',
|
|
config: {
|
|
description: 'Returns file with specified id.',
|
|
notes: 'Notes go here',
|
|
tags: ['files'],
|
|
validate: {
|
|
params: {
|
|
id: Joi.string(),
|
|
},
|
|
},
|
|
response: {
|
|
schema: Joi.alternatives().try(
|
|
Serialization.jsonSchema('File'),
|
|
Joi.string()
|
|
),
|
|
},
|
|
},
|
|
handler: (request, reply) => {
|
|
request.getAccountDatabase().then(({File}) => {
|
|
const {params: {id}} = request
|
|
|
|
File.findOne({where: {id}}).then((file) => {
|
|
if (!file) {
|
|
return reply.notFound(`File ${id} not found`)
|
|
}
|
|
return reply(Serialization.jsonStringify(file));
|
|
})
|
|
.catch((err) => {
|
|
request.logger.error(err, 'Error fetching file')
|
|
reply(err)
|
|
})
|
|
})
|
|
},
|
|
})
|
|
|
|
server.route({
|
|
method: 'GET',
|
|
path: '/files/{id}/download',
|
|
config: {
|
|
description: 'Returns binary data for file with specified id.',
|
|
notes: 'Notes go here',
|
|
tags: ['files'],
|
|
validate: {
|
|
params: {
|
|
id: Joi.string(),
|
|
},
|
|
},
|
|
},
|
|
handler: (request, reply) => {
|
|
request.getAccountDatabase()
|
|
.then((db) => {
|
|
const {params: {id}} = request
|
|
const account = request.auth.credentials
|
|
|
|
db.File.findOne({where: {id}})
|
|
.then((file) => {
|
|
if (!file) {
|
|
return reply.notFound(`File ${id} not found`)
|
|
}
|
|
return file.fetch({account, db, logger: request.logger})
|
|
.then((stream) => reply(stream))
|
|
})
|
|
.catch((err) => {
|
|
request.logger.error(err, 'Error downloading file')
|
|
reply(err)
|
|
})
|
|
})
|
|
},
|
|
})
|
|
};
|