|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use League\Flysystem\Local\LocalFilesystemAdapter; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\base\InvalidConfigException; |
|
8
|
|
|
use yii\helpers\Json; |
|
9
|
|
|
use yii\helpers\Url; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class LocalComponent |
|
13
|
|
|
* |
|
14
|
|
|
* ```php |
|
15
|
|
|
* 'components' => [ |
|
16
|
|
|
* 'fs' => [ |
|
17
|
|
|
* 'class' => \diecoding\flysystem\LocalComponent::class, |
|
18
|
|
|
* 'path' => dirname(dirname(__DIR__)) . '/storage', // or you can use @alias |
|
19
|
|
|
* 'cipherAlgo' => 'aes-128-cbc', |
|
20
|
|
|
* 'secret' => 'my-secret', |
|
21
|
|
|
* 'action' => '/site/file', |
|
22
|
|
|
* 'basePath' => '', // for multiple project in single storage, will be format to `$basePath . '/' . $path` |
|
23
|
|
|
* ], |
|
24
|
|
|
* ], |
|
25
|
|
|
* ``` |
|
26
|
|
|
* |
|
27
|
|
|
* @package diecoding\flysystem |
|
28
|
|
|
* |
|
29
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
30
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
31
|
|
|
* @copyright Copyright (c) 2023 |
|
32
|
|
|
*/ |
|
33
|
|
|
class LocalComponent extends AbstractComponent |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $path; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $action = '/site/file'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public function init() |
|
49
|
|
|
{ |
|
50
|
|
|
if (empty($this->path)) { |
|
51
|
|
|
throw new InvalidConfigException('The "path" property must be set.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (empty($this->secret)) { |
|
55
|
|
|
throw new InvalidConfigException('The "secret" property must be set.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->path = $this->normalizePath(Yii::getAlias($this->path)); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
parent::init(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get a URL |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $filePath |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getUrl(string $filePath) |
|
70
|
|
|
{ |
|
71
|
|
|
$params = [ |
|
72
|
|
|
'filePath' => $filePath, |
|
73
|
|
|
'expires' => null, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get a pre-signed URL |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $filePath |
|
83
|
|
|
* @param int|string|\DateTimeInterface $expires |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getPresignedUrl(string $filePath, $expires = '+5 Minutes') |
|
87
|
|
|
{ |
|
88
|
|
|
$params = [ |
|
89
|
|
|
'filePath' => $filePath, |
|
90
|
|
|
'expires' => $this->convertToTimestamp($expires), |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
return Url::toRoute([$this->action, 'data' => $this->encrypt(Json::encode($params))], true); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return LocalFilesystemAdapter |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function initAdapter() |
|
100
|
|
|
{ |
|
101
|
|
|
return new LocalFilesystemAdapter($this->path); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|