Add speed and time to progress callbacks

This commit is contained in:
Daniil Gentili 2019-12-29 15:12:32 +01:00
parent 087d105504
commit 135f311023
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
1 changed files with 10 additions and 4 deletions

View File

@ -407,8 +407,10 @@ $sentMessage = yield $MadelineProto->messages->sendMedia([
'_' => 'inputMediaUploadedDocument', '_' => 'inputMediaUploadedDocument',
'file' => new \danog\MadelineProto\FileCallback( 'file' => new \danog\MadelineProto\FileCallback(
'video.mp4', 'video.mp4',
function ($progress) use ($MadelineProto, $peer) { function ($progress, $speed, $time) use ($MadelineProto, $peer) {
yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => 'Upload progress: '.$progress.'%']); try {
yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => "Upload progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"]);
} catch (\Throwable $e) {}
} }
), ),
'attributes' => [ 'attributes' => [
@ -423,8 +425,10 @@ $output_file_name = yield $MadelineProto->downloadToFile(
$sentMessage, $sentMessage,
new \danog\MadelineProto\FileCallback( new \danog\MadelineProto\FileCallback(
'/tmp/myname.mp4', '/tmp/myname.mp4',
function ($progress) use ($MadelineProto, $peer) { function ($progress, $speed, $time) use ($MadelineProto, $peer) {
yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => 'Download progress: '.$progress.'%']); try {
yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => "Download progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"]);
} catch (\Throwable $e) {}
} }
) )
); );
@ -432,6 +436,8 @@ $output_file_name = yield $MadelineProto->downloadToFile(
This will send the file `video.mp4` to [@danogentili](https://t.me/danogentili): while uploading, he will receive progress messages `Upload progress: 24%` until the upload is complete; while downloading, he will receive progress messages `Download progress: 34%` until the download is complete. This will send the file `video.mp4` to [@danogentili](https://t.me/danogentili): while uploading, he will receive progress messages `Upload progress: 24%` until the upload is complete; while downloading, he will receive progress messages `Download progress: 34%` until the download is complete.
You can also add two more parameters `$speed, $time` to the signature of the method to get a partial upload speed in mbps, along with the time elapsed since the start of the download.
A FileCallback object can be provided to `uploadMedia`, `sendMedia`, `uploadProfilePicture`, `upload`, `upload_encrypted`, `download_to_*`: the first parameter to its constructor must be the file path/object that is usually accepted by the function, the second must be a callable function or object. A FileCallback object can be provided to `uploadMedia`, `sendMedia`, `uploadProfilePicture`, `upload`, `upload_encrypted`, `download_to_*`: the first parameter to its constructor must be the file path/object that is usually accepted by the function, the second must be a callable function or object.
You can also write your own callback class, just implement `\danog\MadelineProto\FileCallbackInterface`: You can also write your own callback class, just implement `\danog\MadelineProto\FileCallbackInterface`: