From 6d792c4559d73317d0111da5167303e818f8a5e3 Mon Sep 17 00:00:00 2001 From: Hamid Ghorashi Date: Mon, 10 Jun 2024 17:44:05 +0400 Subject: [PATCH 1/2] prepend the files path with upload directory while searching on S3 --- src/Publiux/laravelcdn/Providers/AwsS3Provider.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php index 6f7c482..de162f2 100644 --- a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php +++ b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php @@ -143,7 +143,7 @@ public function init($configurations) 'cloudfront' => $this->default['providers']['aws']['s3']['cloudfront']['use'], 'cloudfront_url' => $this->default['providers']['aws']['s3']['cloudfront']['cdn_url'], 'http' => $this->default['providers']['aws']['s3']['http'], - 'upload_folder' => $this->default['providers']['aws']['s3']['upload_folder'], + 'upload_folder' => ltrim($this->default['providers']['aws']['s3']['upload_folder'], '/') . DIRECTORY_SEPARATOR, ]; // check if any required configuration is missed @@ -393,7 +393,8 @@ private function getFilesAlreadyOnBucket($assets) } return $assets->filter(function ($file) use (&$filesOnAWS) { - $fileOnAWS = $filesOnAWS->get(str_replace('\\', '/', $file->getPathName())); + $pathOnAWS = $this->supplier['upload_folder'] . str_replace('\\', '/', $file->getPathName()); + $fileOnAWS = $filesOnAWS->get($pathOnAWS); // select to upload files that are different in size AND last modified time. return !$fileOnAWS From 00b612c2ea3a4490f4b11b91a9036cf9ce98e725 Mon Sep 17 00:00:00 2001 From: Hamid Ghorashi Date: Mon, 10 Jun 2024 17:56:21 +0400 Subject: [PATCH 2/2] Fixes #4 - versioning support for S3 uploads --- src/Publiux/laravelcdn/Cdn.php | 13 +++++++++++++ src/Publiux/laravelcdn/CdnHelper.php | 15 +++++++++++++++ src/Publiux/laravelcdn/Commands/PushCommand.php | 6 +++++- .../laravelcdn/Contracts/CdnHelperInterface.php | 2 ++ src/Publiux/laravelcdn/Contracts/CdnInterface.php | 2 ++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Publiux/laravelcdn/Cdn.php b/src/Publiux/laravelcdn/Cdn.php index 2abc2c5..62dcb16 100644 --- a/src/Publiux/laravelcdn/Cdn.php +++ b/src/Publiux/laravelcdn/Cdn.php @@ -92,4 +92,17 @@ public function emptyBucket() return $provider->emptyBucket(); } + + /** + * Appends the given version name to the upload folder path. + * + * @param string $name The name to append to the upload folder. + * @return $this The current instance of the Cdn class. + */ + public function version(string $name) + { + $this->helper->appendUploadFolder(trim($name)); + + return $this; + } } diff --git a/src/Publiux/laravelcdn/CdnHelper.php b/src/Publiux/laravelcdn/CdnHelper.php index 3ff47f4..f90dd91 100644 --- a/src/Publiux/laravelcdn/CdnHelper.php +++ b/src/Publiux/laravelcdn/CdnHelper.php @@ -99,4 +99,19 @@ public function cleanPath($path) { return rtrim(ltrim($path, '/'), '/'); } + + /** + * Appends the specified path to the configured upload folder for the AWS S3 CDN provider. + * + * @param string $path the path to append to the upload folder + */ + public function appendUploadFolder(string $path): self + { + $uploadFolder = $this->configurations->get('cdn.providers.aws.s3.upload_folder'); + $uploadFolder = implode(DIRECTORY_SEPARATOR, array_map([$this, 'cleanPath'], [$uploadFolder, $path])); + + $this->configurations->set('cdn.providers.aws.s3.upload_folder', ltrim(rtrim($uploadFolder, '/'), '/') . DIRECTORY_SEPARATOR); + + return $this; + } } diff --git a/src/Publiux/laravelcdn/Commands/PushCommand.php b/src/Publiux/laravelcdn/Commands/PushCommand.php index 0efcb6a..28151ed 100644 --- a/src/Publiux/laravelcdn/Commands/PushCommand.php +++ b/src/Publiux/laravelcdn/Commands/PushCommand.php @@ -20,7 +20,7 @@ class PushCommand extends Command * * @var string */ - protected $signature = 'cdn:push'; + protected $signature = 'cdn:push {--ver= : The version number to append to the base path}'; /** * The console command description. @@ -50,6 +50,10 @@ public function __construct(CdnInterface $cdn) */ public function handle() { + if (!empty($this->option('ver'))) { + $this->cdn->version($this->option('ver')); + } + $this->cdn->push(); } } diff --git a/src/Publiux/laravelcdn/Contracts/CdnHelperInterface.php b/src/Publiux/laravelcdn/Contracts/CdnHelperInterface.php index 4732a2b..7638f49 100644 --- a/src/Publiux/laravelcdn/Contracts/CdnHelperInterface.php +++ b/src/Publiux/laravelcdn/Contracts/CdnHelperInterface.php @@ -18,4 +18,6 @@ public function parseUrl($url); public function startsWith($haystack, $needle); public function cleanPath($path); + + public function appendUploadFolder(string $path): CdnHelperInterface; } diff --git a/src/Publiux/laravelcdn/Contracts/CdnInterface.php b/src/Publiux/laravelcdn/Contracts/CdnInterface.php index 6f7b6b4..d6a88da 100644 --- a/src/Publiux/laravelcdn/Contracts/CdnInterface.php +++ b/src/Publiux/laravelcdn/Contracts/CdnInterface.php @@ -12,4 +12,6 @@ interface CdnInterface public function push(); public function emptyBucket(); + + public function version(string $name); }