-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMultipartStreamBuilder.php
347 lines (298 loc) · 10.2 KB
/
MultipartStreamBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
namespace Http\Message\MultipartStream;
use Http\Discovery\Exception\NotFoundException;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\StreamFactory as HttplugStreamFactory;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
/**
* Build your own Multipart stream. A Multipart stream is a collection of streams separated with a $bounary. This
* class helps you to create a Multipart stream with stream implementations from any PSR7 library.
*
* @author Michael Dowling and contributors to guzzlehttp/psr7
* @author Tobias Nyholm <[email protected]>
*/
class MultipartStreamBuilder
{
/**
* @var HttplugStreamFactory|StreamFactoryInterface
*/
private $streamFactory;
/**
* @var MimetypeHelper
*/
private $mimetypeHelper;
/**
* @var string
*/
private $boundary;
/**
* @var array Element where each Element is an array with keys ['contents', 'headers']
*/
private $data = [];
/**
* @param HttplugStreamFactory|StreamFactoryInterface|null $streamFactory
*/
public function __construct($streamFactory = null)
{
if ($streamFactory instanceof StreamFactoryInterface || $streamFactory instanceof HttplugStreamFactory) {
$this->streamFactory = $streamFactory;
return;
}
if (null !== $streamFactory) {
throw new \LogicException(sprintf(
'First arguemnt to the constructor of "%s" must be of type "%s", "%s" or null. Got %s',
__CLASS__,
StreamFactoryInterface::class,
HttplugStreamFactory::class,
\is_object($streamFactory) ? \get_class($streamFactory) : \gettype($streamFactory)
));
}
// Try to find a stream factory.
try {
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
} catch (NotFoundException $psr17Exception) {
try {
$this->streamFactory = StreamFactoryDiscovery::find();
} catch (NotFoundException $httplugException) {
// we could not find any factory.
throw $psr17Exception;
}
}
}
/**
* Add a resource to the Multipart Stream.
*
* @param string|resource|\Psr\Http\Message\StreamInterface $resource the filepath, resource or StreamInterface of the data
* @param array $headers additional headers array: ['header-name' => 'header-value']
*
* @return MultipartStreamBuilder
*/
public function addData($resource, array $headers = [])
{
$stream = $this->createStream($resource);
$this->data[] = ['contents' => $stream, 'headers' => $headers];
return $this;
}
/**
* Add a resource to the Multipart Stream.
*
* @param string $name the formpost name
* @param string|resource|StreamInterface $resource
* @param array{'headers': array<string, string>, 'filename': string} $options
*
* Options:
* - headers: additional headers as hashmap ['header-name' => 'header-value']
* - filename: used to determine the mime type
*
* @return MultipartStreamBuilder
*/
public function addResource($name, $resource, array $options = [])
{
$stream = $this->createStream($resource);
// validate options['headers'] exists
if (!isset($options['headers'])) {
$options['headers'] = [];
}
// Try to add filename if it is missing
if (empty($options['filename'])) {
$options['filename'] = null;
$uri = $stream->getMetadata('uri');
if ('php://' !== substr($uri, 0, 6) && 'data://' !== substr($uri, 0, 7)) {
$options['filename'] = $uri;
}
}
$this->prepareHeaders($name, $stream, $options['filename'], $options['headers']);
return $this->addData($stream, $options['headers']);
}
/**
* Build the stream.
*
* @return StreamInterface
*/
public function build()
{
// Open a temporary read-write stream as buffer.
// If the size is less than predefined limit, things will stay in memory.
// If the size is more than that, things will be stored in temp file.
$buffer = fopen('php://temp', 'r+');
foreach ($this->data as $data) {
// Add start and headers
fwrite($buffer, "--{$this->getBoundary()}\r\n".
$this->getHeaders($data['headers'])."\r\n");
/** @var $contentStream StreamInterface */
$contentStream = $data['contents'];
// Read stream into buffer
if ($contentStream->isSeekable()) {
$contentStream->rewind(); // rewind to beginning.
}
if ($contentStream->isReadable()) {
while (!$contentStream->eof()) {
// Read 1MB chunk into buffer until reached EOF.
fwrite($buffer, $contentStream->read(1048576));
}
} else {
fwrite($buffer, $contentStream->__toString());
}
fwrite($buffer, "\r\n");
}
// Append end
fwrite($buffer, "--{$this->getBoundary()}--\r\n");
// Rewind to starting position for reading.
fseek($buffer, 0);
return $this->createStream($buffer);
}
/**
* Add extra headers if they are missing.
*
* @param string $name
* @param string $filename
*/
private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers)
{
$hasFilename = '0' === $filename || $filename;
// Set a default content-disposition header if one was not provided
if (!$this->hasHeader($headers, 'content-disposition')) {
$headers['Content-Disposition'] = sprintf('form-data; name="%s"', $name);
if ($hasFilename) {
$headers['Content-Disposition'] .= sprintf('; filename="%s"', $this->basename($filename));
}
}
// Set a default Content-Type if one was not provided
if (!$this->hasHeader($headers, 'content-type') && $hasFilename) {
if ($type = $this->getMimetypeHelper()->getMimetypeFromFilename($filename)) {
$headers['Content-Type'] = $type;
}
}
}
/**
* Get the headers formatted for the HTTP message.
*
* @return string
*/
private function getHeaders(array $headers)
{
$str = '';
foreach ($headers as $key => $value) {
$str .= sprintf("%s: %s\r\n", $key, $value);
}
return $str;
}
/**
* Check if header exist.
*
* @param string $key case insensitive
*
* @return bool
*/
private function hasHeader(array $headers, $key)
{
$lowercaseHeader = strtolower($key);
foreach ($headers as $k => $v) {
if (strtolower($k) === $lowercaseHeader) {
return true;
}
}
return false;
}
/**
* Get the boundary that separates the streams.
*
* @return string
*/
public function getBoundary()
{
if (null === $this->boundary) {
$this->boundary = uniqid('', true);
}
return $this->boundary;
}
/**
* @param string $boundary
*
* @return MultipartStreamBuilder
*/
public function setBoundary($boundary)
{
$this->boundary = $boundary;
return $this;
}
/**
* @return MimetypeHelper
*/
private function getMimetypeHelper()
{
if (null === $this->mimetypeHelper) {
$this->mimetypeHelper = new ApacheMimetypeHelper();
}
return $this->mimetypeHelper;
}
/**
* If you have custom file extension you may overwrite the default MimetypeHelper with your own.
*
* @return MultipartStreamBuilder
*/
public function setMimetypeHelper(MimetypeHelper $mimetypeHelper)
{
$this->mimetypeHelper = $mimetypeHelper;
return $this;
}
/**
* Reset and clear all stored data. This allows you to use builder for a subsequent request.
*
* @return MultipartStreamBuilder
*/
public function reset()
{
$this->data = [];
$this->boundary = null;
return $this;
}
/**
* Gets the filename from a given path.
*
* PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
*
* @author Drupal 8.2
*
* @param string $path
*
* @return string
*/
private function basename($path)
{
$separators = '/';
if (DIRECTORY_SEPARATOR != '/') {
// For Windows OS add special separator.
$separators .= DIRECTORY_SEPARATOR;
}
// Remove right-most slashes when $path points to directory.
$path = rtrim($path, $separators);
// Returns the trailing part of the $path starting after one of the directory separators.
$filename = preg_match('@[^'.preg_quote($separators, '@').']+$@', $path, $matches) ? $matches[0] : '';
return $filename;
}
/**
* @param string|resource|StreamInterface $resource
*
* @return StreamInterface
*/
private function createStream($resource)
{
if ($resource instanceof StreamInterface) {
return $resource;
}
if ($this->streamFactory instanceof HttplugStreamFactory) {
return $this->streamFactory->createStream($resource);
}
// Assert: We are using a PSR17 stream factory.
if (\is_string($resource)) {
return $this->streamFactory->createStream($resource);
}
if (\is_resource($resource)) {
return $this->streamFactory->createStreamFromResource($resource);
}
throw new \InvalidArgumentException(sprintf('First argument to "%s::createStream()" must be a string, resource or StreamInterface.', __CLASS__));
}
}