From 1bf41186de5c46ed60231de6485601b3e9b4fa9d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 29 Dec 2019 14:21:25 +0100 Subject: [PATCH] Allow automatic upload for URLs --- docs | 2 +- examples/downloadRenameBot.php | 12 +++--- .../MadelineProto/MTProtoTools/Files.php | 38 ++++++++++--------- .../Stream/Transport/DefaultStream.php | 2 +- src/danog/MadelineProto/TL/TL.php | 1 - 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/docs b/docs index 757d12fb..80072aa3 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 757d12fbdda97d22a20d565be8e0aea64ed34cad +Subproject commit 80072aa37b4b8ba238fb57ad009c0101cf783495 diff --git a/examples/downloadRenameBot.php b/examples/downloadRenameBot.php index f583a962..a4fe57f4 100644 --- a/examples/downloadRenameBot.php +++ b/examples/downloadRenameBot.php @@ -90,7 +90,6 @@ class EventHandler extends \danog\MadelineProto\EventHandler $name = $update['message']['message']; list($url, $id) = $this->states[$peerId]; unset($this->states[$peerId]); - $method = 'uploadFromTgFile'; } else { $url = \explode(' ', $update['message']['message'], 2); $name = \trim($url[1] ?? \basename($update['message']['message'])); @@ -101,10 +100,9 @@ class EventHandler extends \danog\MadelineProto\EventHandler if (\strpos($url, 'http') !== 0) { $url = "http://$url"; } - $method = 'uploadFromUrl'; } $id = yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Preparing...', 'reply_to_msg_id' => $messageId])['id']; - $file = yield $this->$method(new \danog\MadelineProto\FileCallback( + $url = new \danog\MadelineProto\FileCallback( $url, function ($progress) use ($peerId, $id) { static $prev = 0; @@ -118,7 +116,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler } catch (\danog\MadelineProto\RPCErrorException $e) { } } - )); + ); yield $this->messages->sendMedia( [ 'peer' => $peerId, @@ -165,6 +163,9 @@ $settings = [ 'min' => 20, 'max' => 1000, ] + ], + 'upload' => [ + 'allow_automatic_upload' => false // IMPORTANT: for security reasons, upload by URL will still be allowed ] ]; @@ -181,6 +182,7 @@ while (true) { try { $MadelineProto->logger("Surfaced: $e"); $MadelineProto->getEventHandler(['async' => false])->report("Surfaced: $e"); - } catch (\Throwable $e) {} + } catch (\Throwable $e) { + } } } diff --git a/src/danog/MadelineProto/MTProtoTools/Files.php b/src/danog/MadelineProto/MTProtoTools/Files.php index a6a08526..0a58eda6 100644 --- a/src/danog/MadelineProto/MTProtoTools/Files.php +++ b/src/danog/MadelineProto/MTProtoTools/Files.php @@ -52,13 +52,13 @@ trait Files * Upload file. * * @param FileCallbackInterface|string|array $file File, URL or Telegram file to upload - * @param string $file_name File name + * @param string $fileName File name * @param callable $cb Callback (DEPRECATED, use FileCallbackInterface) * @param boolean $encrypted Whether to encrypt file for secret chats * * @return \Generator */ - public function upload($file, string $file_name = '', $cb = null, bool $encrypted = false): \Generator + public function upload($file, string $fileName = '', $cb = null, bool $encrypted = false): \Generator { if (\is_object($file) && $file instanceof FileCallbackInterface) { $cb = $file; @@ -66,18 +66,20 @@ trait Files } if (\is_string($file) || (\is_object($file) && \method_exists($file, '__toString'))) { if (\filter_var($file, FILTER_VALIDATE_URL)) { - return yield $this->uploadFromUrl($file); + return yield $this->uploadFromUrl($file, 0, $fileName, $cb, $encrypted); } } elseif (\is_array($file)) { return yield $this->uploadFromTgfile($file, $cb, $encrypted); + } elseif (!$this->API->settings['upload']['allow_automatic_upload']) { + return yield $this->uploadFromUrl($file, 0, $fileName, $cb, $encrypted); } $file = \danog\MadelineProto\Absolute::absolute($file); if (!yield exists($file)) { throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['file_not_exist']); } - if (empty($file_name)) { - $file_name = \basename($file); + if (empty($fileName)) { + $fileName = \basename($file); } StatCache::clear($file); @@ -91,7 +93,7 @@ trait Files $mime = $this->getMimeFromFile($file); try { - return yield $this->uploadFromStream($stream, $size, $mime, $file_name, $cb, $encrypted); + return yield $this->uploadFromStream($stream, $size, $mime, $fileName, $cb, $encrypted); } finally { yield $stream->close(); } @@ -101,13 +103,13 @@ trait Files * * @param string|FileCallbackInterface $url URL of file * @param integer $size Size of file - * @param string $file_name File name + * @param string $fileName File name * @param callable $cb Callback (DEPRECATED, use FileCallbackInterface) * @param boolean $encrypted Whether to encrypt file for secret chats * * @return array */ - public function uploadFromUrl($url, int $size = 0, string $file_name = '', $cb = null, bool $encrypted = false): \Generator + public function uploadFromUrl($url, int $size = 0, string $fileName = '', $cb = null, bool $encrypted = false): \Generator { if (\is_object($url) && $url instanceof FileCallbackInterface) { $cb = $url; @@ -141,7 +143,7 @@ trait Files yield $stream->seek(0); } - return yield $this->uploadFromStream($stream, $size, $mime, $file_name, $cb, $encrypted); + return yield $this->uploadFromStream($stream, $size, $mime, $fileName, $cb, $encrypted); } /** * Upload file from stream. @@ -149,13 +151,13 @@ trait Files * @param mixed $stream PHP resource or AMPHP async stream * @param integer $size File size * @param string $mime Mime type - * @param string $file_name File name + * @param string $fileName File name * @param callable $cb Callback (DEPRECATED, use FileCallbackInterface) * @param boolean $encrypted Whether to encrypt file for secret chats * * @return array */ - public function uploadFromStream($stream, int $size, string $mime, string $file_name = '', $cb = null, bool $encrypted = false): \Generator + public function uploadFromStream($stream, int $size, string $mime, string $fileName = '', $cb = null, bool $encrypted = false): \Generator { if (\is_object($stream) && $stream instanceof FileCallbackInterface) { $cb = $stream; @@ -209,7 +211,7 @@ trait Files $seekable = false; } - $res = yield $this->uploadFromCallable($callable, $size, $mime, $file_name, $cb, $seekable, $encrypted); + $res = yield $this->uploadFromCallable($callable, $size, $mime, $fileName, $cb, $seekable, $encrypted); if ($created) { $stream->disconnect(); } @@ -224,14 +226,14 @@ trait Files * @param mixed $callable Callable * @param integer $size File size * @param string $mime Mime type - * @param string $file_name File name + * @param string $fileName File name * @param callable $cb Callback (DEPRECATED, use FileCallbackInterface) * @param boolean $seekable Whether chunks can be fetched out of order * @param boolean $encrypted Whether to encrypt file for secret chats * * @return \Generator */ - public function uploadFromCallable($callable, int $size, string $mime, string $file_name = '', $cb = null, bool $seekable = true, bool $encrypted = false): \Generator + public function uploadFromCallable($callable, int $size, string $mime, string $fileName = '', $cb = null, bool $seekable = true, bool $encrypted = false): \Generator { if (\is_object($callable) && $callable instanceof FileCallbackInterface) { $cb = $callable; @@ -342,7 +344,7 @@ trait Files $this->logger->logger("Total upload time: $time"); $this->logger->logger("Total upload speed: $speed mbps"); - $constructor = ['_' => $constructor, 'id' => $file_id, 'parts' => $part_total_num, 'name' => $file_name, 'mime_type' => $mime]; + $constructor = ['_' => $constructor, 'id' => $file_id, 'parts' => $part_total_num, 'name' => $fileName, 'mime_type' => $mime]; if ($encrypted === true) { $constructor['key_fingerprint'] = $fingerprint; $constructor['key'] = $key; @@ -357,14 +359,14 @@ trait Files * Upload file to secret chat. * * @param FileCallbackInterface|string|array $file File, URL or Telegram file to upload - * @param string $file_name File name + * @param string $fileName File name * @param callable $cb Callback (DEPRECATED, use FileCallbackInterface) * * @return \Generator */ - public function uploadEncrypted($file, string $file_name = '', $cb = null): \Generator + public function uploadEncrypted($file, string $fileName = '', $cb = null): \Generator { - return $this->upload($file, $file_name, $cb, true); + return $this->upload($file, $fileName, $cb, true); } /** diff --git a/src/danog/MadelineProto/Stream/Transport/DefaultStream.php b/src/danog/MadelineProto/Stream/Transport/DefaultStream.php index d98c540d..9b81c108 100644 --- a/src/danog/MadelineProto/Stream/Transport/DefaultStream.php +++ b/src/danog/MadelineProto/Stream/Transport/DefaultStream.php @@ -78,7 +78,7 @@ class DefaultStream implements ) ); } - + $this->stream = yield ($this->connector ?? connector())->connect((string) $uri, $ctx->getSocketContext(), $ctx->getCancellationToken()); if ($secure) { yield $this->stream->setupTls(); diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index d2fa0be2..3ec8de3f 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -818,7 +818,6 @@ class TL && $this->constructors->findByPredicate($arguments[$current_argument['name']]['_'])['type'] === 'InputFile' ) ) - && $this->API->settings['upload']['allow_automatic_upload'] ) { $arguments[$current_argument['name']] = yield $this->API->upload($arguments[$current_argument['name']]); }