Skip to content

Commit 211978d

Browse files
committed
Merge pull request JeremyFagis#35 from JeremyFagis/maxFileSize
Worked on max filesize for the preview image.
2 parents c96b5e3 + 1e8f666 commit 211978d

File tree

9 files changed

+141
-125
lines changed

9 files changed

+141
-125
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ $('.dropify').dropify();
150150
```
151151

152152

153+
* __allowedFileExtensions:__ You can allow only some file extensions. If you add the attr __data-allowed-file-extensions="pdf png psd"__ only PDF, PNG and PSD files will be allowed. By default, everything is allowed. Default: ['*'].
154+
155+
```html
156+
<input type="file" class="dropify" data-allowed-file-extensions="pdf png psd" />
157+
```
158+
159+
160+
* __maxFileSizePreview:__ Set the max filesize of the previewed document (if it's an image). If the file size is bigger than the option, it will be only the file icon and disabled the preview. You can use unit like K, M and G.
161+
162+
```html
163+
<input type="file" class="dropify" data-max-file-size-preview="3M" />
164+
```
165+
166+
153167
* __messages:__ You can translate default messages. You juste have to add an options array when you init the plugin. This messages will be replaced in the __tpl__ option.
154168

155169
```javascript

dist/css/demo.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)

dist/css/dropify.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)

