/**
  * Get a generic representation for the current digital object.
  *
  * @param string $mimeType
  * @return QubitDigitalObject
  */
 public static function getGenericRepresentation($mimeType, $usageType)
 {
     $representation = new QubitDigitalObject();
     $genericIconPath = QubitDigitalObject::getGenericIconPath($mimeType, $usageType);
     $representation->setPath(dirname($genericIconPath) . '/');
     $representation->setName(basename($genericIconPath));
     return $representation;
 }
 public function execute($request)
 {
     ProjectConfiguration::getActive()->loadHelpers('Qubit');
     $uploadLimt = -1;
     $diskUsage = 0;
     $uploadFiles = array();
     $warning = null;
     $this->informationObject = QubitInformationObject::getById($request->informationObjectId);
     if (!isset($this->informationObject)) {
         $this->forward404();
     }
     // Check user authorization
     if (!QubitAcl::check($this->informationObject, 'update')) {
         throw new sfException();
     }
     $repo = $this->informationObject->getRepository(array('inherit' => true));
     if (isset($repo)) {
         $uploadLimit = $repo->uploadLimit;
         if (0 < $uploadLimit) {
             $uploadLimit *= pow(10, 9);
             // Convert to bytes
         }
         $diskUsage = $repo->getDiskUsage();
     }
     // Create tmp dir, if it doesn't exist already
     $tmpDir = sfConfig::get('sf_upload_dir') . '/tmp';
     if (!file_exists($tmpDir)) {
         mkdir($tmpDir);
         chmod($tmpDir, 0775);
     }
     foreach ($_FILES as $file) {
         if (null != $repo && 0 <= $uploadLimit && $uploadLimit < $diskUsage + $file['size']) {
             $uploadFiles = array('error' => $this->context->i18n->__('%1% upload limit of %2% GB exceeded for %3%', array('%1%' => sfConfig::get('app_ui_label_digitalobject'), '%2%' => round($uploadLimit / pow(10, 9), 2), '%4%' => $this->context->routing->generate(null, array($repo, 'module' => 'repository')), '%3%' => $repo->__toString())));
             continue;
         }
         // Get file extension
         $extension = substr($file['name'], strrpos($file['name'], '.'));
         // Get a unique file name (to avoid clashing file names)
         do {
             $uniqueString = substr(md5(time() . $file['name']), 0, 8);
             $tmpFileName = "TMP{$uniqueString}{$extension}";
             $tmpFilePath = "{$tmpDir}/{$tmpFileName}";
         } while (file_exists($tmpFilePath));
         // Thumbnail name
         $thumbName = "THB{$uniqueString}.jpg";
         $thumbPath = "{$tmpDir}/{$thumbName}";
         // Move file to web/uploads/tmp directory
         if (!move_uploaded_file($file['tmp_name'], $tmpFilePath)) {
             $errorMessage = $this->context->i18n->__('File %1% could not be moved to %2%', array('%1%' => $file['name'], '%2%' => $tmpDir));
             $uploadFiles = array('error' => $errorMessage);
             continue;
         }
         $tmpFileMd5sum = md5_file($tmpFilePath);
         $tmpFileMimeType = QubitDigitalObject::deriveMimeType($tmpFileName);
         if ($canThumbnail = QubitDigitalObject::canThumbnailMimeType($tmpFileMimeType) || QubitDigitalObject::isVideoFile($tmpFilePath)) {
             if (QubitDigitalObject::isImageFile($tmpFilePath) || 'application/pdf' == $tmpFileMimeType) {
                 $resizedObject = QubitDigitalObject::resizeImage($tmpFilePath, 150, 150);
             } else {
                 if (QubitDigitalObject::isVideoFile($tmpFilePath)) {
                     $resizedObject = QubitDigitalObject::createThumbnailFromVideo($tmpFilePath, 150, 150);
                 }
             }
             if (0 < strlen($resizedObject)) {
                 file_put_contents($thumbPath, $resizedObject);
                 chmod($thumbPath, 0644);
             }
             // Show a warning message if object couldn't be thumbnailed when it is
             // supposed to be possible
             if (!file_exists($thumbPath) && 0 >= filesize($thumbPath)) {
                 $warning = $this->context->i18n->__('File %1% could not be thumbnailed', array('%1%' => $file['name']));
             }
         } else {
             $thumbName = '../../images/' . QubitDigitalObject::getGenericIconPath($tmpFileMimeType, QubitTerm::THUMBNAIL_ID);
         }
         $uploadFiles = array('canThumbnail' => $canThumbnail, 'name' => $file['name'], 'md5sum' => $tmpFileMd5sum, 'size' => hr_filesize($file['size']), 'thumb' => $thumbName, 'tmpName' => $tmpFileName, 'warning' => $warning);
         // Keep running total of disk usage
         $diskUsage += $file['size'];
     }
     // Pass file data back to caller for processing on form submit
     $this->response->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
     return $this->renderText(json_encode($uploadFiles));
 }