Skip to content

Commit

Permalink
Env var support for Project ID & Subfolder
Browse files Browse the repository at this point in the history
Resolves #7
  • Loading branch information
brandonkelly committed Feb 8, 2019
1 parent f955c9e commit a28cae9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Google Cloud Storage for Craft CMS

## Unreleased

### Added
- The Project ID and Subfolder settings can now be set to environment variables. ([#7](https://github.com/craftcms/aws-s3/issues/7))

## 1.2.1 - 2019-02-06

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"rss": "https://github.com/craftcms/google-cloud/commits/master.atom"
},
"require": {
"craftcms/cms": "^3.1.0",
"craftcms/cms": "^3.1.5",
"superbalist/flysystem-google-storage": "^7.0.0"
},
"autoload": {
Expand Down
21 changes: 17 additions & 4 deletions src/Volume.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public static function loadBucketList(string $projectId, string $keyFileContents
*/
public function getRootUrl()
{
if (($rootUrl = parent::getRootUrl()) !== false && $this->subfolder) {
$rootUrl .= rtrim($this->subfolder, '/').'/';
if (($rootUrl = parent::getRootUrl()) !== false) {
$rootUrl .= $this->_subfolder();
}
return $rootUrl;
}
Expand Down Expand Up @@ -183,7 +183,7 @@ protected function createAdapter()
$client = static::client($config);
$bucket = $client->bucket($this->bucket);

return new GoogleStorageAdapter($client, $bucket, $this->subfolder);
return new GoogleStorageAdapter($client, $bucket, $this->_subfolder() ?: null);
}

/**
Expand Down Expand Up @@ -220,14 +220,27 @@ protected function addFileMetadataToConfig(array $config): array
// Private Methods
// =========================================================================

/**
* Returns the parsed subfolder path
* @return string
*/
private function _subfolder(): string
{
if ($this->subfolder && ($subfolder = rtrim(Craft::parseEnv($this->subfolder), '/')) !== '') {
return $subfolder . '/';
}
return '';

}

/**
* Get the config array for Google Cloud Storage clients.
*
* @return array
*/
private function _getConfigArray()
{
$projectId = $this->projectId;
$projectId = Craft::parseEnv($this->projectId);
$keyFileContents = $this->keyFileContents;

return static::_buildConfigArray($projectId, $keyFileContents);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function actionLoadBucketData()
$this->requireAcceptsJson();

$request = Craft::$app->getRequest();
$projectId = $request->getRequiredBodyParam('projectId');
$projectId = Craft::parseEnv($request->getRequiredBodyParam('projectId'));
$keyFileContents = $request->getRequiredBodyParam('keyFileContents');

try {
Expand Down
6 changes: 5 additions & 1 deletion src/resources/js/editVolume.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$(document).ready(function() {

var $googleProjectId = $('.google-project-id'),
$googleKeyFileContents = $('.google-key-file-contents'),
$googleBucketSelect = $('.google-bucket-select > select'),
Expand Down Expand Up @@ -91,4 +93,6 @@ var googleChangeExpiryValue = function ()
};

$('.google-expires-amount').keyup(googleChangeExpiryValue).change(googleChangeExpiryValue);
$('.google-expires-period select').change(googleChangeExpiryValue);
$('.google-expires-period select').change(googleChangeExpiryValue);

});
6 changes: 4 additions & 2 deletions src/templates/volumeSettings.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% import "_includes/forms" as forms %}

{{ forms.textField({
{{ forms.autosuggestField({
label: "Project ID"|t('google-cloud'),
id: 'projectId',
name: 'projectId',
suggestEnvVars: true,
value: volume.projectId,
errors: volume.getErrors('projectId'),
class: 'google-project-id',
Expand Down Expand Up @@ -47,12 +48,13 @@
errors: volume.getErrors('bucket'),
}, bucketInput) }}

{{ forms.textField({
{{ forms.autosuggestField({
label: "Subfolder"|t('google-cloud'),
instructions: "If you want to use a bucket’s subfolder as a Volume, specify the path to use here."|t('google-cloud'),
id: 'subfolder',
class: 'ltr',
name: 'subfolder',
suggestEnvVars: true,
value: volume.subfolder,
errors: volume.getErrors('subfolder'),
required: false,
Expand Down

0 comments on commit a28cae9

Please sign in to comment.