|
1
|
|
|
<?php |
|
2
|
|
|
namespace DevOp\Core\Http; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Http\Message\StreamInterface; |
|
5
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
6
|
|
|
|
|
7
|
|
|
class UploadedFile implements UploadedFileInterface |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $clientFilename; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $clientMediaType; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private $error; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $size; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|resource |
|
33
|
|
|
*/ |
|
34
|
|
|
private $file; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var StreamInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $stream; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $moved = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $stream |
|
48
|
|
|
* @param int $size |
|
49
|
|
|
* @param int $error |
|
50
|
|
|
* @param string|null $clientFilename |
|
51
|
|
|
* @param string|null $clientMediaType |
|
52
|
|
|
* @throws \RuntimeException |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function __construct($stream, $size = 0, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null) |
|
55
|
|
|
{ |
|
56
|
4 |
|
if ($stream instanceof StreamInterface) { |
|
57
|
4 |
|
$this->stream = $stream; |
|
58
|
2 |
|
} else if (is_resource($stream)) { |
|
59
|
|
|
$this->stream = new Stream($stream); |
|
60
|
2 |
|
} else if (is_string($stream)) { |
|
61
|
|
|
$this->file = $stream; |
|
62
|
|
|
} else { |
|
63
|
2 |
|
throw new \RuntimeException('Invalid uploaded file.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
$this->size = $size; |
|
67
|
4 |
|
$this->error = $error; |
|
68
|
4 |
|
$this->clientFilename = $clientFilename; |
|
69
|
4 |
|
$this->clientMediaType = $clientMediaType; |
|
70
|
4 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array |
|
74
|
|
|
* @throws \InvalidArgumentException |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function createFromGlobal() |
|
77
|
|
|
{ |
|
78
|
|
|
$normalize = []; |
|
79
|
|
|
foreach ($_FILES AS $key => $value) { |
|
80
|
|
|
if ($value instanceof UploadedFileInterface) { |
|
81
|
|
|
$normalize[$key] = $value; |
|
82
|
|
|
} else if (is_array($value) && isset($value['tmp_name'])) { |
|
83
|
|
|
$normalize[$key] = new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']); |
|
84
|
|
|
} else { |
|
85
|
|
|
throw new \InvalidArgumentException('Invalid value in files specification'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return $normalize; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function getClientFilename() |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->clientFilename; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function getClientMediaType() |
|
103
|
|
|
{ |
|
104
|
2 |
|
return $this->clientMediaType; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return int |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function getError() |
|
111
|
|
|
{ |
|
112
|
2 |
|
return $this->error; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function getSize() |
|
119
|
|
|
{ |
|
120
|
2 |
|
return $this->size; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return StreamInterface |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getStream() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->stream; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param mixed $targetPath |
|
133
|
|
|
* @throws \RuntimeException |
|
134
|
|
|
*/ |
|
135
|
|
|
public function moveTo($targetPath) |
|
136
|
|
|
{ |
|
137
|
|
|
|
|
138
|
|
|
if (!is_dir($targetPath)) { |
|
139
|
|
|
throw new \RuntimeException('Invalid targetPath specified.'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if ($this->file && is_string($this->file)) { |
|
143
|
|
|
$this->moved = move_uploaded_file($this->file, $targetPath); |
|
144
|
|
|
} else if ($this->stream instanceof StreamInterface) { |
|
|
|
|
|
|
145
|
|
|
$this->moved = stream_copy_to_stream($this->stream->detach(), $targetPath) > 0; |
|
146
|
|
|
} else { |
|
147
|
|
|
throw new \RuntimeException('Invalid uploaded file.'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (!$this->moved) { |
|
151
|
|
|
throw new \RuntimeException('Error while uploading file.'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|