Line data Source code
1 : /*
2 : * Famedly Matrix SDK
3 : * Copyright (C) 2019, 2020, 2021 Famedly GmbH
4 : *
5 : * This program is free software: you can redistribute it and/or modify
6 : * it under the terms of the GNU Affero General Public License as
7 : * published by the Free Software Foundation, either version 3 of the
8 : * License, or (at your option) any later version.
9 : *
10 : * This program is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : * GNU Affero General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Affero General Public License
16 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 : */
18 :
19 : import 'dart:core';
20 :
21 : import 'package:matrix/src/client.dart';
22 :
23 : extension MxcUriExtension on Uri {
24 : /// Transforms this `mxc://` Uri into a `http` resource, which can be used
25 : /// to download the content.
26 : ///
27 : /// Throws an exception if the scheme is not `mxc` or the homeserver is not
28 : /// set.
29 : ///
30 : /// Important! To use this link you have to set a http header like this:
31 : /// `headers: {"authorization": "Bearer ${client.accessToken}"}`
32 4 : Future<Uri> getDownloadUri(Client client) async {
33 : String uriPath;
34 :
35 4 : if (await client.authenticatedMediaSupported()) {
36 : uriPath =
37 20 : '_matrix/client/v1/media/download/$host${hasPort ? ':$port' : ''}$path';
38 : } else {
39 : uriPath =
40 12 : '_matrix/media/v3/download/$host${hasPort ? ':$port' : ''}$path';
41 : }
42 :
43 4 : return isScheme('mxc')
44 4 : ? client.homeserver != null
45 8 : ? client.homeserver?.resolve(uriPath) ?? Uri()
46 0 : : Uri()
47 2 : : Uri();
48 : }
49 :
50 : /// Transforms this `mxc://` Uri into a `http` resource, which can be used
51 : /// to download the content with the given `width` and
52 : /// `height`. `method` can be `ThumbnailMethod.crop` or
53 : /// `ThumbnailMethod.scale` and defaults to `ThumbnailMethod.scale`.
54 : /// If `animated` (default false) is set to true, an animated thumbnail is requested
55 : /// as per MSC2705. Thumbnails only animate if the media repository supports that.
56 : ///
57 : /// Throws an exception if the scheme is not `mxc` or the homeserver is not
58 : /// set.
59 : ///
60 : /// Important! To use this link you have to set a http header like this:
61 : /// `headers: {"authorization": "Bearer ${client.accessToken}"}`
62 4 : Future<Uri> getThumbnailUri(
63 : Client client, {
64 : num? width,
65 : num? height,
66 : ThumbnailMethod? method = ThumbnailMethod.crop,
67 : bool? animated = false,
68 : }) async {
69 6 : if (!isScheme('mxc')) return Uri();
70 4 : final homeserver = client.homeserver;
71 : if (homeserver == null) {
72 0 : return Uri();
73 : }
74 :
75 : String requestPath;
76 4 : if (await client.authenticatedMediaSupported()) {
77 : requestPath =
78 20 : '/_matrix/client/v1/media/thumbnail/$host${hasPort ? ':$port' : ''}$path';
79 : } else {
80 : requestPath =
81 12 : '/_matrix/media/v3/thumbnail/$host${hasPort ? ':$port' : ''}$path';
82 : }
83 :
84 4 : return Uri(
85 4 : scheme: homeserver.scheme,
86 4 : host: homeserver.host,
87 : path: requestPath,
88 4 : port: homeserver.port,
89 4 : queryParameters: {
90 12 : if (width != null) 'width': width.round().toString(),
91 12 : if (height != null) 'height': height.round().toString(),
92 16 : if (method != null) 'method': method.toString().split('.').last,
93 8 : if (animated != null) 'animated': animated.toString(),
94 : },
95 : );
96 : }
97 :
98 0 : @Deprecated('Use `getDownloadUri()` instead')
99 0 : Uri getDownloadLink(Client matrix) => isScheme('mxc')
100 0 : ? matrix.homeserver != null
101 0 : ? matrix.homeserver?.resolve(
102 0 : '_matrix/media/v3/download/$host${hasPort ? ':$port' : ''}$path',
103 : ) ??
104 0 : Uri()
105 0 : : Uri()
106 0 : : Uri();
107 :
108 : /// Returns a scaled thumbnail link to this content with the given `width` and
109 : /// `height`. `method` can be `ThumbnailMethod.crop` or
110 : /// `ThumbnailMethod.scale` and defaults to `ThumbnailMethod.scale`.
111 : /// If `animated` (default false) is set to true, an animated thumbnail is requested
112 : /// as per MSC2705. Thumbnails only animate if the media repository supports that.
113 0 : @Deprecated('Use `getThumbnailUri()` instead')
114 : Uri getThumbnail(
115 : Client matrix, {
116 : num? width,
117 : num? height,
118 : ThumbnailMethod? method = ThumbnailMethod.crop,
119 : bool? animated = false,
120 : }) {
121 0 : if (!isScheme('mxc')) return Uri();
122 0 : final homeserver = matrix.homeserver;
123 : if (homeserver == null) {
124 0 : return Uri();
125 : }
126 0 : return Uri(
127 0 : scheme: homeserver.scheme,
128 0 : host: homeserver.host,
129 0 : path: '/_matrix/media/v3/thumbnail/$host${hasPort ? ':$port' : ''}$path',
130 0 : port: homeserver.port,
131 0 : queryParameters: {
132 0 : if (width != null) 'width': width.round().toString(),
133 0 : if (height != null) 'height': height.round().toString(),
134 0 : if (method != null) 'method': method.toString().split('.').last,
135 0 : if (animated != null) 'animated': animated.toString(),
136 : },
137 : );
138 : }
139 : }
140 :
141 : enum ThumbnailMethod { crop, scale }
|