|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\UrlGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use DateTimeInterface; |
|
6
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
|
7
|
|
|
use Illuminate\Filesystem\FilesystemManager; |
|
8
|
|
|
|
|
9
|
|
|
class S3UrlGenerator extends BaseUrlGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var \Illuminate\Filesystem\FilesystemManager */ |
|
12
|
|
|
protected $filesystemManager; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Config $config, FilesystemManager $filesystemManager) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->filesystemManager = $filesystemManager; |
|
17
|
|
|
|
|
18
|
|
|
parent::__construct($config); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the url for a media item. |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getUrl(): string |
|
27
|
|
|
{ |
|
28
|
|
|
$url = $this->getPathRelativeToRoot(); |
|
29
|
|
|
|
|
30
|
|
|
if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) { |
|
31
|
|
|
$url = $root.'/'.$url; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$url = $this->rawUrlEncodeFilename($url); |
|
35
|
|
|
|
|
36
|
|
|
$url = $this->versionUrl($url); |
|
37
|
|
|
|
|
38
|
|
|
return config('medialibrary.s3.domain').'/'.$url; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the temporary url for a media item. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \DateTimeInterface $expiration |
|
45
|
|
|
* @param array $options |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string |
|
50
|
|
|
{ |
|
51
|
|
|
return $this |
|
52
|
|
|
->filesystemManager |
|
53
|
|
|
->disk($this->media->disk) |
|
54
|
|
|
->temporaryUrl($this->getPath(), $expiration, $options); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the url for the profile of a media item. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getPath(): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getPathRelativeToRoot(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the url to the directory containing responsive images. |
|
69
|
|
|
* |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getResponsiveImagesDirectoryUrl(): string |
|
73
|
|
|
{ |
|
74
|
|
|
$url = $this->pathGenerator->getPathForResponsiveImages($this->media); |
|
75
|
|
|
if ($root = config('filesystems.disks.'.$this->media->disk.'.root')) { |
|
76
|
|
|
$url = $root.'/'.$url; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return config('medialibrary.s3.domain').'/'.$url; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|