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

62 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');
2018-10-17 15:32:21 +02: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 {};
}
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 22:13:18 +02:00
if (file) validateFileTypeAndSize(file);
// return results
2018-10-17 15:32:21 +02:00
const obj = {
fileName : file.name,
filePath : file.path,
fileExtension: path.extname(file.path),
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;