Exemple #1
0
 public function mockStore(Picture $pic, UploadedFile $dummy)
 {
     if ($dummy->getMimeType() != 'image/png') {
         throw new \Exception('fail');
     }
     $pic->setStorageKey('123.jpg');
     $pic->setMimeType($dummy->getMimeType());
 }
 /**
  * Stores an uploaded file to the storage and update a Picture document for this picture
  * (no persistence only file creation)
  *
  * @param Picture $pub
  * @param UploadedFile $picFile
  *
  * @throws \InvalidArgumentException Bad mimetype
  */
 public function insertUpload(Picture $pub, UploadedFile $picFile)
 {
     if (!$picFile->isValid()) {
         throw new \RuntimeException('Upload was incomplete');
     }
     $serverMimeType = $picFile->getMimeType();
     $nick = $pub->getAuthor()->getNickname();
     $extension = [];
     if (!preg_match(self::MIMETYPE_REGEX, $serverMimeType, $extension)) {
         throw new \InvalidArgumentException($serverMimeType . ' is not a valid mime type');
     }
     $syntheticName = $this->hashForNick($nick) . '.' . $extension[1];
     $pub->setMimeType($serverMimeType);
     $pub->setStorageKey($syntheticName);
     $path = $this->getStoragePath($syntheticName);
     // because diskspace is costly, I don't want to keep original picture
     // that's why I resize & recompress at a full-hd res (mobile first and I don't intend to
     // clone Picasa)
     Image::open($picFile->getPathname())->setCacheDir($this->cacheDir)->cropResize($this->sizeConfig[self::MAX_RES], $this->sizeConfig[self::MAX_RES])->save($path);
     if (!file_exists($path)) {
         throw new \RuntimeException("Cannot save {$syntheticName}");
     }
     $pub->setFileSize(filesize($path));
 }