1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-10 16:54:44 +02:00

Update InputfieldFile non-ajax upload mode to warn about file uploads that exceed the max upload size.

This commit is contained in:
Ryan Cramer
2017-09-15 10:48:01 -04:00
parent b1048297e9
commit 064da79bae
4 changed files with 51 additions and 13 deletions

View File

@@ -282,6 +282,8 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
if(empty($options['caller'])) {
$caller = $this->className() . ".get($selectorString)";
$options['caller'] = $caller;
} else {
$caller = $options['caller'];
}
if(ctype_digit("$selectorString")) {
@@ -547,10 +549,11 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called just before a page is saved
*
* #pw-hooker
* #pw-internal
*
* @param Page $page The page about to be saved
* @return array Optional extra data to add to pages save query.
* @deprecated
*
*/
public function ___saveReady(Page $page) {
@@ -564,11 +567,12 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
* This is the same as Pages::save, except that it occurs before other save-related hooks (below),
* Whereas Pages::save occurs after. In most cases, the distinction does not matter.
*
* #pw-hooker
* #pw-internal
*
* @param Page $page The page that was saved
* @param array $changes Array of field names that changed
* @param array $values Array of values that changed, if values were being recorded, see Wire::getChanges(true) for details.
* @deprecated
*
*/
public function ___saved(Page $page, array $changes = array(), $values = array()) { }
@@ -576,9 +580,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called when a new page has been added
*
* #pw-hooker
* #pw-internal
*
* @param Page $page
* @deprecated
*
*/
public function ___added(Page $page) { }
@@ -586,9 +591,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called when a page is about to be deleted, but before data has been touched
*
* #pw-hooker
* #pw-internal
*
* @param Page $page
* @deprecated
*
*/
public function ___deleteReady(Page $page) { }
@@ -596,9 +602,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called when a page and it's data have been deleted
*
* #pw-hooker
* #pw-internal
*
* @param Page $page
* @deprecated
*
*/
public function ___deleted(Page $page) { }

View File

@@ -129,6 +129,22 @@ $(document).ready(function() {
var $list = $inputfield.find('.InputfieldFileList');
var maxFiles = parseInt($upload.find('.InputfieldFileMaxFiles').val());
var numFiles = $list.children('li').length + $upload.find('input[type=file]').length + 1;
var maxFilesize = parseInt($upload.attr('data-maxfilesize'));
var abort = false;
$upload.find("input[type=file]").each(function() {
if(typeof this.files[0] !== 'undefined'){
var size = this.files[0].size;
if(size > maxFilesize) {
ProcessWire.alert(
"File " + this.files[0].name +" is " + size + " bytes which exceeds max allowed size of " + maxFilesize + " bytes"
);
$(this).val('').closest('.InputMask').removeClass('ui-state-disabled ui-state-active');
abort = true;
}
}
});
if(abort) return false;
if(maxFiles > 0 && numFiles >= maxFiles) {
// no more files allowed

File diff suppressed because one or more lines are too long

View File

@@ -184,26 +184,41 @@ class InputfieldFile extends Inputfield implements InputfieldItemList, Inputfiel
*
*/
public function setMaxFilesize($filesize) {
$max = $this->strToBytes($filesize);
$phpMax = $this->strToBytes(ini_get('upload_max_filesize'));
if($phpMax < $max) $max = $phpMax;
$this->maxFilesize = $max;
return $this;
}
/**
* Convert string like "32M" to bytes (integer)
*
* @param string|int $filesize
* @return int
*
*/
protected function strToBytes($filesize) {
if(ctype_digit("$filesize")) {
$this->maxFilesize = (int) $filesize;
$bytes = (int) $filesize;
} else {
$filesize = rtrim($filesize, 'bB'); // convert mb=>m, gb=>g, kb=>k
$last = strtolower(substr($filesize, -1));
if(ctype_alpha($last)) $filesize = rtrim($filesize, $last);
$filesize = (int) $filesize;
if($last == 'g') {
$this->maxFilesize = (($filesize * 1024) * 1024) * 1024;
$bytes = (($filesize * 1024) * 1024) * 1024;
} else if($last == 'm') {
$this->maxFilesize = ($filesize * 1024) * 1024;
$bytes = ($filesize * 1024) * 1024;
} else if($last == 'k') {
$this->maxFilesize = $filesize * 1024;
$bytes = $filesize * 1024;
} else if($filesize > 0) {
$this->maxFilesize = $filesize;
$bytes = $filesize;
} else {
$this->maxFilesize = (5 * 1024) * 1024;
$bytes = (5 * 1024) * 1024;
}
}
return $this;
return $bytes;
}
/**