Merge pull request #875 from jessopb/publisherrors

Rudimentary dropdown file errors
This commit is contained in:
Shawn K 2019-01-22 14:11:24 -06:00 committed by GitHub
commit 5d337a09bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 26 deletions

View file

@ -5,9 +5,12 @@ import View from './view';
import siteConfig from '@config/siteConfig.json'; import siteConfig from '@config/siteConfig.json';
import createCanonicalLink from '@globalutils/createCanonicalLink'; import createCanonicalLink from '@globalutils/createCanonicalLink';
const { assetDefaults: { thumbnail: defaultThumbnail } } = siteConfig; const {
assetDefaults: { thumbnail: defaultThumbnail },
} = siteConfig;
const mapStateToProps = ({ show, publish: { file, thumbnail, fileError, isUpdate } }) => { const mapStateToProps = ({ show, publish: { file, thumbnail, error, isUpdate } }) => {
const fileError = error.file;
const obj = { file, thumbnail, fileError, isUpdate }; const obj = { file, thumbnail, fileError, isUpdate };
let asset, name, claimId, fileExt, outpoint, sourceUrl; let asset, name, claimId, fileExt, outpoint, sourceUrl;
if (isUpdate) { if (isUpdate) {
@ -18,7 +21,7 @@ const mapStateToProps = ({ show, publish: { file, thumbnail, fileError, isUpdate
if (obj.fileExt === 'mp4') { if (obj.fileExt === 'mp4') {
obj.sourceUrl = claimData.thumbnail ? claimData.thumbnail : defaultThumbnail; obj.sourceUrl = claimData.thumbnail ? claimData.thumbnail : defaultThumbnail;
} else { } else {
({fileExt, outpoint} = claimData); ({ fileExt, outpoint } = claimData);
obj.sourceUrl = `${createCanonicalLink({ asset: claimData })}.${fileExt}?${outpoint}`; obj.sourceUrl = `${createCanonicalLink({ asset: claimData })}.${fileExt}?${outpoint}`;
} }
} }
@ -28,14 +31,17 @@ const mapStateToProps = ({ show, publish: { file, thumbnail, fileError, isUpdate
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
selectFile: (file) => { selectFile: file => {
dispatch(selectFile(file)); dispatch(selectFile(file));
}, },
setFileError: (value) => { setFileError: value => {
dispatch(clearFile()); dispatch(clearFile());
dispatch(updateError('file', value)); dispatch(updateError('file', value));
}, },
}; };
}; };
export default connect(mapStateToProps, mapDispatchToProps)(View); export default connect(
mapStateToProps,
mapDispatchToProps
)(View);

View file

@ -1,15 +1,12 @@
import siteConfig from '@config/siteConfig.json'; import siteConfig from '@config/siteConfig.json';
const { const {
publishing: { publishing: { maxSizeImage = 10000000, maxSizeGif = 50000000, maxSizeVideo = 50000000 },
maxSizeImage = 10000000,
maxSizeGif = 50000000,
maxSizeVideo = 50000000,
}
} = siteConfig; } = siteConfig;
// TODO: central constants location
const SIZE_MB = 1000000;
export function validateFile(file) {
export function validateFile (file) {
if (!file) { if (!file) {
throw new Error('no file provided'); throw new Error('no file provided');
} }
@ -36,6 +33,9 @@ export function validateFile (file) {
} }
break; break;
default: default:
throw new Error(file.type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.'); throw new Error(
file.type +
' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.'
);
} }
} }

View file

@ -1,7 +1,7 @@
const path = require('path'); const path = require('path');
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js'); const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
const parsePublishApiRequestFiles = ({file, thumbnail}, isUpdate) => { const parsePublishApiRequestFiles = ({ file, thumbnail }, isUpdate) => {
// make sure a file was provided // make sure a file was provided
if (!file) { if (!file) {
if (isUpdate) { if (isUpdate) {
@ -14,39 +14,39 @@ const parsePublishApiRequestFiles = ({file, thumbnail}, isUpdate) => {
} }
return {}; return {};
} }
throw new Error('no file with key of [file] found in request'); throw new Error('No file with key of [file] found in request');
} }
if (!file.path) { if (!file.path) {
throw new Error('no file path found'); throw new Error('No file path found');
} }
if (!file.type) { if (!file.type) {
throw new Error('no file type found'); throw new Error('No file type found');
} }
if (!file.size) { if (!file.size) {
throw new Error('no file size found'); throw new Error('No file size found');
} }
// validate the file name // validate the file name
if (!file.name) { if (!file.name) {
throw new Error('no file name found'); throw new Error('No file name found');
} }
if (file.name.indexOf('.') < 0) { if (file.name.indexOf('.') < 0) {
throw new Error('no file extension found in file name'); throw new Error('No file extension found in file name');
} }
if (file.name.indexOf('.') === 0) { if (file.name.indexOf('.') === 0) {
throw new Error('file name cannot start with "."'); throw new Error('File name cannot start with "."');
} }
if (/'/.test(file.name)) { if (/'/.test(file.name)) {
throw new Error('apostrophes are not allowed in the file name'); throw new Error('Apostrophes are not allowed in the file name');
} }
// validate the file // validate the file
if (file) validateFileTypeAndSize(file); if (file) validateFileTypeAndSize(file);
// return results // return results
const obj = { const obj = {
fileName : file.name, fileName: file.name,
filePath : file.path, filePath: file.path,
fileExtension: path.extname(file.path), fileExtension: path.extname(file.path),
fileType : file.type, fileType: file.type,
}; };
if (thumbnail) { if (thumbnail) {