dist/css/dropify.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/dropify.js

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)
@@ -41,8 +41,10 @@ function Dropify(element, options) {
4141
showErrors: true,
4242
errorTimeout: 3000,
4343
errorsPosition: 'overlay',
44+
imgFileExtensions: ['png', 'jpg', 'jpeg', 'gif', 'bmp'],
45+
maxFileSizePreview: "5M",
4446
allowedFormats: ['portrait', 'square', 'landscape'],
45-
allowedFileExtensions: ['*'],
47+
allowedFileExtensions: ['*'],
4648
messages: {
4749
'default': 'Drag and drop a file here or click',
4850
'replace': 'Drag and drop or click to replace',
@@ -70,17 +72,16 @@ function Dropify(element, options) {
7072
}
7173
};
7274

73-
this.element = element;
74-
this.input = $(this.element);
75-
this.wrapper = null;
76-
this.preview = null;
77-
this.filenameWrapper = null;
78-
this.settings = $.extend(true, defaults, options, this.input.data());
79-
this.imgFileExtensions = ['png', 'jpg', 'jpeg', 'gif', 'bmp'];
80-
this.errorsEvent = $.Event('dropify.errors');
81-
this.isDisabled = false;
82-
this.isInit = false;
83-
this.file = {
75+
this.element = element;
76+
this.input = $(this.element);
77+
this.wrapper = null;
78+
this.preview = null;
79+
this.filenameWrapper = null;
80+
this.settings = $.extend(true, defaults, options, this.input.data());
81+
this.errorsEvent = $.Event('dropify.errors');
82+
this.isDisabled = false;
83+
this.isInit = false;
84+
this.file = {
8485
object: null,
8586
name: null,
8687
size: null,
@@ -93,6 +94,10 @@ function Dropify(element, options) {
9394
this.settings.allowedFormats = this.settings.allowedFormats.split(' ');
9495
}
9596

97+
if (!Array.isArray(this.settings.allowedFileExtensions)) {
98+
this.settings.allowedFileExtensions = this.settings.allowedFileExtensions.split(' ');
99+
}
100+
96101
this.onChange = this.onChange.bind(this);
97102
this.clearElement = this.clearElement.bind(this);
98103
this.onFileReady = this.onFileReady.bind(this);
@@ -165,9 +170,9 @@ Dropify.prototype.createElements = function()
165170

166171
var defaultFile = this.settings.defaultFile || '';
167172

168-
if (defaultFile.trim() != '') {
173+
if (defaultFile.trim() !== '') {
169174
this.file.name = this.cleanFilename(defaultFile);
170-
this.setPreview(defaultFile);
175+
this.setPreview(this.isImage(), defaultFile);
171176
}
172177
};
173178

@@ -188,46 +193,43 @@ Dropify.prototype.readFile = function(input)
188193

189194
this.clearErrors();
190195
this.showLoader();
191-
192196
this.setFileInformations(file);
193-
reader.readAsDataURL(file);
194-
195197
this.errorsEvent.errors = [];
196-
197198
this.checkFileSize();
198-
199199
this.isFileExtensionAllowed();
200200

201-
reader.onload = function(_file) {
202-
srcBase64 = _file.target.result;
203-
if (this.isImage()) {
201+
if (this.isImage() && this.file.size < this.sizeToByte(this.settings.maxFileSizePreview)) {
202+
this.input.on('dropify.fileReady', this.onFileReady);
203+
reader.readAsDataURL(file);
204+
reader.onload = function(_file) {
205+
srcBase64 = _file.target.result;
204206
image.src = _file.target.result;
205207
image.onload = function() {
206208
_this.setFileDimensions(this.width, this.height);
207209
_this.validateImage();
208-
_this.input.trigger(eventFileReady, [srcBase64]);
210+
_this.input.trigger(eventFileReady, [true, srcBase64]);
209211
};
210-
} else {
211-
this.input.trigger(eventFileReady, [srcBase64]);
212-
}
213-
}.bind(this);
214212

215-
this.input.on('dropify.fileReady', this.onFileReady);
213+
}.bind(this);
214+
} else {
215+
this.onFileReady(false);
216+
}
216217
}
217218
};
218219

219220
/**
220221
* On file ready to show
221222
*
222223
* @param {Event} event
224+
* @param {Bool} previewable
223225
* @param {String} src
224226
*/
225-
Dropify.prototype.onFileReady = function(event, src)
227+
Dropify.prototype.onFileReady = function(event, previewable, src)
226228
{
227229
this.input.off('dropify.fileReady', this.onFileReady);
228230

229231
if (this.errorsEvent.errors.length === 0) {
230-
this.setPreview(src, this.file.name);
232+
this.setPreview(previewable, src);
231233
} else {
232234
this.input.trigger(this.errorsEvent, [this]);
233235
for (var i = this.errorsEvent.errors.length - 1; i >= 0; i--) {
@@ -281,17 +283,17 @@ Dropify.prototype.setFileDimensions = function(width, height)
281283
*
282284
* @param {String} src
283285
*/
284-
Dropify.prototype.setPreview = function(src)
286+
Dropify.prototype.setPreview = function(previewable, src)
285287
{
286288
this.wrapper.removeClass('has-error').addClass('has-preview');
287289
this.filenameWrapper.children('.dropify-filename-inner').html(this.file.name);
288290
var render = this.preview.children('.dropify-render');
289291

290292
this.hideLoader();
291293

292-
if (this.isImage() === true) {
294+
if (previewable === true) {
293295
var imgTag = $('<img />').attr('src', src);
294-
296+
295297
if (this.settings.height) {
296298
imgTag.css("max-height", this.settings.height);
297299
}
@@ -332,7 +334,7 @@ Dropify.prototype.cleanFilename = function(src)
332334
filename = src.split('/').pop();
333335
}
334336

335-
return src != "" ? filename : '';
337+
return src !== "" ? filename : '';
336338
};
337339

338340
/**
@@ -388,9 +390,9 @@ Dropify.prototype.setContainerSize = function()
388390
*/
389391
Dropify.prototype.isTouchDevice = function()
390392
{
391-
return (('ontouchstart' in window)
392-
|| (navigator.MaxTouchPoints > 0)
393-
|| (navigator.msMaxTouchPoints > 0));
393+
return (('ontouchstart' in window) ||
394+
(navigator.MaxTouchPoints > 0) ||
395+
(navigator.msMaxTouchPoints > 0));
394396
};
395397

396398
/**
@@ -410,7 +412,7 @@ Dropify.prototype.getFileType = function()
410412
*/
411413
Dropify.prototype.isImage = function()
412414
{
413-
if (this.imgFileExtensions.indexOf(this.getFileType()) != "-1") {
415+
if (this.settings.imgFileExtensions.indexOf(this.getFileType()) != "-1") {
414416
return true;
415417
}
416418

@@ -424,15 +426,14 @@ Dropify.prototype.isImage = function()
424426
*/
425427
Dropify.prototype.isFileExtensionAllowed = function () {
426428

427-
if (this.settings.allowedFileExtensions.indexOf('*') != "-1") {
428-
return true;
429-
} else if (this.settings.allowedFileExtensions.indexOf(this.getFileType()) != "-1") {
429+
if (this.settings.allowedFileExtensions.indexOf('*') != "-1" || 
430+
this.settings.allowedFileExtensions.indexOf(this.getFileType()) != "-1") {
430431
return true;
431432
}
432433
this.pushError("fileExtension");
433434

434435
return false;
435-
}
436+
};
436437

437438
/**
438439
* Translate messages if needed.
@@ -451,7 +452,7 @@ Dropify.prototype.translateMessages = function()
451452
*/
452453
Dropify.prototype.checkFileSize = function()
453454
{
454-
if (this.maxFileSizeToByte() !== 0 && this.file.size > this.maxFileSizeToByte()) {
455+
if (this.sizeToByte(this.settings.maxFileSize) !== 0 && this.file.size > this.sizeToByte(this.settings.maxFileSize)) {
455456
this.pushError("fileSize");
456457
}
457458
};
@@ -461,22 +462,22 @@ Dropify.prototype.checkFileSize = function()
461462
*
462463
* @return {Int} value
463464
*/
464-
Dropify.prototype.maxFileSizeToByte = function()
465+
Dropify.prototype.sizeToByte = function(size)
465466
{
466467
var value = 0;
467468

468-
if (this.settings.maxFileSize !== 0) {
469-
var unit = this.settings.maxFileSize.slice(-1).toUpperCase(),
469+
if (size !== 0) {
470+
var unit = size.slice(-1).toUpperCase(),
470471
kb = 1024,
471472
mb = kb * 1024,
472473
gb = mb * 1024;
473474

474475
if (unit === 'K') {
475-
value = parseFloat(this.settings.maxFileSize) * kb;
476+
value = parseFloat(size) * kb;
476477
} else if (unit === 'M') {
477-
value = parseFloat(this.settings.maxFileSize) * mb;
478+
value = parseFloat(size) * mb;
478479
} else if (unit === 'G') {
479-
value = parseFloat(this.settings.maxFileSize) * gb;
480+
value = parseFloat(size) * gb;
480481
}
481482
}
482483

@@ -583,7 +584,7 @@ Dropify.prototype.getError = function(errorKey)
583584
} else if (errorKey === 'maxHeight') {
584585
value = this.settings.maxHeight;
585586
} else if (errorKey === 'imageFormat') {
586-
value = this.settings.allowedFormats.join(' ');
587+
value = this.settings.allowedFormats.join(', ');
587588
} else if (errorKey === 'fileExtension') {
588589
value = this.settings.allowedFileExtensions.join(', ');
589590
}

0 commit comments

Comments
 (0)