Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId)
 {
     $mediaId = ArticleMedia::handleMediaId($mediaId);
     $this->saveFile($uploadedFile, $mediaId);
     $asset = $this->mediaFactory->createMediaAsset($uploadedFile, $mediaId);
     $this->mediaRepository->add($asset);
     return $asset;
 }
Exemplo n.º 2
0
 /**
  * Handle Article Media with Image (add renditions, set mimetype etc.).
  *
  * @param ArticleMedia  $articleMedia
  * @param string        $key          unique key shared between media and image rendition
  * @param ItemInterface $item
  *
  * @return ArticleMedia
  */
 protected function createImageMedia(ArticleMedia $articleMedia, string $key, ItemInterface $item)
 {
     if (0 === $item->getRenditions()->count()) {
         return $articleMedia;
     }
     $originalRendition = $item->getRenditions()['original'];
     $criteria = new Criteria();
     $criteria->set('assetId', ArticleMedia::handleMediaId($originalRendition->getMedia()));
     $articleMedia->setMimetype($originalRendition->getMimetype());
     $articleMedia->setKey($key);
     $image = $this->imageRepository->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult();
     $articleMedia->setImage($image);
     foreach ($item->getRenditions() as $key => $rendition) {
         $criteria->set('assetId', ArticleMedia::handleMediaId($rendition->getMedia()));
         $image = $this->imageRepository->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult();
         if (null === $image) {
             continue;
         }
         $imageRendition = $image->getRendition();
         if (null === $image->getRendition()) {
             $imageRendition = $this->createImageRendition($image, $articleMedia, $key, $rendition);
             $this->imageRepository->persist($imageRendition);
         }
         $articleMedia->addRendition($imageRendition);
     }
     return $articleMedia;
 }
 /**
  * Receives HTTP Push Request's assets payload which is then processed and stored.
  *
  * @ApiDoc(
  *     resource=true,
  *     description="Adds new assets from HTTP Push",
  *     statusCodes={
  *         201="Returned on successful post.",
  *         500="Returned on invalid file.",
  *         200="Returned on form errors"
  *     },
  *     input="SWP\Bundle\ContentBundle\Form\Type\MediaFileType"
  * )
  * @Route("/api/{version}/assets/push", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_assets_push")
  * @Method("POST")
  */
 public function pushAssetsAction(Request $request)
 {
     $form = $this->createForm(MediaFileType::class);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $mediaManager = $this->container->get('swp_content_bundle.manager.media');
         $uploadedFile = $form->getData()['media'];
         $mediaId = $request->request->get('media_id');
         if ($uploadedFile->isValid()) {
             $media = $mediaManager->handleUploadedFile($uploadedFile, ArticleMedia::handleMediaId($mediaId));
             return new SingleResourceResponse(['media_id' => $mediaId, 'URL' => $mediaManager->getMediaPublicUrl($media), 'media' => base64_encode($mediaManager->getFile($media)), 'mime_type' => Mime::getMimeFromExtension($media->getFileExtension()), 'filemeta' => []], new ResponseContext(201));
         }
         throw new \Exception('Uploaded file is not valid:' . $uploadedFile->getErrorMessage());
     }
     return new SingleResourceResponse($form);
 }