consolidate validation
This commit is contained in:
parent
2b02f06152
commit
f575edc9f3
3 changed files with 6 additions and 107 deletions
|
@ -1,53 +1,9 @@
|
|||
const path = require('path');
|
||||
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
|
||||
|
||||
const parsePublishApiRequestFiles = ({file, thumbnail}) => {
|
||||
const parsePublishApiRequestFiles = ({file}, isUpdate) => {
|
||||
// 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) {
|
||||
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
|
||||
validateFileTypeAndSize(file);
|
||||
// return results
|
||||
return {
|
||||
fileName : file.name,
|
||||
filePath : file.path,
|
||||
fileExtension : path.extname(file.path),
|
||||
fileType : file.type,
|
||||
thumbnailFileName: (thumbnail ? thumbnail.name : null),
|
||||
thumbnailFilePath: (thumbnail ? thumbnail.path : null),
|
||||
thumbnailFileType: (thumbnail ? thumbnail.type : null),
|
||||
};
|
||||
};
|
||||
|
||||
const parsePublishApiRequestFile = ({file, thumbnail}, isUpdate) => {
|
||||
// make sure a file was provided
|
||||
if (!file) {
|
||||
if (isUpdate) {
|
||||
return;
|
||||
}
|
||||
if (!file && !isUpdate) {
|
||||
throw new Error('no file with key of [file] found in request');
|
||||
}
|
||||
if (!file.path) {
|
||||
|
@ -74,7 +30,7 @@ const parsePublishApiRequestFile = ({file, thumbnail}, isUpdate) => {
|
|||
}
|
||||
|
||||
// validate the file
|
||||
validateFileTypeAndSize(file);
|
||||
if (file) validateFileTypeAndSize(file);
|
||||
// return results
|
||||
return {
|
||||
fileName : file.name,
|
||||
|
@ -84,7 +40,7 @@ const parsePublishApiRequestFile = ({file, thumbnail}, isUpdate) => {
|
|||
};
|
||||
};
|
||||
|
||||
const parsePublishApiRequestThumbnail = ({file, thumbnail}) => {
|
||||
const parsePublishApiRequestThumbnail = ({thumbnail}) => {
|
||||
if (!thumbnail) {
|
||||
return;
|
||||
}
|
||||
|
@ -97,6 +53,5 @@ const parsePublishApiRequestThumbnail = ({file, thumbnail}) => {
|
|||
|
||||
module.exports = {
|
||||
parsePublishApiRequestFiles,
|
||||
parsePublishApiRequestFile,
|
||||
parsePublishApiRequestThumbnail,
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js');
|
|||
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
||||
const publish = require('../publish/publish.js');
|
||||
const parsePublishApiRequestBody = require('../publish/parsePublishApiRequestBody');
|
||||
const {parsePublishApiRequestFile, parsePublishApiRequestThumbnail} = require('../publish/parsePublishApiRequestFiles.js');
|
||||
const {parsePublishApiRequestFiles, parsePublishApiRequestThumbnail} = require('../publish/parsePublishApiRequestFiles.js');
|
||||
const authenticateUser = require('../publish/authentication.js');
|
||||
const createThumbnailPublishParams = require('../publish/createThumbnailPublishParams.js');
|
||||
|
||||
|
@ -67,7 +67,7 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res)
|
|||
try {
|
||||
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
|
||||
if (files.file) {
|
||||
({fileName, filePath, fileExtension, fileType} = parsePublishApiRequestFile(files));
|
||||
({fileName, filePath, fileExtension, fileType} = parsePublishApiRequestFiles(files));
|
||||
if (files.thumbnail) {
|
||||
({thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestThumbnail(files));
|
||||
}
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
const path = require('path');
|
||||
const validateFileTypeAndSize = require('../publish/validateFileTypeAndSize.js');
|
||||
|
||||
const parseUpdateFile = ({file}) => {
|
||||
// make sure a file was provided
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
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
|
||||
validateFileTypeAndSize(file);
|
||||
// return results
|
||||
return {
|
||||
fileName : file.name,
|
||||
filePath : file.path,
|
||||
fileExtension: path.extname(file.path),
|
||||
fileType : file.type,
|
||||
};
|
||||
};
|
||||
|
||||
const parseUpdateThumbnail = ({thumbnail}) => {
|
||||
if (!thumbnail) {
|
||||
return false;
|
||||
}
|
||||
return {
|
||||
thumbnailFileName: thumbnail.name,
|
||||
thumbnailFilePath: thumbnail.path,
|
||||
thumbnailFileType: thumbnail.type,
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = [
|
||||
parseUpdateFile,
|
||||
parseUpdateThumbnail,
|
||||
];
|
Loading…
Reference in a new issue