Skip to content

Commit cd7d0d5

Browse files
committed
Initial commit
0 parents  commit cd7d0d5

File tree

30 files changed

+1744
-0
lines changed

30 files changed

+1744
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Thai\S3\Block\MediaStorage\System\Config\System\Storage\Media\Synchronise;
3+
4+
class Plugin
5+
{
6+
public function aroundGetTemplate()
7+
{
8+
return 'Thai_S3::system/config/system/storage/media/synchronise.phtml';
9+
}
10+
}

Console/Command/ConfigListCommand.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Thai\S3\Console\Command;
3+
4+
use Magento\Config\Model\Config\Factory;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class ConfigListCommand extends \Symfony\Component\Console\Command\Command
9+
{
10+
private $configFactory;
11+
12+
private $state;
13+
14+
public function __construct(
15+
\Magento\Framework\App\State $state,
16+
Factory $configFactory
17+
) {
18+
$this->state = $state;
19+
$this->configFactory = $configFactory;
20+
parent::__construct();
21+
}
22+
23+
protected function configure()
24+
{
25+
$this->setName('s3:config:list');
26+
$this->setDescription('Lists whatever credentials for S3 you have provided for Magento.');
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output)
30+
{
31+
$this->state->setAreaCode('adminhtml');
32+
$config = $this->configFactory->create();
33+
$output->writeln('Here are your AWS credentials.');
34+
$output->writeln('');
35+
$output->writeln(sprintf('Access Key ID: %s', $config->getConfigDataValue('thai_s3/general/access_key')));
36+
$output->writeln(sprintf('Secret Access Key: %s', $config->getConfigDataValue('thai_s3/general/secret_key')));
37+
$output->writeln(sprintf('Bucket: %s', $config->getConfigDataValue('thai_s3/general/bucket')));
38+
$output->writeln(sprintf('Region: %s', $config->getConfigDataValue('thai_s3/general/region')));
39+
}
40+
}

