From 4823babb29e8d2c2f65b775e72210aac7a2175c5 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 17 May 2020 20:09:38 -0300 Subject: Add Matrix::Client::MediaStore class This is to wrap all the endpoints pointing to the media store. --- lib/Matrix/Client.pm6 | 20 +++++------ lib/Matrix/Client/Exception.pm6 | 6 ++++ lib/Matrix/Client/MediaStore.rakumod | 70 ++++++++++++++++++++++++++++++++++++ lib/Matrix/Client/Requester.pm6 | 2 +- 4 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 lib/Matrix/Client/MediaStore.rakumod (limited to 'lib') diff --git a/lib/Matrix/Client.pm6 b/lib/Matrix/Client.pm6 index 9022c65..2e26ee7 100644 --- a/lib/Matrix/Client.pm6 +++ b/lib/Matrix/Client.pm6 @@ -5,6 +5,7 @@ use Matrix::Response; use Matrix::Client::Common; use Matrix::Client::Room; use Matrix::Client::Requester; +use Matrix::Client::MediaStore; unit class Matrix::Client does Matrix::Client::Requester; @@ -331,16 +332,15 @@ method remove-room-alias($room-alias) { # Media -#| POST - /_matrix/media/r0/upload -method upload(IO::Path $path, Str $filename?) { - my $buf = slurp $path, :bin; - my $fn = $filename ?? $filename !! $path.basename; - my $res = $.post-bin("/upload", $buf, - content-type => "image/png", - filename => $fn, - ); - my $data = from-json($res.content); - $data // ""; +method media(--> Matrix::Client::MediaStore) { + return Matrix::Client::MediaStore.new( + home-server => $!home-server, + access-token => $!access-token + ) +} + +method upload(IO::Path $path, Str $filename?) is DEPRECATED('media.upload') { + self.media.upload($path, $filename) } # Misc diff --git a/lib/Matrix/Client/Exception.pm6 b/lib/Matrix/Client/Exception.pm6 index 924eece..e02f572 100644 --- a/lib/Matrix/Client/Exception.pm6 +++ b/lib/Matrix/Client/Exception.pm6 @@ -7,4 +7,10 @@ package X::Matrix { "$!code: $!error" } } + + class MXCParse is Exception { + has $.uri; + + method message { "Cannot parse '$!uri'" } + } } diff --git a/lib/Matrix/Client/MediaStore.rakumod b/lib/Matrix/Client/MediaStore.rakumod new file mode 100644 index 0000000..34fbda3 --- /dev/null +++ b/lib/Matrix/Client/MediaStore.rakumod @@ -0,0 +1,70 @@ +use JSON::Fast; +use Matrix::Client::Requester; +use Matrix::Client::Exception; +use URI::Escape; + +unit class Matrix::Client::MediaStore does Matrix::Client::Requester; + +class Matrix::Client::MediaStore::File { + has Str $.content-type; + has Str $.content-disposition; + has Buf $.content; +} + +submethod TWEAK { + # Different client endpoint for media + $!client-endpoint = "/_matrix/media/r0"; +} + +# https://matrix.deprecated.org/_matrix/media/r0/thumbnail/matrix.org/TKTUVTAazFocrTjezhiXZiIe?width=25&height=25&method=crop +method parse-mxc(Str $uri) { + if $uri ~~ m/"mxc://" $ = [.*] "/" $ = [ .* ]/ { + return { + server-name => $, + media-id => $ + } + } + + X::Matrix::MXCParse.new(:$uri).throw; +} + +#| POST - /_matrix/media/r0/upload +method upload(IO::Path $path, Str $filename?, Str :$content-type is copy = "image/png" --> Str) { + my $buf = slurp $path, :bin; + my $fn = $filename ?? $filename !! $path.basename; + + # The filename is passed on a query param. + my $endpoint = "/upload?filename=" ~ uri-escape($fn); + + + my $res = $.post-bin( + $endpoint, $buf, + :$content-type, + ); + + my $data = from-json($res.content); + $data // ""; +} + +# GET - /_matrix/media/r0/download/{serverName}/{mediaId} +multi method download(Str $mxc-uri, :$allow-remote = True) { + my $mxc = self.parse-mxc($mxc-uri); + + samewith($mxc, $mxc, :$allow-remote) +} + +# GET - /_matrix/media/r0/download/{serverName}/{mediaId} +multi method download(Str $server-name, Str $media-id, Bool :$allow-remote = True) { + my $response = $.get( + "/download/{$server-name}/{$media-id}", + allow_remote => $allow-remote.Str.lc + ); + + my %headers = $response.header.hash(); + + Matrix::Client::MediaStore::File.new( + content-type => %headers.head, + content-disposition => %headers.head, + content => $response.content + ) +} diff --git a/lib/Matrix/Client/Requester.pm6 b/lib/Matrix/Client/Requester.pm6 index ff543c9..36a9f69 100644 --- a/lib/Matrix/Client/Requester.pm6 +++ b/lib/Matrix/Client/Requester.pm6 @@ -73,7 +73,7 @@ multi method post(Str $path, :$media = False, *%params) { method post-bin(Str $path, Buf $buf, :$content-type) { my $encoded-path = $path.subst('#', '%23'); my $req = POST( - $.base-url(:media) ~ $encoded-path, + $.base-url() ~ $encoded-path, content => $buf, Content-Type => $content-type ); -- cgit v1.2.3-54-g00ecf