refactors publishValidate
This commit is contained in:
parent
a6a3524dfd
commit
815d0437ac
2 changed files with 87 additions and 1 deletions
|
@ -23,7 +23,7 @@
|
|||
"publishing": {
|
||||
"primaryClaimAddress": null,
|
||||
"uploadDirectory": "/home/lbry/Uploads",
|
||||
"lbrynetHome": "/home/lbry",
|
||||
"lbrynetHome": "/CURRENTLYUNUSED",
|
||||
"thumbnailChannel": null,
|
||||
"thumbnailChannelId": null,
|
||||
"additionalClaimAddresses": [],
|
||||
|
@ -36,6 +36,17 @@
|
|||
"publishingChannelWhitelist": [],
|
||||
"channelClaimBidAmount": "0.1",
|
||||
"fileClaimBidAmount": "0.01",
|
||||
"fileSizeLimits": {
|
||||
"image": 50000000,
|
||||
"video": 50000000,
|
||||
"audio": 50000000,
|
||||
"text": 50000000,
|
||||
"model": 50000000,
|
||||
"application": 50000000,
|
||||
"customByContentType": {
|
||||
"application/octet-stream": 50000000
|
||||
}
|
||||
},
|
||||
"maxSizeImage": 10000000,
|
||||
"maxSizeGif": 50000000,
|
||||
"maxSizeVideo": 50000000
|
||||
|
|
75
utils/validateFileForPublish.js
Normal file
75
utils/validateFileForPublish.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
import { publishing } from '@config/siteConfig.json';
|
||||
|
||||
const {
|
||||
fileSizeLimits: {
|
||||
image: maxSizeImage = 10000000,
|
||||
video: maxSizeVideo = 50000000,
|
||||
audio: maxSizeAudio = 50000000,
|
||||
text: maxSizeText = 50000000,
|
||||
model: maxSizeModel = 50000000,
|
||||
application: maxSizeApplication = 50000000,
|
||||
customByContentType,
|
||||
},
|
||||
} = publishing;
|
||||
|
||||
const SIZE_MB = 1000000;
|
||||
|
||||
export const validateFileForPublish = file => {
|
||||
let contentType = file.type;
|
||||
let mediaType = contentType ? contentType.substr(0, contentType.indexOf('/')) : '';
|
||||
|
||||
if (!file) {
|
||||
throw new Error('no file provided');
|
||||
}
|
||||
|
||||
if (/'/.test(file.name)) {
|
||||
throw new Error('apostrophes are not allowed in the file name');
|
||||
}
|
||||
|
||||
if (Object.keys(customByContentType).includes(contentType)) {
|
||||
if (file.size > customByContentType[contentType]) {
|
||||
throw new Error(
|
||||
`Sorry, type ${contentType} is limited to ${customByContentType[contentType] / SIZE_MB} MB.`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
switch (mediaType) {
|
||||
case 'image':
|
||||
if (file.size > maxSizeImage) {
|
||||
throw new Error(`Sorry, type ${mediaType} is limited to ${maxSizeImage / SIZE_MB} MB.`);
|
||||
}
|
||||
break;
|
||||
case 'audio':
|
||||
if (file.size > maxSizeAudio) {
|
||||
throw new Error(`Sorry, type ${mediaType} is limited to ${maxSizeAudio / SIZE_MB} MB.`);
|
||||
}
|
||||
break;
|
||||
case 'video':
|
||||
if (file.size > maxSizeVideo) {
|
||||
throw new Error(`Sorry, type ${mediaType} is limited to ${maxSizeVideo / SIZE_MB} MB.`);
|
||||
}
|
||||
break;
|
||||
case 'text':
|
||||
if (file.size > maxSizeText) {
|
||||
throw new Error(`Sorry, type ${mediaType} is limited to ${maxSizeText / SIZE_MB} MB.`);
|
||||
}
|
||||
break;
|
||||
case 'model':
|
||||
if (file.size > maxSizeModel) {
|
||||
throw new Error(`Sorry, type ${mediaType} is limited to ${maxSizeModel / SIZE_MB} MB.`);
|
||||
}
|
||||
break;
|
||||
case 'application':
|
||||
if (file.size > maxSizeApplication) {
|
||||
throw new Error(
|
||||
`Sorry, type ${mediaType} is limited to ${maxSizeApplication / SIZE_MB} MB.`
|
||||
);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Missing or unrecognized file type`);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return file;
|
||||
};
|
Loading…
Reference in a new issue