Fix file download

This commit is contained in:
Daniil Gentili 2020-02-12 18:16:08 +01:00
parent e4d56f32c2
commit 4289661094
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 55 additions and 8 deletions

View File

@ -0,0 +1,38 @@
<?php
/**
* MadelineProto fileserver.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2019 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto\Files;
use Amp\Http\Server\HttpServer;
use danog\MadelineProto\Exception;
/**
* MadelineProto fileserver.
*/
class Server
{
/**
* Constructor function.
*/
public function __construct()
{
if (!\class_exists(HttpServer::class)) {
throw new Exception("Please install https://github.com/amphp/http-server to use this module (composer require amphp/http-server)");
}
}
}

View File

@ -861,7 +861,7 @@ trait Files
$headers = [];
if (isset($_SERVER['HTTP_RANGE'])) {
$headers['content-range'] = $_SERVER['HTTP_RANGE'];
$headers['range'] = $_SERVER['HTTP_RANGE'];
}
$messageMedia = yield from $this->getDownloadInfo($messageMedia);
@ -909,6 +909,7 @@ trait Files
}
$messageMedia = yield from $this->getDownloadInfo($messageMedia);
$result = self::parseHeaders(
$request->getMethod(),
\array_map(fn (array $headers) => $headers[0], $request->getHeaders()),
@ -920,6 +921,10 @@ trait Files
$body = new IteratorStream(
new Producer(
function (callable $emit) use (&$messageMedia, &$cb, &$result) {
$emit = static function (string $payload) use ($emit): \Generator {
yield $emit($payload);
return \strlen($payload);
};
yield Tools::call($this->downloadToCallable($messageMedia, $emit, $cb, false, ...$result['serve']));
}
)
@ -965,8 +970,8 @@ trait Files
*/
private static function parseHeaders(string $method, array $headers, array $messageMedia): array
{
if (isset($headers['content-range'])) {
$range = \explode('=', $headers['content-range'], 2);
if (isset($headers['range'])) {
$range = \explode('=', $headers['range'], 2);
if (\count($range) == 1) {
$range[1] = '';
}
@ -1011,12 +1016,12 @@ trait Files
'code' => Status::OK,
'headers' => []
];
if ($seek_start > 0 || $seek_end < $messageMedia['file_size'] - 1) {
if ($seek_start > 0 || $seek_end < $messageMedia['size'] - 1) {
$result['code'] = Status::PARTIAL_CONTENT;
$result['headers']['Content-Range'] = "bytes ${seek_start}-${seek_end}/${messageMedia['file_size']}";
$result['headers']['Content-Range'] = "bytes ${seek_start}-${seek_end}/${messageMedia['size']}";
$result['headers']['Content-Length'] = $seek_end - $seek_start + 1;
} else {
$result['headers']['Content-Length'] = $messageMedia['file_size'];
$result['headers']['Content-Length'] = $messageMedia['size'];
}
$result['headers']['Content-Type'] = $messageMedia['mime'];
$result['headers']['Cache-Control'] = 'max-age=31556926';
@ -1214,7 +1219,7 @@ trait Files
$cdn = false;
$params[0]['previous_promise'] = new Success(true);
$start = \microtime(true);
$size = (yield from $this->downloadPart($messageMedia, $cdn, $datacenter, $old_dc, $ige, $cb, $initParam = \array_shift($params), $callable, $seekable));
$size = yield from $this->downloadPart($messageMedia, $cdn, $datacenter, $old_dc, $ige, $cb, $initParam = \array_shift($params), $callable, $seekable);
if ($initParam['part_end_at'] - $initParam['part_start_at'] !== $size) {
// Premature end for undefined length files
$origCb(100, 0, 0);
@ -1312,6 +1317,7 @@ trait Files
}
}
}
if ($res['_'] === 'upload.fileCdnRedirect') {
$cdn = true;
$messageMedia['file_token'] = $res['file_token'];

View File

@ -21,6 +21,7 @@ namespace danog\MadelineProto\TL\Conversion;
use danog\Decoder\FileId;
use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProtoTools\PeerHandler;
use const danog\Decoder\TYPES_IDS;
@ -237,7 +238,9 @@ trait BotAPI
$newd['forward_from'] = (yield from $this->getPwrChat($data['fwd_from']['from_id']));
}
if (isset($data['fwd_from']['channel_id'])) {
$newd['forward_from_chat'] = (yield from $this->getPwrChat($data['fwd_from']['channel_id']));
try {
$newd['forward_from_chat'] = yield from $this->getPwrChat(PeerHandler::toSupergroup($data['fwd_from']['channel_id']));
} catch (\Throwable $e) {}
}
if (isset($data['fwd_from']['date'])) {
$newd['forward_date'] = $data['fwd_from']['date'];