|
| 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 <vuongxuongminh@gmail.com> |
| 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 | +} |
0 commit comments