Skip to content

Commit

Permalink
Add Glide cloud sources to allow Glide and S3 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Ribeiro authored and ifox committed Apr 29, 2021
1 parent 5327f61 commit 11f0f50
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/Services/Cloud/Aws.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace A17\Twill\Services\Cloud;

use Aws\S3\S3Client;
use Illuminate\Support\Str;
use League\Flysystem\Filesystem;
use League\Flysystem\AwsS3v3\AwsS3Adapter;

class Aws
{
public function filesystemFactory($source)
{
$config = $this->getConfigFor($source);

$client = new S3Client($config);

$adapter = new AwsS3Adapter($client, $config['bucket'], $config['root']);

return new Filesystem($adapter);
}

public function getConfigFor($disk)
{
return [
'credentials' => [
'key' => $this->config($disk, 'key'),

'secret' => $this->config($disk, 'secret'),
],

'region' => $this->config($disk, 'region'),

'root' => $this->config($disk, 'root', ''),

'bucket' => $this->config($disk, 'bucket'),

'version' => $this->config($disk, 'version', 'latest'),
];
}

public function config($disk, $key, $default = null)
{
$env1 = Str::upper(Str::snake($disk));

$env2 = $env1 === 'AWS' ? 'S3' : 'AWS';

$envSuffix = Str::upper($key);

if (filled($value = config("filesystems.disks.{$disk}.{$key}"))) {
return $value;
}

return env("{$env1}_{$envSuffix}", env("{$env2}_{$envSuffix}", $default));
}
}
13 changes: 13 additions & 0 deletions src/Services/Cloud/Azure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace A17\Twill\Services\Cloud;

use Exception;

class Azure
{
public function filesystemFactory($prefix)
{
throw new Exception('Azure is not implemented yey.');
}
}
27 changes: 27 additions & 0 deletions src/Services/Cloud/Glide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace A17\Twill\Services\Cloud;

use Exception;

class Glide
{
function makeCloudSource($source = null)
{
if (blank($source)) {
throw new Exception(
'Glide source was not set, please set your GLIDE_SOURCE environment variable or pass the source.'
);
}

if ($source == 's3' || $source == 'aws') {
return app(Aws::class)->filesystemFactory($source);
}

if ($source == 'azure') {
return app(Azure::class)->filesystemFactory($source);
}

return $source;
}
}

1 comment on commit 11f0f50

@madalinignisca
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I make Glide use S3 actually?

I have my disks like this:

's3_private' = [], 's3_public' = []

and my default disk is 's3_private'

The implementation makes me thing that you expect the user to have an 's3' disk defined in config/filesystems.php?

Should Twill have a config telling it's cloud to be used for Glide and the GLIDE_SOURCE to be given the disk to use?

Please sign in to comment.