added support to ignore existing files

This commit is contained in:
2023-04-21 10:05:40 +10:00
parent 68d59eda69
commit 6c25cd029f

View File

@@ -37,18 +37,26 @@ class StoreUploadedFileJob implements ShouldQueue
*/ */
protected $uploadedFilePath; protected $uploadedFilePath;
/**
* Replace existing files
*
* @var string
*/
protected $replaceExisting;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @param Media $media The media model. * @param Media $media The media model.
* @param string $filePath The uploaded file. * @param string $filePath The uploaded file.
* @param boolean $replaceExisting Replace existing files.
* @return void * @return void
*/ */
public function __construct(Media $media, string $filePath) public function __construct(Media $media, string $filePath, bool $replaceExisting = true)
{ {
$this->media = $media; $this->media = $media;
$this->uploadedFilePath = $filePath; $this->uploadedFilePath = $filePath;
$this->replaceExisting = $replaceExisting;
} }
/** /**
@@ -64,8 +72,13 @@ class StoreUploadedFileJob implements ShouldQueue
try { try {
$this->media->status = "Uploading to CDN"; $this->media->status = "Uploading to CDN";
$this->media->save(); $this->media->save();
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName);
Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}"); if (Storage::disk($storageDisk)->exists($fileName) == false || $this->replaceExisting == true) {
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName);
Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}");
} else {
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}");
}
if (strpos($this->media->mime_type, 'image/') === 0) { if (strpos($this->media->mime_type, 'image/') === 0) {
$this->media->status = "Optimizing image"; $this->media->status = "Optimizing image";
@@ -98,32 +111,36 @@ class StoreUploadedFileJob implements ShouldQueue
$newFilename = pathinfo($this->media->name, PATHINFO_FILENAME) . "-$postfix." . pathinfo($this->media->name, PATHINFO_EXTENSION); $newFilename = pathinfo($this->media->name, PATHINFO_FILENAME) . "-$postfix." . pathinfo($this->media->name, PATHINFO_EXTENSION);
// Get the largest available variant // Store the variant in the variants array
if ($dimensions[0] >= $size[0] && $dimensions[1] >= $size[1]) { $variants[$variantName] = $newFilename;
// $largestVariant = $newFilename;
// Resize the image to the variant size if its dimensions are greater than the specified size if (Storage::disk($storageDisk)->exists($newFilename) == false || $this->replaceExisting == true) {
$image = clone $originalImage; // Get the largest available variant
if ($dimensions[0] >= $size[0] && $dimensions[1] >= $size[1]) {
// $largestVariant = $newFilename;
$imageSize = $image->getSize(); // Resize the image to the variant size if its dimensions are greater than the specified size
if ($imageSize->getWidth() > $size[0] || $imageSize->getHeight() > $size[1]) { $image = clone $originalImage;
$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 $imageSize = $image->getSize();
$variants[$variantName] = $newFilename; 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');
}
// Optimize and store image // Optimize and store image
$tempImagePath = tempnam(sys_get_temp_dir(), 'optimize'); $tempImagePath = tempnam(sys_get_temp_dir(), 'optimize');
$image->save($tempImagePath); $image->save($tempImagePath);
$optimizerChain->optimize($tempImagePath); $optimizerChain->optimize($tempImagePath);
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename); Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename);
unlink($tempImagePath); unlink($tempImagePath);
}//end if }//end if
} else {
Log::info("variant {$variantName} already exists for file {$fileName}");
}
}//end foreach }//end foreach
// Set missing variants to the largest available variant // Set missing variants to the largest available variant