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 League\Flysystem\FilesystemInterface; |
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
|
|
|
public function getUrl(): string |
27
|
|
|
{ |
28
|
|
|
if (!$this->media->hasFile()) { |
29
|
|
|
return $this->media->getCollection()->getDefaultMediaUrl(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->getDisk()->getUrl($this->getPath()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getPath(): string |
36
|
|
|
{ |
37
|
|
|
$convestion = $this->conversion instanceof Conversion ? $this->conversion->getName() : ''; |
|
|
|
|
38
|
|
|
return $this->media->getPath($convestion); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function setMedia(Media $media): UrlGeneratorInterface |
42
|
|
|
{ |
43
|
1 |
|
$this->media = $media; |
44
|
|
|
|
45
|
1 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setConversion(Conversion $conversion): UrlGeneratorInterface |
49
|
|
|
{ |
50
|
|
|
$this->conversion = $conversion; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function setPathGenerator(AbstractPathGenerator $pathGenerator): UrlGeneratorInterface |
56
|
|
|
{ |
57
|
1 |
|
$this->pathGenerator = $pathGenerator; |
|
|
|
|
58
|
|
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function __toString(): string |
63
|
|
|
{ |
64
|
|
|
return $this->getUrl(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// protected function getDiskName(): string |
68
|
|
|
// { |
69
|
|
|
// return $this->conversion === null |
70
|
|
|
// ? $this->media->disk |
71
|
|
|
// : $this->media->conversions_disk; |
72
|
|
|
// } |
73
|
|
|
|
74
|
|
|
protected function getDisk(): FilesystemInterface |
75
|
|
|
{ |
76
|
|
|
// return app('filesystem')->disk($this->getDiskName()); |
77
|
|
|
return $this->media->getFile()->getFilesystem(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|