Upload from stream

This commit is contained in:
Daniil Gentili 2020-02-15 20:02:32 +01:00
parent 5d50e90f59
commit 0aa75af3e4
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -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();