1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\UrlGenerator; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Conversions\Conversion; |
6
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
7
|
|
|
use ByTIC\MediaLibrary\PathGenerator\AbstractPathGenerator; |
8
|
|
|
use Nip\Filesystem\FileDisk; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AbstractUrlGenerator |
12
|
|
|
* @package ByTIC\MediaLibrary\UrlGenerator |
13
|
|
|
*/ |
14
|
|
|
class AbstractUrlGenerator implements UrlGeneratorInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Media |
18
|
|
|
*/ |
19
|
|
|
protected $media; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Conversion |
23
|
|
|
*/ |
24
|
|
|
protected $conversion = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AbstractPathGenerator |
28
|
|
|
*/ |
29
|
|
|
protected $pathGenerator; |
30
|
|
|
|
31
|
|
|
public function getUrl(): string |
32
|
|
|
{ |
33
|
|
|
if (!$this->media->hasFile()) { |
34
|
|
|
return $this->media->getCollection()->getDefaultMediaUrl(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->getDisk()->getUrl($this->getPath()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getPath(): string |
41
|
1 |
|
{ |
42
|
|
|
$convestion = $this->conversion instanceof Conversion ? $this->conversion->getName() : ''; |
|
|
|
|
43
|
1 |
|
return $this->media->getPath($convestion); |
44
|
|
|
} |
45
|
1 |
|
|
46
|
|
|
public function setMedia(Media $media): UrlGeneratorInterface |
47
|
|
|
{ |
48
|
|
|
$this->media = $media; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setConversion(Conversion $conversion): UrlGeneratorInterface |
54
|
|
|
{ |
55
|
1 |
|
$this->conversion = $conversion; |
56
|
|
|
|
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
1 |
|
|
60
|
|
|
public function setPathGenerator(AbstractPathGenerator $pathGenerator): UrlGeneratorInterface |
61
|
|
|
{ |
62
|
|
|
$this->pathGenerator = $pathGenerator; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __toString(): string |
68
|
|
|
{ |
69
|
|
|
return $this->getUrl(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// protected function getDiskName(): string |
73
|
|
|
// { |
74
|
|
|
// return $this->conversion === null |
75
|
|
|
// ? $this->media->disk |
76
|
|
|
// : $this->media->conversions_disk; |
77
|
|
|
// } |
78
|
|
|
|
79
|
|
|
protected function getDisk(): FileDisk |
80
|
|
|
{ |
81
|
|
|
// return app('filesystem')->disk($this->getDiskName()); |
82
|
|
|
return $this->media->getFile()->getFilesystem(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|