From df1e6b2ba8848fb78b5f406cd917dbd6a0ad697c Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 13 Aug 2023 17:23:33 +0200 Subject: [PATCH] Add downloadToReturnedStream method --- psalm-baseline.xml | 9 +++++++++ src/Db/DriverArray.php | 2 -- src/InternalDoc.php | 12 ++++++++++++ src/MTProtoTools/FilesLogic.php | 21 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 80e3ed2ec..7e1b16cc4 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -204,6 +204,12 @@ $cacheTtl + + + Traversable + Traversable + + $index @@ -244,6 +250,9 @@ $settings $settings + + DriverArray + $v $value diff --git a/src/Db/DriverArray.php b/src/Db/DriverArray.php index c1ce46070..de197b1fd 100644 --- a/src/Db/DriverArray.php +++ b/src/Db/DriverArray.php @@ -16,13 +16,11 @@ namespace danog\MadelineProto\Db; -use Closure; use danog\MadelineProto\Logger; use danog\MadelineProto\Magic; use danog\MadelineProto\Settings\Database\DriverDatabaseAbstract; use danog\MadelineProto\Settings\Database\SerializerType; use danog\MadelineProto\Settings\DatabaseAbstract; -use Exception; use IteratorAggregate; use function Amp\async; diff --git a/src/InternalDoc.php b/src/InternalDoc.php index 58aac98fb..9f4fd6e68 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -446,6 +446,18 @@ abstract class InternalDoc { return $this->wrapper->getAPI()->downloadToResponse($messageMedia, $request, $cb, $size, $mime, $name); } + /** + * Download file to an amphp stream, returning it. + * + * @param mixed $messageMedia File to download + * @param callable $cb Callback + * @param int $offset Offset where to start downloading + * @param int $end Offset where to end download + */ + public function downloadToReturnedStream(mixed $messageMedia, ?callable $cb = null, int $offset = 0, int $end = -1): \Amp\ByteStream\ReadableStream + { + return $this->wrapper->getAPI()->downloadToReturnedStream($messageMedia, $cb, $offset, $end); + } /** * Download file to stream. * diff --git a/src/MTProtoTools/FilesLogic.php b/src/MTProtoTools/FilesLogic.php index 3f44694fc..c8cac5315 100644 --- a/src/MTProtoTools/FilesLogic.php +++ b/src/MTProtoTools/FilesLogic.php @@ -124,6 +124,27 @@ trait FilesLogic $this->downloadToStream($messageMedia, \fopen('php://output', 'w'), $cb, ...$result->getServeRange()); } } + /** + * Download file to an amphp stream, returning it. + * + * @param mixed $messageMedia File to download + * @param callable $cb Callback + * @param int $offset Offset where to start downloading + * @param int $end Offset where to end download + */ + public function downloadToReturnedStream(mixed $messageMedia, ?callable $cb = null, int $offset = 0, int $end = -1): ReadableStream + { + $pipe = new Pipe(1024*1024); + $sink = $pipe->getSink(); + EventLoop::queue(function () use ($messageMedia, $sink, $cb, $offset, $end): void { + try { + $this->downloadToStream($messageMedia, $sink, $cb, $offset, $end); + } finally { + $sink->close(); + } + }); + return $pipe->getSource(); + } /** * Download file to stream. *