Skip to content

Commit

Permalink
Fixed #38
Browse files Browse the repository at this point in the history
  • Loading branch information
andris-sevcenko committed Jan 31, 2019
1 parent d8b95b4 commit 3cb4190
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
### Changed
- Show validation error when creating a volume and not specify a bucket.

### Fixed
- Fixed an error where it was not possible to use this plugin on installs that were updated from Craft 2 to Craft 3.1 ([#38](https://github.com/craftcms/aws-s3/issues/38))

## 1.0.8 - 2018-01-02

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Plugin extends \craft\base\Plugin
/**
* @inheritdoc
*/
public $schemaVersion = '1.0.1';
public $schemaVersion = '1.1';


// Public Methods
Expand Down
8 changes: 7 additions & 1 deletion src/Volume.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Aws\CloudFront\Exception\CloudFrontException;
use Aws\Credentials\Credentials;
use Aws\Handler\GuzzleV6\GuzzleHandler;
use Aws\Rekognition\RekognitionClient;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use Aws\Sts\StsClient;
Expand All @@ -28,7 +29,7 @@
* @property mixed $settingsHtml
* @property string $rootUrl
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
* @since 1.0
*/
class Volume extends FlysystemVolume
{
Expand Down Expand Up @@ -103,6 +104,11 @@ public static function displayName(): string
*/
public $cfDistributionId;

/**
* @var bool Whether facial detection should be attempted to set the focal point automatically
*/
public $autoFocalPoint = false;

// Public Methods
// =========================================================================

Expand Down
44 changes: 18 additions & 26 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
use craft\db\Migration;
use craft\db\Query;
use craft\helpers\Json;
use craft\services\Volumes;

/**
* Installation Migration
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
* @since 1.0
*/
class Install extends Migration
{
Expand Down Expand Up @@ -53,39 +54,30 @@ public function safeDown()
*/
private function _convertVolumes()
{
$volumes = (new Query())
->select([
'id',
'fieldLayoutId',
'settings',
])
->where(['type' => 'craft\volumes\AwsS3'])
->from(['{{%volumes}}'])
->all();
$projectConfig = Craft::$app->getProjectConfig();
$projectConfig->muteEvents = true;

$dbConnection = Craft::$app->getDb();
foreach ($projectConfig->get(Volumes::CONFIG_VOLUME_KEY) as $uid => &$volume) {
if ($volume['type'] == Volume::class && isset($volume['settings']) && is_array($volume['settings'])) {
$settings = $volume['settings'];

foreach ($volumes as $volume) {

$settings = Json::decode($volume['settings']);

if ($settings !== null) {
$hasUrls = !empty($settings['publicURLs']);
$hasUrls = !empty($volume['hasUrls']);
$url = ($hasUrls && !empty($settings['urlPrefix'])) ? $settings['urlPrefix'] : null;
$settings['region'] = $settings['location'];
unset($settings['publicURLs'], $settings['urlPrefix'], $settings['location']);
unset($settings['urlPrefix'], $settings['location'], $settings['storageClass']);

$values = [
'type' => Volume::class,
'hasUrls' => $hasUrls,
$volume['url'] = $url;
$volume['settings'] = $settings;

$this->update('{{%volumes}}', [
'settings' => Json::encode($settings),
'url' => $url,
'settings' => Json::encode($settings)
];
], ['uid' => $uid]);

$dbConnection->createCommand()
->update('{{%volumes}}', $values, ['id' => $volume['id']])
->execute();
$projectConfig->set(Volumes::CONFIG_VOLUME_KEY . '.' . $uid, $volume);
}
}

$projectConfig->muteEvents = false;
}
}
33 changes: 16 additions & 17 deletions src/migrations/m180929_165000_remove_storageclass_setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace craft\awss3\migrations;

use Craft;
use craft\awss3\Volume;
use craft\db\Migration;
use craft\db\Query;
use craft\helpers\Json;
use craft\services\Volumes;

/**
* m180929_165000_remove_storageclass_setting migration.
Expand All @@ -17,25 +19,22 @@ class m180929_165000_remove_storageclass_setting extends Migration
*/
public function safeUp()
{
$volumes = (new Query())
->select([
'id',
'settings',
])
->where(['type' => Volume::class])
->from(['{{%volumes}}'])
->all();


foreach ($volumes as $volume) {
$settings = Json::decode($volume['settings']);

if ($settings !== null) {
unset($settings['storageClass']);
$settings = Json::encode($settings);
$this->update('{{%volumes}}', ['settings' => $settings], ['id' => $volume['id']]);
$projectConfig = Craft::$app->getProjectConfig();
$projectConfig->muteEvents = true;

foreach ($projectConfig->get(Volumes::CONFIG_VOLUME_KEY) as $uid => &$volume) {
if ($volume['type'] == Volume::class && isset($volume['settings']) && is_array($volume['settings'])) {
unset($volume['settings']['storageClass']);

$this->update('{{%volumes}}', [
'settings' => Json::encode($settings),
], ['uid' => $uid]);

$projectConfig->set(Volumes::CONFIG_VOLUME_KEY . '.' . $uid, $volume);
}
}

$projectConfig->muteEvents = false;
}

/**
Expand Down
83 changes: 83 additions & 0 deletions src/migrations/m190131_214300_cleanup_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\awss3\migrations;

use Craft;
use craft\awss3\Volume;
use craft\db\Migration;
use craft\db\Query;
use craft\helpers\Json;
use craft\services\Volumes;

/**
* Installation Migration
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 1.2
*/
class m190131_214300_cleanup_config extends Migration
{
// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function safeUp()
{
// Cleanup failed conversions
$this->_convertVolumes();

return true;
}

/**
* @inheritdoc
*/
public function safeDown()
{
return true;
}

// Private Methods
// =========================================================================

/**
* Converts any old school S3 volumes to this one
*
* @return void
*/
private function _convertVolumes()
{
$projectConfig = Craft::$app->getProjectConfig();
$projectConfig->muteEvents = true;

foreach ($projectConfig->get(Volumes::CONFIG_VOLUME_KEY) as $uid => &$volume) {
if ($volume['type'] == Volume::class && !empty($volume['settings']) && is_array($volume['settings']) && array_key_exists('urlPrefix ', $volume['settings'])) {
$settings = $volume['settings'];

$hasUrls = !empty($volume['hasUrls']);
$url = ($hasUrls && !empty($settings['urlPrefix'])) ? $settings['urlPrefix'] : null;
$settings['region'] = $settings['location'];
unset($settings['urlPrefix'], $settings['location'], $settings['storageClass']);

$volume['url'] = $url;
$volume['settings'] = $settings;

$this->update('{{%volumes}}', [
'settings' => Json::encode($settings),
'url' => $url,
], ['uid' => $uid]);

$projectConfig->set(Volumes::CONFIG_VOLUME_KEY . '.' . $uid, $volume);
}
}

$projectConfig->muteEvents = false;
}
}

0 comments on commit 3cb4190

Please # to comment.