PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / HashingStream.php

HashingStream.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/Aws/HashingStream.php

53 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\StreamDecoratorTrait;
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
7 /**
8 * Stream decorator that calculates a rolling hash of the stream as it is read.
9 */
10 class HashingStream implements StreamInterface
11 {
12 use StreamDecoratorTrait;
13 /** @var StreamInterface */
14 private $stream;
15 /** @var HashInterface */
16 private $hash;
17 /** @var callable|null */
18 private $callback;
19 /**
20 * @param StreamInterface $stream Stream that is being read.
21 * @param HashInterface $hash Hash used to calculate checksum.
22 * @param callable $onComplete Optional function invoked when the
23 * hash calculation is completed.
24 */
25 public function __construct(StreamInterface $stream, HashInterface $hash, ?callable $onComplete = null)
26 {
27 $this->stream = $stream;
28 $this->hash = $hash;
29 $this->callback = $onComplete;
30 }
31 public function read($length) : string
32 {
33 $data = $this->stream->read($length);
34 $this->hash->update($data);
35 if ($this->eof()) {
36 $result = $this->hash->complete();
37 if ($this->callback) {
38 \call_user_func($this->callback, $result);
39 }
40 }
41 return $data;
42 }
43 public function seek($offset, $whence = \SEEK_SET) : void
44 {
45 // Seeking arbitrarily is not supported.
46 if ($offset !== 0) {
47 return;
48 }
49 $this->hash->reset();
50 $this->stream->seek($offset);
51 }
52 }
53