public function postGif(Gif $gif)
 {
     $gifUrl = $this->router->generate('gif', ['permalink' => $gif->getPermalink()], true);
     $gifUrl = Util::fixSymfonyGeneratedURLs($gifUrl);
     $tweetContent = $gif->generateTweet($gifUrl);
     return $this->postTweet($tweetContent);
 }
Exemplo n.º 2
0
 /**
  * Publishes a gif and posts a link on social networks
  * @param Gif $gif
  * @return bool
  */
 public function publish(Gif $gif)
 {
     if (!$gif) {
         return false;
     }
     if (!$gif->getGifStatus() == GifState::ACCEPTED) {
         return false;
     }
     $gif->setPublishDate(new DateTime());
     $gif->setGifStatus(GifState::PUBLISHED);
     $gif->generateUrlReadyPermalink();
     // Check if permalink is unique
     /** @var GifRepository $gifsRepo */
     $gifsRepo = $this->em->getRepository('LjdsBundle:Gif');
     $permalink = $gif->getPermalink();
     $i = 1;
     while (!empty($gifsRepo->findBy(['permalink' => $gif->getPermalink(), 'gifStatus' => GifState::PUBLISHED]))) {
         // Generate a new permalink
         $gif->setPermalink($permalink . $i);
         $i++;
     }
     $this->em->flush();
     if ($this->facebookAutopost) {
         $this->facebookService->postGif($gif);
     }
     if ($this->twitterAutopost) {
         $this->twitterService->postGif($gif);
     }
     return true;
 }
 /**
  * Deletes the downloaded gif once the original gifs gets deleted
  * @param Gif $gif
  * @return bool
  */
 public function delete(Gif $gif)
 {
     $downloadDir = $this->getDownloadDir();
     // Get file path from gif URL
     $fileName = basename($gif->getGifUrl());
     if (file_exists($downloadDir . $fileName)) {
         unlink($downloadDir . $fileName);
         return true;
     } else {
         return false;
     }
 }
 public function postGif(Gif $gif)
 {
     $appId = $this->container->getParameter('facebook_app_id');
     $appSecret = $this->container->getParameter('facebook_app_secret');
     $accessToken = $this->container->getParameter('facebook_access_token');
     FacebookSession::setDefaultApplication($appId, $appSecret);
     // Open Facebook SDK session
     $session = FacebookSession::newAppSession();
     // To validate the session:
     try {
         $session->validate();
     } catch (FacebookRequestException $ex) {
         // Session not valid, Graph API returned an exception with the reason.
         //echo $ex->getMessage();
         return false;
     } catch (\Exception $ex) {
         // Graph API returned info, but it may mismatch the current app or have expired.
         //echo $ex->getMessage();
         return false;
     }
     $link = $this->router->generate('gif', ['permalink' => $gif->getPermalink()], true);
     $link = Util::fixSymfonyGeneratedURLs($link);
     try {
         $requestParaps = ['access_token' => $accessToken, 'link' => $link, 'message' => $gif->getCaption()];
         // Only provide picture if this is a gif
         if ($gif->getFileType() == 'gif') {
             $requestParaps['picture'] = $gif->getGifUrl();
         }
         $facebookRequest = new FacebookRequest($session, 'POST', '/joiesDeSupinfo/feed', $requestParaps);
         /*$response = */
         $facebookRequest->execute()->getGraphObject();
         //echo "Posted with id: " . $response->getProperty('id');
     } catch (FacebookRequestException $e) {
         //echo "Exception occured, code: " . $e->getCode();
         //echo " with message: " . $e->getMessage();
         return false;
     }
     return true;
 }
 private function getJson(Gif $gif)
 {
     return ['caption' => $gif->getCaption(), 'type' => $gif->getFileType(), 'file' => $gif->getGifUrl(), 'permalink' => $this->generateUrl('gif', ['permalink' => $gif->getPermalink()], true)];
 }
 /**
  * @Route("/submit", name="submit", options={"sitemap"=true})
  */
 public function submitAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $response = new Response();
     $gifSubmitted = false;
     $gifSubmittedError = false;
     // Form is submitted
     $post = $request->request;
     if ($post->has('caption')) {
         // Check if mandatory fields are filled up
         if (trim($post->get('submittedBy')) == '' || trim($post->get('caption')) == '' || trim($post->get('gifUrl')) == '') {
             $gifSubmittedError = "un des champs requis n'est pas renseigné, veuillez rééssayer.";
         }
         // Check if URL is a gif/mp4 video
         $allowedFilesTypes = ['gif', 'mp4', 'webm', 'ogg'];
         $gifUrl = $post->get('gifUrl');
         if (!in_array(Util::getFileExtension($gifUrl), $allowedFilesTypes)) {
             $gifSubmittedError = "l'URL ne semble pas être celle d'un fichier gif. Les types autorisés sont : gif, mp4, webm et ogg.";
         }
         $gifSubmitted = true;
         $submittedBy = $post->get('submittedBy');
         $caption = $post->get('caption');
         $source = $post->get('source');
         $label = $post->get('label');
         // Create cookie with submittedBy value
         $cookie = new Cookie('submittedBy', $submittedBy, time() + 60 * 60 * 24 * 30);
         $response->headers->setCookie($cookie);
         if ($gifSubmittedError === false) {
             $gif = new Gif();
             $gif->setCaption($caption);
             $gif->setGifUrl($gifUrl);
             $gif->setReportStatus(ReportState::NONE);
             $gif->setGifStatus(GifState::SUBMITTED);
             $gif->generateUrlReadyPermalink();
             $gif->setSubmissionDate(new \DateTime());
             $gif->setSubmittedBy($submittedBy);
             $gif->setSource($source);
             $gif->setLabel($label);
             $em->persist($gif);
             $em->flush();
             $gifRepo = $this->getDoctrine()->getRepository('LjdsBundle:Gif');
             $params['estimatedPublishDate'] = $gifRepo->getEstimatedPublicationDate();
         } else {
             $params['submitError'] = $gifSubmittedError;
         }
     }
     $params['submittedBy'] = $request->cookies->has('submittedBy') ? $request->cookies->get('submittedBy') : '';
     $params['submitted'] = $gifSubmitted;
     $response->setContent($this->renderView('LjdsBundle:Gifs:submit.html.twig', $params));
     return $response;
 }