Merge branch 'soru/animated-thumbnail' into 'main'

feat: Add animated property to thumbnails

See merge request famedly/famedlysdk!590
This commit is contained in:
Krille Fear 2020-12-29 09:38:47 +00:00
commit 03b9c6e2ee
4 changed files with 33 additions and 14 deletions

View File

@ -438,14 +438,16 @@ class Event extends MatrixEvent {
/// Set [getThumbnail] to true to fetch the thumbnail, set [width], [height] and [method] /// Set [getThumbnail] to true to fetch the thumbnail, set [width], [height] and [method]
/// for the respective thumbnailing properties. /// for the respective thumbnailing properties.
/// [minNoThumbSize] is the minimum size that an original image may be to not fetch its thumbnail, defaults to 80k /// [minNoThumbSize] is the minimum size that an original image may be to not fetch its thumbnail, defaults to 80k
/// [useThumbnailMxcUrl] says weather to use the mxc url of the thumbnail, rather than the original attachment /// [useThumbnailMxcUrl] says weather to use the mxc url of the thumbnail, rather than the original attachment.
/// [animated] says weather the thumbnail is animated
String getAttachmentUrl( String getAttachmentUrl(
{bool getThumbnail = false, {bool getThumbnail = false,
bool useThumbnailMxcUrl = false, bool useThumbnailMxcUrl = false,
double width = 800.0, double width = 800.0,
double height = 800.0, double height = 800.0,
ThumbnailMethod method = ThumbnailMethod.scale, ThumbnailMethod method = ThumbnailMethod.scale,
int minNoThumbSize = _minNoThumbSize}) { int minNoThumbSize = _minNoThumbSize,
bool animated = false}) {
if (![EventTypes.Message, EventTypes.Sticker].contains(type) || if (![EventTypes.Message, EventTypes.Sticker].contains(type) ||
!hasAttachment || !hasAttachment ||
isAttachmentEncrypted) { isAttachmentEncrypted) {
@ -471,6 +473,7 @@ class Event extends MatrixEvent {
width: width, width: width,
height: height, height: height,
method: method, method: method,
animated: animated,
); );
} else { } else {
return Uri.parse(thisMxcUrl).getDownloadLink(room.client); return Uri.parse(thisMxcUrl).getDownloadLink(room.client);

View File

@ -31,15 +31,28 @@ extension MxcUriExtension on Uri {
/// Returns a scaled thumbnail link to this content with the given [width] and /// Returns a scaled thumbnail link to this content with the given [width] and
/// [height]. [method] can be [ThumbnailMethod.crop] or /// [height]. [method] can be [ThumbnailMethod.crop] or
/// [ThumbnailMethod.scale] and defaults to [ThumbnailMethod.scale]. /// [ThumbnailMethod.scale] and defaults to [ThumbnailMethod.scale].
/// If [animated] (default false) is set to true, an animated thumbnail is requested
/// as per MSC2705. Thumbnails only animate if the media repository supports that.
String getThumbnail(Client matrix, String getThumbnail(Client matrix,
{num width, num height, ThumbnailMethod method = ThumbnailMethod.crop}) { {num width,
num height,
ThumbnailMethod method = ThumbnailMethod.crop,
bool animated = false}) {
if (!isScheme('mxc')) return toString(); if (!isScheme('mxc')) return toString();
final methodStr = method.toString().split('.').last; if (matrix.homeserver == null) {
width = width.round(); return '';
height = height.round(); }
return matrix.homeserver != null return Uri(
? '${matrix.homeserver.toString()}/_matrix/media/r0/thumbnail/$host$path?width=$width&height=$height&method=$methodStr' scheme: matrix.homeserver.scheme,
: ''; host: matrix.homeserver.host,
path: '/_matrix/media/r0/thumbnail/$host$path',
queryParameters: {
if (width != null) 'width': width.round().toString(),
if (height != null) 'height': height.round().toString(),
if (method != null) 'method': method.toString().split('.').last,
if (animated != null) 'animated': animated.toString(),
},
).toString();
} }
} }

View File

@ -1104,12 +1104,12 @@ void main() {
expect(event.getAttachmentUrl(), expect(event.getAttachmentUrl(),
'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file');
expect(event.getAttachmentUrl(getThumbnail: true), expect(event.getAttachmentUrl(getThumbnail: true),
'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/file?width=800&height=800&method=scale'); 'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/file?width=800&height=800&method=scale&animated=false');
expect(event.getAttachmentUrl(useThumbnailMxcUrl: true), expect(event.getAttachmentUrl(useThumbnailMxcUrl: true),
'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/thumb'); 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/thumb');
expect( expect(
event.getAttachmentUrl(getThumbnail: true, useThumbnailMxcUrl: true), event.getAttachmentUrl(getThumbnail: true, useThumbnailMxcUrl: true),
'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/thumb?width=800&height=800&method=scale'); 'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/thumb?width=800&height=800&method=scale&animated=false');
expect( expect(
event.getAttachmentUrl(getThumbnail: true, minNoThumbSize: 9000000), event.getAttachmentUrl(getThumbnail: true, minNoThumbSize: 9000000),
'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file');

View File

@ -38,11 +38,14 @@ void main() {
expect(content.getDownloadLink(client), expect(content.getDownloadLink(client),
'${client.homeserver.toString()}/_matrix/media/r0/download/exampleserver.abc/abcdefghijklmn'); '${client.homeserver.toString()}/_matrix/media/r0/download/exampleserver.abc/abcdefghijklmn');
expect(content.getThumbnail(client, width: 50, height: 50), expect(content.getThumbnail(client, width: 50, height: 50),
'${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop'); '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop&animated=false');
expect( expect(
content.getThumbnail(client, content.getThumbnail(client,
width: 50, height: 50, method: ThumbnailMethod.scale), width: 50,
'${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale'); height: 50,
method: ThumbnailMethod.scale,
animated: true),
'${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale&animated=true');
}); });
}); });
} }