Refactor publish and validation and servability for all file types #902
|
@ -23,7 +23,7 @@
|
||||||
"publishing": {
|
"publishing": {
|
||||||
"primaryClaimAddress": null,
|
"primaryClaimAddress": null,
|
||||||
"uploadDirectory": "/home/lbry/Uploads",
|
"uploadDirectory": "/home/lbry/Uploads",
|
||||||
"lbrynetHome": "/home/lbry",
|
"lbrynetHome": "/CURRENTLYUNUSED",
|
||||||
"thumbnailChannel": null,
|
"thumbnailChannel": null,
|
||||||
"thumbnailChannelId": null,
|
"thumbnailChannelId": null,
|
||||||
"additionalClaimAddresses": [],
|
"additionalClaimAddresses": [],
|
||||||
|
@ -36,6 +36,17 @@
|
||||||
"publishingChannelWhitelist": [],
|
"publishingChannelWhitelist": [],
|
||||||
"channelClaimBidAmount": "0.1",
|
"channelClaimBidAmount": "0.1",
|
||||||
"fileClaimBidAmount": "0.01",
|
"fileClaimBidAmount": "0.01",
|
||||||
|
"fileSizeLimits": {
|
||||||
|
"image": 50000000,
|
||||||
|
"video": 50000000,
|
||||||
|
"audio": 50000000,
|
||||||
|
"text": 50000000,
|
||||||
|
"model": 50000000,
|
||||||
|
"application": 50000000,
|
||||||
|
"customByContentType": {
|
||||||
|
"application/octet-stream": 50000000
|
||||||
|
}
|
||||||
|
},
|
||||||
"maxSizeImage": 10000000,
|
"maxSizeImage": 10000000,
|
||||||
"maxSizeGif": 50000000,
|
"maxSizeGif": 50000000,
|
||||||
"maxSizeVideo": 50000000
|
"maxSizeVideo": 50000000
|
||||||
|
@ -49,7 +60,19 @@
|
||||||
"publicDisallowedTypesMain": []
|
"publicDisallowedTypesMain": []
|
||||||
},
|
},
|
||||||
"customFileExtensions": {
|
"customFileExtensions": {
|
||||||
"application/example-type": "example"
|
"application/x-troff-man": ".man",
|
||||||
|
"application/x-troff-me": ".me",
|
||||||
|
"application/x-mif": ".mif",
|
||||||
|
"application/x-troff-ms": ".ms",
|
||||||
|
"application/x-troff": ".roff",
|
||||||
|
"application/x-python-code": ".pyc",
|
||||||
|
"text/x-python": ".py",
|
||||||
|
"application/x-pn-realaudio": ".ram",
|
||||||
|
"application/x-sgml": ".sgm",
|
||||||
|
"model/stl": ".stl",
|
||||||
|
"image/pict": ".pct",
|
||||||
|
"text/xul": ".xul",
|
||||||
|
"text/x-go": "go"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"startup": {
|
"startup": {
|
||||||
|
|
|
@ -5,8 +5,9 @@ class PublishPreview extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
imgSource : '',
|
imgSource : '',
|
||||||
defaultThumbnail: '/assets/img/video_thumb_default.png',
|
defaultVideoThumbnail: '/assets/img/video_thumb_default.png',
|
||||||
|
defaultThumbnail : '/assets/img/Speech_Logo_Main@OG-02.jpg',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
|
@ -37,12 +38,13 @@ class PublishPreview extends React.Component {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
setPreviewImageSource (file) {
|
setPreviewImageSource (file) {
|
||||||
if (file.type !== 'video/mp4') {
|
if (this.props.thumbnail) {
|
||||||
|
this.setPreviewImageSourceFromFile(this.props.thumbnail);
|
||||||
|
} else if (file.type.substr(0, file.type.indexOf('/')) === 'image'){
|
||||||
this.setPreviewImageSourceFromFile(file);
|
this.setPreviewImageSourceFromFile(file);
|
||||||
|
} else if (file.type === 'video'){
|
||||||
|
this.setState({imgSource: this.state.defaultVideoThumbnail});
|
||||||
} else {
|
} else {
|
||||||
if (this.props.thumbnail) {
|
|
||||||
this.setPreviewImageSourceFromFile(this.props.thumbnail);
|
|
||||||
}
|
|
||||||
this.setState({imgSource: this.state.defaultThumbnail});
|
this.setState({imgSource: this.state.defaultThumbnail});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { validateFile } from '../../utils/file';
|
|
||||||
import Memeify from '@components/Memeify';
|
import Memeify from '@components/Memeify';
|
||||||
import DropzonePreviewImage from '@components/DropzonePreviewImage';
|
import DropzonePreviewImage from '@components/DropzonePreviewImage';
|
||||||
import DropzoneDropItDisplay from '@components/DropzoneDropItDisplay';
|
import DropzoneDropItDisplay from '@components/DropzoneDropItDisplay';
|
||||||
import DropzoneInstructionsDisplay from '@components/DropzoneInstructionsDisplay';
|
import DropzoneInstructionsDisplay from '@components/DropzoneInstructionsDisplay';
|
||||||
|
import validateFileForPublish from '@globalutils/validateFileForPublish';
|
||||||
|
|
||||||
import { library } from '@fortawesome/fontawesome-svg-core'
|
import { library } from '@fortawesome/fontawesome-svg-core';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import { faEdit } from '@fortawesome/free-solid-svg-icons';
|
import { faEdit } from '@fortawesome/free-solid-svg-icons';
|
||||||
|
|
||||||
const isFacebook = (() => {
|
const isFacebook = (() => {
|
||||||
|
@ -29,7 +29,7 @@ class Dropzone extends React.Component {
|
||||||
memeify : false,
|
memeify : false,
|
||||||
};
|
};
|
||||||
|
|
||||||
if(props.file) {
|
if (props.file) {
|
||||||
// No side effects allowed with `getDerivedStateFromProps`, so
|
// No side effects allowed with `getDerivedStateFromProps`, so
|
||||||
// we must use `componentDidUpdate` and `constructor` routines.
|
// we must use `componentDidUpdate` and `constructor` routines.
|
||||||
// Note: `FileReader` has an `onloadend` side-effect
|
// Note: `FileReader` has an `onloadend` side-effect
|
||||||
|
@ -133,7 +133,7 @@ class Dropzone extends React.Component {
|
||||||
chooseFile (file) {
|
chooseFile (file) {
|
||||||
if (file) {
|
if (file) {
|
||||||
try {
|
try {
|
||||||
validateFile(file); // validate the file's name, type, and size
|
validateFileForPublish(file); // validate the file's name, type, and size
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return this.props.setFileError(error.message);
|
return this.props.setFileError(error.message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,8 +47,6 @@ const createAssetMetaTags = asset => {
|
||||||
const ogThumbnailContentType = determineContentTypeFromExtension(claimData.thumbnail);
|
const ogThumbnailContentType = determineContentTypeFromExtension(claimData.thumbnail);
|
||||||
const ogThumbnail = claimData.thumbnail || defaultThumbnail;
|
const ogThumbnail = claimData.thumbnail || defaultThumbnail;
|
||||||
|
|
||||||
console.log('asset.claimData', asset.claimData);
|
|
||||||
|
|
||||||
// {property: 'og:title'] = ogTitle},
|
// {property: 'og:title'] = ogTitle},
|
||||||
const metaTags = {
|
const metaTags = {
|
||||||
'og:title': ogTitle,
|
'og:title': ogTitle,
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const mime = require('mime-types');
|
const mime = require('mime-types');
|
||||||
|
const {
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
serving: { customFileExtensions },
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
} = require('@config/siteConfig');
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
|
||||||
const getterMethods = {
|
const getterMethods = {
|
||||||
generated_extension() {
|
generated_extension() {
|
||||||
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
|
logger.info('trying to generate extension', this.content_type);
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
if (customFileExtensions.hasOwnProperty(this.content_type)) {
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
return customFileExtensions[this.content_type];
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
} else {
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
|
}
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
Please use (for legibility):
Please use (for legibility):
```
} else {
return mime.extension(this.content_type) ? mime.extension(this.content_type) : 'jpg';
}
```
|
|
@ -1,5 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
|
const validateFileTypeAndSize = require('./validateFileTypeAndSize.js');
|
||||||
|
const validateFileForPublish = require('./validateFileForPublish.js');
|
||||||
|
|
||||||
const parsePublishApiRequestFiles = ({ file, thumbnail }, isUpdate) => {
|
const parsePublishApiRequestFiles = ({ file, thumbnail }, isUpdate) => {
|
||||||
// make sure a file was provided
|
// make sure a file was provided
|
||||||
|
@ -40,7 +41,7 @@ const parsePublishApiRequestFiles = ({ file, thumbnail }, isUpdate) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate the file
|
// validate the file
|
||||||
if (file) validateFileTypeAndSize(file);
|
if (file) validateFileForPublish(file);
|
||||||
// return results
|
// return results
|
||||||
const obj = {
|
const obj = {
|
||||||
fileName: file.name,
|
fileName: file.name,
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
const logger = require('winston');
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
const { publishing } = require('@config/siteConfig.json');
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
const { fileSizeLimits } = publishing;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
const SIZE_MB = 1000000;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
const validateFileForPublish = file => {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
let contentType = file.type;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
let mediaType = contentType ? contentType.substr(0, contentType.indexOf('/')) : '';
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
let mediaTypeLimit = fileSizeLimits[mediaType] || false;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
let customLimits = fileSizeLimits['customByContentType'];
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (!file) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
throw new Error('no file provided');
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (/'/.test(file.name)) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
throw new Error('apostrophes are not allowed in the file name');
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (Object.keys(customLimits).includes(contentType)) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (file.size > customLimits[contentType]) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
throw new Error(
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
`Sorry, type ${contentType} is limited to ${customLimits[contentType] / SIZE_MB} MB.`
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
);
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (mediaTypeLimit) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
if (file.size > mediaTypeLimit) {
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
}
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
return file;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
};
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
|||||||
|
module.exports = validateFileForPublish;
|
||||||
Remove? Remove?
Can make an object of
Can make an object of `{ [mediaType]: maxSize }`, which would map to:
```
let mediaTypeLimit = mediaTypeLimits[mediaType] || false;
if (mediaTypeLimit) {
if (file.size > mediaTypeLimit) {
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
}
}
```
Done and DRY. Done and DRY.
|
34
utils/validateFileForPublish.js
Normal file
|
@ -0,0 +1,34 @@
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
import { publishing } from '@config/siteConfig.json';
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
const { fileSizeLimits } = publishing;
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
const SIZE_MB = 1000000;
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
export default function validateFileForPublish(file) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
let contentType = file.type;
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
let mediaType = contentType ? contentType.substr(0, contentType.indexOf('/')) : '';
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
let mediaTypeLimit = fileSizeLimits[mediaType] || false;
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
let customLimits = fileSizeLimits['customByContentType'];
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (!file) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
throw new Error('no file provided');
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (/'/.test(file.name)) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
throw new Error('apostrophes are not allowed in the file name');
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (Object.keys(customLimits).includes(contentType)) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (file.size > customLimits[contentType]) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
throw new Error(
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
`Sorry, type ${contentType} is limited to ${customLimits[contentType] / SIZE_MB} MB.`
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
);
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (mediaTypeLimit) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
if (file.size > mediaTypeLimit) {
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
throw new Error(`Sorry, type ${mediaType} is limited to ${mediaTypeLimit / SIZE_MB} MB.`);
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
return file;
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
|||||||
|
}
|
||||||
Much of this is duplicated in the other file I commented. Much of this is duplicated in the other file I commented.
|
Please use (for legibility):