From 8b5b3204c10cdfa60059291094963a50f747eafe Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sun, 22 Apr 2018 18:53:26 +0100 Subject: [PATCH] Feature, upload media to S3 as part of micropub request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed commit of the following: commit 1f1175a4d944f573868dc2282e62fbd1b4e88b6a Author: Jonny Barnes 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 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 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. --- app/Http/Controllers/MicropubController.php | 8 +++++++ app/Jobs/ProcessMedia.php | 6 ----- tests/Feature/MicropubControllerTest.php | 26 +++++++++++++++++---- tests/Unit/BookmarksTest.php | 3 +++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 69e45e35..02d5a580 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -6,6 +6,7 @@ namespace App\Http\Controllers; use Monolog\Logger; use Ramsey\Uuid\Uuid; +use Illuminate\Http\File; use App\Jobs\ProcessMedia; use App\Services\TokenService; use Illuminate\Http\JsonResponse; @@ -203,6 +204,13 @@ class MicropubController extends Controller 'image_widths' => $width, ]); + // put the file on S3 initially, the ProcessMedia job may edit this + Storage::disk('s3')->putFileAs( + 'media', + new File(storage_path('app') . '/' . $filename), + $filename + ); + ProcessMedia::dispatch($filename); return response()->json([ diff --git a/app/Jobs/ProcessMedia.php b/app/Jobs/ProcessMedia.php index 9efbe37a..19c95910 100644 --- a/app/Jobs/ProcessMedia.php +++ b/app/Jobs/ProcessMedia.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace App\Jobs; -use Illuminate\Http\File; use Illuminate\Bus\Queueable; use Intervention\Image\ImageManager; use Illuminate\Queue\SerializesModels; @@ -37,11 +36,6 @@ class ProcessMedia implements ShouldQueue */ public function handle(ImageManager $manager) { - Storage::disk('s3')->putFileAs( - 'media', - new File(storage_path('app') . '/' . $this->filename), - $this->filename - ); //open file try { $image = $manager->make(storage_path('app') . '/' . $this->filename); diff --git a/tests/Feature/MicropubControllerTest.php b/tests/Feature/MicropubControllerTest.php index 20db8222..d9891d1b 100644 --- a/tests/Feature/MicropubControllerTest.php +++ b/tests/Feature/MicropubControllerTest.php @@ -770,7 +770,8 @@ class MicropubControllerTest extends TestCase public function test_media_endpoint_upload_a_file() { Queue::fake(); - Storage::fake('local'); + Storage::fake('s3'); + $file = __DIR__ . '/../aaron.png'; $response = $this->call( 'POST', @@ -778,7 +779,14 @@ class MicropubControllerTest extends TestCase [], [], [ - 'file' => UploadedFile::fake()->image('scrot.png', 1920, 1080)->size(250), + 'file' => new UploadedFile( + $file, + 'aaron.png', + 'image/png', + filesize(__DIR__ . '/../aaron.png'), + null, + true + ), ], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()] ); @@ -787,12 +795,14 @@ class MicropubControllerTest extends TestCase $filename = substr($path, 7); Queue::assertPushed(ProcessMedia::class); Storage::disk('local')->assertExists($filename); + // now remove file + unlink(storage_path('app/') . $filename); } public function test_media_endpoint_upload_an_audio_file() { Queue::fake(); - Storage::fake('local'); + Storage::fake('s3'); $file = __DIR__ . '/../audio.mp3'; $response = $this->call( @@ -810,12 +820,14 @@ class MicropubControllerTest extends TestCase $filename = substr($path, 7); Queue::assertPushed(ProcessMedia::class); Storage::disk('local')->assertExists($filename); + // now remove file + unlink(storage_path('app/') . $filename); } public function test_media_endpoint_upload_a_video_file() { Queue::fake(); - Storage::fake('local'); + Storage::fake('s3'); $file = __DIR__ . '/../video.ogv'; $response = $this->call( @@ -833,12 +845,14 @@ class MicropubControllerTest extends TestCase $filename = substr($path, 7); Queue::assertPushed(ProcessMedia::class); Storage::disk('local')->assertExists($filename); + // now remove file + unlink(storage_path('app/') . $filename); } public function test_media_endpoint_upload_a_document_file() { Queue::fake(); - Storage::fake('local'); + Storage::fake('s3'); $response = $this->call( 'POST', @@ -855,6 +869,8 @@ class MicropubControllerTest extends TestCase $filename = substr($path, 7); Queue::assertPushed(ProcessMedia::class); Storage::disk('local')->assertExists($filename); + // now remove file + unlink(storage_path('app/') . $filename); } public function test_media_endpoint_upload_an_invalid_file_return_error() diff --git a/tests/Unit/BookmarksTest.php b/tests/Unit/BookmarksTest.php index 83638e8b..78617273 100644 --- a/tests/Unit/BookmarksTest.php +++ b/tests/Unit/BookmarksTest.php @@ -11,6 +11,9 @@ use GuzzleHttp\Handler\MockHandler; class BookmarksTest extends TestCase { + /** + * @group puppeteer + */ public function test_screenshot_of_google() { $uuid = (new BookmarkService())->saveScreenshot('https://www.google.co.uk');