/**
  * Update digital object properties, or upload new digital object derivatives.
  *
  * @return DigitalObjectEditAction this action
  */
 public function processForm()
 {
     // Set property 'displayAsCompound'
     $this->resource->setDisplayAsCompoundObject($this->form->getValue('displayAsCompound'));
     // Update media type
     $this->resource->mediaTypeId = $this->form->getValue('mediaType');
     // Process master rights component
     $this->rightEditComponent->processForm();
     // Process reference/thumbnail rights components
     foreach ($this->representations as $usageId => $representation) {
         $this["rightEditComponent_{$usageId}"]->processForm();
         $representation->save();
     }
     // Upload new representations
     $uploadedFiles = array();
     foreach ($this->representations as $usageId => $representation) {
         if (null !== ($uf = $this->form->getValue("repFile_{$usageId}"))) {
             $uploadedFiles[$usageId] = $uf;
         }
     }
     foreach ($uploadedFiles as $usageId => $uploadFile) {
         $content = file_get_contents($uploadFile->getTempName());
         if (QubitDigitalObject::isImageFile($uploadFile->getOriginalName())) {
             $tmpFile = Qubit::saveTemporaryFile($uploadFile->getOriginalName(), $content);
             if (QubitTerm::REFERENCE_ID == $usageId) {
                 $maxwidth = sfConfig::get('app_reference_image_maxwidth') ? sfConfig::get('app_reference_image_maxwidth') : 480;
                 $maxheight = null;
             } else {
                 if (QubitTerm::THUMBNAIL_ID == $usageId) {
                     $maxwidth = 100;
                     $maxheight = 100;
                 }
             }
             $content = QubitDigitalObject::resizeImage($tmpFile, $maxwidth, $maxheight);
             @unlink($tmpFile);
         }
         $representation = new QubitDigitalObject();
         $representation->usageId = $usageId;
         $representation->assets[] = new QubitAsset($uploadFile->getOriginalName(), $content);
         $representation->parentId = $this->resource->id;
         $representation->createDerivatives = false;
         $representation->save();
     }
     // Generate new reference
     if (null != $this->form->getValue('generateDerivative_' . QubitTerm::REFERENCE_ID)) {
         $this->resource->createReferenceImage();
     }
     // Generate new thumb
     if (null != $this->form->getValue('generateDerivative_' . QubitTerm::THUMBNAIL_ID)) {
         $this->resource->createThumbnail();
     }
 }
 /**
  * Resize this digital object (image)
  *
  * @param integer $maxwidth  Max width of resized image
  * @param integer $maxheight Max height of resized image
  *
  * @return boolean success or failure
  */
 public function resize($maxwidth, $maxheight = null)
 {
     // Only operate on digital objects that are images
     if ($this->isImage()) {
         $filename = $this->getAbsolutePath();
         return QubitDigitalObject::resizeImage($filename, $maxwidth, $maxheight);
     }
     return false;
 }
 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));
 }