|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\FileAdder\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Collections\Collection; |
|
6
|
|
|
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded; |
|
7
|
|
|
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection; |
|
8
|
|
|
use ByTIC\MediaLibrary\Loaders\Filesystem; |
|
9
|
|
|
use ByTIC\MediaLibrary\Media\Manipulators\ManipulatorFactory; |
|
10
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
|
11
|
|
|
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory; |
|
12
|
|
|
use ByTIC\MediaLibrary\Support\MediaModels; |
|
13
|
|
|
use ByTIC\MediaLibrary\Validation\Violations\ViolationsBag; |
|
14
|
|
|
use Nip\Filesystem\File; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Trait FileAdderProcessesTrait. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait FileAdderProcessesTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param string|Collection $collection |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function toMediaCollection($collection) |
|
25
|
|
|
{ |
|
26
|
3 |
|
if (is_string($collection)) { |
|
27
|
2 |
|
$collection = $this->getMediaRepository()->getCollection($collection); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
3 |
|
$this->processMediaItem($collection); |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Collection $collection |
|
34
|
|
|
* @param Media|null $media |
|
35
|
|
|
*/ |
|
36
|
3 |
|
protected function processMediaItem(Collection $collection, Media $media = null) |
|
37
|
|
|
{ |
|
38
|
3 |
|
$this->guardAgainstDisallowedFileAdditions($collection, $this->getFile()); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
2 |
|
$media = $media ? $media : $this->getMedia(); |
|
|
|
|
|
|
41
|
2 |
|
$media->setCollection($collection); |
|
42
|
|
|
|
|
43
|
2 |
|
$this->copyMediaToFilesystem($media); |
|
44
|
2 |
|
$this->createMediaConversions($media); |
|
45
|
2 |
|
$this->saveMediaRecord($media); |
|
46
|
|
|
|
|
47
|
2 |
|
$collection->appendMedia($media); |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Media|null $media |
|
52
|
|
|
*/ |
|
53
|
2 |
|
protected function copyMediaToFilesystem(Media $media = null) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$media = $media ? $media : $this->getMedia(); |
|
56
|
2 |
|
$destination = PathGeneratorFactory::create()::getBasePathForMediaOriginal($media); |
|
57
|
2 |
|
$destination .= DIRECTORY_SEPARATOR . $media->getCollection()->getStrategy()::makeFileName($this); |
|
58
|
|
|
|
|
59
|
2 |
|
$media->generateFileFromContent($destination, fopen($this->getPathToFile(), 'r')); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
2 |
|
$file = new File($media->getCollection()->getFilesystem(), $destination); |
|
62
|
2 |
|
$media->setFile($file); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Media|null $media |
|
67
|
|
|
*/ |
|
68
|
2 |
|
protected function createMediaConversions(Media $media = null) |
|
69
|
|
|
{ |
|
70
|
2 |
|
$media = $media ? $media : $this->getMedia(); |
|
71
|
|
|
|
|
72
|
2 |
|
ManipulatorFactory::createForMedia($media)->performConversions( |
|
73
|
2 |
|
$this->getSubject()->getMediaConversions()->forCollection($media->getCollection()->getName()), |
|
|
|
|
|
|
74
|
|
|
$media |
|
75
|
|
|
); |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Media|null $media |
|
80
|
|
|
*/ |
|
81
|
2 |
|
protected function saveMediaRecord(Media $media = null) |
|
82
|
|
|
{ |
|
83
|
2 |
|
$media = $media ? $media : $this->getMedia(); |
|
84
|
|
|
|
|
85
|
2 |
|
if ($media->getCollection()->getLoader() instanceof Filesystem) { |
|
86
|
2 |
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
$propertiesRecord = $this->getSubject()->mediaProperties($media->getCollection()); |
|
89
|
|
|
$propertiesRecord->saveDbLoaded(true); |
|
90
|
|
|
|
|
91
|
|
|
MediaModels::records()->createFor( |
|
92
|
|
|
$media->getFile(), |
|
93
|
|
|
$this->getSubject(), |
|
94
|
|
|
$media->getCollection() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Collection $collection |
|
100
|
|
|
* @param mixed $file |
|
101
|
|
|
*/ |
|
102
|
3 |
|
protected function guardAgainstDisallowedFileAdditions(Collection $collection, $file) |
|
103
|
|
|
{ |
|
104
|
3 |
|
$guardTest = ($collection->acceptsMedia)($file); |
|
105
|
3 |
|
if ($guardTest instanceof ViolationsBag) { |
|
106
|
|
|
throw FileUnacceptableForCollection::createFromViolationsBag($file, $collection, $guardTest); |
|
107
|
|
|
} |
|
108
|
3 |
|
if ($guardTest === false) { |
|
109
|
1 |
|
throw new FileCannotBeAdded(); |
|
110
|
|
|
} |
|
111
|
2 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|