Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thaiphan committed Mar 21, 2017
0 parents commit cd7d0d5
Show file tree
Hide file tree
Showing 30 changed files with 1,744 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Thai\S3\Block\MediaStorage\System\Config\System\Storage\Media\Synchronise;

class Plugin
{
public function aroundGetTemplate()
{
return 'Thai_S3::system/config/system/storage/media/synchronise.phtml';
}
}
40 changes: 40 additions & 0 deletions Console/Command/ConfigListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigListCommand extends \Symfony\Component\Console\Command\Command
{
private $configFactory;

private $state;

public function __construct(
\Magento\Framework\App\State $state,
Factory $configFactory
) {
$this->state = $state;
$this->configFactory = $configFactory;
parent::__construct();
}

protected function configure()
{
$this->setName('s3:config:list');
$this->setDescription('Lists whatever credentials for S3 you have provided for Magento.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode('adminhtml');
$config = $this->configFactory->create();
$output->writeln('Here are your AWS credentials.');
$output->writeln('');
$output->writeln(sprintf('Access Key ID: %s', $config->getConfigDataValue('thai_s3/general/access_key')));
$output->writeln(sprintf('Secret Access Key: %s', $config->getConfigDataValue('thai_s3/general/secret_key')));
$output->writeln(sprintf('Bucket: %s', $config->getConfigDataValue('thai_s3/general/bucket')));
$output->writeln(sprintf('Region: %s', $config->getConfigDataValue('thai_s3/general/region')));
}
}
94 changes: 94 additions & 0 deletions Console/Command/ConfigSetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigSetCommand extends \Symfony\Component\Console\Command\Command
{
private $configFactory;

private $state;

private $helper;

public function __construct(
\Magento\Framework\App\State $state,
Factory $configFactory,
\Thai\S3\Helper\S3 $helper
) {
$this->state = $state;
$this->helper = $helper;
$this->configFactory = $configFactory;
parent::__construct();
}

protected function configure()
{
$this->setName('s3:config:set');
$this->setDescription('Allows you to set your S3 configuration via the CLI.');
$this->setDefinition($this->getOptionsList());
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('region') && !$input->getOption('bucket') && !$input->getOption('secret-key') && !$input->getOption('access-key-id')) {
$output->writeln($this->getSynopsis());
return;
}

$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return;
}

$this->state->setAreaCode('adminhtml');
$config = $this->configFactory->create();

if (!empty($input->getOption('access-key-id'))) {
$config->setDataByPath('thai_s3/general/access_key', $input->getOption('access-key-id'));
$config->save();
}

if (!empty($input->getOption('secret-key'))) {
$config->setDataByPath('thai_s3/general/secret_key', $input->getOption('secret-key'));
$config->save();
}

if (!empty($input->getOption('bucket'))) {
$config->setDataByPath('thai_s3/general/bucket', $input->getOption('bucket'));
$config->save();
}

if (!empty($input->getOption('region'))) {
$config->setDataByPath('thai_s3/general/region', $input->getOption('region'));
$config->save();
}

$output->writeln('<info>You have successfully updated your S3 credentials.</info>');
}

public function getOptionsList()
{
return [
new InputOption('access-key-id', null, InputOption::VALUE_OPTIONAL, 'a valid AWS access key ID'),
new InputOption('secret-key', null, InputOption::VALUE_OPTIONAL, 'a valid AWS secret access key'),
new InputOption('bucket', null, InputOption::VALUE_OPTIONAL, 'an S3 bucket name'),
new InputOption('region', null, InputOption::VALUE_OPTIONAL, 'an S3 region, e.g. us-east-1')
];
}

