From 401d859f7a8f78f432f731791e03849394413077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 29 Jan 2020 01:12:52 +0100 Subject: [PATCH] Refactor file uploads to support multiple disks (#3257) --- app/Jobs/Util/UploadFile.php | 84 ++++++++++++++-------------- tests/Integration/UploadFileTest.php | 11 ++-- tests/Unit/Migration/ImportTest.php | 2 - 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/app/Jobs/Util/UploadFile.php b/app/Jobs/Util/UploadFile.php index 30cddeb10eeb..729dc1e383ff 100644 --- a/app/Jobs/Util/UploadFile.php +++ b/app/Jobs/Util/UploadFile.php @@ -11,44 +11,52 @@ namespace App\Jobs\Util; -use App\Libraries\MultiDB; use App\Models\Document; -use App\Utils\Traits\MakesHash; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Dispatchable; +use App\Libraries\MultiDB; +use Illuminate\Support\Str; use Illuminate\Http\Request; -use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Bus\Queueable; +use App\Utils\Traits\MakesHash; +use Intervention\Image\ImageManager; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Storage; -use Intervention\Image\ImageManager; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; class UploadFile implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash; - use MakesHash; + const IMAGE = 1; + const DOCUMENT = 2; + + const PROPERTIES = [ + self::IMAGE => [ + 'path' => 'public/images', + ], + self::DOCUMENT => [ + 'path' => 'public/documents', + ] + ]; protected $file; - protected $user; - protected $company; + protected $type; public $entity; - /** - * Create a new job instance. - * - * @return void - */ - - public function __construct($file, $user, $company, $entity) + public function __construct($file, $type, $user, $company, $entity, $disk = 'local') { $this->file = $file; + $this->type = $type; $this->user = $user; $this->company = $company; $this->entity = $entity; + $this->disk = $disk ?? config('filesystems.default'); + + MultiDB::setDB($this->company->db); } /** @@ -58,41 +66,31 @@ class UploadFile implements ShouldQueue */ public function handle() : ?Document { - MultiDB::setDB($this->company->db); + $instance = Storage::disk($this->disk)->putFileAs( + self::PROPERTIES[$this->type]['path'], $this->file, $this->file->hashName() + ); - $path = $this->encodePrimaryKey($this->company->id); + if (in_array($this->file->extension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) { + $image_size = getimagesize($this->file); - $file_path = $path . '/' . $this->file->hashName(); - - Storage::put($path, $this->file); - - $width = 0; - - $height = 0; - - if (in_array($this->file->getClientOriginalExtension(), ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) { - $imageSize = getimagesize($this->file); - - $width = $imageSize[0]; - - $height = $imageSize[1]; + $width = $image_size[0]; + $height = $image_size[1]; } $document = new Document(); $document->user_id = $this->user->id; $document->company_id = $this->company->id; - $document->path = $path; + $document->path = $instance; $document->name = $this->file->getClientOriginalName(); - $document->type = $this->file->getClientOriginalExtension(); - $document->disk = config('filesystems.default'); + $document->type = $this->file->extension(); + $document->disk = $this->disk; $document->hash = $this->file->hashName(); - $document->size = filesize(Storage::path($file_path)); - $document->width = $width; - $document->height = $height; + $document->size = $this->file->getSize(); + $document->width = isset($width) ?? null; + $document->height = isset($height) ?? null; - $preview_path = $this->encodePrimaryKey($this->company->id); - - $document->preview = $this->generatePreview($preview_path); + // $preview_path = $this->encodePrimaryKey($this->company->id); + // $document->preview = $this->generatePreview($preview_path); $this->entity->documents()->save($document); diff --git a/tests/Integration/UploadFileTest.php b/tests/Integration/UploadFileTest.php index 3e059e3fb40b..31bb992da3d6 100644 --- a/tests/Integration/UploadFileTest.php +++ b/tests/Integration/UploadFileTest.php @@ -40,13 +40,12 @@ class UploadFileTest extends TestCase public function testFileUploadWorks() { + $image = UploadedFile::fake()->image('avatar.jpg'); - $document = UploadFile::dispatchNow(UploadedFile::fake()->image('avatar.jpg'), $this->invoice->user, $this->invoice->company, $this->invoice); + $document = UploadFile::dispatchNow( + $image, UploadFile::IMAGE, $this->invoice->user, $this->invoice->company, $this->invoice + ); $this->assertNotNull($document); - } - - - -} \ No newline at end of file +} diff --git a/tests/Unit/Migration/ImportTest.php b/tests/Unit/Migration/ImportTest.php index 55caa7f40114..82be96c83897 100644 --- a/tests/Unit/Migration/ImportTest.php +++ b/tests/Unit/Migration/ImportTest.php @@ -43,8 +43,6 @@ class ImportTest extends TestCase } - } - public function testExceptionOnUnavailableResource() { $data['panda_bears'] = [