/** * 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(); } }
/** * Create a digital object representation of an asset * * @param mixed parent object (digital object or information object) * @param QubitAsset asset to represent * @param array options array of optional paramaters * @return QubitDigitalObject */ public function writeToFileSystem($asset, $options = array()) { // Fail if filename is empty if (0 == strlen($asset->getName())) { throw new sfException('Not a valid filename'); } // Fail if "thumbnail" is not an image if (QubitTerm::THUMBNAIL_ID == $this->usageId && !QubitDigitalObject::isImageFile($asset->getName())) { throw new sfException('Thumbnail must be valid image type (jpeg, png, gif)'); } // Get clean file name (no bad chars) $cleanFileName = self::sanitizeFilename($asset->getName()); // If file has not extension, try to get it from asset mime type if (0 == strlen(pathinfo($cleanFileName, PATHINFO_EXTENSION)) && null !== ($assetMimeType = $asset->mimeType) && 0 < strlen($newFileExtension = array_search($assetMimeType, self::$qubitMimeTypes))) { $cleanFileName .= '.' . $newFileExtension; } // Upload paths for this information object / digital object $infoObjectPath = $this->getAssetPath(); $filePath = sfConfig::get('sf_web_dir') . $infoObjectPath . '/'; $relativePath = $infoObjectPath . '/'; $filePathName = $filePath . $cleanFileName; // make the target directory if necessary // NB: this will always return false if the path exists if (!file_exists($filePath)) { mkdir($filePath, 0755, true); } // Write file if (false === file_put_contents($filePathName, $asset->getContents())) { throw new sfException('File write to ' . $filePathName . ' failed. See setting directory and file permissions documentation.'); } // Test asset checksum against generated checksum from file $this->generateChecksumFromFile($filePathName); if ($this->getChecksum() != $asset->getChecksum()) { unlink($filePathName); rmdir($infoObjectPath); throw new sfException('Checksum values did not validate.'); } // set file permissions if (!chmod($filePathName, 0644)) { throw new sfException('Failed to set permissions on ' . $filePathName); } // Iterate through new directories and set permissions (mkdir() won't do this properly) $pathToDir = sfConfig::get('sf_web_dir'); foreach (explode('/', $infoObjectPath) as $dir) { $pathToDir .= '/' . $dir; @chmod($pathToDir, 0755); } // Save digital object in database $this->setName($cleanFileName); $this->setPath($relativePath); $this->setByteSize(filesize($filePathName)); $this->setMimeAndMediaType(); return $this; }
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)); }