diff --git a/lib/src/client.dart b/lib/src/client.dart index e0428782..9a3f6506 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -285,7 +285,7 @@ class Client extends MatrixApi { String MatrixIdOrDomain, ) async { final response = await http - .get('https://${MatrixIdOrDomain.domain}/.well-known/matrix/client'); + .get(Uri.https(MatrixIdOrDomain.domain, '/.well-known/matrix/client')); var respBody = response.body; try { respBody = utf8.decode(response.bodyBytes); diff --git a/lib/src/event.dart b/lib/src/event.dart index 4cfaaf59..72904934 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -434,7 +434,7 @@ class Event extends MatrixEvent { /// [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. /// [animated] says weather the thumbnail is animated - String getAttachmentUrl( + Uri getAttachmentUrl( {bool getThumbnail = false, bool useThumbnailMxcUrl = false, double width = 800.0, @@ -503,7 +503,7 @@ class Event extends MatrixEvent { /// true to download the thumbnail instead. Future downloadAndDecryptAttachment( {bool getThumbnail = false, - Future Function(String) downloadCallback}) async { + Future Function(Uri) downloadCallback}) async { if (![EventTypes.Message, EventTypes.Sticker].contains(type)) { throw ("This event has the type '$type' and so it can't contain an attachment."); } @@ -534,7 +534,7 @@ class Event extends MatrixEvent { // Download the file if (uint8list == null) { - downloadCallback ??= (String url) async { + downloadCallback ??= (Uri url) async { return (await http.get(url)).bodyBytes; }; uint8list = diff --git a/lib/src/utils/uri_extension.dart b/lib/src/utils/uri_extension.dart index a0925df5..c4032b22 100644 --- a/lib/src/utils/uri_extension.dart +++ b/lib/src/utils/uri_extension.dart @@ -22,25 +22,25 @@ import '../client.dart'; extension MxcUriExtension on Uri { /// Returns a download Link to this content. - String getDownloadLink(Client matrix) => isScheme('mxc') + Uri getDownloadLink(Client matrix) => isScheme('mxc') ? matrix.homeserver != null - ? '${matrix.homeserver.toString()}/_matrix/media/r0/download/$host$path' - : '' - : toString(); + ? matrix.homeserver.resolve('_matrix/media/r0/download/$host$path') + : Uri() + : this; /// 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`. /// 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, + Uri getThumbnail(Client matrix, {num width, num height, ThumbnailMethod method = ThumbnailMethod.crop, bool animated = false}) { - if (!isScheme('mxc')) return toString(); + if (!isScheme('mxc')) return this; if (matrix.homeserver == null) { - return ''; + return Uri(); } return Uri( scheme: matrix.homeserver.scheme, @@ -52,7 +52,7 @@ extension MxcUriExtension on Uri { if (method != null) 'method': method.toString().split('.').last, if (animated != null) 'animated': animated.toString(), }, - ).toString(); + ); } } diff --git a/pubspec.yaml b/pubspec.yaml index 8fc6db89..24870c6e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: ">=2.7.0 <3.0.0" dependencies: - http: ^0.12.2 + http: ^0.13.0 mime: ^1.0.0 canonical_json: ^1.0.0 markdown: ^3.0.0 @@ -22,8 +22,8 @@ dependencies: olm: ^2.0.0 matrix_file_e2ee: ^1.1.0 isolate: ^2.0.3 - logger: ^0.9.4 - matrix_api_lite: ^0.2.1 + logger: ^1.0.0 + matrix_api_lite: ^0.2.2 dev_dependencies: test: ^1.15.7 diff --git a/test/event_test.dart b/test/event_test.dart index c39b1c0b..e5fb2f73 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1052,13 +1052,11 @@ void main() { test('attachments', () async { final FILE_BUFF = Uint8List.fromList([0]); final THUMBNAIL_BUFF = Uint8List.fromList([2]); - final downloadCallback = (String url) async { + final downloadCallback = (Uri uri) async { return { - 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file': - FILE_BUFF, - 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/thumb': - THUMBNAIL_BUFF, - }[url]; + '/_matrix/media/r0/download/example.org/file': FILE_BUFF, + '/_matrix/media/r0/download/example.org/thumb': THUMBNAIL_BUFF, + }[uri.path]; }; await client.checkHomeserver('https://fakeserver.notexisting', checkWellKnown: false); @@ -1109,17 +1107,21 @@ void main() { expect(event.attachmentOrThumbnailMxcUrl(), 'mxc://example.org/file'); expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true), 'mxc://example.org/thumb'); - expect(event.getAttachmentUrl(), + expect(event.getAttachmentUrl().toString(), 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); - expect(event.getAttachmentUrl(getThumbnail: true), + expect(event.getAttachmentUrl(getThumbnail: true).toString(), '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).toString(), 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/thumb'); expect( - event.getAttachmentUrl(getThumbnail: true, useThumbnailMxcUrl: true), + event + .getAttachmentUrl(getThumbnail: true, useThumbnailMxcUrl: true) + .toString(), 'https://fakeserver.notexisting/_matrix/media/r0/thumbnail/example.org/thumb?width=800&height=800&method=scale&animated=false'); expect( - event.getAttachmentUrl(getThumbnail: true, minNoThumbSize: 9000000), + event + .getAttachmentUrl(getThumbnail: true, minNoThumbSize: 9000000) + .toString(), 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/file'); buffer = await event.downloadAndDecryptAttachment( @@ -1139,13 +1141,11 @@ void main() { Uint8List.fromList([0x55, 0xD7, 0xEB, 0x72, 0x05, 0x13]); final THUMB_BUFF_DEC = Uint8List.fromList([0x74, 0x68, 0x75, 0x6D, 0x62, 0x0A]); - final downloadCallback = (String url) async { + final downloadCallback = (Uri uri) async { return { - 'https://fakeserver.notexisting/_matrix/media/r0/download/example.com/file': - FILE_BUFF_ENC, - 'https://fakeserver.notexisting/_matrix/media/r0/download/example.com/thumb': - THUMB_BUFF_ENC, - }[url]; + '/_matrix/media/r0/download/example.com/file': FILE_BUFF_ENC, + '/_matrix/media/r0/download/example.com/thumb': THUMB_BUFF_ENC, + }[uri.path]; }; final room = Room(id: '!localpart:server.abc', client: await getClient()); var event = Event.fromJson({ @@ -1237,12 +1237,11 @@ void main() { test('downloadAndDecryptAttachment store', () async { final FILE_BUFF = Uint8List.fromList([0]); var serverHits = 0; - final downloadCallback = (String url) async { + final downloadCallback = (Uri uri) async { serverHits++; return { - 'https://fakeserver.notexisting/_matrix/media/r0/download/example.org/newfile': - FILE_BUFF, - }[url]; + '/_matrix/media/r0/download/example.org/newfile': FILE_BUFF, + }[uri.path]; }; await client.checkHomeserver('https://fakeserver.notexisting', checkWellKnown: false); diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 22a2fba1..da340f24 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -55,6 +55,11 @@ class FakeMatrixApi extends MockClient { if (action.endsWith('?')) { action = action.substring(0, action.length - 1); } + if (action.endsWith('?server_name')) { + // This can be removed after matrix_api_lite is released with: + // https://gitlab.com/famedly/libraries/matrix_api_lite/-/merge_requests/16 + action = action.substring(0, action.length - 12); + } if (action.endsWith('/')) { action = action.substring(0, action.length - 1); } diff --git a/test/mxc_uri_extension_test.dart b/test/mxc_uri_extension_test.dart index 6d73e9b6..5510216b 100644 --- a/test/mxc_uri_extension_test.dart +++ b/test/mxc_uri_extension_test.dart @@ -36,16 +36,18 @@ void main() { final content = Uri.parse(mxc); expect(content.isScheme('mxc'), true); - expect(content.getDownloadLink(client), + expect(content.getDownloadLink(client).toString(), '${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).toString(), '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop&animated=false'); expect( - content.getThumbnail(client, - width: 50, - height: 50, - method: ThumbnailMethod.scale, - animated: true), + content + .getThumbnail(client, + width: 50, + height: 50, + method: ThumbnailMethod.scale, + animated: true) + .toString(), '${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale&animated=true'); }); });