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

Merge pull request #1323 from danog/v8_fix_files

Fix: file download
This commit is contained in:
Daniil Gentili 2023-03-26 18:38:31 +02:00 committed by GitHub
commit beacc3b8fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 12 deletions

View File

@ -38,6 +38,7 @@
"ext-fileinfo": "*",
"amphp/amp": "^3",
"amphp/http-client": "^5",
"amphp/http": "^2",
"amphp/socket": "^2",
"amphp/dns": "^2",
"amphp/byte-stream": "^2",

View File

@ -14,7 +14,7 @@ use Amp\File\Driver\BlockingFile;
use Amp\File\File;
use Amp\Http\Server\Request as ServerRequest;
use Amp\Http\Server\Response;
use Amp\Http\Status;
use Amp\Http\HttpStatus;
use Amp\Sync\LocalMutex;
use Amp\Sync\Lock;
use danog\MadelineProto\Exception;
@ -25,6 +25,7 @@ use danog\MadelineProto\Settings;
use danog\MadelineProto\Stream\Common\BufferedRawStream;
use danog\MadelineProto\Stream\Common\SimpleBufferedRawStream;
use danog\MadelineProto\Stream\ConnectionContext;
use danog\MadelineProto\Stream\StreamInterface;
use danog\MadelineProto\Stream\Transport\PremadeStream;
use danog\MadelineProto\TL\Conversion\Extension;
use danog\MadelineProto\Tools;
@ -94,7 +95,7 @@ trait FilesLogic
}
\http_response_code($result->getCode());
if (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
if (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT])) {
Tools::echo($result->getCodeExplanation());
} elseif ($result->shouldServe()) {
if (!empty($messageMedia['name']) && !empty($messageMedia['ext'])) {
@ -198,13 +199,13 @@ trait FilesLogic
$pipe = new Pipe(1024 * 1024);
EventLoop::queue($this->downloadToStream(...), $messageMedia, $pipe->getSink(), $cb, ...$result->getServeRange());
$body = $pipe->getSource();
} elseif (!\in_array($result->getCode(), [Status::OK, Status::PARTIAL_CONTENT])) {
} elseif (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT])) {
$body = $result->getCodeExplanation();
}
$response = new Response($result->getCode(), $result->getHeaders(), $body);
if ($result->shouldServe() && !empty($result->getHeaders()['Content-Length'])) {
$response->setHeader('content-length', $result->getHeaders()['Content-Length']);
$response->setHeader('content-length', (string)$result->getHeaders()['Content-Length']);
if (!empty($messageMedia['name']) && !empty($messageMedia['ext'])) {
$response->setHeader('content-disposition', "inline; filename=\"{$messageMedia['name']}{$messageMedia['ext']}\"");
}
@ -328,6 +329,9 @@ trait FilesLogic
$created = true;
}
$callable = static function (int $offset, int $size) use ($stream) {
if (!$stream instanceof BufferedRawStream) {
throw new \InvalidArgumentException('Invalid stream type');
}
$reader = $stream->getReadBuffer($l);
try {
return $reader->bufferRead($size);
@ -358,6 +362,7 @@ trait FilesLogic
}
$res = ($this->uploadFromCallable($callable, $size, $mime, $fileName, $cb, $seekable, $encrypted));
if ($created) {
/** @var StreamInterface $stream */
$stream->disconnect();
}
return $res;

View File

@ -20,7 +20,7 @@ declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools;
use Amp\Http\Status;
use Amp\Http\HttpStatus;
/**
* Obtain response information for file to server.
@ -46,7 +46,7 @@ final class ResponseInfo
/**
* HTTP response code.
*/
private int $code = Status::OK;
private int $code = HttpStatus::OK;
/**
* Header array.
*/
@ -76,7 +76,7 @@ final class ResponseInfo
[$range, $extra_ranges] = $list;
} else {
$this->serve = false;
$this->code = Status::RANGE_NOT_SATISFIABLE;
$this->code = HttpStatus::RANGE_NOT_SATISFIABLE;
$this->headers = self::NO_CACHE;
return;
}
@ -94,7 +94,7 @@ final class ResponseInfo
if (!empty($seek_start) && $seek_end < \abs(\intval($seek_start))) {
$this->serve = false;
$this->code = Status::RANGE_NOT_SATISFIABLE;
$this->code = HttpStatus::RANGE_NOT_SATISFIABLE;
$this->headers = self::NO_CACHE;
return;
}
@ -102,7 +102,7 @@ final class ResponseInfo
$this->serve = $method !== 'HEAD';
if ($seek_start > 0 || $seek_end < $size - 1) {
$this->code = Status::PARTIAL_CONTENT;
$this->code = HttpStatus::PARTIAL_CONTENT;
$this->headers['Content-Range'] = "bytes $seek_start-$seek_end/$size";
$this->headers['Content-Length'] = $seek_end - $seek_start + 1;
} elseif ($size > 0) {
@ -137,9 +137,9 @@ final class ResponseInfo
*/
public function getCodeExplanation(): string
{
$reason = Status::getReason($this->code);
$body = "<html><body><h1>{$this->code} $reason</h1><br>";
if ($this->code === Status::RANGE_NOT_SATISFIABLE) {
$reason = HttpStatus::getReason($this->code);
$body = "<html lang='en'><body><h1>{$this->code} $reason</h1><br>";
if ($this->code === HttpStatus::RANGE_NOT_SATISFIABLE) {
$body .= '<p>Could not use selected range.</p>';
}
$body .= self::POWERED_BY;