PluginProbe
Media Cloud Sync / 1.2.12
Media Cloud Sync v1.2.12
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.2.12, at includes/sdk/s3/Aws/HashingStream.php

51 lines 1.5 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 HashInterface */
14 private $hash;
15 /** @var callable|null */
16 private $callback;
17 /**
18 * @param StreamInterface $stream Stream that is being read.
19 * @param HashInterface $hash Hash used to calculate checksum.
20 * @param callable $onComplete Optional function invoked when the
21 * hash calculation is completed.
22 */
23 public function __construct(StreamInterface $stream, HashInterface $hash, callable $onComplete = null)
24 {
25 $this->stream = $stream;
26 $this->hash = $hash;
27 $this->callback = $onComplete;
28 }
29 public function read($length)
30 {
31 $data = $this->stream->read($length);
32 $this->hash->update($data);
33 if ($this->eof()) {
34 $result = $this->hash->complete();
35 if ($this->callback) {
36 \call_user_func($this->callback, $result);
37 }
38 }
39 return $data;
40 }
41 public function seek($offset, $whence = \SEEK_SET)
42 {
43 if ($offset === 0) {
44 $this->hash->reset();
45 return $this->stream->seek($offset);
46 }
47 // Seeking arbitrarily is not supported.
48 return \false;
49 }
50 }
51