Skip to content

Add min/max file size limits by mime type. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions dist/filepond-plugin-file-validate-size.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ const plugin = ({ addFilter, utils }) => {
// get quick reference to Type utils
const { Type, replaceInString, toNaturalFileSize } = utils;

// There is some magic happening that makes it unclear how the
// query('GET_MAX_FILE_SIZE') is converting strings like '5mb'
// to ints like 5000000, so hacking that behavior in here for
// mime-based limits. Ideally should use the same logic the other
// max file size limit uses, but api documentation for plugins
// is sadly rather lacking. :(

const _strToBytes = str => {
// Multipliers should probably be 1024 vs 1000,
// but this mimics the internal behavior.
const multiplier = {
b: 1,
kb: 1000,
mb: 1000 ** 2,
gb: 1000 ** 3,
tb: 1000 ** 4,
pb: 1000 ** 5,
};
const num = Number(str.match(/\d/).toString());
const type = str
.match(/[a-zA-Z]+/)
.toString()
.toLowerCase();
return num * multiplier[type];
};

// filtering if an item is allowed in hopper
addFilter('ALLOW_HOPPER_ITEM', (file, { query }) => {
if (!query('GET_ALLOW_FILE_SIZE_VALIDATION')) {
Expand Down Expand Up @@ -47,6 +73,64 @@ const plugin = ({ addFilter, utils }) => {
return resolve(file);
}

// reject or resolve base on max file size by mime type
const mimeMaxFileSizes = query('GET_MIME_MAX_FILE_SIZES');
if (mimeMaxFileSizes !== null) {
Object.keys(mimeMaxFileSizes).forEach(mime => {
if (
file.type === mime ||
(mime.substring(2, -2) === '/*' &&
file.type.split('/')[0] === mime.slice(0, -2))
) {
if (file.size > _strToBytes(mimeMaxFileSizes[mime])) {
reject({
status: {
main: query('GET_LABEL_MAX_FILE_SIZE_EXCEEDED'),
sub: replaceInString(query('GET_LABEL_MAX_FILE_SIZE'), {
filesize: toNaturalFileSize(
_strToBytes(mimeMaxFileSizes[mime]),
'.',
query('GET_FILE_SIZE_BASE'),
query('GET_FILE_SIZE_LABELS', query)
),
}),
},
});
return;
}
}
});
}

// reject or resolve base on min file size by mime type
const mimeMinFileSizes = query('GET_MIME_MIN_FILE_SIZES');
if (mimeMinFileSizes !== null) {
Object.keys(mimeMinFileSizes).forEach(mime => {
if (
file.type === mime ||
(mime.substring(2, -2) === '/*' &&
file.type.split('/')[0] === mime.slice(0, -2))
) {
if (file.size < _strToBytes(mimeMinFileSizes[mime])) {
reject({
status: {
main: query('GET_LABEL_MIN_FILE_SIZE_EXCEEDED'),
sub: replaceInString(query('GET_LABEL_MIN_FILE_SIZE'), {
filesize: toNaturalFileSize(
_strToBytes(mimeMinFileSizes[mime]),
'.',
query('GET_FILE_SIZE_BASE'),
query('GET_FILE_SIZE_LABELS', query)
),
}),
},
});
return;
}
}
});
}

// reject or resolve based on file size
const sizeMax = query('GET_MAX_FILE_SIZE');
if (sizeMax !== null && file.size > sizeMax) {
Expand Down Expand Up @@ -122,6 +206,12 @@ const plugin = ({ addFilter, utils }) => {
// Enable or disable file type validation
allowFileSizeValidation: [true, Type.BOOLEAN],

// Max individual file size in bytes by mime type
mimeMaxFileSizes: [null, Type.OBJECT],

// Max individual file size in bytes by mime type
mimeMinFileSizes: [null, Type.OBJECT],

// Max individual file size in bytes
maxFileSize: [null, Type.INT],

Expand Down
2 changes: 1 addition & 1 deletion dist/filepond-plugin-file-validate-size.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions dist/filepond-plugin-file-validate-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@
replaceInString = utils.replaceInString,
toNaturalFileSize = utils.toNaturalFileSize;

// There is some magic happening that makes it unclear how the
// query('GET_MAX_FILE_SIZE') is converting strings like '5mb'
// to ints like 5000000, so hacking that behavior in here for
// mime-based limits. Ideally should use the same logic the other
// max file size limit uses, but api documentation for plugins
// is sadly rather lacking. :(

var _strToBytes = function _strToBytes(str) {
// Multipliers should probably be 1024 vs 1000,
// but this mimics the internal behavior.
var multiplier = {
b: 1,
kb: 1000,
mb: Math.pow(1000, 2),
gb: Math.pow(1000, 3),
tb: Math.pow(1000, 4),
pb: Math.pow(1000, 5),
};
var num = Number(str.match(/\d/).toString());
var type = str
.match(/[a-zA-Z]+/)
.toString()
.toLowerCase();
return num * multiplier[type];
};

// filtering if an item is allowed in hopper
addFilter('ALLOW_HOPPER_ITEM', function(file, _ref2) {
var query = _ref2.query;
Expand Down Expand Up @@ -60,6 +86,66 @@
return resolve(file);
}

// reject or resolve base on max file size by mime type
var mimeMaxFileSizes = query('GET_MIME_MAX_FILE_SIZES');
if (mimeMaxFileSizes !== null) {
Object.keys(mimeMaxFileSizes).forEach(function(mime) {
if (
file.type === mime ||
(mime.substring(2, -2) === '/*' &&
file.type.split('/')[0] === mime.slice(0, -2))
) {
if (file.size > _strToBytes(mimeMaxFileSizes[mime])) {
reject({
status: {
main: query('GET_LABEL_MAX_FILE_SIZE_EXCEEDED'),
sub: replaceInString(query('GET_LABEL_MAX_FILE_SIZE'), {
filesize: toNaturalFileSize(
_strToBytes(mimeMaxFileSizes[mime]),
'.',
query('GET_FILE_SIZE_BASE'),
query('GET_FILE_SIZE_LABELS', query)
),
}),
},
});

return;
}
}
});
}

// reject or resolve base on min file size by mime type
var mimeMinFileSizes = query('GET_MIME_MIN_FILE_SIZES');
if (mimeMinFileSizes !== null) {
Object.keys(mimeMinFileSizes).forEach(function(mime) {
if (
file.type === mime ||
(mime.substring(2, -2) === '/*' &&
file.type.split('/')[0] === mime.slice(0, -2))
) {
if (file.size < _strToBytes(mimeMinFileSizes[mime])) {
reject({
status: {
main: query('GET_LABEL_MIN_FILE_SIZE_EXCEEDED'),
sub: replaceInString(query('GET_LABEL_MIN_FILE_SIZE'), {
filesize: toNaturalFileSize(
_strToBytes(mimeMinFileSizes[mime]),
'.',
query('GET_FILE_SIZE_BASE'),
query('GET_FILE_SIZE_LABELS', query)
),
}),
},
});

return;
}
}
});
}

// reject or resolve based on file size
var sizeMax = query('GET_MAX_FILE_SIZE');
if (sizeMax !== null && file.size > sizeMax) {
Expand Down Expand Up @@ -138,6 +224,12 @@
// Enable or disable file type validation
allowFileSizeValidation: [true, Type.BOOLEAN],

// Max individual file size in bytes by mime type
mimeMaxFileSizes: [null, Type.OBJECT],

// Max individual file size in bytes by mime type
mimeMinFileSizes: [null, Type.OBJECT],

// Max individual file size in bytes
maxFileSize: [null, Type.INT],

Expand Down
Loading