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 : /// Returns a download Link to this content. 25 6 : Uri getDownloadLink(Client matrix) => isScheme('mxc') 26 3 : ? matrix.homeserver != null 27 6 : ? matrix.homeserver?.resolve( 28 16 : '_matrix/media/v3/download/$host${hasPort ? ':$port' : ''}$path') ?? 29 0 : Uri() 30 0 : : Uri() 31 2 : : Uri(); 32 : 33 : /// Returns a scaled thumbnail link to this content with the given `width` and 34 : /// `height`. `method` can be `ThumbnailMethod.crop` or 35 : /// `ThumbnailMethod.scale` and defaults to `ThumbnailMethod.scale`. 36 : /// If `animated` (default false) is set to true, an animated thumbnail is requested 37 : /// as per MSC2705. Thumbnails only animate if the media repository supports that. 38 3 : Uri getThumbnail(Client matrix, 39 : {num? width, 40 : num? height, 41 : ThumbnailMethod? method = ThumbnailMethod.crop, 42 : bool? animated = false}) { 43 5 : if (!isScheme('mxc')) return Uri(); 44 3 : final homeserver = matrix.homeserver; 45 : if (homeserver == null) { 46 0 : return Uri(); 47 : } 48 3 : return Uri( 49 3 : scheme: homeserver.scheme, 50 3 : host: homeserver.host, 51 16 : path: '/_matrix/media/v3/thumbnail/$host${hasPort ? ':$port' : ''}$path', 52 3 : port: homeserver.port, 53 3 : queryParameters: { 54 9 : if (width != null) 'width': width.round().toString(), 55 9 : if (height != null) 'height': height.round().toString(), 56 12 : if (method != null) 'method': method.toString().split('.').last, 57 6 : if (animated != null) 'animated': animated.toString(), 58 : }, 59 : ); 60 : } 61 : } 62 : 63 : enum ThumbnailMethod { crop, scale }