Skip to content

Commit 46b9e84

Browse files
Added Viettel driver
1 parent a89fa23 commit 46b9e84

File tree

7 files changed

+313
-0
lines changed

7 files changed

+313
-0
lines changed

src/Drivers/AwsS3v2Adapter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers;
9+
10+
use DateTimeInterface;
11+
use League\Flysystem\AwsS3v2\AwsS3Adapter;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class AwsS3v2Adapter extends AwsS3Adapter
18+
{
19+
/**
20+
* Trả về AWS S3v2 Temporary URL theo đường dẫn chỉ định.
21+
*
22+
* @param string $path
23+
* @param DateTimeInterface $expiration
24+
* @param array $options
25+
* @return string
26+
*/
27+
public function getTemporaryUrl(string $path, DateTimeInterface $expiration, array $options = [])
28+
{
29+
return (string) $this->getClient()->getObjectUrl(
30+
$this->getBucket(),
31+
$this->getPathPrefix().$path,
32+
$expiration,
33+
$options
34+
);
35+
}
36+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers;
9+
10+
use Aws\S3\S3Client;
11+
use Illuminate\Filesystem\Cache;
12+
use League\Flysystem\AdapterInterface;
13+
use League\Flysystem\Cached\CachedAdapter;
14+
use League\Flysystem\Cached\CacheInterface;
15+
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
16+
17+
/**
18+
* @author Vuong Minh <[email protected]>
19+
* @since 1.0.0
20+
*/
21+
trait AwsS3v2DriverCompatible
22+
{
23+
/**
24+
* Khởi tạo AwsS3v2 adapter.
25+
*
26+
* @return \League\Flysystem\AdapterInterface
27+
*/
28+
protected function createFilesystemAdapter(): AdapterInterface
29+
{
30+
$adapter = new AwsS3v2Adapter(
31+
$this->createS3Client(),
32+
$this->config['bucket'],
33+
$this->config['root'] ?? null,
34+
$this->config['options'] ?? []
35+
);
36+
37+
if (isset($this->config['cache'])) {
38+
$adapter = new CachedAdapter(
39+
$adapter,
40+
$this->createCacheStore()
41+
);
42+
}
43+
44+
return $adapter;
45+
}
46+
47+
/**
48+
* Khởi tạo Aws S3 client.
49+
*
50+
* @return \Aws\S3\S3Client
51+
*/
52+
protected function createS3Client(): S3Client
53+
{
54+
$config = array_merge([
55+
'endpoint' => $this->getEndPoint(),
56+
'version' => 'latest',
57+
'signature_version' => 's3',
58+
], $this->config);
59+
60+
return S3Client::factory($config);
61+
}
62+
63+
/**
64+
* Khởi tạo cache.
65+
*
66+
* @return \League\Flysystem\Cached\CacheInterface
67+
*/
68+
protected function createCacheStore(): CacheInterface
69+
{
70+
$config = $this->config['cache'];
71+
72+
if (true === $config) {
73+
return new MemoryStore();
74+
}
75+
76+
return new Cache(
77+
$this->app['cache']->store($config['store']),
78+
$config['prefix'] ?? 'flysystem',
79+
$config['expire'] ?? null
80+
);
81+
}
82+
83+
/**
84+
* Phương thức trừu tượng trả về đường dẫn endpoint.
85+
*
86+
* @return string
87+
*/
88+
abstract protected function getEndPoint(): string;
89+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers;
9+
10+
/**
11+
* @author Vuong Minh <[email protected]>
12+
* @since 1.0.0
13+
*/
14+
trait AwsS3v2FilesystemCompatible
15+
{
16+
/**
17+
* Trả về AWS S3v2 URL theo đường dẫn chỉ định.
18+
*
19+
* @param $path
20+
* @return string
21+
*/
22+
public function getUrl($path): string
23+
{
24+
$adapter = $this->getAdapter();
25+
26+
if (! is_null($url = $this->getConfig()->get('url'))) {
27+
$path = $adapter->getPathPrefix().$path;
28+
29+
return rtrim($url, '/').'/'.ltrim($path, '/');
30+
}
31+
32+
return $adapter->getClient()->getObjectUrl(
33+
$adapter->getBucket(),
34+
$adapter->getPathPrefix().$path
35+
);
36+
}
37+
}

src/Drivers/BaseDriver.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers;
9+
10+
use League\Flysystem\FilesystemInterface;
11+
use Illuminate\Contracts\Foundation\Application;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
abstract class BaseDriver
18+
{
19+
/**
20+
* @var Application
21+
*/
22+
protected $app;
23+
24+
/**
25+
* @var array
26+
*/
27+
protected $config;
28+
29+
/**
30+
* Khởi tạo đối tượng Driver.
31+
*
32+
* @param \Illuminate\Contracts\Foundation\Application $app
33+
* @param array $config
34+
* @return \League\Flysystem\FilesystemInterface
35+
*/
36+
public function __invoke(Application $app, array $config): FilesystemInterface
37+
{
38+
$this->app = $app;
39+
$this->config = $config;
40+
41+
return $this->createFilesystem();
42+
}
43+
44+
/**
45+
* Phương thức trừu tượng tạo Filesystem theo config chỉ định.
46+
*
47+
* @return \League\Flysystem\FilesystemInterface
48+
*/
49+
abstract protected function createFilesystem(): FilesystemInterface;
50+
}

src/Drivers/Viettel/Driver.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers\Viettel;
9+
10+
use Illuminate\Support\Arr;
11+
use League\Flysystem\FilesystemInterface;
12+
use PHPViet\Laravel\Flysystem\Drivers\BaseDriver;
13+
use PHPViet\Laravel\Flysystem\Drivers\AwsS3v2DriverCompatible;
14+
15+
/**
16+
* @author Vuong Minh <[email protected]>
17+
* @since 1.0.0
18+
*/
19+
class Driver extends BaseDriver
20+
{
21+
use AwsS3v2DriverCompatible;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
protected function createFilesystem(): FilesystemInterface
27+
{
28+
$config = Arr::only($this->config, ['visibility', 'disable_asserts', 'url']);
29+
30+
return new Filesystem(
31+
$this->createFilesystemAdapter(),
32+
count($config) > 0 ? $config : null
33+
);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected function getEndPoint(): string
40+
{
41+
return 'http://'.$this->config['key'].'.cloudstorage.com.vn';
42+
}
43+
}

src/Drivers/Viettel/Filesystem.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Drivers\Viettel;
9+
10+
use League\Flysystem\Filesystem as BaseFilesystem;
11+
use PHPViet\Laravel\Flysystem\Drivers\AwsS3v2FilesystemCompatible;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class Filesystem extends BaseFilesystem
18+
{
19+
use AwsS3v2FilesystemCompatible;
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-flysystem
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Laravel\Flysystem\Tests\Drivers;
9+
10+
use Aws\S3\S3Client;
11+
use League\Flysystem\AwsS3v2\AwsS3Adapter;
12+
use PHPViet\Laravel\Flysystem\Tests\TestCase;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class ViettelDriverTest extends TestCase
19+
{
20+
public function testCanGetUrl()
21+
{
22+
$adapter = $this->app['filesystem']->disk('viettel');
23+
$this->assertNotEmpty($adapter->url('aaaaaaaaa', now()));
24+
$this->assertNotEmpty($adapter->temporaryUrl('aaaaaaaaa', now()));
25+
}
26+
27+
public function testCanAccessS3Client()
28+
{
29+
$adapter = $this->app['filesystem']->disk('viettel');
30+
$this->assertInstanceOf(S3Client::class, $adapter->getDriver()->getAdapter()->getClient());
31+
}
32+
33+
public function testIsAwsS3v2Adapter()
34+
{
35+
$adapter = $this->app['filesystem']->disk('viettel');
36+
$this->assertInstanceOf(AwsS3Adapter::class, $adapter->getDriver()->getAdapter());
37+
}
38+
}

0 commit comments

Comments
 (0)