From 2ff9397afc1ce1057d93791e5abb8472eb733e1c Mon Sep 17 00:00:00 2001 From: Alexander Pankratov Date: Mon, 20 Jan 2020 23:14:16 +0300 Subject: [PATCH] downloadToResponse function --- src/MadelineProtoExtensions/ApiExtensions.php | 69 +++++++++++-------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/MadelineProtoExtensions/ApiExtensions.php b/src/MadelineProtoExtensions/ApiExtensions.php index 2204069..1a15b56 100644 --- a/src/MadelineProtoExtensions/ApiExtensions.php +++ b/src/MadelineProtoExtensions/ApiExtensions.php @@ -330,7 +330,7 @@ function() use ($data) { ); } - return $this->downloadToResponse($info); + return yield $this->downloadToResponse($info); } ); } @@ -394,7 +394,7 @@ function() use ($data) { $info = $infoFull; } - return $this->downloadToResponse($info); + return yield $this->downloadToResponse($info); } ); } @@ -425,40 +425,53 @@ function() use ($data) { ); } - public function downloadToResponse(array $info): array + /** + * Download to Amp HTTP response. + * + * @param array $info + * Any downloadable array: message, media etc... + * + * @return Promise + */ + public function downloadToResponse(array $info): Promise { - $range = $this->getByteRange($this->request->getHeader('Range')); + return call(function() use($info) { + if (empty($info['size'])) { + $info = yield $this->madelineProto->getDownloadInfo($info); + } + $range = $this->getByteRange($this->request->getHeader('Range')); - if ($range['end'] === -1) { - $range['end'] = $info['size'] - 1; - } else { - $range['end'] = min($range['end'], $info['size'] - 1); - } + if ($range['end'] === -1) { + $range['end'] = $info['size'] - 1; + } else { + $range['end'] = min($range['end'], $info['size'] - 1); + } - $stream = new IteratorStream(new Producer(function (callable $emit) use($info, $range) { - yield $this->madelineProto->downloadToCallable($info, static function($payload) use($emit) { - yield $emit($payload); - return strlen($payload); - }, null, false, $range['start'], $range['end'] + 1); - })); + $stream = new IteratorStream(new Producer(function (callable $emit) use($info, $range) { + yield $this->madelineProto->downloadToCallable($info, static function($payload) use($emit) { + yield $emit($payload); + return strlen($payload); + }, null, false, $range['start'], $range['end'] + 1); + })); - $headers = [ - 'Content-Type' => $info['mime'], + $headers = [ + 'Content-Type' => $info['mime'], // 'Accept-Ranges' => 'bytes', // 'Content-Transfer-Encoding'=> 'Binary', - ]; + ]; - if ($range['start'] > 0 || $range['end'] < $info['size'] - 1) { - $headers['Content-Length'] = ($range['end'] - $range['start'] + 1); - $headers['Content-Range'] = "bytes {$range['start']}-{$range['end']}/{$info['size']}"; - } else { - $headers['Content-Length'] = $info['size']; - } + if ($range['start'] > 0 || $range['end'] < $info['size'] - 1) { + $headers['Content-Length'] = ($range['end'] - $range['start'] + 1); + $headers['Content-Range'] = "bytes {$range['start']}-{$range['end']}/{$info['size']}"; + } else { + $headers['Content-Length'] = $info['size']; + } - return [ - 'headers' => $headers, - 'stream' => $stream, - ]; + return [ + 'headers' => $headers, + 'stream' => $stream, + ]; + }); } private function getByteRange(?string $header) {