1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Spatie\Glide\GlideImage; |
7
|
|
|
use Spatie\MediaLibrary\Conversion\Conversion; |
8
|
|
|
use Spatie\MediaLibrary\Conversion\ConversionCollection; |
9
|
|
|
use Spatie\MediaLibrary\Events\ConversionHasBeenCompleted; |
10
|
|
|
use Spatie\MediaLibrary\Helpers\File as MediaLibraryFileHelper; |
11
|
|
|
use Spatie\MediaLibrary\Jobs\PerformConversions; |
12
|
|
|
|
13
|
|
|
class FileManipulator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Create all derived files for the given media. |
17
|
|
|
* |
18
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
19
|
|
|
*/ |
20
|
|
|
public function createDerivedFiles(Media $media) |
21
|
|
|
{ |
22
|
|
|
$profileCollection = ConversionCollection::createForMedia($media); |
23
|
|
|
|
24
|
|
|
$this->performConversions( |
25
|
|
|
$profileCollection->getNonQueuedConversions($media->collection_name), |
26
|
|
|
$media |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$queuedConversions = $profileCollection->getQueuedConversions($media->collection_name); |
30
|
|
|
|
31
|
|
|
if (count($queuedConversions)) { |
32
|
|
|
$this->dispatchQueuedConversions($media, $queuedConversions); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Perform the given conversions for the given media. |
38
|
|
|
* |
39
|
|
|
* @param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions |
40
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
41
|
|
|
*/ |
42
|
|
|
public function performConversions(ConversionCollection $conversions, Media $media) |
43
|
|
|
{ |
44
|
|
|
$imageGenerator = $this->determineImageGenerator($media); |
45
|
|
|
|
46
|
|
|
if (! $imageGenerator) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$tempDirectory = $this->createTempDirectory(); |
51
|
|
|
|
52
|
|
|
$copiedOriginalFile = $tempDirectory.'/'.str_random(16).'.'.$media->extension; |
53
|
|
|
|
54
|
|
|
app(Filesystem::class)->copyFromMediaLibrary($media, $copiedOriginalFile); |
55
|
|
|
|
56
|
|
|
foreach ($conversions as $conversion) { |
57
|
|
|
$copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); |
58
|
|
|
|
59
|
|
|
$conversionResult = $this->performConversion($media, $conversion, $copiedOriginalFile); |
60
|
|
|
|
61
|
|
|
$renamedFile = MediaLibraryFileHelper::renameInDirectory($conversionResult, $conversion->getName().'.'. |
62
|
|
|
$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION))); |
63
|
|
|
|
64
|
|
|
app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, true); |
65
|
|
|
|
66
|
|
|
event(new ConversionHasBeenCompleted($media, $conversion)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
File::deleteDirectory($tempDirectory); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Perform the conversion. |
74
|
|
|
* |
75
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
76
|
|
|
* @param Conversion $conversion |
77
|
|
|
* @param string $copiedOriginalFile |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function performConversion(Media $media, Conversion $conversion, string $copiedOriginalFile) |
82
|
|
|
{ |
83
|
|
|
$conversionTempFile = pathinfo($copiedOriginalFile, PATHINFO_DIRNAME).'/'.string()->random(16). |
84
|
|
|
$conversion->getName().'.'.$media->extension; |
85
|
|
|
|
86
|
|
|
File::copy($copiedOriginalFile, $conversionTempFile); |
87
|
|
|
|
88
|
|
|
foreach ($conversion->getManipulations() as $manipulation) { |
89
|
|
|
GlideImage::create($conversionTempFile) |
90
|
|
|
->modify($manipulation) |
91
|
|
|
->save($conversionTempFile); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $conversionTempFile; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Create a directory to store some working files. |
99
|
|
|
*/ |
100
|
|
|
public function createTempDirectory() : string |
101
|
|
|
{ |
102
|
|
|
$tempDirectory = storage_path('medialibrary/temp/'.str_random(16)); |
103
|
|
|
|
104
|
|
|
File::makeDirectory($tempDirectory, 493, true); |
105
|
|
|
|
106
|
|
|
return $tempDirectory; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* |
110
|
|
|
* Dispatch the given conversions. |
111
|
|
|
*/ |
112
|
|
|
protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions) |
113
|
|
|
{ |
114
|
|
|
$job = new PerformConversions($queuedConversions, $media); |
115
|
|
|
|
116
|
|
|
$customQueue = config('laravel-medialibrary.queue_name'); |
117
|
|
|
|
118
|
|
|
if ($customQueue != '') { |
119
|
|
|
$job->onQueue($customQueue); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \Spatie\MediaLibrary\Media $media |
127
|
|
|
* |
128
|
|
|
* @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null |
129
|
|
|
*/ |
130
|
|
|
public function determineImageGenerator(Media $media) |
131
|
|
|
{ |
132
|
|
|
$imageGenerators = $media->getImageGenerators() |
133
|
|
|
->map(function (string $imageGeneratorClassName) { |
134
|
|
|
return app($imageGeneratorClassName); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
foreach ($imageGenerators as $imageGenerator) { |
138
|
|
|
if ($imageGenerator->canConvert($media->getPath())) { |
139
|
|
|
return $imageGenerator; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|