From a9f2634935e0cb65c77f693c0ed7074bbf909c3a Mon Sep 17 00:00:00 2001 From: Merel Jossart Date: Tue, 29 Aug 2023 16:12:38 +0200 Subject: [PATCH 1/2] Allow overriding the default file visibility set by FlysystemFS --- src/Fs.php | 15 +++++++++++++++ src/templates/fsSettings.html | 21 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Fs.php b/src/Fs.php index 97ad6f5..53efdd6 100644 --- a/src/Fs.php +++ b/src/Fs.php @@ -73,6 +73,11 @@ public static function displayName(): string */ public string $bucketSelectionMode = 'choose'; + /** + * @var string Default Object Visibility ('' for default behaviour or 'public' or 'private') + */ + public string $visibility = ''; + /** * @inheritdoc */ @@ -316,4 +321,14 @@ private static function _buildConfigArray(string $projectId, string $keyFileCont return $config; } + + protected function visibility(): string + { + if(empty($this->visibility)) + { + return parent::visibility(); + } + + return $this->visibility; + } } diff --git a/src/templates/fsSettings.html b/src/templates/fsSettings.html index 5289a44..a585cdb 100644 --- a/src/templates/fsSettings.html +++ b/src/templates/fsSettings.html @@ -39,7 +39,7 @@ toggle: true, targetPrefix: '.bsm-' }) }} - +
{{ forms.select({ id: 'bucket', @@ -56,7 +56,7 @@
- +
{{ forms.autosuggest({ label: "Bucket"|t('google-cloud'), @@ -77,6 +77,23 @@ errors: fs.getErrors('bucket'), }, bucketInput) }} + +
+
+ +
{{ "The default visibility setting to apply to new files."|t('google-cloud') }}
+
+ {{ forms.select({ + name: 'visibility', + options: [ + { label: 'Default'|t('google-cloud'), value: '' }, + { label: 'Public'|t('google-cloud'), value: 'public' }, + { label: 'Private'|t('google-cloud'), value: 'private' } + ], + value: fs.visibility, + }) }} +
+ {{ forms.autosuggestField({ label: "Subfolder"|t('google-cloud'), instructions: "If you want to use a bucket’s subfolder as a fs, specify the path to use here."|t('google-cloud'), From 80f8154e3096fd7046ef5fe7341d35707a5e4f84 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Wed, 20 Mar 2024 06:03:10 -0400 Subject: [PATCH 2/2] Visibility tweaks --- src/Fs.php | 29 ++++++++++++++++++++--------- src/templates/fsSettings.html | 30 ++++++++++++------------------ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/Fs.php b/src/Fs.php index 53efdd6..5910bf0 100644 --- a/src/Fs.php +++ b/src/Fs.php @@ -24,6 +24,7 @@ use League\Flysystem\FilesystemAdapter; use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter; use League\Flysystem\GoogleCloudStorage\PortableVisibilityHandler; +use League\Flysystem\Visibility; /** * Class Fs @@ -74,9 +75,10 @@ public static function displayName(): string public string $bucketSelectionMode = 'choose'; /** - * @var string Default Object Visibility ('' for default behaviour or 'public' or 'private') + * @var ?string Default object visibility (null, 'public', 'private') + * If null, visibility will be determined by self::$hasUrls */ - public string $visibility = ''; + public ?string $visibility = null; /** * @inheritdoc @@ -117,10 +119,24 @@ public function rules(): array { $rules = parent::rules(); $rules[] = [['bucket', 'projectId'], 'required']; - + $rules[] = [ + ['visibility'], + 'in', + 'range' => array_keys($this->getVisibilityOptions()), + 'strict' => true, + ]; return $rules; } + public function getVisibilityOptions(): array + { + return [ + null => Craft::t('google-cloud', 'Automatic'), + Visibility::PUBLIC => Craft::t('google-cloud', 'Public'), + Visibility::PRIVATE => Craft::t('google-cloud', 'Private'), + ]; + } + /** * @inheritdoc */ @@ -324,11 +340,6 @@ private static function _buildConfigArray(string $projectId, string $keyFileCont protected function visibility(): string { - if(empty($this->visibility)) - { - return parent::visibility(); - } - - return $this->visibility; + return $this->visibility ?? parent::visibility(); } } diff --git a/src/templates/fsSettings.html b/src/templates/fsSettings.html index a585cdb..66a8b5c 100644 --- a/src/templates/fsSettings.html +++ b/src/templates/fsSettings.html @@ -77,23 +77,6 @@ errors: fs.getErrors('bucket'), }, bucketInput) }} - -
-
- -
{{ "The default visibility setting to apply to new files."|t('google-cloud') }}
-
- {{ forms.select({ - name: 'visibility', - options: [ - { label: 'Default'|t('google-cloud'), value: '' }, - { label: 'Public'|t('google-cloud'), value: 'public' }, - { label: 'Private'|t('google-cloud'), value: 'private' } - ], - value: fs.visibility, - }) }} -
- {{ forms.autosuggestField({ label: "Subfolder"|t('google-cloud'), instructions: "If you want to use a bucket’s subfolder as a fs, specify the path to use here."|t('google-cloud'), @@ -137,8 +120,19 @@ {{ forms.field({ label: "Cache Duration"|t('google-cloud'), - instructions: "The Cache-Control duration that assets should be uploaded to the cloud with.", + instructions: 'The Cache-Control duration that assets should be uploaded with.'|t('google-cloud'), id: 'cacheDuration', }, cacheInput) }} +{{ forms.selectField({ + label: 'Visibility'|t('google-cloud'), + instructions: 'The default visibility that assets should be uploaded with.'|t('google-cloud'), + id: 'visibility', + name: 'visibility', + errors: fs.getErrors('visibility'), + value: fs.visibility, + options: fs.getVisibilityOptions(), + tip: "If automatic, the value will be determined by whether this filesystem has public URLs." +}) }} + {% do view.registerAssetBundle("craft\\googlecloud\\GoogleCloudBundle") %}