1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Media\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Conversions\ConversionCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait HasConversionsTrait |
9
|
|
|
* @package ByTIC\MediaLibrary\Media\Traits |
10
|
|
|
*/ |
11
|
|
|
trait HasConversionsTrait |
12
|
|
|
{ |
13
|
|
|
protected $conversions = null; |
14
|
|
|
|
15
|
|
|
protected $conversionNames; |
16
|
|
|
|
17
|
4 |
|
public function getConversionNames(): array |
18
|
|
|
{ |
19
|
4 |
|
if ($this->conversionNames == null) { |
20
|
4 |
|
$this->conversionNames = $this->generateConversionNames(); |
21
|
|
|
} |
22
|
4 |
|
return $this->conversionNames; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
4 |
|
protected function generateConversionNames() |
29
|
|
|
{ |
30
|
4 |
|
$conversions = $this->getConversions(); |
31
|
|
|
|
32
|
4 |
|
$return = []; |
33
|
4 |
|
foreach ($conversions as $conversion) { |
34
|
2 |
|
$return[] = $conversion->getName(); |
35
|
|
|
} |
36
|
4 |
|
return $return; |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
public function getConversions(): ConversionCollection |
40
|
|
|
{ |
41
|
4 |
|
if ($this->conversions == null) { |
42
|
4 |
|
$this->conversions = $this->generateConversions(); |
43
|
|
|
} |
44
|
4 |
|
return $this->conversions; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $name |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
1 |
|
public function hasConversion($name) |
52
|
|
|
{ |
53
|
1 |
|
$names = is_array($name) ? $name : [$name]; |
54
|
1 |
|
$conversions = $this->getConversionNames(); |
55
|
1 |
|
foreach ($names as $name) { |
|
|
|
|
56
|
1 |
|
if (!in_array($name, $conversions)) { |
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
1 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ConversionCollection |
65
|
|
|
*/ |
66
|
4 |
|
protected function generateConversions() |
67
|
|
|
{ |
68
|
4 |
|
$collection = $this->getCollection(); |
|
|
|
|
69
|
4 |
|
$model = $this->getModel(); |
|
|
|
|
70
|
4 |
|
if ($collection && $model) { |
71
|
4 |
|
return $model->getMediaConversions()->forCollection($collection->getName()); |
72
|
|
|
} |
73
|
|
|
return new ConversionCollection(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
2 |
|
public function removeConversions() |
78
|
|
|
{ |
79
|
2 |
|
$converstions = $this->getConversionNames(); |
80
|
2 |
|
$converstions[] = 'full'; |
81
|
2 |
|
$filesystem = $this->getFile()->getFilesystem(); |
|
|
|
|
82
|
2 |
|
foreach ($converstions as $converstion) { |
83
|
2 |
|
$path = $this->getPath($converstion); |
|
|
|
|
84
|
2 |
|
if ($filesystem->has($path)) { |
85
|
|
|
$filesystem->delete($path); |
86
|
|
|
} |
87
|
|
|
} |
88
|
2 |
|
} |
89
|
|
|
} |
90
|
|
|
|