Exemple #1
0
 /**
  * @Route("/upload.php",name="content_media_upload")
  * @Method({"POST"})
  */
 public function uploadAction(Request $request)
 {
     $parentId = $request->get('parent', null);
     $type = $request->get('type', null);
     $schema = $request->get('schema', null);
     if (is_null($type) || is_null($schema) || is_null($parentId)) {
         return $this->returnErrorResponse();
     }
     try {
         $uploadedFile = $request->files->get('file');
         $mediaInfo = $this->mm()->handleUpload($uploadedFile);
     } catch (\Exception $e) {
         return new Response($e->getMessage(), 500);
     }
     if (!empty($mediaInfo)) {
         /**
          * @var \Bellwether\BWCMSBundle\Entity\ContentRepository $contentRepository
          * @var \Bellwether\BWCMSBundle\Entity\ContentEntity $content
          */
         $contentRepository = $this->em()->getRepository('BWCMSBundle:ContentEntity');
         $content = new ContentEntity();
         $content->setType($type);
         $content->setSchema($schema);
         $content->setSite($this->getSite());
         if ($parentId == 'Root') {
             $content->setTreeParent(null);
         } else {
             $parentEntity = $contentRepository->find($parentId);
             $content->setTreeParent($parentEntity);
         }
         $content->setTitle($mediaInfo['originalName']);
         $content->setTemplate('');
         $contentMedia = new ContentMediaEntity();
         $contentMedia->setFile($mediaInfo['filename']);
         $contentMedia->setExtension($mediaInfo['extension']);
         $contentMedia->setMime($mediaInfo['mimeType']);
         $contentMedia->setSize($mediaInfo['size']);
         $contentMedia->setHeight($mediaInfo['height']);
         $contentMedia->setWidth($mediaInfo['width']);
         if (!is_null($mediaInfo['binary'])) {
             $contentMedia->setData($mediaInfo['binary']);
         }
         $contentMedia->setContent($content);
         $this->em()->persist($contentMedia);
         $content->setSlug($this->cm()->generateSlug($content->getTitle(), $content->getType(), $parentId));
         $content->setStatus(ContentPublishType::Published);
         $this->cm()->save($content);
     }
     return new Response('Ok', 200);
 }