Console/Command/ConfigSetCommand.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace Thai\S3\Console\Command;
3+
4+
use Magento\Config\Model\Config\Factory;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Input\InputOption;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class ConfigSetCommand extends \Symfony\Component\Console\Command\Command
10+
{
11+
private $configFactory;
12+
13+
private $state;
14+
15+
private $helper;
16+
17+
public function __construct(
18+
\Magento\Framework\App\State $state,
19+
Factory $configFactory,
20+
\Thai\S3\Helper\S3 $helper
21+
) {
22+
$this->state = $state;
23+
$this->helper = $helper;
24+
$this->configFactory = $configFactory;
25+
parent::__construct();
26+
}
27+
28+
protected function configure()
29+
{
30+
$this->setName('s3:config:set');
31+
$this->setDescription('Allows you to set your S3 configuration via the CLI.');
32+
$this->setDefinition($this->getOptionsList());
33+
}
34+
35+
protected function execute(InputInterface $input, OutputInterface $output)
36+
{
37+
if (!$input->getOption('region') && !$input->getOption('bucket') && !$input->getOption('secret-key') && !$input->getOption('access-key-id')) {
38+
$output->writeln($this->getSynopsis());
39+
return;
40+
}
41+
42+
$errors = $this->validate($input);
43+
if ($errors) {
44+
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
45+
return;
46+
}
47+
48+
$this->state->setAreaCode('adminhtml');
49+
$config = $this->configFactory->create();
50+
51+
if (!empty($input->getOption('access-key-id'))) {
52+
$config->setDataByPath('thai_s3/general/access_key', $input->getOption('access-key-id'));
53+
$config->save();
54+
}
55+
56+
if (!empty($input->getOption('secret-key'))) {
57+
$config->setDataByPath('thai_s3/general/secret_key', $input->getOption('secret-key'));
58+
$config->save();
59+
}
60+
61+
if (!empty($input->getOption('bucket'))) {
62+
$config->setDataByPath('thai_s3/general/bucket', $input->getOption('bucket'));
63+
$config->save();
64+
}
65+
66+
if (!empty($input->getOption('region'))) {
67+
$config->setDataByPath('thai_s3/general/region', $input->getOption('region'));
68+
$config->save();
69+
}
70+
71+
$output->writeln('<info>You have successfully updated your S3 credentials.</info>');
72+
}
73+
74+
public function getOptionsList()
75+
{
76+
return [
77+
new InputOption('access-key-id', null, InputOption::VALUE_OPTIONAL, 'a valid AWS access key ID'),
78+
new InputOption('secret-key', null, InputOption::VALUE_OPTIONAL, 'a valid AWS secret access key'),
79+
new InputOption('bucket', null, InputOption::VALUE_OPTIONAL, 'an S3 bucket name'),
80+
new InputOption('region', null, InputOption::VALUE_OPTIONAL, 'an S3 region, e.g. us-east-1')
81+
];
82+
}
83+
84+
public function validate(InputInterface $input)
85+
{
86+
$errors = [];
87+
if ($input->getOption('region')) {
88+
if (!$this->helper->isValidRegion($input->getOption('region'))) {
89+
$errors[] = sprintf('The region "%s" is invalid.', $input->getOption('region'));
90+
}
91+
}
92+
return $errors;
93+
}
94+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Thai\S3\Console\Command;
3+
4+
use Magento\Config\Model\Config\Factory;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class StorageDisableCommand extends \Symfony\Component\Console\Command\Command
9+
{
10+
private $configFactory;
11+
12+
private $state;
13+
14+
private $helper;
15+
16+
private $client;
17+
18+
private $coreFileStorage;
19+
20+
private $storageHelper;
21+
22+
public function __construct(
23+
\Magento\Framework\App\State $state,
24+
Factory $configFactory,
25+
\Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
26+
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
27+
\Thai\S3\Helper\Data $helper
28+
) {
29+
$this->state = $state;
30+
$this->configFactory = $configFactory;
31+
$this->coreFileStorage = $coreFileStorage;
32+
$this->helper = $helper;
33+
$this->storageHelper = $storageHelper;
34+
parent::__construct();
35+
}
36+
37+
protected function configure()
38+
{
39+
$this->setName('s3:storage:disable');
40+
$this->setDescription('Revert to using the local filesystem as your Magento 2 file storage backend.');
41+
}
42+
43+
protected function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
$errors = $this->validate($input);
46+
if ($errors) {
47+
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
48+
return;
49+
}
50+
51+
try {
52+
$this->client = new \Aws\S3\S3Client([
53+
'version' => 'latest',
54+
'region' => $this->helper->getRegion(),
55+
'credentials' => [
56+
'key' => $this->helper->getAccessKey(),
57+
'secret' => $this->helper->getSecretKey()
58+
]
59+
]);
60+
} catch (\Exception $e) {
61+
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
62+
return;
63+
}
64+
65+
if (!$this->client->doesBucketExist($this->helper->getBucket())) {
66+
$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>');
67+
return;
68+
}
69+
70+
if ($this->coreFileStorage->getCurrentStorageCode() != \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) {
71+
$output->writeln('<error>You are not using S3 as your media file storage backend!</error>');
72+
return;
73+
}
74+
75+
$output->writeln('Updating configuration to use the local filesystem.');
76+
77+
$this->state->setAreaCode('adminhtml');
78+
$config = $this->configFactory->create();
79+
$config->setDataByPath('system/media_storage_configuration/media_storage', \Magento\MediaStorage\Model\File\Storage::STORAGE_MEDIA_FILE_SYSTEM);
80+
$config->save();
81+
$output->writeln(sprintf('<info>Magento now uses the local filesystem for its file backend storage.</info>'));
82+
}
83+
84+
public function validate(InputInterface $input)
85+
{
86+
$errors = [];
87+
88+
if (is_null($this->helper->getAccessKey())) {
89+
$errors[] = 'You have not provided an AWS access key ID. You can do so using our config script.';
90+
}
91+
if (is_null($this->helper->getSecretKey())) {
92+
$errors[] = 'You have not provided an AWS secret access key. You can do so using our config script.';
93+
}
94+
if (is_null($this->helper->getBucket())) {
95+
$errors[] = 'You have not provided an S3 bucket. You can do so using our config script.';
96+
}
97+
if (is_null($this->helper->getRegion())) {
98+
$errors[] = 'You have not provided an S3 region. You can do so using our config script.';
99+
}
100+
101+
return $errors;
102+
}
103+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Thai\S3\Console\Command;
3+
4+
use Magento\Config\Model\Config\Factory;
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class StorageEnableCommand extends \Symfony\Component\Console\Command\Command
9+
{
10+
private $configFactory;
11+
12+
private $state;
13+
14+
private $helper;
15+
16+
private $client;
17+
18+
private $coreFileStorage;
19+
20+
private $storageHelper;
21+
22+
public function __construct(
23+
\Magento\Framework\App\State $state,
24+
Factory $configFactory,
25+
\Magento\MediaStorage\Helper\File\Storage\Database $storageHelper,
26+
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
27+
\Thai\S3\Helper\Data $helper
28+
) {
29+
$this->state = $state;
30+
$this->configFactory = $configFactory;
31+
$this->coreFileStorage = $coreFileStorage;
32+
$this->helper = $helper;
33+
$this->storageHelper = $storageHelper;
34+
parent::__construct();
35+
}
36+
37+
protected function configure()
38+
{
39+
$this->setName('s3:storage:enable');
40+
$this->setDescription('Enable use of S3 as your Magento 2 file storage backend.');
41+
}
42+
43+
protected function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
$errors = $this->validate($input);
46+
if ($errors) {
47+
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
48+
return;
49+
}
50+
51+
try {
52+
$this->client = new \Aws\S3\S3Client([
53+
'version' => 'latest',
54+
'region' => $this->helper->getRegion(),
55+
'credentials' => [
56+
'key' => $this->helper->getAccessKey(),
57+
'secret' => $this->helper->getSecretKey()
58+
]
59+
]);
60+
} catch (\Exception $e) {
61+
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
62+
return;
63+
}
64+
65+
if (!$this->client->doesBucketExist($this->helper->getBucket())) {
66+
$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>');
67+
return;
68+
}
69+
70+
if ($this->coreFileStorage->getCurrentStorageCode() == \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) {
71+
$output->writeln('<error>You are already using S3 as your media file storage backend!</error>');
72+
return;
73+
}
74+
75+
$output->writeln('Updating configuration to use S3.');
76+
77+
$this->state->setAreaCode('adminhtml');
78+
$config = $this->configFactory->create();
79+
$config->setDataByPath('system/media_storage_configuration/media_storage', \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3);
80+
$config->save();
81+
$output->writeln(sprintf('<info>Magento now uses S3 for its file backend storage.</info>'));
82+
}
83+
84+
public function validate(InputInterface $input)
85+
{
86+
$errors = [];
87+
88+
if (is_null($this->helper->getAccessKey())) {
89+
$errors[] = 'You have not provided an AWS access key ID. You can do so using our config script.';
90+
}
91+
if (is_null($this->helper->getSecretKey())) {
92+
$errors[] = 'You have not provided an AWS secret access key. You can do so using our config script.';
93+
}
94+
if (is_null($this->helper->getBucket())) {
95+
$errors[] = 'You have not provided an S3 bucket. You can do so using our config script.';
96+
}
97+
if (is_null($this->helper->getRegion())) {
98+
$errors[] = 'You have not provided an S3 region. You can do so using our config script.';
99+
}
100+
101+
return $errors;
102+
}
103+
}

0 commit comments

Comments
 (0)