Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 feature request versioning support for s3 uploads #5

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Publiux/laravelcdn/Cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions src/Publiux/laravelcdn/CdnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 5 additions & 1 deletion src/Publiux/laravelcdn/Commands/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions src/Publiux/laravelcdn/Contracts/CdnHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function parseUrl($url);
public function startsWith($haystack, $needle);

public function cleanPath($path);

public function appendUploadFolder(string $path): CdnHelperInterface;
}
2 changes: 2 additions & 0 deletions src/Publiux/laravelcdn/Contracts/CdnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ interface CdnInterface
public function push();

public function emptyBucket();

public function version(string $name);
}
5 changes: 3 additions & 2 deletions src/Publiux/laravelcdn/Providers/AwsS3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading