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