Example #1
0
 /**
  * Handles a single upload by given CUploadedFile and returns an array
  * of informations.
  *
  * The 'error' attribute of the array, indicates there was an error.
  *
  * Informations on error:
  *       - error: true
  *       - errorMessage: some message
  *       - name: name of the file
  *       - size: file size
  *
  * Informations on success:
  *      - error: false
  *      - name: name of the uploaded file
  *      - size: file size
  *      - guid: of the file
  *      - url: url to the file
  *      - thumbnailUrl: url to the thumbnail if exists
  *
  * @param type $cFile
  * @return Array Informations about the uploaded file
  */
 protected function handleFileUpload($cFile)
 {
     $output = array();
     // Set some basic information
     $output['name'] = $cFile->getName();
     $output['size'] = $cFile->getSize();
     // Received a file?
     if ($cFile == null) {
         $output['error'] = true;
         $output['errorMessage'] = Yii::t('FileModule.controllers_FileController', 'No file received!');
         return $output;
     }
     // Maximum File Size
     if ($cFile->getSize() > HSetting::Get('maxFileSize', 'file')) {
         $output['error'] = true;
         $output['errorMessage'] = Yii::t('FileModule.controllers_FileController', 'Maximum file size has been {maxFileSize} reached!', array("{maxFileSize}" => Yii::app()->format->formatSize(HSetting::Get('maxFileSize', 'file'))));
         return $output;
     }
     if (!File::HasValidExtension($cFile->getName())) {
         $output['error'] = true;
         $output['errorMessage'] = Yii::t('FileModule.controllers_FileController', 'This file type is not allowed!');
         return $output;
     }
     // Store File
     $file = File::store($cFile);
     // Check File Storage
     if ($file == null) {
         $output['state'] = 'error';
         $output['errorMessage'] = Yii::t('FileModule.controllers_FileController', 'Internal Error: Could not store file!');
         return $output;
     }
     // Write successful array
     $output['error'] = false;
     $output['guid'] = $file->guid;
     $output['name'] = $file->file_name;
     $output['title'] = $file->title;
     $output['url'] = "";
     $output['thumbnailUrl'] = "";
     $output['size'] = $file->size;
     $output['deleteUrl'] = "";
     $output['deleteType'] = "";
     $output['mimeIcon'] = $file->getMimeIconClass();
     return $output;
 }