1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Filesystem; |
4
|
|
|
|
5
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\InteractsWithHttpHeaders; |
6
|
|
|
|
7
|
|
|
class MediaOnNetwork |
8
|
|
|
{ |
9
|
|
|
use InteractsWithHttpHeaders; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $path; |
15
|
|
|
|
16
|
|
|
public function __construct(string $path, array $headers = []) |
17
|
|
|
{ |
18
|
|
|
$this->path = $path; |
19
|
|
|
$this->headers = $headers; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function make(string $path, array $headers = []): self |
23
|
|
|
{ |
24
|
|
|
return new static($path, $headers); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getPath(): string |
28
|
|
|
{ |
29
|
|
|
return $this->path; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getDisk(): Disk |
33
|
|
|
{ |
34
|
|
|
return Disk::make(config('filesystems.default')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getLocalPath(): string |
38
|
|
|
{ |
39
|
|
|
return $this->path; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getFilenameWithoutExtension(): string |
43
|
|
|
{ |
44
|
|
|
return pathinfo($this->getPath())['filename']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getFilename(): string |
48
|
|
|
{ |
49
|
|
|
return pathinfo($this->getPath())['basename']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getCompiledHeaders(): array |
53
|
|
|
{ |
54
|
|
|
return static::compileHeaders($this->getHeaders()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Downloads the Media from the internet and stores it in |
59
|
|
|
* a temporary directory. |
60
|
|
|
* |
61
|
|
|
* @param callable $withCurl |
62
|
|
|
* @return \ProtoneMedia\LaravelFFMpeg\Filesystem\Media |
63
|
|
|
*/ |
64
|
|
|
public function toMedia(callable $withCurl = null): Media |
65
|
|
|
{ |
66
|
|
|
$disk = Disk::makeTemporaryDisk(); |
67
|
|
|
|
68
|
|
|
$curl = curl_init(); |
69
|
|
|
curl_setopt($curl, CURLOPT_URL, $this->path); |
|
|
|
|
70
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
71
|
|
|
|
72
|
|
|
if (!empty($this->headers)) { |
73
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($withCurl) { |
77
|
|
|
$curl = $withCurl($curl) ?: $curl; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$contents = curl_exec($curl); |
81
|
|
|
curl_close($curl); |
82
|
|
|
|
83
|
|
|
$disk->getFilesystemAdapter()->put( |
84
|
|
|
$filename = $this->getFilename(), |
85
|
|
|
$contents |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return new Media($disk, $filename); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|