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