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