aboutsummaryrefslogtreecommitdiff
path: root/lib/Matrix/Client/MediaStore.rakumod
blob: 9779db235aa1cb4d9708631e2f963c1bbe8b0a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use JSON::Fast;
use URI::Escape;

use Matrix::Client::Requester;
use Matrix::Client::Exception;
use Matrix::Response;

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";
}

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, Str :$filename?) {
    my $mxc = self.parse-mxc($mxc-uri);

    samewith($mxc<server-name>, $mxc<media-id>, :$allow-remote, :$filename)
}

#| GET - /_matrix/media/r0/download/{serverName}/{mediaId}/{fileName}
multi method download(Str $server-name, Str $media-id,
                      Bool :$allow-remote = True, Str :$filename?) {
    my $endpoint = "/download/{$server-name}/{$media-id}";

    $endpoint ~= "/{$filename}" if $filename.defined;
    my $response = $.get(
        $endpoint,
        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
    )
}

#| GET - /_matrix/media/r0/thumbnail/{serverName}/{mediaId}
multi method thumbnail(Str $mxc-uri, Int $width, Int $height,
                       Str :$method where * eq 'crop'|'scale',
                       Bool :$allow-remote = True) {
    my $mxc = self.parse-mxc($mxc-uri);
    samewith(
        $mxc<server-name>, $mxc<media-id>,
        $width, $height,
        :$method, :$allow-remote
    )
}

#| GET - /_matrix/media/r0/thumbnail/{serverName}/{mediaId}
multi method thumbnail(Str $server-name, Str $media-id,
                       Int $width, Int $height,
                       Str :$method where * eq 'crop'|'scale',
                       Bool :$allow-remote = True) {
    my $endpoint = "/thumbnail/{$server-name}/{$media-id}";

    my $response = $.get(
        $endpoint,
        :$height,
        :$width,
        :$method,
        allow_remote => $allow-remote.Str.lc
    );

    my %headers = $response.header.hash();

    Matrix::Client::MediaStore::File.new(
        content-type => %headers<Content-Type>.head,
        content => $response.content
    )
}

#| GET - /_matrix/media/r0/config
method config(--> Matrix::Response::MediaStore::Config) {
    my $response = $.get("/config");
    Matrix::Response::MediaStore::Config.new(from-json($response.content))
}