Started work on replacing guzzle/guzzle (v3) with guzzlehttp/guzzle (v6.)

This commit is contained in:
Andy Heathershaw 2020-04-18 22:41:30 +01:00
parent fdf4d72236
commit e3892a037f
11 changed files with 38 additions and 41 deletions

View File

@ -4,9 +4,10 @@ namespace App\AlbumSources;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
use App\Photo; use App\Photo;
use Guzzle\Http\EntityBody; use GuzzleHttp\Exception\GuzzleException;
use Guzzle\Http\Exception\ClientErrorResponseException; use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use function GuzzleHttp\Psr7\stream_for;
class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource
{ {
@ -37,7 +38,7 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQ
'Key' => $fileToDelete 'Key' => $fileToDelete
]); ]);
} }
catch (ClientErrorResponseException $ex) catch (GuzzleException $ex)
{ {
// Don't worry if the file no longer exists // Don't worry if the file no longer exists
Log::warning('Failed deleting image from S3.', ['error' => $ex->getMessage(), 'path' => $fileToDelete]); Log::warning('Failed deleting image from S3.', ['error' => $ex->getMessage(), 'path' => $fileToDelete]);
@ -61,7 +62,7 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQ
'Key' => $this->getPathToPhoto($photo, $thumbnail) 'Key' => $this->getPathToPhoto($photo, $thumbnail)
]); ]);
} }
catch (ClientErrorResponseException $ex) catch (GuzzleException $ex)
{ {
// Don't worry if the file no longer exists // Don't worry if the file no longer exists
Log::warning('Failed deleting image from S3.', ['error' => $ex->getMessage(), 'path' => $photoPath]); Log::warning('Failed deleting image from S3.', ['error' => $ex->getMessage(), 'path' => $photoPath]);
@ -91,7 +92,7 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQ
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
public function fetchPhotoContent(Photo $photo, $thumbnail = null) public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{ {
@ -105,7 +106,7 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQ
'SaveAs' => $tempFile 'SaveAs' => $tempFile
]); ]);
return EntityBody::factory(fopen($tempFile, 'r+')); return stream_for(fopen($tempFile, 'r+'));
} }
finally finally
{ {

View File

@ -6,8 +6,9 @@ use App\BackblazeB2FileIdCache;
use App\Photo; use App\Photo;
use App\Services\BackblazeB2Service; use App\Services\BackblazeB2Service;
use App\Storage; use App\Storage;
use Guzzle\Http\EntityBody; use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use function GuzzleHttp\Psr7\stream_for;
class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
{ {
@ -67,7 +68,7 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
public function fetchPhotoContent(Photo $photo, $thumbnail = null) public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{ {
@ -78,12 +79,10 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
$b2Cache = $this->getB2FileFromCache($pathOnStorage); $b2Cache = $this->getB2FileFromCache($pathOnStorage);
if (is_null($b2Cache)) if (is_null($b2Cache))
{ {
return EntityBody::fromString(''); return stream_for('');
} }
return EntityBody::fromString( return stream_for($this->getClient()->downloadFile($b2Cache->b2_file_id));
$this->getClient()->downloadFile($b2Cache->b2_file_id)
);
} }
/** /**

View File

@ -4,7 +4,8 @@ namespace App\AlbumSources;
use App\Photo; use App\Photo;
use App\Services\DropboxService; use App\Services\DropboxService;
use Guzzle\Http\EntityBody; use GuzzleHttp\Psr7\Stream;
use function GuzzleHttp\Psr7\stream_for;
class DropboxSource extends AlbumSourceBase implements IAlbumSource class DropboxSource extends AlbumSourceBase implements IAlbumSource
{ {
@ -37,13 +38,13 @@ class DropboxSource extends AlbumSourceBase implements IAlbumSource
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
public function fetchPhotoContent(Photo $photo, $thumbnail = null) public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{ {
$pathOnStorage = $this->getPathToPhoto($photo, $thumbnail); $pathOnStorage = $this->getPathToPhoto($photo, $thumbnail);
return EntityBody::fromString($this->getClient()->downloadFile($pathOnStorage)); return stream_for($this->getClient()->downloadFile($pathOnStorage));
} }
/** /**

View File

@ -5,7 +5,7 @@ namespace App\AlbumSources;
use App\Album; use App\Album;
use App\Photo; use App\Photo;
use App\Storage; use App\Storage;
use Guzzle\Http\EntityBody; use GuzzleHttp\Psr7\Stream;
interface IAlbumSource interface IAlbumSource
{ {
@ -27,7 +27,7 @@ interface IAlbumSource
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
function fetchPhotoContent(Photo $photo, $thumbnail = null); function fetchPhotoContent(Photo $photo, $thumbnail = null);

View File

@ -5,8 +5,9 @@ namespace App\AlbumSources;
use App\Helpers\FileHelper; use App\Helpers\FileHelper;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
use App\Photo; use App\Photo;
use Guzzle\Http\EntityBody; use GuzzleHttp\Psr7\Stream;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
use function GuzzleHttp\Psr7\stream_for;
/** /**
* Driver for managing files on the local filesystem. * Driver for managing files on the local filesystem.
@ -37,7 +38,7 @@ class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource, IAn
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
public function fetchPhotoContent(Photo $photo, $thumbnail = null) public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{ {
@ -50,7 +51,7 @@ class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource, IAn
'r+' 'r+'
); );
return EntityBody::factory($fh); return stream_for($fh);
} }
public function getName() public function getName()

View File

@ -3,12 +3,11 @@
namespace App\AlbumSources; namespace App\AlbumSources;
use App\Photo; use App\Photo;
use Guzzle\Http\EntityBody; use GuzzleHttp\Exception\GuzzleException;
use Guzzle\Http\Exception\ClientErrorResponseException; use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use OpenCloud\ObjectStore\Exception\ObjectNotFoundException; use OpenStack\OpenStack;
use OpenCloud\OpenStack; use function GuzzleHttp\Psr7\stream_for;
use Symfony\Component\HttpFoundation\File\File;
/** /**
* Driver for managing files on an OpenStack Keystone+Swift compatible platform. * Driver for managing files on an OpenStack Keystone+Swift compatible platform.
@ -40,7 +39,7 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource
{ {
$this->getContainer()->deleteObject($photoPath); $this->getContainer()->deleteObject($photoPath);
} }
catch (ClientErrorResponseException $ex) catch (GuzzleException $ex)
{ {
// Don't worry if the file no longer exists // Don't worry if the file no longer exists
Log::warning('Failed deleting image from OpenStack.', ['error' => $ex->getMessage(), 'path' => $photoPath]); Log::warning('Failed deleting image from OpenStack.', ['error' => $ex->getMessage(), 'path' => $photoPath]);
@ -51,13 +50,13 @@ class OpenStackSource extends AlbumSourceBase implements IAlbumSource
* Fetches the contents of a thumbnail for a photo. * Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for. * @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.) * @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return EntityBody * @return Stream
*/ */
public function fetchPhotoContent(Photo $photo, $thumbnail = null) public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{ {
$object = $this->getContainer()->getObject($this->getPathToPhoto($photo, $thumbnail)); $object = $this->getContainer()->getObject($this->getPathToPhoto($photo, $thumbnail));
return $object->getContent(); return stream_for($object->getContent());
} }
/** /**

View File

@ -3,8 +3,6 @@
namespace App\AlbumSources; namespace App\AlbumSources;
use App\Photo; use App\Photo;
use App\Storage;
use OpenCloud\Rackspace;
class RackspaceSource extends OpenStackSource class RackspaceSource extends OpenStackSource
{ {

View File

@ -6,17 +6,15 @@ use App\Album;
use App\Facade\Theme; use App\Facade\Theme;
use App\Facade\UserConfig; use App\Facade\UserConfig;
use App\Helpers\DbHelper; use App\Helpers\DbHelper;
use App\Helpers\MiscHelper;
use app\Http\Controllers\Admin\AlbumController;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Middleware\VerifyCsrfToken;
use App\Photo; use App\Photo;
use App\VisitorHit; use App\VisitorHit;
use Guzzle\Http\Mimetypes; use GuzzleHttp\Psr7\Stream;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function GuzzleHttp\Psr7\mimetype_from_extension;
class PhotoController extends Controller class PhotoController extends Controller
{ {
@ -71,8 +69,9 @@ class PhotoController extends Controller
}); });
} }
/** @var Stream $photoStream */
$photoStream = $album->getAlbumSource()->fetchPhotoContent($photo, $thumbnail); $photoStream = $album->getAlbumSource()->fetchPhotoContent($photo, $thumbnail);
$mimeType = Mimetypes::getInstance()->fromFilename($photo->storage_file_name); $mimeType = mimetype_from_extension(pathinfo($photo->storage_file_name, PATHINFO_EXTENSION));
return response()->stream( return response()->stream(
function() use ($photoStream) function() use ($photoStream)
@ -81,7 +80,7 @@ class PhotoController extends Controller
}, },
200, 200,
[ [
'Content-Length' => $photoStream->getContentLength(), 'Content-Length' => strlen($photoStream->getContents()),
'Content-Type' => $mimeType 'Content-Type' => $mimeType
] ]
); );

View File

@ -6,12 +6,10 @@ use App\Album;
use App\AlbumSources\IAlbumSource; use App\AlbumSources\IAlbumSource;
use App\AlbumSources\IAnalysisQueueSource; use App\AlbumSources\IAnalysisQueueSource;
use App\Helpers\AnalysisQueueHelper; use App\Helpers\AnalysisQueueHelper;
use App\Helpers\FileHelper;
use App\Helpers\ImageHelper; use App\Helpers\ImageHelper;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
use App\Helpers\ThemeHelper; use App\Helpers\ThemeHelper;
use App\Photo; use App\Photo;
use Symfony\Component\HttpFoundation\File\File;
class PhotoService class PhotoService
{ {
@ -207,7 +205,7 @@ class PhotoService
$stream = $this->albumSource->fetchPhotoContent($this->photo); $stream = $this->albumSource->fetchPhotoContent($this->photo);
$stream->rewind(); $stream->rewind();
while (!$stream->feof()) while (!$stream->eof())
{ {
fwrite($photoHandle, $stream->read(4096)); fwrite($photoHandle, $stream->read(4096));
} }
@ -326,7 +324,7 @@ class PhotoService
$stream = $this->albumSource->fetchPhotoContent($this->photo); $stream = $this->albumSource->fetchPhotoContent($this->photo);
$stream->rewind(); $stream->rewind();
while (!$stream->feof()) while (!$stream->eof())
{ {
fwrite($photoHandle, $stream->read(4096)); fwrite($photoHandle, $stream->read(4096));
} }

View File

@ -10,6 +10,7 @@
"ext-json": "*", "ext-json": "*",
"aws/aws-sdk-php": "^3.134", "aws/aws-sdk-php": "^3.134",
"doctrine/dbal": "^2.5", "doctrine/dbal": "^2.5",
"guzzlehttp/guzzle": "^6.5",
"laravel/framework": "^6.0", "laravel/framework": "^6.0",
"laravel/socialite": "^4.3", "laravel/socialite": "^4.3",
"php-amqplib/php-amqplib": "^2.9", "php-amqplib/php-amqplib": "^2.9",

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "62c34970835f89cbd6bb144d978a42c0", "content-hash": "9a11d044f41aef4c08fcddf00fd6f7ed",
"packages": [ "packages": [
{ {
"name": "aws/aws-sdk-php", "name": "aws/aws-sdk-php",