Skip to content

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

Open
willvincent wants to merge 2 commits intopqina:masterfrom
willvincent:master
Open

Add min/max file size limits by mime type.#40
willvincent wants to merge 2 commits intopqina:masterfrom
willvincent:master

Conversation

@willvincent
Copy link
Copy Markdown

Closes issue #3.

Adds optional mimeMinFileSizes and mimeMaxFileSizes config options, these should work in conjunction with existing min/max and max total limits, and allow for setting min or max limits by explicit mime type or mime type wildcard group:

ie:

<script>
        // Register the plugin with FilePond
        FilePond.registerPlugin(FilePondPluginFileValidateSize);

        // Get a reference to the file input element
        const inputElement = document.querySelector('input[type="file"]');

        // Create the FilePond instance
        const pond = FilePond.create(inputElement, {
            mimeMaxFileSizes: {
                'image/*': '3MB',
                'image/gif': '5MB',
                'video/mp4': '100MB'
            }
        });
</script>

That config will allow images of any type up to 3mb, but allow gifs to be up to 5mb, and restrict mp4 videos to 100MB.

@rikschennink
Copy link
Copy Markdown
Collaborator

Appreciate the PR, will consider for v5

Comment thread src/js/index.js
if (mimeMaxFileSizes !== null) {
Object.keys(mimeMaxFileSizes).forEach(mime => {
if (file.type === mime ||
(mime.substring(2, -2) === '/*'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(mime.substring(2, -2) === '/*'
(mime.substring(mime.length-2) === '/*'

Comment thread src/js/index.js
if (mimeMinFileSizes !== null) {
Object.keys(mimeMinFileSizes).forEach(mime => {
if (file.type === mime ||
(mime.substring(2, -2) === '/*'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(mime.substring(2, -2) === '/*'
(mime.substring(mime.length-2) === '/*'

Comment thread src/js/index.js
// Max individual file size in bytes
maxFileSize: [null, Type.INT],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread src/js/index.js
// 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())
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const num = Number(str.match(/\d/).toString())
const num = Number(str.match(/\d+/).toString())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works better for values like 1024KB. For example, see the different max sizes in https://community.sinch.com/t5/MMS/What-is-the-maximum-size-of-MMS-message-that-you-can-send/ta-p/7847

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants