spee.ch/server/controllers/api/claim/publish/parsePublishApiRequestFiles.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

const path = require('path');
2018-04-27 18:54:36 +02:00
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
const validateFileForPublish = require('./validateFileForPublish.js');
2018-04-27 18:54:36 +02:00
2019-01-16 00:19:19 +01:00
const parsePublishApiRequestFiles = ({ file, thumbnail }, isUpdate) => {
2018-04-27 18:54:36 +02:00
// make sure a file was provided
2018-10-23 00:16:38 +02:00
if (!file) {
if (isUpdate) {
if (thumbnail) {
const obj = {};
obj.thumbnailFileName = thumbnail.name;
obj.thumbnailFilePath = thumbnail.path;
obj.thumbnailFileType = thumbnail.type;
return obj;
}
return {};
}
2019-01-16 00:19:19 +01:00
throw new Error('No file with key of [file] found in request');
}
if (!file.path) {
2019-01-16 00:19:19 +01:00
throw new Error('No file path found');
}
if (!file.type) {
2019-01-16 00:19:19 +01:00
throw new Error('No file type found');
}
if (!file.size) {
2019-01-16 00:19:19 +01:00
throw new Error('No file size found');
}
// validate the file name
if (!file.name) {
2019-01-16 00:19:19 +01:00
throw new Error('No file name found');
}
if (file.name.indexOf('.') < 0) {
2019-01-16 00:19:19 +01:00
throw new Error('No file extension found in file name');
}
if (file.name.indexOf('.') === 0) {
2019-01-16 00:19:19 +01:00
throw new Error('File name cannot start with "."');
}
if (/'/.test(file.name)) {
2019-01-16 00:19:19 +01:00
throw new Error('Apostrophes are not allowed in the file name');
}
// validate the file
if (file) validateFileForPublish(file);
// return results
2018-10-17 15:32:21 +02:00
const obj = {
2019-01-16 00:19:19 +01:00
fileName: file.name,
filePath: file.path,
fileExtension: path.extname(file.path),
2019-01-16 00:19:19 +01:00
fileType: file.type,
};
2018-10-17 15:32:21 +02:00
if (thumbnail) {
obj.thumbnailFileName = thumbnail.name;
obj.thumbnailFilePath = thumbnail.path;
obj.thumbnailFileType = thumbnail.type;
}
2018-10-17 15:32:21 +02:00
return obj;
};
2018-10-17 15:32:21 +02:00
module.exports = parsePublishApiRequestFiles;