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
|
3 |
|
public function getConversionNames(): array |
18
|
|
|
{ |
19
|
3 |
|
if ($this->conversionNames == null) { |
20
|
3 |
|
$this->conversionNames = $this->generateConversionNames(); |
21
|
|
|
} |
22
|
3 |
|
return $this->conversionNames; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
3 |
|
protected function generateConversionNames() |
29
|
|
|
{ |
30
|
3 |
|
$conversions = $this->getConversions(); |
31
|
|
|
|
32
|
3 |
|
$return = []; |
33
|
3 |
|
foreach ($conversions as $conversion) { |
34
|
1 |
|
$return[] = $conversion->getName(); |
35
|
|
|
} |
36
|
3 |
|
return $return; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
public function getConversions(): ConversionCollection |
40
|
|
|
{ |
41
|
3 |
|
if ($this->conversions == null) { |
42
|
3 |
|
$this->conversions = $this->generateConversions(); |
43
|
|
|
} |
44
|
3 |
|
return $this->conversions; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return ConversionCollection |
49
|
|
|
*/ |
50
|
3 |
|
protected function generateConversions() |
51
|
|
|
{ |
52
|
3 |
|
$collection = $this->getCollection(); |
|
|
|
|
53
|
3 |
|
$model = $this->getModel(); |
|
|
|
|
54
|
3 |
|
if ($collection && $model) { |
55
|
3 |
|
return $model->getMediaConversions()->forCollection($collection->getName()); |
56
|
|
|
} |
57
|
|
|
return new ConversionCollection(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
2 |
|
public function removeConversions() |
62
|
|
|
{ |
63
|
2 |
|
$converstions = $this->getConversionNames(); |
64
|
2 |
|
$converstions[] = 'full'; |
65
|
2 |
|
$filesystem = $this->getFile()->getFilesystem(); |
|
|
|
|
66
|
2 |
|
foreach ($converstions as $converstion) { |
67
|
2 |
|
$path = $this->getPath($converstion); |
|
|
|
|
68
|
2 |
|
if ($filesystem->has($path)) { |
69
|
|
|
$filesystem->delete($path); |
70
|
|
|
} |
71
|
|
|
} |
72
|
2 |
|
} |
73
|
|
|
} |
74
|
|
|
|