|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tgallice\FBMessenger\Model\Attachment; |
|
4
|
|
|
|
|
5
|
|
|
use Tgallice\FBMessenger\ExtensionChecker; |
|
6
|
|
|
use Tgallice\FBMessenger\Model\Attachment; |
|
7
|
|
|
|
|
8
|
|
|
class File extends Attachment |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string|null |
|
12
|
|
|
*/ |
|
13
|
|
|
private $path; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
private $isRemoteFile; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var null|resource |
|
22
|
|
|
*/ |
|
23
|
|
|
private $stream; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $allowedExtensions = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $type |
|
32
|
|
|
* @param string $filePath |
|
33
|
|
|
*/ |
|
34
|
13 |
|
public function __construct($filePath, $type = Attachment::TYPE_FILE) |
|
35
|
|
|
{ |
|
36
|
13 |
|
$this->path = $filePath; |
|
37
|
|
|
|
|
38
|
|
|
// Local image is sent with a multipart request |
|
39
|
13 |
|
$payload = []; |
|
40
|
|
|
|
|
41
|
13 |
|
if ($this->isRemoteFile()) { |
|
42
|
|
|
$payload = [ |
|
43
|
2 |
|
'url' => $this->path, |
|
44
|
2 |
|
]; |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
13 |
|
$this->validateExtension(); |
|
48
|
|
|
|
|
49
|
12 |
|
parent::__construct($type, $payload); |
|
50
|
12 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getPath() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->path; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public function open() |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($this->stream) { |
|
63
|
1 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
if ($this->isRemoteFile()) { |
|
67
|
1 |
|
throw new \RuntimeException('A remote file can not be open'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
if (!is_readable($this->path)) { |
|
71
|
1 |
|
throw new \InvalidArgumentException(sprintf('The file "%s" should be readable', $this->path)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$this->stream = fopen($this->path, 'r'); |
|
75
|
|
|
|
|
76
|
1 |
|
if (!$this->stream) { |
|
77
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to open the media "%s"', $this->path)); |
|
78
|
|
|
} |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return null|resource |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getStream() |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->open(); |
|
87
|
|
|
|
|
88
|
1 |
|
return $this->stream; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
13 |
|
public function isRemoteFile() |
|
95
|
|
|
{ |
|
96
|
13 |
|
if (isset($this->isRemoteFile)) { |
|
97
|
5 |
|
return $this->isRemoteFile; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
13 |
|
return $this->isRemoteFile = preg_match('/^(https?|ftp):\/\/.*/', $this->path) === 1; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
13 |
|
private function validateExtension() |
|
104
|
|
|
{ |
|
105
|
13 |
|
if (!empty($this->allowedExtensions) && !ExtensionChecker::check($this->path, $this->allowedExtensions)) { |
|
106
|
1 |
|
throw new \InvalidArgumentException( |
|
107
|
1 |
|
sprintf('"%s" this file has not an allowed extension. Only %s extensions are supported', $this->path, '["'. implode('", "', $this->allowedExtensions) .'"]') |
|
108
|
1 |
|
); |
|
109
|
|
|
} |
|
110
|
12 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|