Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEXT-14744 - fix visibility setting in s3 storage fs
- Loading branch information
1 parent
00ac2d7
commit ba52f68
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
changelog/_unreleased/2021-05-10-fix-visibility-setting-in-s3-storage-fs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: fix visibility setting in s3 storage fs | ||
issue: NEXT-14744 | ||
author: d.neustadt | ||
author_email: d.neustadt@shopware.com | ||
author_github: dneustadt | ||
--- | ||
# Core | ||
* Changed setting of visibility from fallback config to nested `options` parameter as per documentation | ||
* Added `s3:set-visibility` CLI command for retroactively setting visibility of already stored objects |
112 changes: 112 additions & 0 deletions
112
src/Core/Framework/Adapter/Command/S3FilesystemVisibilityCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\Adapter\Command; | ||
|
||
use League\Flysystem\FilesystemInterface; | ||
use Shopware\Core\Framework\Adapter\Console\ShopwareStyle; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class S3FilesystemVisibilityCommand extends Command | ||
{ | ||
protected static $defaultName = 's3:set-visibility'; | ||
|
||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private $filesystemPrivate; | ||
|
||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private $filesystemPublic; | ||
|
||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private $filesystemTheme; | ||
|
||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private $filesystemSitemap; | ||
|
||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
private $filesystemAsset; | ||
|
||
public function __construct( | ||
FilesystemInterface $filesystemPrivate, | ||
FilesystemInterface $filesystemPublic, | ||
FilesystemInterface $filesystemTheme, | ||
FilesystemInterface $filesystemSitemap, | ||
FilesystemInterface $filesystemAsset | ||
) { | ||
parent::__construct(); | ||
$this->filesystemPrivate = $filesystemPrivate; | ||
$this->filesystemPublic = $filesystemPublic; | ||
$this->filesystemTheme = $filesystemTheme; | ||
$this->filesystemSitemap = $filesystemSitemap; | ||
$this->filesystemAsset = $filesystemAsset; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Sets visibility for all objects in corresponding bucket of S3 storage.'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new ShopwareStyle($input, $output); | ||
|
||
$style->warning('If both private and public objects are stored in the same bucket, this command will set all of them public.'); | ||
$continue = $style->confirm('Continue?'); | ||
|
||
if (!$continue) { | ||
return 0; | ||
} | ||
|
||
$style->comment('Setting visibility to private in private bucket.'); | ||
$this->setVisibility($this->filesystemPrivate, $style, 'private'); | ||
$style->comment('Setting visibility to public in public bucket.'); | ||
$this->setVisibility($this->filesystemPublic, $style, 'public'); | ||
$style->comment('Setting visibility to public in theme bucket.'); | ||
$this->setVisibility($this->filesystemTheme, $style, 'public'); | ||
$style->comment('Setting visibility to public in sitemap bucket.'); | ||
$this->setVisibility($this->filesystemSitemap, $style, 'public'); | ||
$style->comment('Setting visibility to public in asset bucket.'); | ||
$this->setVisibility($this->filesystemAsset, $style, 'public'); | ||
|
||
$style->info('Finished setting visibility of objects in all pre-defined buckets.'); | ||
|
||
return 0; | ||
} | ||
|
||
private function setVisibility(FilesystemInterface $filesystem, ShopwareStyle $style, string $visibility): void | ||
{ | ||
$files = array_filter($filesystem->listContents('/', true), function (array $object): bool { | ||
return $object['type'] === 'file'; | ||
}); | ||
ProgressBar::setFormatDefinition('custom', '[%bar%] %current%/%max% -- %message%'); | ||
$progressBar = new ProgressBar($style, \count($files)); | ||
$progressBar->setFormat('custom'); | ||
|
||
foreach ($files as $file) { | ||
if ($file['type'] === 'file') { | ||
$filesystem->setVisibility($file['path'], $visibility); | ||
|
||
$progressBar->advance(); | ||
$progressBar->setMessage($file['path']); | ||
} | ||
} | ||
|
||
$progressBar->finish(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters