/** * Imports the given temporary file into the storage and creates the new resource object. * * @param string $temporaryPathAndFilename Path and filename leading to the temporary file * @param string $collectionName Name of the collection to import into * @return Resource The imported resource */ protected function importTemporaryFile($temporaryPathAndFilename, $collectionName) { $sha1Hash = sha1_file($temporaryPathAndFilename); $md5Hash = md5_file($temporaryPathAndFilename); $resource = new Resource(); $resource->setFileSize(filesize($temporaryPathAndFilename)); $resource->setCollectionName($collectionName); $resource->setSha1($sha1Hash); $resource->setMd5($md5Hash); $objectName = $this->keyPrefix . $sha1Hash; $options = array('Bucket' => $this->bucketName, 'Body' => fopen($temporaryPathAndFilename, 'rb'), 'ContentLength' => $resource->getFileSize(), 'ContentType' => $resource->getMediaType(), 'Key' => $objectName); if (!$this->s3Client->doesObjectExist($this->bucketName, $this->keyPrefix . $sha1Hash)) { $this->s3Client->putObject($options); $this->systemLogger->log(sprintf('Successfully imported resource as object "%s" into bucket "%s" with MD5 hash "%s"', $objectName, $this->bucketName, $resource->getMd5() ?: 'unknown'), LOG_INFO); } else { $this->systemLogger->log(sprintf('Did not import resource as object "%s" into bucket "%s" because that object already existed.', $objectName, $this->bucketName), LOG_INFO); } return $resource; }
/** * Imports the given temporary file into the storage and creates the new resource object. * * Note: the temporary file is (re-)moved by this method. * * @param string $temporaryPathAndFileName * @param string $collectionName * @return PersistentResource * @throws StorageException */ protected function importTemporaryFile($temporaryPathAndFileName, $collectionName) { $this->fixFilePermissions($temporaryPathAndFileName); $sha1Hash = sha1_file($temporaryPathAndFileName); $targetPathAndFilename = $this->getStoragePathAndFilenameByHash($sha1Hash); if (!is_file($targetPathAndFilename)) { $this->moveTemporaryFileToFinalDestination($temporaryPathAndFileName, $targetPathAndFilename); } else { unlink($temporaryPathAndFileName); } $resource = new PersistentResource(); $resource->setFileSize(filesize($targetPathAndFilename)); $resource->setCollectionName($collectionName); $resource->setSha1($sha1Hash); $resource->setMd5(md5_file($targetPathAndFilename)); return $resource; }
/** * {@inheritDoc} */ public function setSha1($sha1) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setSha1', array($sha1)); return parent::setSha1($sha1); }
/** * Imports the given temporary file into the storage and creates the new resource object. * * @param string $temporaryFile * @param string $collectionName * @return Resource * @throws Exception */ protected function importTemporaryFile($temporaryFile, $collectionName) { $this->fixFilePermissions($temporaryFile); $sha1Hash = sha1_file($temporaryFile); $finalTargetPathAndFilename = $this->getStoragePathAndFilenameByHash($sha1Hash); if (!file_exists(dirname($finalTargetPathAndFilename))) { Files::createDirectoryRecursively(dirname($finalTargetPathAndFilename)); } if (rename($temporaryFile, $finalTargetPathAndFilename) === FALSE) { unlink($temporaryFile); throw new Exception(sprintf('The temporary file of the file import could not be moved to the final target "%s".', $finalTargetPathAndFilename), 1381156103); } $this->fixFilePermissions($finalTargetPathAndFilename); $resource = new Resource(); $resource->setFileSize(filesize($finalTargetPathAndFilename)); $resource->setCollectionName($collectionName); $resource->setSha1($sha1Hash); $resource->setMd5(md5_file($finalTargetPathAndFilename)); return $resource; }
/** * Imports a resource (file) as specified in the given upload info array as a * persistent resource. * * On a successful import this method returns a Resource object representing * the newly imported persistent resource. * * @param array $uploadInfo An array detailing the resource to import (expected keys: name, tmp_name) * @param string $collectionName Name of the collection this uploaded resource should be part of * @return string A resource object representing the imported resource * @throws Exception * @api */ public function importUploadedResource(array $uploadInfo, $collectionName) { if ($this->debug) { $this->systemLogger->log('storage ' . $this->name . ': importUploadedResource'); } $pathInfo = pathinfo($uploadInfo['name']); $originalFilename = $pathInfo['basename']; $sourcePathAndFilename = $uploadInfo['tmp_name']; if (!file_exists($sourcePathAndFilename)) { throw new Exception(sprintf('The temporary file "%s" of the file upload does not exist (anymore).', $sourcePathAndFilename), 1375267007); } $newSourcePathAndFilename = $this->environment->getPathToTemporaryDirectory() . 'Sbruggmann_FlowKeyCDN_' . uniqid() . '.tmp'; if (move_uploaded_file($sourcePathAndFilename, $newSourcePathAndFilename) === FALSE) { throw new Exception(sprintf('The uploaded file "%s" could not be moved to the temporary location "%s".', $sourcePathAndFilename, $newSourcePathAndFilename), 1375267045); } $sha1Hash = sha1_file($newSourcePathAndFilename); $md5Hash = md5_file($newSourcePathAndFilename); $resource = new Resource(); $resource->setFilename($originalFilename); $resource->setCollectionName($collectionName); $resource->setFileSize(filesize($newSourcePathAndFilename)); $resource->setSha1($sha1Hash); $resource->setMd5($md5Hash); $this->uploadFile($newSourcePathAndFilename, $originalFilename); return $resource; }