diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index 2798c973..9b6380de 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -79,6 +79,9 @@ trait Files } elseif (\is_array($file)) { return yield from $this->uploadFromTgfile($file, $cb, $encrypted); } + if (\is_resource($file) || (is_object($file) && $file instanceof InputStream)) { + return yield from $this->uploadFromStream($file, 0, '', $fileName, $cb, $encrypted); + } if (!$this->settings['upload']['allow_automatic_upload']) { return yield from $this->uploadFromUrl($file, 0, $fileName, $cb, $encrypted); } @@ -165,7 +168,7 @@ trait Files } /* @var $stream \Amp\ByteStream\OutputStream */ if (!\is_object($stream)) { - $stream = new ResourceOutputStream($stream); + $stream = new ResourceInputStream($stream); } if (!$stream instanceof InputStream) { throw new Exception("Invalid stream provided"); @@ -205,6 +208,24 @@ trait Files }; $seekable = false; } + if (!$size && $seekable && \method_exists($stream, 'tell')) { + yield $stream->seek(0, \SEEK_END); + $size = yield $stream->tell(); + yield $stream->seek(0); + } else if (!$size) { + $this->logger->logger("No content length for stream, caching first"); + $body = $stream; + $stream = new BlockingFile(\fopen('php://temp', 'r+b'), 'php://temp', 'r+b'); + while (null !== ($chunk = yield $body->read())) { + yield $stream->write($chunk); + } + $size = $stream->tell(); + if (!$size) { + throw new Exception('Wrong size!'); + } + yield $stream->seek(0); + return yield from $this->uploadFromStream($stream, $size, $mime, $fileName, $cb, $encrypted); + } $res = (yield from $this->uploadFromCallable($callable, $size, $mime, $fileName, $cb, $seekable, $encrypted)); if ($created) { $stream->disconnect();