public function uploadMedia(Media $media, $acl = 'public-read')
 {
     $targetPath = parse_url($media->getUrl(), PHP_URL_PATH);
     if ($media->getUrl() === $targetPath) {
         throw new \InvalidArgumentException('Media has to have a public URL set before uploading');
     }
     if (null === $media->getContent()) {
         throw new \RuntimeException('Dunno how to get file contents');
     }
     $fd = fopen($media->getContent()->getRealPath(), 'rb');
     $storageResponse = $this->storage->putObject(['ACL' => $acl, 'Bucket' => $this->bucketName, 'Key' => ltrim($targetPath, '/'), 'Body' => $fd, 'ContentType' => $media->getContentType(), 'CacheControl' => 'public, max-age=283824000', 'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+9 years'))]);
     fclose($fd);
     return $storageResponse->get('ObjectURL');
 }
 /**
  * @param File $file
  */
 public function setFile(File $file)
 {
     $this->file = $file;
     $this->media->setContent($file);
     $this->media->setContentType($file->getMimeType());
     $this->media->setUrl('/uploads/media/' . $this->media->getUuid() . '.' . $this->media->getContent()->getExtension());
 }
 /**
  * @param Media $media
  *
  * @return void
  */
 public function saveMedia(Media $media)
 {
     if (!$media->getContent() instanceof File) {
         return;
     }
     $this->uploader->uploadMedia($media);
 }
 /**
  * @param mixed $data
  *
  * @return Media
  */
 public function createNew($data)
 {
     if ($data instanceof File) {
         /** @var $data File */
         $media = new Media();
         if (method_exists($data, 'getClientOriginalName')) {
             $media->setOriginalFilename($data->getClientOriginalName());
         } else {
             $media->setOriginalFilename($data->getFilename());
         }
         $media->setContent($data);
         $contentType = $this->mimeTypeGuesser->guess($media->getContent()->getPathname());
         $media->setContentType($contentType);
         return $media;
     }
     return null;
 }
Esempio n. 5
0
 /**
  * @covers Kunstmaan\MediaBundle\Entity\Media::getContent
  * @covers Kunstmaan\MediaBundle\Entity\Media::setContent
  */
 public function testGetContent()
 {
     $this->object->setContent('content');
     $this->assertEquals('content', $this->object->getContent());
 }