LCOV - code coverage report
Current view: top level - lib/src/utils - uri_extension.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 23 47 48.9 %
Date: 2024-09-30 15:57:20 Functions: 0 0 -

          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           3 :   Future<Uri> getDownloadUri(Client client) async {
      33             :     String uriPath;
      34             : 
      35           3 :     if (await client.authenticatedMediaSupported()) {
      36             :       uriPath =
      37          16 :           '_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           3 :     return isScheme('mxc')
      44           3 :         ? client.homeserver != null
      45           6 :             ? 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           3 :   Future<Uri> getThumbnailUri(Client client,
      63             :       {num? width,
      64             :       num? height,
      65             :       ThumbnailMethod? method = ThumbnailMethod.crop,
      66             :       bool? animated = false}) async {
      67           5 :     if (!isScheme('mxc')) return Uri();
      68           3 :     final homeserver = client.homeserver;
      69             :     if (homeserver == null) {
      70           0 :       return Uri();
      71             :     }
      72             : 
      73             :     String requestPath;
      74           3 :     if (await client.authenticatedMediaSupported()) {
      75             :       requestPath =
      76          16 :           '/_matrix/client/v1/media/thumbnail/$host${hasPort ? ':$port' : ''}$path';
      77             :     } else {
      78             :       requestPath =
      79          12 :           '/_matrix/media/v3/thumbnail/$host${hasPort ? ':$port' : ''}$path';
      80             :     }
      81             : 
      82           3 :     return Uri(
      83           3 :       scheme: homeserver.scheme,
      84           3 :       host: homeserver.host,
      85             :       path: requestPath,
      86           3 :       port: homeserver.port,
      87           3 :       queryParameters: {
      88           9 :         if (width != null) 'width': width.round().toString(),
      89           9 :         if (height != null) 'height': height.round().toString(),
      90          12 :         if (method != null) 'method': method.toString().split('.').last,
      91           6 :         if (animated != null) 'animated': animated.toString(),
      92             :       },
      93             :     );
      94             :   }
      95             : 
      96           0 :   @Deprecated('Use `getDownloadUri()` instead')
      97           0 :   Uri getDownloadLink(Client matrix) => isScheme('mxc')
      98           0 :       ? matrix.homeserver != null
      99           0 :           ? matrix.homeserver?.resolve(
     100           0 :                   '_matrix/media/v3/download/$host${hasPort ? ':$port' : ''}$path') ??
     101           0 :               Uri()
     102           0 :           : Uri()
     103           0 :       : Uri();
     104             : 
     105             :   /// Returns a scaled thumbnail link to this content with the given `width` and
     106             :   /// `height`. `method` can be `ThumbnailMethod.crop` or
     107             :   /// `ThumbnailMethod.scale` and defaults to `ThumbnailMethod.scale`.
     108             :   /// If `animated` (default false) is set to true, an animated thumbnail is requested
     109             :   /// as per MSC2705. Thumbnails only animate if the media repository supports that.
     110           0 :   @Deprecated('Use `getThumbnailUri()` instead')
     111             :   Uri getThumbnail(Client matrix,
     112             :       {num? width,
     113             :       num? height,
     114             :       ThumbnailMethod? method = ThumbnailMethod.crop,
     115             :       bool? animated = false}) {
     116           0 :     if (!isScheme('mxc')) return Uri();
     117           0 :     final homeserver = matrix.homeserver;
     118             :     if (homeserver == null) {
     119           0 :       return Uri();
     120             :     }
     121           0 :     return Uri(
     122           0 :       scheme: homeserver.scheme,
     123           0 :       host: homeserver.host,
     124           0 :       path: '/_matrix/media/v3/thumbnail/$host${hasPort ? ':$port' : ''}$path',
     125           0 :       port: homeserver.port,
     126           0 :       queryParameters: {
     127           0 :         if (width != null) 'width': width.round().toString(),
     128           0 :         if (height != null) 'height': height.round().toString(),
     129           0 :         if (method != null) 'method': method.toString().split('.').last,
     130           0 :         if (animated != null) 'animated': animated.toString(),
     131             :       },
     132             :     );
     133             :   }
     134             : }
     135             : 
     136             : enum ThumbnailMethod { crop, scale }

Generated by: LCOV version 1.14