mirror of
https://github.com/danog/MadelineProto.git
synced 2025-01-23 07:51:14 +01:00
Handle downloadToBrowser errors
This commit is contained in:
parent
d40d5b759a
commit
45cc77dac5
@ -25,6 +25,7 @@ use danog\MadelineProto\Exception;
|
||||
use danog\MadelineProto\FileCallbackInterface;
|
||||
use danog\MadelineProto\Lang;
|
||||
use danog\MadelineProto\LocalFile;
|
||||
use danog\MadelineProto\Logger;
|
||||
use danog\MadelineProto\NothingInTheSocketException;
|
||||
use danog\MadelineProto\RemoteUrl;
|
||||
use danog\MadelineProto\Settings;
|
||||
@ -36,6 +37,7 @@ use danog\MadelineProto\Stream\Transport\PremadeStream;
|
||||
use danog\MadelineProto\TL\Conversion\Extension;
|
||||
use danog\MadelineProto\Tools;
|
||||
use Revolt\EventLoop;
|
||||
use Throwable;
|
||||
|
||||
use const FILTER_VALIDATE_URL;
|
||||
|
||||
@ -78,21 +80,27 @@ trait FilesLogic
|
||||
$headers['range'] = $_SERVER['HTTP_RANGE'];
|
||||
}
|
||||
|
||||
$messageMedia = $this->getDownloadInfo($messageMedia);
|
||||
$messageMedia['size'] = $size ?? $messageMedia['size'];
|
||||
$messageMedia['mime'] = $mime ?? $messageMedia['mime'];
|
||||
if ($name) {
|
||||
$name = explode('.', $name, 2);
|
||||
$messageMedia['name'] = $name[0];
|
||||
$messageMedia['ext'] = isset($name[1]) ? '.'.$name[1] : '';
|
||||
try {
|
||||
$messageMedia = $this->getDownloadInfo($messageMedia);
|
||||
$messageMedia['size'] = $size ?? $messageMedia['size'];
|
||||
$messageMedia['mime'] = $mime ?? $messageMedia['mime'];
|
||||
if ($name) {
|
||||
$name = explode('.', $name, 2);
|
||||
$messageMedia['name'] = $name[0];
|
||||
$messageMedia['ext'] = isset($name[1]) ? '.'.$name[1] : '';
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->logger("An error occurred inside of downloadToBrowser: $e", Logger::FATAL_ERROR);
|
||||
$messageMedia = null;
|
||||
}
|
||||
|
||||
|
||||
$result = ResponseInfo::parseHeaders(
|
||||
$_SERVER['REQUEST_METHOD'],
|
||||
$headers,
|
||||
$messageMedia,
|
||||
);
|
||||
|
||||
\http_response_code($result->getCode());
|
||||
foreach ($result->getHeaders() as $key => $value) {
|
||||
if (\is_array($value)) {
|
||||
foreach ($value as $subValue) {
|
||||
@ -102,7 +110,6 @@ trait FilesLogic
|
||||
\header("$key: $value");
|
||||
}
|
||||
}
|
||||
\http_response_code($result->getCode());
|
||||
|
||||
if (!\in_array($result->getCode(), [HttpStatus::OK, HttpStatus::PARTIAL_CONTENT], true)) {
|
||||
Tools::echo($result->getCodeExplanation());
|
||||
|
@ -21,6 +21,7 @@ declare(strict_types=1);
|
||||
namespace danog\MadelineProto\MTProtoTools;
|
||||
|
||||
use Amp\Http\HttpStatus;
|
||||
use danog\MadelineProto\Lang;
|
||||
|
||||
/**
|
||||
* Obtain response information for file to server.
|
||||
@ -29,7 +30,6 @@ use Amp\Http\HttpStatus;
|
||||
*/
|
||||
final class ResponseInfo
|
||||
{
|
||||
private const POWERED_BY = "<p><small>Powered by <a href='https://docs.madelineproto.xyz'>MadelineProto</a></small></p>";
|
||||
private const NO_CACHE = [
|
||||
'Cache-Control' => ['no-store, no-cache, must-revalidate, max-age=0', 'post-check=0, pre-check=0'],
|
||||
'Pragma' => 'no-cache',
|
||||
@ -56,10 +56,15 @@ final class ResponseInfo
|
||||
*
|
||||
* @param string $method HTTP method
|
||||
* @param array $headers HTTP headers
|
||||
* @param array $messageMedia Media info
|
||||
* @param ?array $messageMedia Media info
|
||||
*/
|
||||
private function __construct(string $method, array $headers, array $messageMedia)
|
||||
private function __construct(string $method, array $headers, ?array $messageMedia)
|
||||
{
|
||||
if ($messageMedia === null) {
|
||||
$this->code = HttpStatus::NOT_FOUND;
|
||||
$this->serve = false;
|
||||
$this->headers = self::NO_CACHE;
|
||||
}
|
||||
if (isset($headers['range'])) {
|
||||
$range = \explode('=', $headers['range'], 2);
|
||||
if (\count($range) == 1) {
|
||||
@ -137,7 +142,7 @@ final class ResponseInfo
|
||||
* @param array $headers HTTP headers
|
||||
* @param array $messageMedia Media info
|
||||
*/
|
||||
public static function parseHeaders(string $method, array $headers, array $messageMedia): self
|
||||
public static function parseHeaders(string $method, array $headers, ?array $messageMedia): self
|
||||
{
|
||||
return new self($method, $headers, $messageMedia);
|
||||
}
|
||||
@ -151,7 +156,7 @@ final class ResponseInfo
|
||||
if ($this->code === HttpStatus::RANGE_NOT_SATISFIABLE) {
|
||||
$body .= '<p>Could not use selected range.</p>';
|
||||
}
|
||||
$body .= self::POWERED_BY;
|
||||
$body .= Lang::$current_lang["dl.php_powered_by_madelineproto"];
|
||||
$body .= '</body></html>';
|
||||
return $body;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user