|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\ChecksumAlgoIsNotSupported; |
|
6
|
|
|
use League\Flysystem\ChecksumProvider; |
|
7
|
|
|
use League\Flysystem\Config; |
|
8
|
|
|
use League\Flysystem\UnableToGeneratePublicUrl; |
|
9
|
|
|
use League\Flysystem\UnableToGenerateTemporaryUrl; |
|
10
|
|
|
use League\Flysystem\UrlGeneration\PublicUrlGenerator; |
|
11
|
|
|
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator; |
|
12
|
|
|
use League\Flysystem\WebDAV\WebDAVAdapter; |
|
13
|
|
|
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider; |
|
14
|
|
|
use League\Flysystem\ZipArchive\ZipArchiveAdapter; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Interacting with an ZipArchive filesystem |
|
20
|
|
|
* ! Notice |
|
21
|
|
|
* It's important to know this adapter does not fully comply with the adapter contract. The difference(s) is/are: |
|
22
|
|
|
* - Checksum setting or retrieving is not supported. |
|
23
|
|
|
* - PublicUrl setting or retrieving is not supported. |
|
24
|
|
|
* - TemporaryUrl setting or retrieving is not supported. |
|
25
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/zip-archive/ |
|
26
|
|
|
* |
|
27
|
|
|
* ```php |
|
28
|
|
|
* 'components' => [ |
|
29
|
|
|
* 'fs' => [ |
|
30
|
|
|
* 'class' => \diecoding\flysystem\ZipArchiveComponent::class, |
|
31
|
|
|
* 'pathToZip' => dirname(dirname(__DIR__)) . '/storage.zip', // or you can use @alias |
|
32
|
|
|
* 'prefix' => '', // root directory inside zip file |
|
33
|
|
|
* ], |
|
34
|
|
|
* ], |
|
35
|
|
|
* ``` |
|
36
|
|
|
* |
|
37
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
38
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
39
|
|
|
* @copyright Copyright (c) 2023 |
|
40
|
|
|
*/ |
|
41
|
|
|
class ZipArchiveComponent extends AbstractComponent implements PublicUrlGenerator, TemporaryUrlGenerator, ChecksumProvider |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $pathToZip; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
|
|
public function init() |
|
52
|
|
|
{ |
|
53
|
|
|
if (empty($this->pathToZip)) { |
|
54
|
|
|
throw new InvalidConfigException('The "pathToZip" property must be set.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
parent::init(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return WebDAVAdapter |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function initAdapter() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->pathToZip = (string) Yii::getAlias($this->pathToZip); |
|
66
|
|
|
|
|
67
|
|
|
return new ZipArchiveAdapter( |
|
|
|
|
|
|
68
|
|
|
new FilesystemZipArchiveProvider($this->pathToZip), |
|
69
|
|
|
$this->prefix |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function publicUrl(string $path, Config $config): string |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->debug) { |
|
76
|
|
|
throw new UnableToGeneratePublicUrl('ZipArchiveComponent does not support this operation.', $path); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function temporaryUrl(string $path, \DateTimeInterface $expiresAt, Config $config): string |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->debug) { |
|
85
|
|
|
throw new UnableToGenerateTemporaryUrl('ZipArchiveComponent does not support this operation.', $path); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function checksum(string $path, Config $config): string |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->debug) { |
|
94
|
|
|
throw new ChecksumAlgoIsNotSupported('ZipArchiveComponent does not support this operation.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return ''; |
|
98
|
|
|
} |
|
99
|
|
|
} |