|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
|
4
|
|
|
|
|
5
|
|
|
use FFMpeg\Format\FormatInterface; |
|
6
|
|
|
|
|
7
|
|
|
class MediaExporter |
|
8
|
|
|
{ |
|
9
|
|
|
protected $media; |
|
10
|
|
|
|
|
11
|
|
|
protected $disk; |
|
12
|
|
|
|
|
13
|
|
|
protected $format; |
|
14
|
|
|
|
|
15
|
|
|
protected $visibility; |
|
16
|
|
|
|
|
17
|
|
|
protected $saveMethod = 'saveAudioOrVideo'; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Media $media) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->media = $media; |
|
22
|
|
|
|
|
23
|
|
|
$this->disk = $media->getFile()->getDisk(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getFormat(): FormatInterface |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->format; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function inFormat(FormatInterface $format): MediaExporter |
|
32
|
|
|
{ |
|
33
|
|
|
$this->format = $format; |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function getDisk(): Disk |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->disk; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function toDisk($diskOrName): MediaExporter |
|
44
|
|
|
{ |
|
45
|
|
|
if ($diskOrName instanceof Disk) { |
|
46
|
|
|
$this->disk = $diskOrName; |
|
47
|
|
|
} else { |
|
48
|
|
|
$this->disk = Disk::fromName($diskOrName); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function withVisibility(string $visibility = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->visibility = $visibility; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function save(string $path): Media |
|
62
|
|
|
{ |
|
63
|
|
|
$disk = $this->getDisk(); |
|
64
|
|
|
$file = $disk->newFile($path); |
|
65
|
|
|
|
|
66
|
|
|
$destinationPath = $this->getDestinationPathForSaving($file); |
|
67
|
|
|
|
|
68
|
|
|
$this->createDestinationPathForSaving($file); |
|
69
|
|
|
|
|
70
|
|
|
$this->{$this->saveMethod}($destinationPath); |
|
71
|
|
|
|
|
72
|
|
|
if (!$disk->isLocal()) { |
|
73
|
|
|
$this->moveSavedFileToRemoteDisk($destinationPath, $file); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($this->visibility) { |
|
|
|
|
|
|
77
|
|
|
$disk->setVisibility($path, $this->visibility); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->media; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $fileOnRemoteDisk->put($localSourcePath) && @unlink($localSourcePath); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function getDestinationPathForSaving(File $file): string |
|
89
|
|
|
{ |
|
90
|
|
|
if (!$file->getDisk()->isLocal()) { |
|
91
|
|
|
$tempName = FFMpeg::newTemporaryFile(); |
|
92
|
|
|
|
|
93
|
|
|
return $tempName . '.' . $file->getExtension(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $file->getFullPath(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function createDestinationPathForSaving(File $file) |
|
100
|
|
|
{ |
|
101
|
|
|
if (!$file->getDisk()->isLocal()) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$directory = pathinfo($file->getPath())['dirname']; |
|
106
|
|
|
|
|
107
|
|
|
return $file->getDisk()->createDirectory($directory); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function saveAudioOrVideo(string $fullPath): MediaExporter |
|
111
|
|
|
{ |
|
112
|
|
|
$this->media->save($this->getFormat(), $fullPath); |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: