aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix/Client/MediaStore.rakumod
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@gmail.com>2020-05-17 20:09:38 -0300
committerMatias Linares <matiaslina@gmail.com>2020-05-17 20:09:38 -0300
commit4823babb29e8d2c2f65b775e72210aac7a2175c5 (patch)
tree7433d7c5c662e4ebea5b55349a1e34632bcc8030 /lib/Matrix/Client/MediaStore.rakumod
parentba413134ddcc6177b3673ba18d0035d78714e3fe (diff)
downloadperl6-matrix-client-4823babb29e8d2c2f65b775e72210aac7a2175c5.tar.gz
Add Matrix::Client::MediaStore class
This is to wrap all the endpoints pointing to the media store.
Diffstat (limited to 'lib/Matrix/Client/MediaStore.rakumod')
-rw-r--r--lib/Matrix/Client/MediaStore.rakumod70
1 files changed, 70 insertions, 0 deletions
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://" $<server-name> = [.*] "/" $<media-id> = [ .* ]/ {
+ return {
+ server-name => $<server-name>,
+ media-id => $<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<content_uri> // "";
+}
+
+# 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<server-name>, $mxc<media-id>, :$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<Content-Type>.head,
+ content-disposition => %headers<Content-Disposition>.head,
+ content => $response.content
+ )
+}