GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 72a7c8...f0e9fd )
by Freek
23s queued 10s
created

Client::getThumbnail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Spatie\Dropbox;
4
5
use Exception;
6
use GuzzleHttp\Psr7;
7
use GuzzleHttp\Psr7\PumpStream;
8
use GuzzleHttp\Psr7\StreamWrapper;
9
use Psr\Http\Message\StreamInterface;
10
use GuzzleHttp\Client as GuzzleClient;
11
use Psr\Http\Message\ResponseInterface;
12
use GuzzleHttp\Exception\ClientException;
13
use Spatie\Dropbox\Exceptions\BadRequest;
14
use GuzzleHttp\Exception\RequestException;
15
use GrahamCampbell\GuzzleFactory\GuzzleFactory;
16
17
class Client
18
{
19
    const THUMBNAIL_FORMAT_JPEG = 'jpeg';
20
    const THUMBNAIL_FORMAT_PNG = 'png';
21
22
    const THUMBNAIL_SIZE_XS = 'w32h32';
23
    const THUMBNAIL_SIZE_S = 'w64h64';
24
    const THUMBNAIL_SIZE_M = 'w128h128';
25
    const THUMBNAIL_SIZE_L = 'w640h480';
26
    const THUMBNAIL_SIZE_XL = 'w1024h768';
27
28
    const MAX_CHUNK_SIZE = 1024 * 1024 * 150;
29
30
    const UPLOAD_SESSION_START = 0;
31
    const UPLOAD_SESSION_APPEND = 1;
32
33
    /** @var string */
34
    protected $accessToken;
35
36
    /** @var \GuzzleHttp\Client */
37
    protected $client;
38
39
    /** @var int */
40
    protected $maxChunkSize;
41
42
    /** @var int */
43
    protected $maxUploadChunkRetries;
44
45
    /**
46
     * @param string $accessToken
47
     * @param GuzzleClient|null $client
48
     * @param int $maxChunkSize Set max chunk size per request (determines when to switch from "one shot upload" to upload session and defines chunk size for uploads via session).
49
     * @param int $maxUploadChunkRetries How many times to retry an upload session start or append after RequestException.
50
     */
51
    public function __construct(string $accessToken, GuzzleClient $client = null, int $maxChunkSize = self::MAX_CHUNK_SIZE, int $maxUploadChunkRetries = 0)
52
    {
53
        $this->accessToken = $accessToken;
54
55
        $this->client = $client ?? new GuzzleClient(['handler' => GuzzleFactory::handler()]);
56
57
        $this->maxChunkSize = ($maxChunkSize < self::MAX_CHUNK_SIZE ? ($maxChunkSize > 1 ? $maxChunkSize : 1) : self::MAX_CHUNK_SIZE);
58
        $this->maxUploadChunkRetries = $maxUploadChunkRetries;
59
    }
60
61
    /**
62
     * Copy a file or folder to a different location in the user's Dropbox.
63
     *
64
     * If the source path is a folder all its contents will be copied.
65
     *
66
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2
67
     */
68
    public function copy(string $fromPath, string $toPath): array
69
    {
70
        $parameters = [
71
            'from_path' => $this->normalizePath($fromPath),
72
            'to_path' => $this->normalizePath($toPath),
73
        ];
74
75
        return $this->rpcEndpointRequest('files/copy_v2', $parameters);
76
    }
77
78
    /**
79
     * Create a folder at a given path.
80
     *
81
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder
82
     */
83
    public function createFolder(string $path): array
84
    {
85
        $parameters = [
86
            'path' => $this->normalizePath($path),
87
        ];
88
89
        $object = $this->rpcEndpointRequest('files/create_folder', $parameters);
90
91
        $object['.tag'] = 'folder';
92
93
        return $object;
94
    }
95
96
    /**
97
     * Create a shared link with custom settings.
98
     *
99
     * If no settings are given then the default visibility is RequestedVisibility.public.
100
     * The resolved visibility, though, may depend on other aspects such as team and
101
     * shared folder settings). Only for paid users.
102
     *
103
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-create_shared_link_with_settings
104
     */
105
    public function createSharedLinkWithSettings(string $path, array $settings = [])
106
    {
107
        $parameters = [
108
            'path' => $this->normalizePath($path),
109
        ];
110
111
        if (count($settings)) {
112
            $parameters = array_merge(compact('settings'), $parameters);
113
        }
114
115
        return $this->rpcEndpointRequest('sharing/create_shared_link_with_settings', $parameters);
116
    }
117
118
    /**
119
     * List shared links.
120
     *
121
     * For empty path returns a list of all shared links. For non-empty path
122
     * returns a list of all shared links with access to the given path.
123
     *
124
     * If direct_only is set true, only direct links to the path will be returned, otherwise
125
     * it may return link to the path itself and parent folders as described on docs.
126
     *
127
     * @link https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
128
     */
129
    public function listSharedLinks(string $path = null, bool $direct_only = false, string $cursor = null): array
130
    {
131
        $parameters = [
132
            'path' => $path ? $this->normalizePath($path) : null,
133
            'cursor' => $cursor,
134
            'direct_only' => $direct_only,
135
        ];
136
137
        $body = $this->rpcEndpointRequest('sharing/list_shared_links', $parameters);
138
139
        return $body['links'];
140
    }
141
142
    /**
143
     * Delete the file or folder at a given path.
144
     *
145
     * If the path is a folder, all its contents will be deleted too.
146
     * A successful response indicates that the file or folder was deleted.
147
     *
148
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-delete
149
     */
150
    public function delete(string $path): array
151
    {
152
        $parameters = [
153
            'path' => $this->normalizePath($path),
154
        ];
155
156
        return $this->rpcEndpointRequest('files/delete', $parameters);
157
    }
158
159
    /**
160
     * Download a file from a user's Dropbox.
161
     *
162
     * @param string $path
163
     *
164
     * @return resource
165
     *
166
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-download
167
     */
168
    public function download(string $path)
169
    {
170
        $arguments = [
171
            'path' => $this->normalizePath($path),
172
        ];
173
174
        $response = $this->contentEndpointRequest('files/download', $arguments);
175
176
        return StreamWrapper::getResource($response->getBody());
177
    }
178
179
    /**
180
     * Returns the metadata for a file or folder.
181
     *
182
     * Note: Metadata for the root folder is unsupported.
183
     *
184
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata
185
     */
186
    public function getMetadata(string $path): array
187
    {
188
        $parameters = [
189
            'path' => $this->normalizePath($path),
190
        ];
191
192
        return $this->rpcEndpointRequest('files/get_metadata', $parameters);
193
    }
194
195
    /**
196
     * Get a temporary link to stream content of a file.
197
     *
198
     * This link will expire in four hours and afterwards you will get 410 Gone.
199
     * Content-Type of the link is determined automatically by the file's mime type.
200
     *
201
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link
202
     */
203
    public function getTemporaryLink(string $path): string
204
    {
205
        $parameters = [
206
            'path' => $this->normalizePath($path),
207
        ];
208
209
        $body = $this->rpcEndpointRequest('files/get_temporary_link', $parameters);
210
211
        return $body['link'];
212
    }
213
214
    /**
215
     * Get a thumbnail for an image.
216
     *
217
     * This method currently supports files with the following file extensions:
218
     * jpg, jpeg, png, tiff, tif, gif and bmp.
219
     *
220
     * Photos that are larger than 20MB in size won't be converted to a thumbnail.
221
     *
222
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail
223
     */
224
    public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64'): string
225
    {
226
        $arguments = [
227
            'path' => $this->normalizePath($path),
228
            'format' => $format,
229
            'size' => $size,
230
        ];
231
232
        $response = $this->contentEndpointRequest('files/get_thumbnail', $arguments);
233
234
        return (string) $response->getBody();
235
    }
236
237
    /**
238
     * Starts returning the contents of a folder.
239
     *
240
     * If the result's ListFolderResult.has_more field is true, call
241
     * list_folder/continue with the returned ListFolderResult.cursor to retrieve more entries.
242
     *
243
     * Note: auth.RateLimitError may be returned if multiple list_folder or list_folder/continue calls
244
     * with same parameters are made simultaneously by same API app for same user. If your app implements
245
     * retry logic, please hold off the retry until the previous request finishes.
246
     *
247
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder
248
     */
249
    public function listFolder(string $path = '', bool $recursive = false): array
250
    {
251
        $parameters = [
252
            'path' => $this->normalizePath($path),
253
            'recursive' => $recursive,
254
        ];
255
256
        return $this->rpcEndpointRequest('files/list_folder', $parameters);
257
    }
258
259
    /**
260
     * Once a cursor has been retrieved from list_folder, use this to paginate through all files and
261
     * retrieve updates to the folder, following the same rules as documented for list_folder.
262
     *
263
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-list_folder-continue
264
     */
265
    public function listFolderContinue(string $cursor = ''): array
266
    {
267
        return $this->rpcEndpointRequest('files/list_folder/continue', compact('cursor'));
268
    }
269
270
    /**
271
     * Move a file or folder to a different location in the user's Dropbox.
272
     *
273
     * If the source path is a folder all its contents will be moved.
274
     *
275
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2
276
     */
277
    public function move(string $fromPath, string $toPath): array
278
    {
279
        $parameters = [
280
            'from_path' => $this->normalizePath($fromPath),
281
            'to_path' => $this->normalizePath($toPath),
282
        ];
283
284
        return $this->rpcEndpointRequest('files/move_v2', $parameters);
285
    }
286
287
    /**
288
     * The file should be uploaded in chunks if it size exceeds the 150 MB threshold
289
     * or if the resource size could not be determined (eg. a popen() stream).
290
     *
291
     * @param string|resource $contents
292
     *
293
     * @return bool
294
     */
295
    protected function shouldUploadChunked($contents): bool
296
    {
297
        $size = is_string($contents) ? strlen($contents) : fstat($contents)['size'];
298
299
        if ($this->isPipe($contents)) {
300
            return true;
301
        }
302
303
        if ($size === null) {
304
            return true;
305
        }
306
307
        return $size > $this->maxChunkSize;
308
    }
309
310
    /**
311
     * Check if the contents is a pipe stream (not seekable, no size defined).
312
     *
313
     * @param string|resource $contents
314
     *
315
     * @return bool
316
     */
317
    protected function isPipe($contents): bool
318
    {
319
        return is_resource($contents) ? (fstat($contents)['mode'] & 010000) != 0 : false;
320
    }
321
322
    /**
323
     * Create a new file with the contents provided in the request.
324
     *
325
     * Do not use this to upload a file larger than 150 MB. Instead, create an upload session with upload_session/start.
326
     *
327
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload
328
     *
329
     * @param string $path
330
     * @param string|resource $contents
331
     * @param string $mode
332
     *
333
     * @return array
334
     */
335
    public function upload(string $path, $contents, $mode = 'add'): array
336
    {
337
        if ($this->shouldUploadChunked($contents)) {
338
            return $this->uploadChunked($path, $contents, $mode);
339
        }
340
341
        $arguments = [
342
            'path' => $this->normalizePath($path),
343
            'mode' => $mode,
344
        ];
345
346
        $response = $this->contentEndpointRequest('files/upload', $arguments, $contents);
347
348
        $metadata = json_decode($response->getBody(), true);
349
350
        $metadata['.tag'] = 'file';
351
352
        return $metadata;
353
    }
354
355
    /**
356
     * Upload file split in chunks. This allows uploading large files, since
357
     * Dropbox API v2 limits the content size to 150MB.
358
     *
359
     * The chunk size will affect directly the memory usage, so be careful.
360
     * Large chunks tends to speed up the upload, while smaller optimizes memory usage.
361
     *
362
     * @param string $path
363
     * @param string|resource $contents
364
     * @param string $mode
365
     * @param int $chunkSize
366
     *
367
     * @return array
368
     */
369
    public function uploadChunked(string $path, $contents, $mode = 'add', $chunkSize = null): array
370
    {
371
        if ($chunkSize === null || $chunkSize > $this->maxChunkSize) {
372
            $chunkSize = $this->maxChunkSize;
373
        }
374
375
        $stream = $this->getStream($contents);
376
377
        $cursor = $this->uploadChunk(self::UPLOAD_SESSION_START, $stream, $chunkSize, null);
0 ignored issues
show
Bug introduced by
It seems like $stream defined by $this->getStream($contents) on line 375 can also be of type object<GuzzleHttp\Psr7\PumpStream>; however, Spatie\Dropbox\Client::uploadChunk() does only seem to accept object<GuzzleHttp\Psr7\Stream>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
378
379
        while (! $stream->eof()) {
380
            $cursor = $this->uploadChunk(self::UPLOAD_SESSION_APPEND, $stream, $chunkSize, $cursor);
381
        }
382
383
        return $this->uploadSessionFinish('', $cursor, $path, $mode);
384
    }
385
386
    /**
387
     * @param int $type
388
     * @param Psr7\Stream $stream
389
     * @param int $chunkSize
390
     * @param \Spatie\Dropbox\UploadSessionCursor|null $cursor
391
     * @return \Spatie\Dropbox\UploadSessionCursor
392
     * @throws Exception
393
     */
394
    protected function uploadChunk($type, &$stream, $chunkSize, $cursor = null): UploadSessionCursor
395
    {
396
        $maximumTries = $stream->isSeekable() ? $this->maxUploadChunkRetries : 0;
397
        $pos = $stream->tell();
398
399
        $tries = 0;
400
401
        tryUpload:
402
        try {
403
            $tries++;
404
405
            $chunkStream = new Psr7\LimitStream($stream, $chunkSize, $stream->tell());
406
407
            if ($type === self::UPLOAD_SESSION_START) {
408
                return $this->uploadSessionStart($chunkStream);
409
            }
410
411
            if ($type === self::UPLOAD_SESSION_APPEND && $cursor !== null) {
412
                return $this->uploadSessionAppend($chunkStream, $cursor);
413
            }
414
415
            throw new Exception('Invalid type');
416
        } catch (RequestException $exception) {
417
            if ($tries < $maximumTries) {
418
                // rewind
419
                $stream->seek($pos, SEEK_SET);
420
                goto tryUpload;
421
            }
422
            throw $exception;
423
        }
424
    }
425
426
    /**
427
     * Upload sessions allow you to upload a single file in one or more requests,
428
     * for example where the size of the file is greater than 150 MB.
429
     * This call starts a new upload session with the given data.
430
     *
431
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-start
432
     *
433
     * @param string|StreamInterface $contents
434
     * @param bool $close
435
     *
436
     * @return UploadSessionCursor
437
     */
438
    public function uploadSessionStart($contents, bool $close = false): UploadSessionCursor
439
    {
440
        $arguments = compact('close');
441
442
        $response = json_decode(
443
            $this->contentEndpointRequest('files/upload_session/start', $arguments, $contents)->getBody(),
444
            true
445
        );
446
447
        return new UploadSessionCursor($response['session_id'], ($contents instanceof StreamInterface ? $contents->tell() : strlen($contents)));
448
    }
449
450
    /**
451
     * Append more data to an upload session.
452
     * When the parameter close is set, this call will close the session.
453
     * A single request should not upload more than 150 MB.
454
     *
455
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-append_v2
456
     *
457
     * @param string|StreamInterface $contents
458
     * @param UploadSessionCursor $cursor
459
     * @param bool $close
460
     *
461
     * @return \Spatie\Dropbox\UploadSessionCursor
462
     */
463
    public function uploadSessionAppend($contents, UploadSessionCursor $cursor, bool $close = false): UploadSessionCursor
464
    {
465
        $arguments = compact('cursor', 'close');
466
467
        $pos = $contents instanceof StreamInterface ? $contents->tell() : 0;
468
        $this->contentEndpointRequest('files/upload_session/append_v2', $arguments, $contents);
469
470
        $cursor->offset += $contents instanceof StreamInterface ? ($contents->tell() - $pos) : strlen($contents);
471
472
        return $cursor;
473
    }
474
475
    /**
476
     * Finish an upload session and save the uploaded data to the given file path.
477
     * A single request should not upload more than 150 MB.
478
     *
479
     * @link https://www.dropbox.com/developers/documentation/http/documentation#files-upload_session-finish
480
     *
481
     * @param string|StreamInterface $contents
482
     * @param \Spatie\Dropbox\UploadSessionCursor $cursor
483
     * @param string $path
484
     * @param string|array $mode
485
     * @param bool $autorename
486
     * @param bool $mute
487
     *
488
     * @return array
489
     */
490
    public function uploadSessionFinish($contents, UploadSessionCursor $cursor, string $path, $mode = 'add', $autorename = false, $mute = false): array
491
    {
492
        $arguments = compact('cursor');
493
        $arguments['commit'] = compact('path', 'mode', 'autorename', 'mute');
494
495
        $response = $this->contentEndpointRequest(
496
            'files/upload_session/finish',
497
            $arguments,
498
            ($contents == '') ? null : $contents
499
        );
500
501
        $metadata = json_decode($response->getBody(), true);
502
503
        $metadata['.tag'] = 'file';
504
505
        return $metadata;
506
    }
507
508
    /**
509
     * Get Account Info for current authenticated user.
510
     *
511
     * @link https://www.dropbox.com/developers/documentation/http/documentation#users-get_current_account
512
     *
513
     * @return array
514
     */
515
    public function getAccountInfo(): array
516
    {
517
        return $this->rpcEndpointRequest('users/get_current_account');
518
    }
519
520
    /**
521
     * Revoke current access token.
522
     *
523
     * @link https://www.dropbox.com/developers/documentation/http/documentation#auth-token-revoke
524
     */
525
    public function revokeToken()
526
    {
527
        $this->rpcEndpointRequest('auth/token/revoke');
528
    }
529
530
    protected function normalizePath(string $path): string
531
    {
532
        if (preg_match("/^id:.*|^rev:.*|^(ns:[0-9]+(\/.*)?)/", $path) === 1) {
533
            return $path;
534
        }
535
536
        $path = trim($path, '/');
537
538
        return ($path === '') ? '' : '/'.$path;
539
    }
540
541
    protected function getEndpointUrl(string $subdomain, string $endpoint): string
542
    {
543
        if (count($parts = explode('::', $endpoint)) === 2) {
544
            [$subdomain, $endpoint] = $parts;
545
        }
546
547
        return "https://{$subdomain}.dropboxapi.com/2/{$endpoint}";
548
    }
549
550
    /**
551
     * @param string $endpoint
552
     * @param array $arguments
553
     * @param string|resource|StreamInterface $body
554
     *
555
     * @return \Psr\Http\Message\ResponseInterface
556
     *
557
     * @throws \Exception
558
     */
559
    public function contentEndpointRequest(string $endpoint, array $arguments, $body = ''): ResponseInterface
560
    {
561
        $headers = ['Dropbox-API-Arg' => json_encode($arguments)];
562
563
        if ($body !== '') {
564
            $headers['Content-Type'] = 'application/octet-stream';
565
        }
566
567
        try {
568
            $response = $this->client->post($this->getEndpointUrl('content', $endpoint), [
569
                'headers' => $this->getHeaders($headers),
570
                'body' => $body,
571
            ]);
572
        } catch (ClientException $exception) {
573
            throw $this->determineException($exception);
574
        }
575
576
        return $response;
577
    }
578
579
    public function rpcEndpointRequest(string $endpoint, array $parameters = null): array
580
    {
581
        try {
582
            $options = ['headers' => $this->getHeaders()];
583
584
            if ($parameters) {
585
                $options['json'] = $parameters;
586
            }
587
588
            $response = $this->client->post($this->getEndpointUrl('api', $endpoint), $options);
589
        } catch (ClientException $exception) {
590
            throw $this->determineException($exception);
591
        }
592
593
        $response = json_decode($response->getBody(), true);
594
595
        return $response ?? [];
596
    }
597
598
    protected function determineException(ClientException $exception): Exception
599
    {
600
        if (in_array($exception->getResponse()->getStatusCode(), [400, 409])) {
601
            return new BadRequest($exception->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
602
        }
603
604
        return $exception;
605
    }
606
607
    /**
608
     * @param $contents
609
     *
610
     * @return \GuzzleHttp\Psr7\PumpStream|\GuzzleHttp\Psr7\Stream
611
     */
612
    protected function getStream($contents)
613
    {
614
        if ($this->isPipe($contents)) {
615
            /* @var resource $contents */
616
            return new PumpStream(function ($length) use ($contents) {
617
                $data = fread($contents, $length);
618
                if (strlen($data) === 0) {
619
                    return false;
620
                }
621
622
                return $data;
623
            });
624
        }
625
626
        return Psr7\stream_for($contents);
627
    }
628
629
    /**
630
     * Get the access token.
631
     */
632
    public function getAccessToken(): string
633
    {
634
        return $this->accessToken;
635
    }
636
637
    /**
638
     * Set the access token.
639
     */
640
    public function setAccessToken(string $accessToken): self
641
    {
642
        $this->accessToken = $accessToken;
643
644
        return $this;
645
    }
646
647
    /**
648
     * Get the HTTP headers.
649
     */
650
    protected function getHeaders(array $headers = []): array
651
    {
652
        return array_merge([
653
            'Authorization' => "Bearer {$this->accessToken}",
654
        ], $headers);
655
    }
656
}
657