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 resource |
33
|
|
|
*/ |
34
|
|
|
private $file; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Psr\Http\Message\StreamInterface |
38
|
|
|
*/ |
39
|
|
|
private $stream; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param StreamInterface $stream |
43
|
|
|
* @param int $size |
44
|
|
|
* @param int $error |
45
|
|
|
* @param string|null $clientFilename |
46
|
|
|
* @param string|null $clientMediaType |
47
|
|
|
* @throws \RuntimeException |
48
|
|
|
*/ |
49
|
4 |
|
public function __construct($stream, $size, $error = UPLOAD_ERR_OK, $clientFilename = null, $clientMediaType = null) |
50
|
|
|
{ |
51
|
4 |
|
if (is_string($stream) || is_resource($stream)) { |
|
|
|
|
52
|
|
|
$this->stream = new Stream($stream); |
53
|
4 |
|
} else if ($stream instanceof StreamInterface) { |
|
|
|
|
54
|
4 |
|
$this->stream = $stream; |
55
|
|
|
} else { |
56
|
2 |
|
throw new \RuntimeException('Invalid uploaded file.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$this->size = $size; |
60
|
4 |
|
$this->error = $error; |
61
|
4 |
|
$this->clientFilename = $clientFilename; |
62
|
4 |
|
$this->clientMediaType = $clientMediaType; |
63
|
4 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return self |
67
|
|
|
* @throws \InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
public static function createFromGlobal() |
70
|
|
|
{ |
71
|
|
|
$normalize = []; |
72
|
|
|
foreach ($_FILES AS $key => $value) { |
73
|
|
|
if ($value instanceof UploadedFileInterface) { |
74
|
|
|
$normalize[$key] = $value; |
75
|
|
|
} else if (is_array($value) && isset($value['tmp_name'])) { |
76
|
|
|
$normalize[$key] = new UploadedFile($value['tmp_name'], $value['size'], $value['error'], $value['name'], $value['type']); |
77
|
|
|
} else { |
78
|
|
|
throw new \InvalidArgumentException('Invalid value in files specification'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return $normalize; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getClientFilename() |
88
|
|
|
{ |
89
|
|
|
return $this->clientFilename; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getClientMediaType() |
96
|
|
|
{ |
97
|
|
|
return $this->clientMediaType; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getError() |
104
|
|
|
{ |
105
|
|
|
return $this->error; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
2 |
|
public function getSize() |
112
|
|
|
{ |
113
|
2 |
|
return $this->size; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return resource |
118
|
|
|
*/ |
119
|
|
|
public function getStream() |
120
|
|
|
{ |
121
|
|
|
return $this->stream; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $targetPath |
126
|
|
|
* @throws \RuntimeException |
127
|
|
|
*/ |
128
|
|
|
public function moveTo($targetPath) |
129
|
|
|
{ |
130
|
|
|
|
131
|
|
|
if (!is_dir($targetPath)) { |
132
|
|
|
throw new \RuntimeException('Invalid targetPath specified.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->file) { |
136
|
|
|
$upload = move_uploaded_file($this->file, $targetPath); |
|
|
|
|
137
|
|
|
} else if ($this->stream) { |
138
|
|
|
$upload = copy_to_stream($this->stream, $targetPath); |
|
|
|
|
139
|
|
|
} else { |
140
|
|
|
throw new \RuntimeException('Invalid uploaded file.'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!$upload) { |
144
|
|
|
throw new \RuntimeException('Erro while uploading file.'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|