From c44f123719632f295467ae1c89bb43e1d86d6221 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 28 Jan 2018 18:14:07 -0300 Subject: Refactor change-avatar and upload methods --- lib/Matrix/Client.pm6 | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 7dfcd27..36e0bb7 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -122,14 +122,12 @@ method avatar-url(Str :$user-id?) { $data // "" } -method change-avatar(Str:D $avatar!, Bool :$upload) { - my $mxc-url; - if so $upload { - $mxc-url = $.upload($avatar); - } else { - $mxc-url = $avatar; - } +multi method change-avatar(IO::Path $avatar) { + my $mxc-url = $.upload($avatar.IO); + samewith($mxc-url); +} +multi method change-avatar(Str:D $mxc-url!) { my $res = $.put("/profile/" ~ $.user-id ~ "/avatar_url", avatar_url => $mxc-url); return $.check-res($res); @@ -193,9 +191,13 @@ method send(Str $room-id, Str $body, :$type? = "m.text") { # Media -method upload(Str $path where *.IO.f) { +method upload(IO::Path $path, Str $filename?) { my $buf = slurp $path, :bin; - my $res = $.post-bin("/upload", $buf, content-type => "image/png"); + my $fn = $filename ?? $filename !! $path.basename; + my $res = $.post-bin("/upload", $buf, + content-type => "image/png", + filename => $fn, + ); $.check-res($res); my $data = from-json($res.content); $data // ""; -- cgit v1.2.3-70-g09d2