2018-07-17 21:23:14 +02:00
|
|
|
const path = require('path');
|
2018-04-27 18:54:36 +02:00
|
|
|
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
|
2019-02-06 18:56:28 +01:00
|
|
|
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');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (!file.path) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('No file path found');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (!file.type) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('No file type found');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (!file.size) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('No file size found');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
// validate the file name
|
|
|
|
if (!file.name) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('No file name found');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (file.name.indexOf('.') < 0) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('No file extension found in file name');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (file.name.indexOf('.') === 0) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('File name cannot start with "."');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
if (/'/.test(file.name)) {
|
2019-01-16 00:19:19 +01:00
|
|
|
throw new Error('Apostrophes are not allowed in the file name');
|
2018-08-21 14:53:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate the file
|
2019-02-06 18:56:28 +01:00
|
|
|
if (file) validateFileForPublish(file);
|
2018-08-21 14:53:17 +02:00
|
|
|
// 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,
|
2018-08-21 14:53:17 +02:00
|
|
|
fileExtension: path.extname(file.path),
|
2019-01-16 00:19:19 +01:00
|
|
|
fileType: file.type,
|
2018-08-21 14:53:17 +02:00
|
|
|
};
|
|
|
|
|
2018-10-17 15:32:21 +02:00
|
|
|
if (thumbnail) {
|
|
|
|
obj.thumbnailFileName = thumbnail.name;
|
|
|
|
obj.thumbnailFilePath = thumbnail.path;
|
|
|
|
obj.thumbnailFileType = thumbnail.type;
|
|
|
|
}
|
2018-08-21 14:53:17 +02:00
|
|
|
|
2018-10-17 15:32:21 +02:00
|
|
|
return obj;
|
2018-08-21 14:53:17 +02:00
|
|
|
};
|
2018-10-17 15:32:21 +02:00
|
|
|
|
|
|
|
module.exports = parsePublishApiRequestFiles;
|