Last active
November 7, 2022 09:43
-
-
Save kublermdk/cc1cec6b4ecd5b4477f55c2b0ff6eedd to your computer and use it in GitHub Desktop.
Update Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter to support VISIBILITY_NOT_SET
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
<?php | |
namespace app\adaptors; | |
use Google\Cloud\Storage\Acl; | |
use League\Flysystem\AdapterInterface; | |
/** | |
* @package app\adaptors\GoogleStorageAdapter | |
* Updated by Michael Kubler @kublermdk to support VISIBILITY_NOT_SET | |
* | |
* We rework the Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter | |
* So that it can support NOT setting the Access Control Lists | |
* This allows it to be used on Google Storage buckets with Access Control set to Unified | |
* | |
* If you attempt to upload a file to Google Storage with a bucket set to Unified Access Control then you get an error: | |
* | |
* > Cannot insert legacy ACL for an object when uniform bucket-level access is enabled. Read more at https://cloud.google.com/storage/docs/uniform-bucket-level-access | |
* | |
* This allows you to set the visibility config to VISIBILITY_NOT_SET and it won't set the ACLs and will allow you to upload | |
* @license Where applicable the license extends the existing Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter license or uses http://opensource.org/licenses/BSD-3-Clause | |
* | |
*/ | |
class GoogleStorageAdapter extends \Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter | |
{ | |
/** | |
* @const VISIBILITY_NOT_SET ACLs not set | |
*/ | |
const VISIBILITY_NOT_SET = 'not-set'; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setVisibility($path, $visibility) | |
{ | |
$object = $this->getObject($path); | |
if (in_array($visibility, [AdapterInterface::VISIBILITY_PRIVATE, self::VISIBILITY_NOT_SET])) { | |
$object->acl()->delete('allUsers'); | |
} elseif ($visibility === AdapterInterface::VISIBILITY_PUBLIC) { | |
$object->acl()->add('allUsers', Acl::ROLE_READER); | |
} | |
$normalised = $this->normaliseObject($object); | |
$normalised['visibility'] = $visibility; | |
return $normalised; | |
} | |
/** | |
* @param string $visibility | |
* | |
* @return string | |
*/ | |
protected function getPredefinedAclForVisibility($visibility) | |
{ | |
// -- Allow for NOT having the Access Control List set, which is required when you have the uniform bucket-level access enabled | |
if ($visibility === self::VISIBILITY_NOT_SET) { | |
return null; | |
} | |
return $visibility === AdapterInterface::VISIBILITY_PUBLIC ? 'publicRead' : 'projectPrivate'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment