[MatrixFile] Fix thumbnail encoding

This commit is contained in:
Christian Pauly 2020-04-23 08:18:33 +00:00
parent 78667536c5
commit 7ce5002237
3 changed files with 7 additions and 5 deletions

View file

@ -488,7 +488,7 @@ class Client {
/// Uploads a new user avatar for this user. /// Uploads a new user avatar for this user.
Future<void> setAvatar(MatrixFile file) async { Future<void> setAvatar(MatrixFile file) async {
await file.resize(width: 128); await file.resize(width: Client.defaultThumbnailSize);
final uploadResp = await upload(file); final uploadResp = await upload(file);
await jsonRequest( await jsonRequest(
type: HTTPType.PUT, type: HTTPType.PUT,
@ -557,6 +557,7 @@ class Client {
static const List<String> supportedGroupEncryptionAlgorithms = [ static const List<String> supportedGroupEncryptionAlgorithms = [
'm.megolm.v1.aes-sha2' 'm.megolm.v1.aes-sha2'
]; ];
static const int defaultThumbnailSize = 256;
http.Client httpClient = http.Client(); http.Client httpClient = http.Client();

View file

@ -503,10 +503,10 @@ class Room {
if (msgType == 'm.image') { if (msgType == 'm.image') {
var thumbnailPathParts = file.path.split('/'); var thumbnailPathParts = file.path.split('/');
thumbnailPathParts.last = 'thumbnail_' + thumbnailPathParts.last; thumbnailPathParts.last = 'thumbnail_' + thumbnailPathParts.last + '.jpg';
final thumbnailPath = thumbnailPathParts.join('/'); final thumbnailPath = thumbnailPathParts.join('/');
thumbnail = MatrixFile(bytes: file.bytes, path: thumbnailPath); thumbnail = MatrixFile(bytes: file.bytes, path: thumbnailPath);
await thumbnail.resize(width: 512); await thumbnail.resize(width: Client.defaultThumbnailSize);
fileImage = decodeImage(file.bytes.toList()); fileImage = decodeImage(file.bytes.toList());
thumbnailImage = decodeImage(thumbnail.bytes.toList()); thumbnailImage = decodeImage(thumbnail.bytes.toList());
} }
@ -1250,7 +1250,7 @@ class Room {
/// Uploads a new user avatar for this room. Returns the event ID of the new /// Uploads a new user avatar for this room. Returns the event ID of the new
/// m.room.avatar event. /// m.room.avatar event.
Future<String> setAvatar(MatrixFile file) async { Future<String> setAvatar(MatrixFile file) async {
await file.resize(width: 256); await file.resize(width: Client.defaultThumbnailSize);
final uploadResp = await client.upload(file); final uploadResp = await client.upload(file);
final setAvatarResp = await client.jsonRequest( final setAvatarResp = await client.jsonRequest(
type: HTTPType.PUT, type: HTTPType.PUT,

View file

@ -12,6 +12,7 @@ class MatrixFile {
/// If this file is an Image, this will resize it to the /// If this file is an Image, this will resize it to the
/// given width and height. Otherwise returns false. /// given width and height. Otherwise returns false.
/// At least width or height must be set! /// At least width or height must be set!
/// The bytes will be encoded as jpg.
Future<bool> resize({int width, int height}) async { Future<bool> resize({int width, int height}) async {
if (width == null && height == null) { if (width == null && height == null) {
throw ('At least width or height must be set!'); throw ('At least width or height must be set!');
@ -23,7 +24,7 @@ class MatrixFile {
return false; return false;
} }
final resizedImage = copyResize(image, width: width, height: height); final resizedImage = copyResize(image, width: width, height: height);
bytes = encodePng(resizedImage); bytes = encodeJpg(resizedImage, quality: 10);
return true; return true;
} }