Skip to content

Instantly share code, notes, and snippets.

@optlsnd
Created August 12, 2019 21:37
Show Gist options
  • Save optlsnd/c30bc5052e6e481f02db47a80a1d73b7 to your computer and use it in GitHub Desktop.
Save optlsnd/c30bc5052e6e481f02db47a80a1d73b7 to your computer and use it in GitHub Desktop.
var $ = uploadcare.jQuery
function fileTypeLimit(types) {
types = types.split(' ')
return function(fileInfo) {
if (fileInfo.name === null) {
return
}
var extension = fileInfo.name.split('.').pop()
if (types.indexOf(extension) == -1) {
throw new Error('fileType')
}
}
}
function fileSizeLimit(max) {
return function(fileInfo) {
if (fileInfo.size === null) {
return
}
if (max && fileInfo.size > max) {
throw new Error('fileMaximumSize')
}
}
}
$(document).ready(function($) {
$('[role=uploadcare-uploader][data-file-types]').each(function() {
var input = $(this)
var widget = uploadcare.Widget(input)
widget.validators.push(fileTypeLimit(input.data('fileTypes')))
})
$('[role=uploadcare-uploader][data-max-size]').each(function() {
var input = $(this)
var widget = uploadcare.Widget(input)
widget.validators.push(fileSizeLimit(input.data('maxSize')))
})
})
UPLOADCARE_LOCALE_TRANSLATIONS = {
// messages for widget
errors: {
fileType: 'This type of file is not allowed.',
fileMaximumSize: 'This file exceeds 1500mb',
},
// messages for dialog’s error page
dialog: {
tabs: {
preview: {
error: {
fileType: {
title: 'Oops, we dont accept that type of file.',
text: 'Please make sure you are uploading either an MP4 JPG or PDF file.',
back: 'Try Again',
},
fileMaximumSize: {
title: 'Looks like your upload is too big.',
text: 'Please ensure your file is no larger than 1500mb',
back: 'Try Again',
},
},
},
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment