Пример #1
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;
 }
 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);
 }
 /**
  * Downloads a gif on the server. Useful if our domain is not accepted as referrer (avoids hotlinking)
  * @param Gif $gif
  * @return string
  */
 public function download(Gif $gif)
 {
     $downloadDir = $this->getDownloadDir();
     $gifUrl = $gif->getGifUrl();
     // Generate filename
     $fileName = $gif->getPermalink() . '.' . Util::getFileExtension($gifUrl);
     $i = 0;
     while (file_exists($downloadDir . $fileName)) {
         $fileName = $gif->getPermalink() . '_' . $i . '.' . Util::getFileExtension($gifUrl);
         $i++;
     }
     // Download file
     file_put_contents($downloadDir . $fileName, fopen($gifUrl, 'r'));
     // Check if file has been successfully downloaded (permissions issues)
     if (file_exists($downloadDir . $fileName)) {
         // Generate client-side URL
         $url = $this->requestContextScheme . '://' . $this->requestContextHost . $this->requestContextBaseUrl . '/gifs/' . $fileName;
         $gif->setOriginalGifUrl($gifUrl);
         $gif->setGifUrl($url);
         return $url;
     } 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)];
 }