famedlysdk/lib/src/utils/uri_extension.dart

46 lines
1.8 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +00:00
/*
2020-06-03 10:16:01 +00:00
* Famedly Matrix SDK
* Copyright (C) 2019, 2020 Famedly GmbH
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-06-09 11:57:33 +00:00
*
2020-06-03 10:16:01 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-09 11:57:33 +00:00
*/
import 'package:famedlysdk/src/client.dart';
2019-06-09 10:16:48 +00:00
import 'dart:core';
2020-04-24 07:24:06 +00:00
extension MxcUriExtension on Uri {
/// Returns a download Link to this content.
2020-04-24 07:24:06 +00:00
String getDownloadLink(Client matrix) => isScheme('mxc')
2020-06-03 10:16:01 +00:00
? matrix.api.homeserver != null
? '${matrix.api.homeserver.toString()}/_matrix/media/r0/download/$host$path'
2020-04-24 07:24:06 +00:00
: ''
: toString();
2019-06-09 10:16:48 +00:00
/// Returns a scaled thumbnail link to this content with the given [width] and
/// [height]. [method] can be [ThumbnailMethod.crop] or
/// [ThumbnailMethod.scale] and defaults to [ThumbnailMethod.scale].
String getThumbnail(Client matrix,
2020-04-24 07:24:06 +00:00
{num width, num height, ThumbnailMethod method = ThumbnailMethod.crop}) {
if (!isScheme('mxc')) return toString();
final methodStr = method.toString().split('.').last;
2019-06-09 10:16:48 +00:00
width = width.round();
height = height.round();
2020-06-03 10:16:01 +00:00
return matrix.api.homeserver != null
? '${matrix.api.homeserver.toString()}/_matrix/media/r0/thumbnail/$host$path?width=$width&height=$height&method=$methodStr'
: '';
2019-06-09 10:16:48 +00:00
}
}
enum ThumbnailMethod { crop, scale }