VerifyStream.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace AlibabaCloud\Tea\OSSUtils;
  3. use GuzzleHttp\Psr7\Stream;
  4. class VerifyStream extends Stream
  5. {
  6. /**
  7. * @var Crc64
  8. */
  9. private $crcRead;
  10. private $content;
  11. private $res = [];
  12. public function __construct($stream, &$res = [])
  13. {
  14. $this->crcRead = new Crc64();
  15. $this->res = &$res;
  16. if ($stream instanceof Stream) {
  17. $stream->rewind();
  18. $stream = fopen('data://text/plain;base64,' . base64_encode($stream->getContents()), 'r');
  19. }
  20. parent::__construct($stream, []);
  21. }
  22. public function read($length)
  23. {
  24. $string = parent::read($length);
  25. if (!empty($string)) {
  26. $this->crcRead->append($string);
  27. $this->content .= $string;
  28. }
  29. return $string;
  30. }
  31. public function getVerify()
  32. {
  33. $this->res = [
  34. 'md5' => base64_encode(md5($this->content, true)),
  35. 'crc' => $this->crcRead->getValue(),
  36. ];
  37. return $this->res;
  38. }
  39. }