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');
|
|
|
|
|
2018-10-17 09:32:21 -04: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 {};
|
|
|
|
}
|
2018-08-21 08:53:17 -04:00
|
|
|
throw new Error('no file with key of [file] found in request');
|
|
|
|
}
|
|
|
|
if (!file.path) {
|
|
|
|
throw new Error('no file path found');
|
|
|
|
}
|
|
|
|
if (!file.type) {
|
|
|
|
throw new Error('no file type found');
|
|
|
|
}
|
|
|
|
if (!file.size) {
|
|
|
|
throw new Error('no file size found');
|
|
|
|
}
|
|
|
|
// validate the file name
|
|
|
|
if (!file.name) {
|
|
|
|
throw new Error('no file name found');
|
|
|
|
}
|
|
|
|
if (file.name.indexOf('.') < 0) {
|
|
|
|
throw new Error('no file extension found in file name');
|
|
|
|
}
|
|
|
|
if (file.name.indexOf('.') === 0) {
|
|
|
|
throw new Error('file name cannot start with "."');
|
|
|
|
}
|
|
|
|
if (/'/.test(file.name)) {
|
|
|
|
throw new Error('apostrophes are not allowed in the file name');
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the file
|
2018-09-24 16:13:18 -04:00
|
|
|
if (file) validateFileTypeAndSize(file);
|
2018-08-21 08:53:17 -04:00
|
|
|
// return results
|
2018-10-17 09:32:21 -04:00
|
|
|
const obj = {
|
2018-08-21 08:53:17 -04:00
|
|
|
fileName : file.name,
|
|
|
|
filePath : file.path,
|
|
|
|
fileExtension: path.extname(file.path),
|
|
|
|
fileType : file.type,
|
|
|
|
};
|
|
|
|
|
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;
|