/**
  * 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;
     }
 }
 /**
  * @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;
 }