refactor: upgrade http package

This commit is contained in:
Lukas Lihotzki 2021-03-30 15:05:56 +02:00
parent 08043c06f4
commit 2a2a170609
7 changed files with 49 additions and 43 deletions

View File

@ -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);

View File

@ -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<MatrixFile> downloadAndDecryptAttachment(
{bool getThumbnail = false,
Future<Uint8List> Function(String) downloadCallback}) async {
Future<Uint8List> 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 =

View File

@ -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();
);
}
}

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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,
content
.getThumbnail(client,
width: 50,
height: 50,
method: ThumbnailMethod.scale,
animated: true),
animated: true)
.toString(),
'${client.homeserver.toString()}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale&animated=true');
});
});