Squashed commit of the following: commit 1f1175a4d944f573868dc2282e62fbd1b4e88b6a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun Apr 22 18:46:02 2018 +0100 Fix file upload tests to reflect that we have now inlined the file upload to S3 commit 40d2af5b76e8f390d0275830390dd89ab4d12f54 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun Apr 22 18:45:20 2018 +0100 Markup the bookmrark test that uses puppeteer, doesn’t play nicely with my old MacBook and let’s me skip that one test commit bbae1557c87d4d8f3b324abda1a6b8bf66acb8d8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Sun Apr 22 17:56:25 2018 +0100 Inline the S3 upload for media Smaller images can then be uploaded by the ProcessMedia job. Inline-ing the upload prevents an S3 URL being sent that will initially 404.
70 lines
2.3 KiB
PHP
70 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Intervention\Image\ImageManager;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Intervention\Image\Exception\NotReadableException;
|
|
|
|
class ProcessMedia implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $filename;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param string $filename
|
|
*/
|
|
public function __construct(string $filename)
|
|
{
|
|
$this->filename = $filename;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @param \Intervention\Image\ImageManager $manager
|
|
*/
|
|
public function handle(ImageManager $manager)
|
|
{
|
|
//open file
|
|
try {
|
|
$image = $manager->make(storage_path('app') . '/' . $this->filename);
|
|
} catch (NotReadableException $exception) {
|
|
// not an image; delete file and end job
|
|
unlink(storage_path('app') . '/' . $this->filename);
|
|
|
|
return;
|
|
}
|
|
//create smaller versions if necessary
|
|
if ($image->width() > 1000) {
|
|
$filenameParts = explode('.', $this->filename);
|
|
$extension = array_pop($filenameParts);
|
|
// the following acheives this data flow
|
|
// foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
|
|
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
|
|
return $carry . '.' . $item;
|
|
}, ''), '.');
|
|
$medium = $image->resize(1000, null, function ($constraint) {
|
|
$constraint->aspectRatio();
|
|
});
|
|
Storage::disk('s3')->put('media/'. $basename . '-medium.' . $extension, (string) $medium->encode());
|
|
$small = $image->resize(500, null, function ($constraint) {
|
|
$constraint->aspectRatio();
|
|
});
|
|
Storage::disk('s3')->put('media/' . $basename . '-small.' . $extension, (string) $small->encode());
|
|
}
|
|
|
|
// now we can delete the locally saved image
|
|
unlink(storage_path('app') . '/' . $this->filename);
|
|
}
|
|
}
|