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