/
CommonMixin.php
656 lines (559 loc) · 21.9 KB
/
CommonMixin.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
namespace Filestack\Mixins;
use Filestack\FilestackConfig;
use Filestack\Filelink;
use Filestack\FileSecurity;
use Filestack\FilestackException;
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
/**
* Mixin for common functionalities used by most Filestack objects
*
*/
trait CommonMixin
{
protected $http_client;
protected $http_promises;
protected $user_agent_header;
protected $source_header;
/**
* Check if a string is a valid url.
*
* @param string $url url string to check
*
* @return bool
*/
public function isUrl($url)
{
$path = parse_url($url, PHP_URL_PATH);
$encoded_path = array_map('urlencode', explode('/', $path));
$url = str_replace($path, implode('/', $encoded_path), $url);
return filter_var($url, FILTER_VALIDATE_URL);
}
/**
* Delete a file from cloud storage
*
* @param string $handle Filestack file handle to delete
* @param string $api_key Filestack API Key
* @param FilestackSecurity $security Filestack security object is
* required for this call
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return bool (true = delete success, false = failed)
*/
public function sendDelete($handle, $api_key, $security)
{
$url = sprintf('%s/file/%s?key=%s',
FilestackConfig::API_URL, $handle, $api_key);
if ($security) {
$url = $security->signUrl($url);
}
$response = $this->sendRequest('DELETE', $url);
$status_code = $response->getStatusCode();
// handle response
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
return true;
}
/**
* Download a file to specified destination given a url
*
* @param string $url Filestack file url
* @param string $destination destination filepath to save to,
* can be a directory name
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return bool (true = download success, false = failed)
*/
protected function sendDownload($url, $destination, $security = null)
{
if (is_dir($destination)) {
// destination is a folder
$json = $this->sendGetMetaData($url, ["filename"], $security);
$remote_filename = $json['filename'];
$destination .= $remote_filename;
}
// sign url if security is passed in
if ($security) {
$url = $security->signUrl($url);
}
# send request
$options = ['sink' => $destination];
$url .= '&dl=true';
$response = $this->sendRequest('GET', $url, $options);
$status_code = $response->getStatusCode();
// handle response
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
return true;
}
/**
* Get the content of a file.
*
* @param string $url Filestack file url
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return string (file content)
*/
protected function sendGetContent($url, $security = null)
{
// sign url if security is passed in
if ($security) {
$url = $security->signUrl($url);
}
$response = $this->sendRequest('GET', $url);
$status_code = $response->getStatusCode();
// handle response
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$content = $response->getBody()->getContents();
return $content;
}
/**
* Get the metadata of a remote file. Will only retrieve specific fields
* if optional fields are passed in
*
* @param $url url of file
* @param $fields optional, specific fields to retrieve.
* values are: mimetype, filename, size,
* width, height,location, path,
* container, exif, uploaded (timestamp),
* writable, cloud, source_url
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails, e.g 400 bad request
*
* @return array
*/
protected function sendGetMetaData($url, $fields = [], $security = null)
{
$url .= "/metadata?";
foreach ($fields as $field_name) {
$url .= "&$field_name=true";
}
// sign url if security is passed in
if ($security) {
$url = $security->signUrl($url);
}
$response = $this->sendRequest('GET', $url);
$status_code = $response->getStatusCode();
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$json_response = json_decode($response->getBody(), true);
return $json_response;
}
/**
* Get the safe for work (sfw) flag of a filelink.
*
* @param string $handle Filestack file handle
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return json
*/
protected function sendGetSafeForWork($handle, $security)
{
$url = sprintf('%s/sfw/security=policy:%s,signature:%s/%s',
FilestackConfig::CDN_URL,
$security->policy,
$security->signature,
$handle);
$response = $this->sendRequest('GET', $url);
$status_code = $response->getStatusCode();
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$json_response = json_decode($response->getBody(), true);
return $json_response;
}
/**
* Get the tags of a filelink.
*
* @param string $handle Filestack file handle
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return json
*/
protected function sendGetTags($handle, $security)
{
$url = sprintf('%s/tags/security=policy:%s,signature:%s/%s',
FilestackConfig::CDN_URL,
$security->policy,
$security->signature,
$handle);
$response = $this->sendRequest('GET', $url);
$status_code = $response->getStatusCode();
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$json_response = json_decode($response->getBody(), true);
return $json_response;
}
/**
* Overwrite a file in cloud storage
*
* @param string $resource url or filepath
* @param string $handle Filestack file handle to overwrite
* @param string $api_key Filestack API Key
* @param FilestackSecurity $security Filestack security object is
* required for this call
*
* @throws FilestackException if API call fails, e.g 404 file not found
*
* @return Filestack\Filelink
*/
public function sendOverwrite($resource, $handle, $api_key, $security)
{
$url = sprintf('%s/file/%s?key=%s',
FilestackConfig::API_URL, $handle, $api_key);
if ($security) {
$url = $security->signUrl($url);
}
if ($this->isUrl($resource)) {
// external source (passing url instead of filepath)
$data['form_params'] = ['url' => $resource];
}
else {
// local file
$data['body'] = fopen($resource, 'r');
}
$response = $this->sendRequest('POST', $url, $data);
$filelink = $this->handleResponseCreateFilelink($response);
return $filelink;
}
/**
* Trigger the start of a multipart upload
*
* @param string $api_key Filestack API Key
* @param string $metadata metadata of file: filename, filesize,
* mimetype, location
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails
*
* @return json
*/
public function sendMultipartStart($api_key, $metadata, $security = null)
{
$data = [];
$this->addMultipartData($data, 'apikey', $api_key);
$this->addMultipartData($data, 'filename', $metadata['filename']);
$this->addMultipartData($data, 'mimetype', $metadata['mimetype']);
$this->addMultipartData($data, 'size', $metadata['filesize']);
$this->addMultipartData($data, 'store_location', $metadata['location']);
array_push($data, ['name' => 'files',
'contents' => '',
'filename' => $metadata['filename']
]);
$this->multipartApplySecurity($data, $security);
$url = FilestackConfig::UPLOAD_URL . '/multipart/start';
$response = $this->sendRequest('POST', $url, ['multipart' => $data]);
$json = $this->handleResponseDecodeJson($response);
return $json;
}
/**
* Upload a chunk of the file to server.
*
* @param array $jobs array of jobs to process
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails
*
* @return json
*/
public function sendMultipartJobs($jobs, $security = null)
{
$num_jobs = count($jobs);
$parts_etags = [];
$num_concurrent = FilestackConfig::UPLOAD_CONCURRENT_JOBS;
$jobs_completed = 0;
$start_index = 1;
$end_index = $num_jobs < $num_concurrent ? $num_jobs : $num_concurrent;
// loop through jobs and make async concurrent calls
while ($jobs_completed < $num_jobs) {
$upload_promises = [];
$s3_promises = [];
$this->appendUploadPromises($jobs, $start_index, $end_index,
$upload_promises, $jobs_completed, $security);
// settle promises (concurrent async requests to upload api)
$api_results = \GuzzleHttp\Promise\settle($upload_promises)->wait();
$this->appendS3Promises($jobs, $api_results, $s3_promises);
// settle promises (concurrent async requests to s3 api)
$s3_results = \GuzzleHttp\Promise\settle($s3_promises)->wait();
$this->multipartGetTags($s3_results, $parts_etags);
// clear arrays
unset($upload_promises);
unset($s3_promises);
// increment jobs start and end indexes
$start_index += $num_concurrent;
$end_index += $num_concurrent;
if ($end_index > $jobs_completed) {
$end_index += $num_jobs - $jobs_completed;
}
} // end_while
$parts_etags = implode(';', $parts_etags);
return $parts_etags;
}
/**
* Trigger the end of a multipart upload
*
* @param string $api_key Filestack API Key
* @param string $job_uri uri of job to mark as complete
* @param string $region job region
* @param string $upload_id upload id of job
* @param string $parts parts:etags, semicolon separated
* e.g. '1:etag_1;2:etag_2;3:etag_3'
* @param FilestackSecurity $security Filestack security object if
* security settings is turned on
*
* @throws FilestackException if API call fails
*
* @return json
*/
public function sendMultipartComplete($api_key, $parts, $upload_data,
$metadata, $security = null)
{
$data = [];
$this->addMultipartData($data, 'apikey', $api_key);
$this->addMultipartData($data, 'parts', $parts);
$this->addMultipartData($data, 'uri', $upload_data['uri']);
$this->addMultipartData($data, 'region', $upload_data['region']);
$this->addMultipartData($data, 'upload_id', $upload_data['upload_id']);
$this->addMultipartData($data, 'filename', $metadata['filename']);
$this->addMultipartData($data, 'mimetype', $metadata['mimetype']);
$this->addMultipartData($data, 'size', $metadata['filesize']);
$this->addMultipartData($data, 'store_location', $metadata['location']);
array_push($data, ['name' => 'files',
'contents' => '',
'filename' => $metadata['filename']]);
$this->multipartApplySecurity($data, $security);
$url = FilestackConfig::UPLOAD_URL . '/multipart/complete';
$response = $this->sendRequest('POST', $url, ['multipart' => $data]);
$json = $this->handleResponseCreateFilelink($response);
return $json;
}
/**
* Handle a Filestack response and create a filelink object
*
* @param Http\Message\Response $response response object
*
* @throws FilestackException if statuscode is not OK
*
* @return Filestack\Filelink
*/
protected function handleResponseCreateFilelink($response)
{
$status_code = $response->getStatusCode();
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$json_response = json_decode($response->getBody(), true);
$url = $json_response['url'];
$file_handle = substr($url, strrpos($url, '/') + 1);
$filelink = new Filelink($file_handle, $this->api_key, $this->security);
$filelink->metadata['filename'] = $json_response['filename'];
$filelink->metadata['size'] = $json_response['size'];
$filelink->metadata['mimetype'] = 'unknown';
if (isset($json_response['type'])) {
$filelink->metadata['mimetype'] = $json_response['type'];
} elseif (isset($json_response['mimetype'])) {
$filelink->metadata['mimetype'] = $json_response['mimetype'];
}
return $filelink;
}
protected function handleResponseDecodeJson($response)
{
$status_code = $response->getStatusCode();
if ($status_code !== 200) {
throw new FilestackException($response->getBody(), $status_code);
}
$json_response = json_decode($response->getBody(), true);
return $json_response;
}
/**
* Send request
*
* @param string $url url to post to
* @param array $params optional params to send
* @param array $headers optional headers to send
*/
protected function sendRequest($method, $url, $data = [], $headers = [])
{
$this->addRequestSourceHeader($headers);
$data['http_errors'] = false;
$data['headers'] = $headers;
$response = $this->http_client->request($method, $url, $data);
return $response;
}
/**
* Get source header
*/
protected function getSourceHeaders()
{
$headers = [];
if (!$this->user_agent_header || !$this->source_header) {
$version = trim(file_get_contents(__DIR__ . '/../../VERSION'));
if (!$this->user_agent_header) {
$this->user_agent_header = sprintf('filestack-php-%s',
$version);
}
if (!$this->source_header) {
$this->source_header = sprintf('PHP-%s',
$version);
}
}
$headers['user-agent'] = $this->user_agent_header;
$headers['filestack-source'] = $this->source_header;
//user_agent_header
return $headers;
}
/**
* Get a chunk from a file given starting seek point.
*/
protected function multipartGetChunk($filepath, $seek_point) {
$handle = fopen($filepath, 'r');
fseek($handle, $seek_point);
$chunk = fread($handle, FilestackConfig::UPLOAD_CHUNK_SIZE);
fclose($handle);
$handle = null;
return $chunk;
}
/**
* Append source header to request headers array
*/
protected function addRequestSourceHeader(&$headers)
{
$source_headers = $this->getSourceHeaders();
$headers['User-Agent'] = $source_headers['user-agent'];
$headers['Filestack-Source'] = $source_headers['filestack-source'];
}
/**
* Add a multipart data item
*/
protected function addMultipartData(&$data, $name, $value)
{
array_push($data, ['name' => $name, 'contents' => $value]);
}
/**
* Create data multipart data for multipart upload api request
*/
protected function buildMultipartJobData($job)
{
$data = [];
$this->addMultipartData($data, 'apikey', $job['api_key']);
$this->addMultipartData($data, 'md5', $job['md5']);
$this->addMultipartData($data, 'size', $job['chunksize']);
$this->addMultipartData($data, 'region', $job['region']);
$this->addMultipartData($data, 'upload_id', $job['upload_id']);
$this->addMultipartData($data, 'uri', $job['uri']);
$this->addMultipartData($data, 'part', $job['part_num']);
$this->addMultipartData($data, 'store_location', $job['location']);
array_push($data, [
'name' => 'files',
'contents' => '',
'filename' => $job['filename']]);
return $data;
}
/**
* append promises for multipart async concurrent calls
*/
protected function appendUploadPromises($jobs, $start_index, $end_index,
&$upload_promises, &$jobs_completed, $security)
{
$num_jobs = count($jobs);
$headers = [];
$this->addRequestSourceHeader($headers);
// loop from current start of concurrent jobs to end of of concurrent jobs
for ($i=$start_index; $i <= $end_index; $i++) {
if ($i > $num_jobs) {
break;
}
$job = $jobs[$i];
$data = $this->buildMultipartJobData($job);
$this->multipartApplySecurity($data, $security);
// build promises to execute concurrent POST requests to upload api
$upload_promises[] = $this->http_client->requestAsync('POST',
FilestackConfig::UPLOAD_URL . '/multipart/upload', [
'headers' => $headers,
'multipart' => $data
]);
$jobs_completed++;
}
}
/**
* append promises for multipart async concurrent calls
*/
protected function appendS3Promises($jobs, $api_results, &$s3_promises)
{
foreach ($api_results as $result) {
if (isset($result['value'])) {
$json = json_decode($result['value']->getBody(), true);
$query = parse_url($json['url'], PHP_URL_QUERY);
parse_str($query, $params);
$part_num = 1;
if (array_key_exists('partNumber', $params)) {
$part_num = intval($params['partNumber']);
}
$headers = $json['headers'];
$seek_point = $jobs[$part_num]['seek_point'];
$filepath = $jobs[$part_num]['filepath'];
$chunk = $this->multipartGetChunk($filepath, $seek_point);
// build promises to execute concurrent PUT requests to s3
$s3_promises[$part_num] = $this->http_client->requestAsync('PUT',
$json['url'],
[
'body' => $chunk,
'headers' => $headers
]
);
}
}
}
/**
* apppend security policy and signature to request data if security is on
*/
protected function multipartApplySecurity(&$data, $security)
{
if ($security) {
$this->addMultipartData($data, 'policy', $security->policy);
$this->addMultipartData($data, 'signature', $security->signature);
}
}
/**
* Parse results of s3 calls and append to parts_etags array
*/
protected function multipartGetTags($s3_results, &$parts_etags)
{
foreach ($s3_results as $part_num => $result) {
try {
$etag = $result['value']->getHeader('ETag')[0];
$part_etag = sprintf('%s:%s', $part_num, $etag);
array_push($parts_etags, $part_etag);
} catch (\Exception $e) {
throw new FilestackException(
"Error encountered getting eTags: " . $e->getMessage(),
500);
}
}
}
}