public function validate(InputInterface $input)
{
$errors = [];
if ($input->getOption('region')) {
if (!$this->helper->isValidRegion($input->getOption('region'))) {
$errors[] = sprintf('The region "%s" is invalid.', $input->getOption('region'));
}
}
return $errors;
}
}
103 changes: 103 additions & 0 deletions Console/Command/StorageDisableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StorageDisableCommand extends \Symfony\Component\Console\Command\Command
{
private $configFactory;

private $state;

private $helper;

private $client;

private $coreFileStorage;

private $storageHelper;

public function __construct(
\Magento\Framework\App\State $state,
Factory $configFactory,
\Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
\Thai\S3\Helper\Data $helper
) {
$this->state = $state;
$this->configFactory = $configFactory;
$this->coreFileStorage = $coreFileStorage;
$this->helper = $helper;
$this->storageHelper = $storageHelper;
parent::__construct();
}

protected function configure()
{
$this->setName('s3:storage:disable');
$this->setDescription('Revert to using the local filesystem as your Magento 2 file storage backend.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return;
}

try {
$this->client = new \Aws\S3\S3Client([
'version' => 'latest',
'region' => $this->helper->getRegion(),
'credentials' => [
'key' => $this->helper->getAccessKey(),
'secret' => $this->helper->getSecretKey()
]
]);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return;
}

if (!$this->client->doesBucketExist($this->helper->getBucket())) {
$output->writeln('<error>The AWS credentials you provided did not work. Please review your details and try again. You can do so using our config script.</error>');
return;
}

if ($this->coreFileStorage->getCurrentStorageCode() != \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) {
$output->writeln('<error>You are not using S3 as your media file storage backend!</error>');
return;
}

$output->writeln('Updating configuration to use the local filesystem.');

$this->state->setAreaCode('adminhtml');
$config = $this->configFactory->create();
$config->setDataByPath('system/media_storage_configuration/media_storage', \Magento\MediaStorage\Model\File\Storage::STORAGE_MEDIA_FILE_SYSTEM);
$config->save();
$output->writeln(sprintf('<info>Magento now uses the local filesystem for its file backend storage.</info>'));
}

public function validate(InputInterface $input)
{
$errors = [];

if (is_null($this->helper->getAccessKey())) {
$errors[] = 'You have not provided an AWS access key ID. You can do so using our config script.';
}
if (is_null($this->helper->getSecretKey())) {
$errors[] = 'You have not provided an AWS secret access key. You can do so using our config script.';
}
if (is_null($this->helper->getBucket())) {
$errors[] = 'You have not provided an S3 bucket. You can do so using our config script.';
}
if (is_null($this->helper->getRegion())) {
$errors[] = 'You have not provided an S3 region. You can do so using our config script.';
}

return $errors;
}
}
103 changes: 103 additions & 0 deletions Console/Command/StorageEnableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class StorageEnableCommand extends \Symfony\Component\Console\Command\Command
{
private $configFactory;

private $state;

private $helper;

private $client;

private $coreFileStorage;

private $storageHelper;

public function __construct(
\Magento\Framework\App\State $state,
Factory $configFactory,
\Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
\Thai\S3\Helper\Data $helper
) {
$this->state = $state;
$this->configFactory = $configFactory;
$this->coreFileStorage = $coreFileStorage;
$this->helper = $helper;
$this->storageHelper = $storageHelper;
parent::__construct();
}

protected function configure()
{
$this->setName('s3:storage:enable');
$this->setDescription('Enable use of S3 as your Magento 2 file storage backend.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return;
}

try {
$this->client = new \Aws\S3\S3Client([
'version' => 'latest',
'region' => $this->helper->getRegion(),
'credentials' => [
'key' => $this->helper->getAccessKey(),
'secret' => $this->helper->getSecretKey()
]
]);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return;
}

if (!$this->client->doesBucketExist($this->helper->getBucket())) {
$output->writeln('<error>The AWS credentials you provided did not work. Please review your details and try again. You can do so using our config script.</error>');
return;
}

if ($this->coreFileStorage->getCurrentStorageCode() == \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) {
$output->writeln('<error>You are already using S3 as your media file storage backend!</error>');
return;
}

$output->writeln('Updating configuration to use S3.');

$this->state->setAreaCode('adminhtml');
$config = $this->configFactory->create();
$config->setDataByPath('system/media_storage_configuration/media_storage', \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3);
$config->save();
$output->writeln(sprintf('<info>Magento now uses S3 for its file backend storage.</info>'));
}

public function validate(InputInterface $input)
{
$errors = [];

if (is_null($this->helper->getAccessKey())) {
$errors[] = 'You have not provided an AWS access key ID. You can do so using our config script.';
}
if (is_null($this->helper->getSecretKey())) {
$errors[] = 'You have not provided an AWS secret access key. You can do so using our config script.';
}
if (is_null($this->helper->getBucket())) {
$errors[] = 'You have not provided an S3 bucket. You can do so using our config script.';
}
if (is_null($this->helper->getRegion())) {
$errors[] = 'You have not provided an S3 region. You can do so using our config script.';
}

return $errors;
}
}
Loading

0 comments on commit cd7d0d5

Please sign in to comment.