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');
|
|
|
|
|
|
|
|
const parsePublishApiRequestFiles = ({file, thumbnail}) => {
|
|
|
|
// make sure a file was provided
|
|
|
|
if (!file) {
|
|
|
|
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) {
|
2018-07-05 02:02:43 +02:00
|
|
|
throw new Error('no file size found');
|
2018-04-27 18:54:36 +02:00
|
|
|
}
|
|
|
|
// validate the file name
|
2018-07-05 02:02:43 +02:00
|
|
|
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 "."');
|
|
|
|
}
|
2018-04-27 18:54:36 +02:00
|
|
|
if (/'/.test(file.name)) {
|
|
|
|
throw new Error('apostrophes are not allowed in the file name');
|
|
|
|
}
|
|
|
|
// validate the file
|
|
|
|
validateFileTypeAndSize(file);
|
|
|
|
// return results
|
|
|
|
return {
|
|
|
|
fileName : file.name,
|
|
|
|
filePath : file.path,
|
2018-07-17 21:23:14 +02:00
|
|
|
fileExtension : path.extname(file.path),
|
2018-04-27 18:54:36 +02:00
|
|
|
fileType : file.type,
|
|
|
|
thumbnailFileName: (thumbnail ? thumbnail.name : null),
|
|
|
|
thumbnailFilePath: (thumbnail ? thumbnail.path : null),
|
|
|
|
thumbnailFileType: (thumbnail ? thumbnail.type : null),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = parsePublishApiRequestFiles;
|