1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\UrlGenerator; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined; |
7
|
|
|
|
8
|
|
|
class LocalUrlGenerator extends BaseUrlGenerator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get the url for the profile of a media item. |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
* |
15
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined |
16
|
|
|
*/ |
17
|
|
|
public function getUrl(): string |
18
|
|
|
{ |
19
|
|
|
$url = $this->getBaseMediaDirectoryUrl().'/'.$this->getPathRelativeToRoot(); |
20
|
|
|
|
21
|
|
|
$url = $this->makeCompatibleForNonUnixHosts($url); |
22
|
|
|
|
23
|
|
|
$url = $this->rawUrlEncodeFilename($url); |
24
|
|
|
|
25
|
|
|
return $url; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \DateTimeInterface $expiration |
30
|
|
|
* @param array $options |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
* |
34
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined |
35
|
|
|
*/ |
36
|
|
|
public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string |
37
|
|
|
{ |
38
|
|
|
throw UrlCannotBeDetermined::temporaryUrlOnlyWorksOnS3(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/* |
42
|
|
|
* Get the path for the profile of a media item. |
43
|
|
|
*/ |
44
|
|
|
public function getPath(): string |
45
|
|
|
{ |
46
|
|
|
return $this->getStoragePath().'/'.$this->getPathRelativeToRoot(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function getBaseMediaDirectoryUrl(): string |
50
|
|
|
{ |
51
|
|
|
if ($diskUrl = $this->config->get("filesystems.disks.{$this->media->disk}.url")) { |
52
|
|
|
return str_replace(url('/'), '', $diskUrl); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (! starts_with($this->getStoragePath(), public_path())) { |
56
|
|
|
throw UrlCannotBeDetermined::mediaNotPubliclyAvailable($this->getStoragePath(), public_path()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->getBaseMediaDirectory(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/* |
63
|
|
|
* Get the directory where all files of the media item are stored. |
64
|
|
|
*/ |
65
|
|
|
protected function getBaseMediaDirectory(): string |
66
|
|
|
{ |
67
|
|
|
return str_replace(public_path(), '', $this->getStoragePath()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/* |
71
|
|
|
* Get the path where the whole medialibrary is stored. |
72
|
|
|
*/ |
73
|
|
|
protected function getStoragePath() : string |
74
|
|
|
{ |
75
|
|
|
$diskRootPath = $this->config->get("filesystems.disks.{$this->media->disk}.root"); |
76
|
|
|
|
77
|
|
|
return realpath($diskRootPath); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function makeCompatibleForNonUnixHosts(string $url): string |
81
|
|
|
{ |
82
|
|
|
if (DIRECTORY_SEPARATOR != '/') { |
83
|
|
|
$url = str_replace(DIRECTORY_SEPARATOR, '/', $url); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $url; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function rawUrlEncodeFilename(string $path = ''): string |
90
|
|
|
{ |
91
|
|
|
return pathinfo($path, PATHINFO_DIRNAME).'/'.rawurlencode(pathinfo($path, PATHINFO_BASENAME)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|