1 | <?php |
||
2 | |||
3 | /** |
||
4 | * FileStorage.php |
||
5 | * |
||
6 | * Manage storage systems for uploaded files. |
||
7 | * |
||
8 | * @package jaxon-upload |
||
9 | * @author Thierry Feuzeu <[email protected]> |
||
10 | * @copyright 2022 Thierry Feuzeu <[email protected]> |
||
11 | * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
||
12 | * @link https://github.com/jaxon-php/jaxon-core |
||
13 | */ |
||
14 | |||
15 | namespace Jaxon\Upload\Manager; |
||
16 | |||
17 | use Jaxon\App\Config\ConfigManager; |
||
18 | use Jaxon\App\I18n\Translator; |
||
19 | use Jaxon\Exception\RequestException; |
||
20 | use League\Flysystem\Filesystem; |
||
21 | use League\Flysystem\Local\LocalFilesystemAdapter; |
||
22 | |||
23 | use Closure; |
||
24 | |||
25 | use function call_user_func; |
||
26 | use function is_string; |
||
27 | use function trim; |
||
28 | |||
29 | class FileStorage |
||
30 | { |
||
31 | /** |
||
32 | * @var ConfigManager |
||
33 | */ |
||
34 | protected $xConfigManager; |
||
35 | |||
36 | /** |
||
37 | * @var Translator |
||
38 | */ |
||
39 | protected $xTranslator; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $aAdapters = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $aFilesystems = []; |
||
50 | |||
51 | /** |
||
52 | * The constructor |
||
53 | * |
||
54 | * @param ConfigManager $xConfigManager |
||
55 | * @param Translator $xTranslator |
||
56 | */ |
||
57 | public function __construct(ConfigManager $xConfigManager, Translator $xTranslator) |
||
58 | { |
||
59 | $this->xConfigManager = $xConfigManager; |
||
60 | $this->xTranslator = $xTranslator; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $sStorage |
||
65 | * @param Closure $cFactory |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function registerAdapter(string $sStorage, Closure $cFactory) |
||
70 | { |
||
71 | $this->aAdapters[$sStorage] = $cFactory; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Register the file storage adapters |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function registerAdapters() |
||
80 | { |
||
81 | // Local file system adapter |
||
82 | $this->registerAdapter('local', function(string $sRootDir, $xOptions) { |
||
83 | return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : |
||
84 | new LocalFilesystemAdapter($sRootDir, $xOptions); |
||
85 | }); |
||
86 | |||
87 | // In memory file system adapter |
||
88 | $this->registerAdapter('memory', function() { |
||
89 | return new \League\Flysystem\InMemory\InMemoryFilesystemAdapter(); |
||
0 ignored issues
–
show
|
|||
90 | }); |
||
91 | |||
92 | // AWS S3 file system adapter |
||
93 | $this->registerAdapter('aws-s3', function(string $sRootDir, array $aOptions) { |
||
94 | /** @var \Aws\S3\S3ClientInterface $client */ |
||
95 | $client = new \Aws\S3\S3Client($aOptions['client'] ?? []); |
||
0 ignored issues
–
show
The type
Aws\S3\S3Client was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
96 | return new \League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\AwsS3V3\AwsS3V3Adapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
97 | }); |
||
98 | |||
99 | // Async AWS S3 file system adapter |
||
100 | $this->registerAdapter('async-aws-s3', function(string $sRootDir, array $aOptions) { |
||
101 | $client = isset($aOptions['client']) ? new \AsyncAws\S3\S3Client($aOptions['client']) : new \AsyncAws\S3\S3Client(); |
||
0 ignored issues
–
show
The type
AsyncAws\S3\S3Client was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
102 | return new \League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
103 | }); |
||
104 | |||
105 | // Google Cloud file system adapter |
||
106 | $this->registerAdapter('google-cloud', function(string $sRootDir, array $aOptions) { |
||
107 | $storageClient = new \Google\Cloud\Storage\StorageClient($aOptions['client'] ?? []); |
||
0 ignored issues
–
show
The type
Google\Cloud\Storage\StorageClient was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
108 | $bucket = $storageClient->bucket($aOptions['bucket'] ?? ''); |
||
109 | return new \League\Flysystem\AzureBlobStorage\GoogleCloudStorageAdapter($bucket, $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\AzureBl...ogleCloudStorageAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
110 | }); |
||
111 | |||
112 | // Microsoft Azure file system adapter |
||
113 | $this->registerAdapter('azure-blob', function(string $sRootDir, array $aOptions) { |
||
114 | $client = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($aOptions['dsn']); |
||
0 ignored issues
–
show
The type
MicrosoftAzure\Storage\Blob\BlobRestProxy was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
115 | return new \League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter($client, $aOptions['container'], $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\AzureBl...AzureBlobStorageAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
116 | }); |
||
117 | |||
118 | // FTP file system adapter |
||
119 | $this->registerAdapter('ftp', function(string $sRootDir, array $aOptions) { |
||
120 | $aOptions['root'] = $sRootDir; |
||
121 | $xOptions = \League\Flysystem\Ftp\FtpConnectionOptions::fromArray($aOptions); |
||
0 ignored issues
–
show
The type
League\Flysystem\Ftp\FtpConnectionOptions was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
122 | return new \League\Flysystem\Ftp\FtpAdapter($xOptions); |
||
0 ignored issues
–
show
The type
League\Flysystem\Ftp\FtpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
123 | }); |
||
124 | |||
125 | // SFTP V2 file system adapter |
||
126 | $this->registerAdapter('sftp-v2', function(string $sRootDir, array $aOptions) { |
||
127 | $provider = new \League\Flysystem\PhpseclibV2\SftpConnectionProvider(...$aOptions); |
||
0 ignored issues
–
show
The type
League\Flysystem\Phpsecl...\SftpConnectionProvider was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
128 | return new \League\Flysystem\PhpseclibV2\SftpAdapter($provider, $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\PhpseclibV2\SftpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
129 | }); |
||
130 | |||
131 | // SFTP V3 file system adapter |
||
132 | $this->registerAdapter('sftp-v3', function(string $sRootDir, array $aOptions) { |
||
133 | $provider = new \League\Flysystem\PhpseclibV3\SftpConnectionProvider(...$aOptions); |
||
0 ignored issues
–
show
The type
League\Flysystem\Phpsecl...\SftpConnectionProvider was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
134 | return new \League\Flysystem\PhpseclibV3\SftpAdapter($provider, $sRootDir); |
||
0 ignored issues
–
show
The type
League\Flysystem\PhpseclibV3\SftpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
135 | }); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param string $sField |
||
140 | * |
||
141 | * @return Filesystem |
||
142 | * @throws RequestException |
||
143 | */ |
||
144 | public function filesystem(string $sField = ''): Filesystem |
||
145 | { |
||
146 | $sField = trim($sField); |
||
147 | if(isset($this->aFilesystems[$sField])) |
||
148 | { |
||
149 | return $this->aFilesystems[$sField]; |
||
150 | } |
||
151 | |||
152 | // Default upload dir |
||
153 | $sStorage = $this->xConfigManager->getOption('upload.default.storage', 'local'); |
||
154 | $sRootDir = $this->xConfigManager->getOption('upload.default.dir', ''); |
||
155 | $aOptions = $this->xConfigManager->getOption('upload.default.options'); |
||
156 | $sConfigKey = "upload.files.$sField"; |
||
157 | if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
||
158 | { |
||
159 | $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage); |
||
160 | $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir); |
||
161 | $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions); |
||
162 | } |
||
163 | |||
164 | if(!is_string($sRootDir)) |
||
165 | { |
||
166 | throw new RequestException($this->xTranslator->trans('errors.upload.dir')); |
||
167 | } |
||
168 | if(!isset($this->aAdapters[$sStorage])) |
||
169 | { |
||
170 | throw new RequestException($this->xTranslator->trans('errors.upload.adapter')); |
||
171 | } |
||
172 | |||
173 | $this->aFilesystems[$sField] = new Filesystem(call_user_func($this->aAdapters[$sStorage], $sRootDir, $aOptions)); |
||
174 | return $this->aFilesystems[$sField]; |
||
175 | } |
||
176 | } |
||
177 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths