public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media = null)
 {
     // If we are on a private node, we won't do any remote calls (just as a precaution until
     // we can configure this from config.php for the private nodes)
     if (common_config('site', 'private')) {
         return true;
     }
     if ($media !== 'image') {
         return true;
     }
     // If there is a local filename, it is either a local file already or has already been downloaded.
     if (!empty($file->filename)) {
         return true;
     }
     $this->checkWhitelist($file->getUrl());
     // First we download the file to memory and test whether it's actually an image file
     $imgData = HTTPClient::quickGet($file->getUrl());
     common_debug(sprintf('Downloading remote file id==%u with URL: %s', $file->id, $file->getUrl()));
     $info = @getimagesizefromstring($imgData);
     if ($info === false) {
         throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $file->getUrl());
     } elseif (!$info[0] || !$info[1]) {
         throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)'));
     }
     $filehash = hash(File::FILEHASH_ALG, $imgData);
     try {
         // Exception will be thrown before $file is set to anything, so old $file value will be kept
         $file = File::getByHash($filehash);
         //FIXME: Add some code so we don't have to store duplicate File rows for same hash files.
     } catch (NoResultException $e) {
         $filename = $filehash . '.' . common_supported_mime_to_ext($info['mime']);
         $fullpath = File::path($filename);
         // Write the file to disk if it doesn't exist yet. Throw Exception on failure.
         if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) {
             throw new ServerException(_('Could not write downloaded file to disk.'));
         }
         // Updated our database for the file record
         $orig = clone $file;
         $file->filehash = $filehash;
         $file->filename = $filename;
         $file->width = $info[0];
         // array indexes documented on php.net:
         $file->height = $info[1];
         // https://php.net/manual/en/function.getimagesize.php
         // Throws exception on failure.
         $file->updateWithKeys($orig, 'id');
     }
     // Get rid of the file from memory
     unset($imgData);
     $imgPath = $file->getPath();
     return false;
 }
Example #2
0
 protected function storeRemoteFileThumbnail(File_thumbnail $thumbnail)
 {
     if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) {
         throw new AlreadyFulfilledException(sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id));
     }
     $url = $thumbnail->getUrl();
     $this->checkWhitelist($url);
     // First we download the file to memory and test whether it's actually an image file
     // FIXME: To support remote video/whatever files, this needs reworking.
     common_debug(sprintf('Downloading remote thumbnail for file id==%u with thumbnail URL: %s', $thumbnail->file_id, $url));
     $imgData = HTTPClient::quickGet($url);
     $info = @getimagesizefromstring($imgData);
     if ($info === false) {
         throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $url);
     } elseif (!$info[0] || !$info[1]) {
         throw new UnsupportedMediaException(_('Image file had impossible geometry (0 width or height)'));
     }
     // We'll trust sha256 not to have collision issues any time soon :)
     $filename = hash('sha256', $imgData) . '.' . common_supported_mime_to_ext($info['mime']);
     $fullpath = File_thumbnail::path($filename);
     // Write the file to disk. Throw Exception on failure
     if (!file_exists($fullpath) && file_put_contents($fullpath, $imgData) === false) {
         throw new ServerException(_('Could not write downloaded file to disk.'));
     }
     // Get rid of the file from memory
     unset($imgData);
     // Updated our database for the file record
     $orig = clone $thumbnail;
     $thumbnail->filename = $filename;
     $thumbnail->width = $info[0];
     // array indexes documented on php.net:
     $thumbnail->height = $info[1];
     // https://php.net/manual/en/function.getimagesize.php
     // Throws exception on failure.
     $thumbnail->updateWithKeys($orig, 'file_id');
 }
Example #3
0
 static function guessMimeExtension($mimetype)
 {
     try {
         $ext = common_supported_mime_to_ext($mimetype);
     } catch (Exception $e) {
         // We don't support this mimetype, but let's guess the extension
         $ext = substr(strrchr($mimetype, '/'), 1);
     }
     return strtolower($ext);
 }