Exemplo n.º 1
0
 /**
  * Returns the size of the file type in an appropriate format.
  *
  * @return string|false String value, or false if doesn't exist
  */
 public function getSize()
 {
     $size = $this->getAbsoluteSize();
     if ($size) {
         return File::format_size($size);
     }
     return false;
 }
 public function Field($properties = array())
 {
     // Calculated config as per jquery.fileupload-ui.js
     $allowedMaxFileNumber = $this->getAllowedMaxFileNumber();
     $config = array('url' => $this->Link('upload'), 'urlSelectDialog' => $this->Link('select'), 'urlAttach' => $this->Link('attach'), 'urlFileExists' => $this->Link('fileexists'), 'acceptFileTypes' => '.+$', 'maxNumberOfFiles' => $allowedMaxFileNumber ? $allowedMaxFileNumber - count($this->getItemIDs()) : null, 'replaceFile' => $this->getUpload()->getReplaceFile());
     // Validation: File extensions
     if ($allowedExtensions = $this->getAllowedExtensions()) {
         $config['acceptFileTypes'] = '(\\.|\\/)(' . implode('|', $allowedExtensions) . ')$';
         $config['errorMessages']['acceptFileTypes'] = _t('File.INVALIDEXTENSIONSHORT', 'Extension is not allowed');
     }
     // Validation: File size
     if ($allowedMaxFileSize = $this->getValidator()->getAllowedMaxFileSize()) {
         $config['maxFileSize'] = $allowedMaxFileSize;
         $config['errorMessages']['maxFileSize'] = _t('File.TOOLARGESHORT', 'File size exceeds {size}', array('size' => File::format_size($config['maxFileSize'])));
     }
     // Validation: Number of files
     if ($allowedMaxFileNumber) {
         if ($allowedMaxFileNumber > 1) {
             $config['errorMessages']['maxNumberOfFiles'] = _t('UploadField.MAXNUMBEROFFILESSHORT', 'Can only upload {count} files', array('count' => $allowedMaxFileNumber));
         } else {
             $config['errorMessages']['maxNumberOfFiles'] = _t('UploadField.MAXNUMBEROFFILESONE', 'Can only upload one file');
         }
     }
     // add overwrite warning error message to the config object sent to Javascript
     if ($this->getOverwriteWarning()) {
         $config['errorMessages']['overwriteWarning'] = _t('UploadField.OVERWRITEWARNING', 'File with the same name already exists');
     }
     $mergedConfig = array_merge($config, $this->ufConfig);
     return parent::Field(array('configString' => Convert::raw2json($mergedConfig), 'config' => new ArrayData($mergedConfig), 'multiple' => $allowedMaxFileNumber !== 1));
 }
 public function getSize()
 {
     if ($this->size) {
         return File::format_size($this->size);
     }
     return parent::getSize();
 }
 /**
  * Looser check validation that doesn't do is_upload_file()
  * checks as we're faking a POST request that PHP didn't generate
  * itself.
  *
  * @return boolean
  */
 public function validate()
 {
     $pathInfo = pathinfo($this->tmpFile['name']);
     // filesize validation
     if (!$this->isValidSize()) {
         $ext = isset($pathInfo['extension']) ? $pathInfo['extension'] : '';
         $arg = File::format_size($this->getAllowedMaxFileSize($ext));
         $this->errors[] = _t('File.TOOLARGE', 'File size is too large, maximum {size} allowed', 'Argument 1: File size (e.g. 1MB)', array('size' => $arg));
         return false;
     }
     // extension validation
     if (!$this->isValidExtension()) {
         $this->errors[] = _t('File.INVALIDEXTENSION_SHORT', 'Extension is not allowed');
         return false;
     }
     return true;
 }
 public function Field($properties = array())
 {
     // Calculated config as per jquery.fileupload-ui.js
     $config = array('allowedMaxFileNumber' => 1, 'url' => $this->Link('upload'), 'urlSelectDialog' => $this->Link('select'), 'urlAttach' => $this->Link('attach'), 'urlFileExists' => $this->link('fileexists'), 'acceptFileTypes' => '.+$', 'maxNumberOfFiles' => $this->Value() ? 0 : 1, 'replaceFile' => false);
     // Validation: File extensions
     if ($allowedExtensions = $this->getAllowedExtensions()) {
         $config['acceptFileTypes'] = '(\\.|\\/)(' . implode('|', $allowedExtensions) . ')$';
         $config['errorMessages']['acceptFileTypes'] = _t('File.INVALIDEXTENSIONSHORT', 'Extension is not allowed');
     }
     // Validation: File size
     if ($allowedMaxFileSize = $this->getValidator()->getAllowedMaxFileSize()) {
         $config['maxFileSize'] = $allowedMaxFileSize;
         $config['errorMessages']['maxFileSize'] = _t('File.TOOLARGESHORT', 'Filesize exceeds {size}', array('size' => File::format_size($config['maxFileSize'])));
     }
     $mergedConfig = array_merge($config, $this->ufConfig);
     return $this->customise(array('ConfigString' => Convert::raw2json($mergedConfig), 'UploadFieldFileButtons' => $this->renderWith($this->getTemplateFileButtons())))->renderWith($this->getTemplates());
 }
 /**
  * Test the File::format_size() method
  */
 public function testFormatSize()
 {
     $this->assertEquals("1000 bytes", File::format_size(1000));
     $this->assertEquals("1023 bytes", File::format_size(1023));
     $this->assertEquals("1 KB", File::format_size(1025));
     $this->assertEquals("9.8 KB", File::format_size(10000));
     $this->assertEquals("49 KB", File::format_size(50000));
     $this->assertEquals("977 KB", File::format_size(1000000));
     $this->assertEquals("1 MB", File::format_size(1024 * 1024));
     $this->assertEquals("954 MB", File::format_size(1000000000));
     $this->assertEquals("1 GB", File::format_size(1024 * 1024 * 1024));
     $this->assertEquals("9.3 GB", File::format_size(10000000000));
     // It use any denomination higher than GB.  It also doesn't overflow with >32 bit integers
     $this->assertEquals("93132.3 GB", File::format_size(100000000000000));
 }