Ejemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function upload($file, $name, array $options = array())
 {
     if (!is_file($file)) {
         throw new \RuntimeException('Unable to upload; File [' . $file . '] does not exists.');
     }
     if (substr($name, 0, 8) == ':memory:') {
         $name = substr($name, 8);
     }
     $hash = sprintf(':memory:%s', $name);
     $this->storage[$hash] = array('content' => file_get_contents($file), 'mtime' => time(), 'mime' => FileUtils::getMimeFromExtension(FileUtils::getExtension($name)));
     return $hash;
 }
Ejemplo n.º 2
0
 /**
  * Upload Photo
  *
  * @param PhotoSourceInterface $photoSource
  * @param array $options Options available
  * <pre>
  * transform: options for transforming photo before saving
  * storage: storage system to save photo
  * </pre>
  * @return int|bool Photo ID if successful or false otherwise
  */
 public function upload(PhotoSourceInterface $photoSource, array $options = array())
 {
     $photoSource->process($this->options);
     if ($photoSource->isValid() == false) {
         // No need to go further if source is invalid
         return false;
     }
     $options = array_merge(array('transform' => array(), 'storage_name' => $this->storageManager->getDefault()), $options);
     $saveName = $this->generateOriginalSaveName($photoSource->getName());
     $storage = $this->getStorageManager()->get($options['storage_name']);
     $fileMime = $photoSource->getMime();
     if ($options['transform']) {
         // If we are to perform photo transformation during upload,
         // transformation specs are applied and the new photo is saved
         // as the original image
         list($uploadPath, $fileSize) = $this->transformPhoto($storage, FileUtils::createTempFile($photoSource->getFile(), $this->options['tmp_dir']), $saveName, $fileMime, $options['transform']);
     } else {
         // Just upload as is
         $fileSize = filesize($photoSource->getFile());
         $uploadPath = $storage->upload($photoSource->getFile(), $saveName, $options);
     }
     if ($uploadPath && $this->dataStore != null) {
         // Persist uploaded photo data
         return (int) $this->dataStore->addPhoto(array('storageName' => $options['storage_name'], 'filePath' => $uploadPath, 'fileSize' => $fileSize, 'fileName' => $photoSource->getName(), 'fileExtension' => FileUtils::getExtension($photoSource->getName()), 'fileMime' => $fileMime));
     }
     return false;
 }