|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem\traits; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use diecoding\flysystem\AbstractComponent; |
|
7
|
|
|
use League\Flysystem\Config; |
|
8
|
|
|
use League\Flysystem\PathPrefixer; |
|
9
|
|
|
use yii\helpers\Json; |
|
10
|
|
|
use yii\helpers\Url; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait UrlGeneratorAdapterTrait for Adapter |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
16
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
17
|
|
|
* @copyright Copyright (c) 2023 |
|
18
|
|
|
*/ |
|
19
|
|
|
trait UrlGeneratorAdapterTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var AbstractComponent |
|
23
|
|
|
*/ |
|
24
|
|
|
public $component; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public $skipPrefixer = false; |
|
30
|
|
|
|
|
31
|
|
|
public function publicUrl(string $path, /** @scrutinizer ignore-unused */Config $config): string |
|
32
|
|
|
{ |
|
33
|
|
|
// TODO: Use absolute path and don't encrypt |
|
34
|
|
|
if (!$this->skipPrefixer && $this->component->prefix) { |
|
35
|
|
|
$prefixer = new PathPrefixer((string) $this->component->prefix); |
|
36
|
|
|
$path = $prefixer->stripPrefix($path); |
|
37
|
|
|
} |
|
38
|
|
|
$params = [ |
|
39
|
|
|
'path' => $path, |
|
40
|
|
|
'expires' => 0, |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
return $this->generateUrlAction($params); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function temporaryUrl(string $path, DateTimeInterface $expiresAt, /** @scrutinizer ignore-unused */Config $config): string |
|
47
|
|
|
{ |
|
48
|
|
|
// TODO: Use absolute path and don't encrypt |
|
49
|
|
|
if (!$this->skipPrefixer && $this->component->prefix) { |
|
50
|
|
|
$prefixer = new PathPrefixer((string) $this->component->prefix); |
|
51
|
|
|
$path = $prefixer->stripPrefix($path); |
|
52
|
|
|
} |
|
53
|
|
|
$params = [ |
|
54
|
|
|
'path' => $path, |
|
55
|
|
|
'expires' => (int) $expiresAt->getTimestamp(), |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
return $this->generateUrlAction($params); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function generateUrlAction(array $params): string |
|
62
|
|
|
{ |
|
63
|
|
|
$data = $this->component->/** @scrutinizer ignore-call */encrypt(Json::encode($params)); |
|
64
|
|
|
|
|
65
|
|
|
return Url::toRoute([$this->component->action, 'data' => $data], true); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|