1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 09:14:39 +01:00

Add downloadToReturnedStream method

This commit is contained in:
Daniil Gentili 2023-08-13 17:23:33 +02:00
parent 7da7e5215a
commit df1e6b2ba8
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 42 additions and 2 deletions

View File

@ -204,6 +204,12 @@
<code>$cacheTtl</code> <code>$cacheTtl</code>
</PropertyNotSetInConstructor> </PropertyNotSetInConstructor>
</file> </file>
<file src="src/Db/CachedArray.php">
<LessSpecificImplementedReturnType>
<code>Traversable</code>
<code>Traversable</code>
</LessSpecificImplementedReturnType>
</file>
<file src="src/Db/DbArrayTrait.php"> <file src="src/Db/DbArrayTrait.php">
<InvalidArgument> <InvalidArgument>
<code>$index</code> <code>$index</code>
@ -244,6 +250,9 @@
<code>$settings</code> <code>$settings</code>
<code>$settings</code> <code>$settings</code>
</ArgumentTypeCoercion> </ArgumentTypeCoercion>
<MethodSignatureMismatch>
<code>DriverArray</code>
</MethodSignatureMismatch>
<MissingClosureParamType> <MissingClosureParamType>
<code>$v</code> <code>$v</code>
<code>$value</code> <code>$value</code>

View File

@ -16,13 +16,11 @@
namespace danog\MadelineProto\Db; namespace danog\MadelineProto\Db;
use Closure;
use danog\MadelineProto\Logger; use danog\MadelineProto\Logger;
use danog\MadelineProto\Magic; use danog\MadelineProto\Magic;
use danog\MadelineProto\Settings\Database\DriverDatabaseAbstract; use danog\MadelineProto\Settings\Database\DriverDatabaseAbstract;
use danog\MadelineProto\Settings\Database\SerializerType; use danog\MadelineProto\Settings\Database\SerializerType;
use danog\MadelineProto\Settings\DatabaseAbstract; use danog\MadelineProto\Settings\DatabaseAbstract;
use Exception;
use IteratorAggregate; use IteratorAggregate;
use function Amp\async; use function Amp\async;

View File

@ -446,6 +446,18 @@ abstract class InternalDoc
{ {
return $this->wrapper->getAPI()->downloadToResponse($messageMedia, $request, $cb, $size, $mime, $name); 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. * Download file to stream.
* *

View File

@ -124,6 +124,27 @@ trait FilesLogic
$this->downloadToStream($messageMedia, \fopen('php://output', 'w'), $cb, ...$result->getServeRange()); $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. * Download file to stream.
* *