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 ) }