From 5ee553bfc22850f49c702507da372c4432c71207 Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 27 Nov 2018 23:20:48 -0600 Subject: [PATCH] Add upload limit configurations --- cli/defaults/siteConfig.json | 5 ++++- client/src/utils/file.js | 22 ++++++++++++++----- .../claim/publish/validateFileTypeAndSize.js | 22 ++++++++++++++----- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/cli/defaults/siteConfig.json b/cli/defaults/siteConfig.json index 4d79624d..3b8395c4 100644 --- a/cli/defaults/siteConfig.json +++ b/cli/defaults/siteConfig.json @@ -33,7 +33,10 @@ "approvedChannels": [], "publishingChannelWhitelist": [], "channelClaimBidAmount": "0.1", - "fileClaimBidAmount": "0.01" + "fileClaimBidAmount": "0.01", + "maxSizeImage": 10000000, + "maxSizeGif": 50000000, + "maxSizeVideo": 50000000 }, "startup": { "performChecks": true, diff --git a/client/src/utils/file.js b/client/src/utils/file.js index bd011a84..093573b0 100644 --- a/client/src/utils/file.js +++ b/client/src/utils/file.js @@ -1,3 +1,13 @@ +import siteConfig from '@config/siteConfig.json'; + +const { + publishing: { + maxSizeImage = 10000000, + maxSizeGif = 50000000, + maxSizeVideo = 50000000, + } +} = siteConfig; + module.exports = { validateFile (file) { if (!file) { @@ -11,18 +21,18 @@ module.exports = { case 'image/jpeg': case 'image/jpg': case 'image/png': - if (file.size > 10000000) { - throw new Error('Sorry, images are limited to 10 megabytes.'); + if (file.size > maxSizeImage) { + throw new Error(`Sorry, images are limited to ${maxSizeImage / SIZE_MB} megabytes.`); } break; case 'image/gif': - if (file.size > 50000000) { - throw new Error('Sorry, GIFs are limited to 50 megabytes.'); + if (file.size > maxSizeGif) { + throw new Error(`Sorry, .gifs are limited to ${maxSizeGif / SIZE_MB} megabytes.`); } break; case 'video/mp4': - if (file.size > 50000000) { - throw new Error('Sorry, videos are limited to 50 megabytes.'); + if (file.size > maxSizeVideo) { + throw new Error(`Sorry, videos are limited to ${maxSizeVideo / SIZE_MB} megabytes.`); } break; default: diff --git a/server/controllers/api/claim/publish/validateFileTypeAndSize.js b/server/controllers/api/claim/publish/validateFileTypeAndSize.js index 7f9ddef4..a42d9b29 100644 --- a/server/controllers/api/claim/publish/validateFileTypeAndSize.js +++ b/server/controllers/api/claim/publish/validateFileTypeAndSize.js @@ -1,26 +1,36 @@ const logger = require('winston'); +const { + publishing: { + maxSizeImage = 10000000, + maxSizeGif = 50000000, + maxSizeVideo = 50000000, + } +} = require('@config/siteConfig'); + +const SIZE_MB = 1000000; + const validateFileTypeAndSize = (file) => { // check file type and size switch (file.type) { case 'image/jpeg': case 'image/jpg': case 'image/png': - if (file.size > 10000000) { + if (file.size > maxSizeImage) { logger.debug('publish > file validation > .jpeg/.jpg/.png was too big'); - throw new Error('Sorry, images are limited to 10 megabytes.'); + throw new Error(`Sorry, images are limited to ${maxSizeImage / SIZE_MB} megabytes.`); } break; case 'image/gif': - if (file.size > 50000000) { + if (file.size > maxSizeGif) { logger.debug('publish > file validation > .gif was too big'); - throw new Error('Sorry, .gifs are limited to 50 megabytes.'); + throw new Error(`Sorry, .gifs are limited to ${maxSizeGif / SIZE_MB} megabytes.`); } break; case 'video/mp4': - if (file.size > 50000000) { + if (file.size > maxSizeVideo) { logger.debug('publish > file validation > .mp4 was too big'); - throw new Error('Sorry, videos are limited to 50 megabytes.'); + throw new Error(`Sorry, videos are limited to ${maxSizeVideo / SIZE_MB} megabytes.`); } break; default: