From fd2fbea03f01fb5c8ca9be77847bd050d69bcc75 Mon Sep 17 00:00:00 2001 From: James Collins Date: Mon, 10 Apr 2023 14:47:53 +1000 Subject: [PATCH] S3 jobs --- app/Jobs/MoveMediaJob.php | 84 +++++++++++++++++ app/Jobs/StoreUploadedFileJob.php | 152 ++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 app/Jobs/MoveMediaJob.php create mode 100644 app/Jobs/StoreUploadedFileJob.php diff --git a/app/Jobs/MoveMediaJob.php b/app/Jobs/MoveMediaJob.php new file mode 100644 index 0000000..81b7983 --- /dev/null +++ b/app/Jobs/MoveMediaJob.php @@ -0,0 +1,84 @@ +media = $media; + $this->newStorage = $newStorage; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // Don't continue if the media is already on the new storage disk + if ($this->media->storage === $this->newStorage) { + return; + } + + $this->media->status = 'moving file'; + $this->media->save(); + + $files = ["/{$this->media->name}"]; + if (empty($this->media->variants) === false) { + foreach ($this->media->variants as $variant => $name) { + $files[] = "/{$name}"; + } + } + + $this->media->invalidateCFCache(); + + // Move the files from the old storage disk to the new storage disk + foreach ($files as $file) { + Storage::disk($this->newStorage)->put($file, Storage::disk($this->media->storage)->get($file)); + Storage::disk($this->media->storage)->delete($file); + } + + // Update the media model with the new storage and save it to the database + $this->media->storage = $this->newStorage; + $this->media->status = ''; + $this->media->save(); + } +} diff --git a/app/Jobs/StoreUploadedFileJob.php b/app/Jobs/StoreUploadedFileJob.php new file mode 100644 index 0000000..f2418cb --- /dev/null +++ b/app/Jobs/StoreUploadedFileJob.php @@ -0,0 +1,152 @@ +media = $media; + $this->uploadedFilePath = $filePath; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $storageDisk = $this->media->storage; + $fileName = $this->media->name; + + try { + $this->media->status = "Uploading to CDN"; + $this->media->save(); + Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName); + Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}"); + + if (strpos($this->media->mime_type, 'image/') === 0) { + $this->media->status = "Optimizing image"; + $this->media->save(); + + // Generate additional image sizes + $sizes = [ + 'thumb' => [150, 150], + 'small' => [300, 225], + 'medium' => [768, 576], + 'large' => [1024, 768], + 'xlarge' => [1536, 1152], + 'xxlarge' => [2048, 1536], + 'scaled' => [2560, 1920] + ]; + + $variants = []; + + $originalImage = Image::make($this->uploadedFilePath); + $optimizerChain = OptimizerChainFactory::create(); + + $dimensions = [$originalImage->getWidth(), $originalImage->getHeight()]; + $this->media->dimensions = implode('x', $dimensions); + + foreach ($sizes as $variantName => $size) { + $postfix = "{$size[0]}x{$size[1]}"; + if ($variantName === 'scaled') { + $postfix = 'scaled'; + } + + $newFilename = pathinfo($this->media->name, PATHINFO_FILENAME) . "-$postfix." . pathinfo($this->media->name, PATHINFO_EXTENSION); + + // Get the largest available variant + if ($dimensions[0] >= $size[0] && $dimensions[1] >= $size[1]) { + // $largestVariant = $newFilename; + + // Resize the image to the variant size if its dimensions are greater than the specified size + $image = clone $originalImage; + + $imageSize = $image->getSize(); + if ($imageSize->getWidth() > $size[0] || $imageSize->getHeight() > $size[1]) { + $image->resize($size[0], $size[1], function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + }); + $image->resizeCanvas($size[0], $size[1], 'center', false, '#FFFFFF'); + } + + // Store the variant in the variants array + $variants[$variantName] = $newFilename; + + // Optimize and store image + $tempImagePath = tempnam(sys_get_temp_dir(), 'optimize'); + $image->save($tempImagePath); + $optimizerChain->optimize($tempImagePath); + Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename); + unlink($tempImagePath); + }//end if + }//end foreach + + // Set missing variants to the largest available variant + foreach ($sizes as $variantName => $size) { + if (isset($variants[$variantName]) === false) { + $variants[$variantName] = $this->media->name; + } + } + + $this->media->variants = $variants; + }//end if + + if ($this->uploadedFilePath !== '') { + unlink($this->uploadedFilePath); + } + + $this->media->status = ''; + $this->media->save(); + } catch (\Exception $e) { + Log::error($e->getMessage()); + $this->media->status = "Failed"; + $this->media->save(); + $this->fail($e); + }//end try + } +}