RangeDownload.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Qcloud\Cos;
  3. use GuzzleHttp\Pool;
  4. class RangeDownload {
  5. const DEFAULT_PART_SIZE = 52428800;
  6. private $client;
  7. private $options;
  8. private $partSize;
  9. private $parts;
  10. private $progress;
  11. private $totalSize;
  12. private $resumableJson;
  13. public function __construct( $client, $contentLength, $saveAs, $options = array() ) {
  14. $this->client = $client;
  15. $this->options = $options;
  16. $this->partSize = isset( $options['PartSize'] ) ? $options['PartSize'] : self::DEFAULT_PART_SIZE;
  17. $this->concurrency = isset( $options['Concurrency'] ) ? $options['Concurrency'] : 10;
  18. $this->progress = isset( $options['Progress'] ) ? $options['Progress'] : function( $totalSize, $downloadedSize ) {
  19. }
  20. ;
  21. $this->parts = [];
  22. $this->partNumberList = [];
  23. $this->downloadedSize = 0;
  24. $this->totalSize = $contentLength;
  25. $this->saveAs = $saveAs;
  26. $this->resumableJson = [];
  27. $this->resumableJson = isset( $options['ResumableJson'] ) ? $options['ResumableJson'] : [];
  28. unset( $options['ResumableJson'] );
  29. $this->resumableTaskFile = isset( $options['ResumableTaskFile'] ) ? $options['ResumableTaskFile'] : $saveAs . '.cosresumabletask';
  30. $this->resumableDownload = isset( $options['ResumableDownload'] ) ? $options['ResumableDownload'] : false;
  31. }
  32. public function performdownloading() {
  33. if ( $this->resumableDownload ) {
  34. try {
  35. if ( file_exists( $this->resumableTaskFile ) ) {
  36. $origin_content = file_get_contents( $this->resumableTaskFile );
  37. $this->resumableJsonLocal = json_decode( $origin_content, true );
  38. if ( $this->resumableJsonLocal == null ) {
  39. $this->resumableJsonLocal = [];
  40. } else if ( $this->resumableJsonLocal['LastModified'] != $this->resumableJson['LastModified'] ||
  41. $this->resumableJsonLocal['ContentLength'] != $this->resumableJson['ContentLength'] ||
  42. $this->resumableJsonLocal['ETag'] != $this->resumableJson['ETag'] ||
  43. $this->resumableJsonLocal['Crc64ecma'] != $this->resumableJson['Crc64ecma'] ) {
  44. $this->resumableDownload = false;
  45. }
  46. }
  47. } catch ( \Exception $e ) {
  48. $this->resumableDownload = false;
  49. }
  50. }
  51. try {
  52. if ($this->resumableDownload) {
  53. $this->fp = fopen( $this->saveAs, 'r+' );
  54. } else {
  55. $this->fp = fopen( $this->saveAs, 'wb' );
  56. }
  57. $rt = $this->donwloadParts();
  58. $this->resumableJson['DownloadedBlocks'] = [];
  59. if (file_exists( $this->resumableTaskFile )) {
  60. unlink($this->resumableTaskFile);
  61. }
  62. } catch ( \Exception $e ) {
  63. $this->fp_resume = fopen( $this->resumableTaskFile, 'wb' );
  64. fwrite( $this->fp_resume, json_encode( $this->resumableJson ) );
  65. fclose( $this->fp_resume );
  66. throw ( $e );
  67. }
  68. finally {
  69. fclose( $this->fp );
  70. }
  71. return $rt;
  72. }
  73. public function donwloadParts() {
  74. $uploadRequests = function () {
  75. $index = 1;
  76. $partSize = 0;
  77. for ( $offset = 0; $offset < $this->totalSize; ) {
  78. $partSize = $this->partSize;
  79. if ( $offset + $this->partSize >= $this->totalSize ) {
  80. $partSize = $this->totalSize - $offset;
  81. }
  82. $this->parts[$index]['PartSize'] = $partSize;
  83. $this->parts[$index]['Offset'] = $offset;
  84. $begin = $offset;
  85. $end = $offset + $partSize - 1;
  86. if ( !( $this->resumableDownload &&
  87. isset( $this->resumableJsonLocal['DownloadedBlocks'] ) &&
  88. in_array( ['from' => $begin, 'to' => $end], $this->resumableJsonLocal['DownloadedBlocks'] ) ) ) {
  89. $params = array(
  90. 'Bucket' => $this->options['Bucket'],
  91. 'Key' => $this->options['Key'],
  92. 'Range' => sprintf( 'bytes=%d-%d', $begin, $end )
  93. );
  94. $command = $this->client->getCommand( 'getObject', $params );
  95. $request = $this->client->commandToRequestTransformer( $command );
  96. $index += 1;
  97. yield $request;
  98. } else {
  99. $this->resumableJson['DownloadedBlocks'][] = ['from' => $begin, 'to' => $end];
  100. $this->downloadedSize += $partSize;
  101. call_user_func_array( $this->progress, [$this->totalSize, $this->downloadedSize] );
  102. }
  103. $offset += $partSize;
  104. }
  105. }
  106. ;
  107. $pool = new Pool( $this->client->httpClient, $uploadRequests(), [
  108. 'concurrency' => $this->concurrency,
  109. 'fulfilled' => function ( $response, $index ) {
  110. $index = $index + 1;
  111. $stream = $response->getBody();
  112. $offset = $this->parts[$index]['Offset'];
  113. $partsize = 8192;
  114. $begin = $offset;
  115. fseek( $this->fp, $offset );
  116. while ( !$stream->eof() ) {
  117. $output = $stream->read( $partsize );
  118. $writeLen = fwrite( $this->fp, $output );
  119. $offset += $writeLen;
  120. }
  121. $end = $offset - 1;
  122. $this->resumableJson['DownloadedBlocks'][] = ['from' => $begin, 'to' => $end];
  123. $partSize = $this->parts[$index]['PartSize'];
  124. $this->downloadedSize += $partSize;
  125. call_user_func_array( $this->progress, [$this->totalSize, $this->downloadedSize] );
  126. }
  127. ,
  128. 'rejected' => function ( $reason, $index ) {
  129. throw( $reason );
  130. }
  131. ] );
  132. $promise = $pool->promise();
  133. $promise->wait();
  134. }
  135. }