diff --git a/composer.lock b/composer.lock index 0f8aa9d..669368d 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "amphp/amp", - "version": "v2.4.0", + "version": "v2.4.1", "source": { "type": "git", "url": "https://github.com/amphp/amp.git", - "reference": "13930a582947831bb66ff1aeac28672fd91c38ea" + "reference": "2ac3b550c4997f2ec304faa63c8b2885079a2dc4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/amphp/amp/zipball/13930a582947831bb66ff1aeac28672fd91c38ea", - "reference": "13930a582947831bb66ff1aeac28672fd91c38ea", + "url": "https://api.github.com/repos/amphp/amp/zipball/2ac3b550c4997f2ec304faa63c8b2885079a2dc4", + "reference": "2ac3b550c4997f2ec304faa63c8b2885079a2dc4", "shasum": "" }, "require": { @@ -81,7 +81,7 @@ "non-blocking", "promise" ], - "time": "2019-11-11T19:32:05+00:00" + "time": "2020-02-10T18:10:57+00:00" }, { "name": "amphp/byte-stream", @@ -1581,12 +1581,12 @@ "source": { "type": "git", "url": "https://github.com/danog/MadelineProto.git", - "reference": "c8635c12d6ad8fc20a6bc36786734b4933a44975" + "reference": "ebd4245d9dca70ce9fbddb46ae8f749b189bd871" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/danog/MadelineProto/zipball/c8635c12d6ad8fc20a6bc36786734b4933a44975", - "reference": "c8635c12d6ad8fc20a6bc36786734b4933a44975", + "url": "https://api.github.com/repos/danog/MadelineProto/zipball/ebd4245d9dca70ce9fbddb46ae8f749b189bd871", + "reference": "ebd4245d9dca70ce9fbddb46ae8f749b189bd871", "shasum": "" }, "require": { @@ -1673,7 +1673,7 @@ "telegram", "video" ], - "time": "2020-02-12T20:15:22+00:00" + "time": "2020-02-15T21:03:41+00:00" }, { "name": "danog/magicalserializer", @@ -2550,16 +2550,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.13.1", + "version": "v1.14.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3" + "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f8f0b461be3385e56d6de3dbb5a0df24c0c275e3", - "reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", + "reference": "fbdeaec0df06cf3d51c93de80c7eb76e271f5a38", "shasum": "" }, "require": { @@ -2571,7 +2571,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.13-dev" + "dev-master": "1.14-dev" } }, "autoload": { @@ -2604,7 +2604,7 @@ "polyfill", "portable" ], - "time": "2019-11-27T13:56:44+00:00" + "time": "2020-01-13T11:15:53+00:00" }, { "name": "vlucas/phpdotenv", diff --git a/src/Controllers/AbstractApiController.php b/src/Controllers/AbstractApiController.php index 7059fd4..23e068a 100644 --- a/src/Controllers/AbstractApiController.php +++ b/src/Controllers/AbstractApiController.php @@ -48,6 +48,9 @@ abstract class AbstractApiController $requestCallback = new static($client, $request, $extensionClass); $response = yield from $requestCallback->process(); + if ($response instanceof Response) { + return $response; + } return new Response( $requestCallback->page['code'], $requestCallback->page['headers'], @@ -142,6 +145,7 @@ abstract class AbstractApiController } catch (\Throwable $e) { error($e->getMessage(), [ + 'exception' => get_class($e), 'file' => $e->getFile(), 'line' => $e->getLine() ]); @@ -213,13 +217,13 @@ abstract class AbstractApiController */ private function getResponse() { + if ($this->page['response'] instanceof Response) { + return $this->page['response']; + } + if (!is_array($this->page['response'])) { $this->page['response'] = null; } - if (isset($this->page['response']['stream'])) { - $this->page['headers'] = $this->page['response']['headers']; - return $this->page['response']['stream']; - } $data = [ 'success' => $this->page['success'], diff --git a/src/MadelineProtoExtensions/ApiExtensions.php b/src/MadelineProtoExtensions/ApiExtensions.php index e3d0cf1..19b5e97 100644 --- a/src/MadelineProtoExtensions/ApiExtensions.php +++ b/src/MadelineProtoExtensions/ApiExtensions.php @@ -5,10 +5,8 @@ namespace TelegramApiServer\MadelineProtoExtensions; use Amp\ByteStream\InMemoryStream; -use Amp\ByteStream\IteratorStream; use Amp\Http\Server\FormParser\File; use Amp\Http\Server\Request; -use Amp\Producer; use Amp\Promise; use danog\MadelineProto; use danog\MadelineProto\TL\Conversion\BotAPI; @@ -440,43 +438,19 @@ class ApiExtensions */ public function downloadToResponse(array $info): Promise { - return call(function() use($info) { - if (empty($info['size'])) { - $info = yield $this->madelineProto->getDownloadInfo($info); - } - $range = $this->getByteRange($this->request->getHeader('Range')); + return $this->madelineProto->downloadToResponse($info, $this->request); + } - 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); - })); - - $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']; - } - - return [ - 'headers' => $headers, - 'stream' => $stream, - ]; - }); + /** + * Адаптер для стандартного метода + * + * @param array $info + * + * @return Promise + */ + public function downloadToBrowser(array $info): Promise + { + return $this->downloadToResponse($info); } /** @@ -517,19 +491,4 @@ class ApiExtensions $this->madelineProto->setEventHandler(EventHandler::class); } - private function getByteRange(?string $header): array - { - $matches = [ - 'start' => 0, - 'end' => -1 - ]; - if ($header) { - preg_match("~bytes=(?'start'\d+)-(?'end'\d*)~", $header, $matches); - } - return [ - 'start' => (int) $matches['start'], - 'end' => (int) $matches['end'] ?: -1 - ]; - } - } \ No newline at end of file