From 2a1b90cc42ebc89ee14b5bcdbe971f6c3c3d4bf5 Mon Sep 17 00:00:00 2001 From: Anton Stubenbord Date: Tue, 1 Nov 2022 23:42:43 +0100 Subject: [PATCH] Fixed wrong encodings --- lib/core/logic/timeout_client.dart | 2 +- .../repository/document_repository_impl.dart | 10 +++--- .../repository/saved_views_repository.dart | 2 +- ...sitory.dart => label_repository_impl.dart} | 36 ++++++++++++------- .../services/authentication.service.dart | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) rename lib/features/labels/repository/{label.repository.dart => label_repository_impl.dart} (86%) diff --git a/lib/core/logic/timeout_client.dart b/lib/core/logic/timeout_client.dart index a380945d..781434d9 100644 --- a/lib/core/logic/timeout_client.dart +++ b/lib/core/logic/timeout_client.dart @@ -116,7 +116,7 @@ class TimeoutClient implements BaseClient { Response _handle400Error(Response response) { if (response.statusCode == 400) { // try to parse contained error message, otherwise return response - final JSON json = jsonDecode(response.body); + final JSON json = jsonDecode(utf8.decode(response.bodyBytes)); final Map errorMessages = {}; //TODO: This could be simplified, look at error message format of paperless-ngx for (final entry in json.entries) { diff --git a/lib/features/documents/repository/document_repository_impl.dart b/lib/features/documents/repository/document_repository_impl.dart index bb9d649a..6be24c0d 100644 --- a/lib/features/documents/repository/document_repository_impl.dart +++ b/lib/features/documents/repository/document_repository_impl.dart @@ -133,7 +133,7 @@ class DocumentRepositoryImpl implements DocumentRepository { body: json.encode(doc.toJson()), headers: {"Content-Type": "application/json"}).timeout(requestTimeout); if (response.statusCode == 200) { - return DocumentModel.fromJson(jsonDecode(response.body)); + return DocumentModel.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } else { throw const ErrorMessage(ErrorCode.documentUpdateFailed); } @@ -147,7 +147,7 @@ class DocumentRepositoryImpl implements DocumentRepository { ); if (response.statusCode == 200) { final searchResult = PagedSearchResult.fromJson( - jsonDecode(const Utf8Decoder().convert(response.body.codeUnits)), + jsonDecode(utf8.decode(response.bodyBytes)), DocumentModel.fromJson, ); return searchResult; @@ -249,7 +249,7 @@ class DocumentRepositoryImpl implements DocumentRepository { @override Future getMetaData(DocumentModel document) async { final response = await httpClient.get(Uri.parse("/api/documents/${document.id}/metadata/")); - return DocumentMetaData.fromJson(jsonDecode(response.body)); + return DocumentMetaData.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } @override @@ -257,7 +257,7 @@ class DocumentRepositoryImpl implements DocumentRepository { final response = await httpClient.get(Uri.parse("/api/search/autocomplete/?query=$query&limit=$limit}")); if (response.statusCode == 200) { - return json.decode(response.body) as List; + return jsonDecode(utf8.decode(response.bodyBytes)) as List; } throw const ErrorMessage(ErrorCode.autocompleteQueryError); } @@ -268,7 +268,7 @@ class DocumentRepositoryImpl implements DocumentRepository { await httpClient.get(Uri.parse("/api/documents/?more_like=$docId&pageSize=10")); if (response.statusCode == 200) { return PagedSearchResult.fromJson( - json.decode(response.body), + jsonDecode(utf8.decode(response.bodyBytes)), SimilarDocumentModel.fromJson, ).results; } diff --git a/lib/features/documents/repository/saved_views_repository.dart b/lib/features/documents/repository/saved_views_repository.dart index 4de40b36..22976dd9 100644 --- a/lib/features/documents/repository/saved_views_repository.dart +++ b/lib/features/documents/repository/saved_views_repository.dart @@ -37,7 +37,7 @@ class SavedViewRepositoryImpl implements SavedViewsRepository { headers: {'Content-Type': 'application/json'}, ); if (response.statusCode == 201) { - return SavedView.fromJson(jsonDecode(response.body)); + return SavedView.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw ErrorMessage(ErrorCode.createSavedViewError, httpStatusCode: response.statusCode); } diff --git a/lib/features/labels/repository/label.repository.dart b/lib/features/labels/repository/label_repository_impl.dart similarity index 86% rename from lib/features/labels/repository/label.repository.dart rename to lib/features/labels/repository/label_repository_impl.dart index b3276252..18421779 100644 --- a/lib/features/labels/repository/label.repository.dart +++ b/lib/features/labels/repository/label_repository_impl.dart @@ -75,9 +75,10 @@ class LabelRepositoryImpl implements LabelRepository { Uri.parse('/api/correspondents/'), body: json.encode(correspondent.toJson()), headers: {"Content-Type": "application/json"}, + encoding: Encoding.getByName("utf-8"), ); if (response.statusCode == 201) { - return Correspondent.fromJson(json.decode(response.body)); + return Correspondent.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw ErrorMessage(ErrorCode.correspondentCreateFailed, httpStatusCode: response.statusCode); } @@ -88,9 +89,10 @@ class LabelRepositoryImpl implements LabelRepository { Uri.parse('/api/document_types/'), body: json.encode(type.toJson()), headers: {"Content-Type": "application/json"}, + encoding: Encoding.getByName("utf-8"), ); if (response.statusCode == 201) { - return DocumentType.fromJson(json.decode(response.body)); + return DocumentType.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.documentTypeCreateFailed); } @@ -98,12 +100,17 @@ class LabelRepositoryImpl implements LabelRepository { @override Future saveTag(Tag tag) async { final body = json.encode(tag.toJson()); - final response = await httpClient.post(Uri.parse('/api/tags/'), body: body, headers: { - "Content-Type": "application/json", - "Accept": "application/json; version=2", - }); + final response = await httpClient.post( + Uri.parse('/api/tags/'), + body: body, + headers: { + "Content-Type": "application/json", + "Accept": "application/json; version=2", + }, + encoding: Encoding.getByName("utf-8"), + ); if (response.statusCode == 201) { - return Tag.fromJson(json.decode(response.body)); + return Tag.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.tagCreateFailed); } @@ -112,7 +119,7 @@ class LabelRepositoryImpl implements LabelRepository { Future getStatistics() async { final response = await httpClient.get(Uri.parse('/api/statistics/')); if (response.statusCode == 200) { - return json.decode(response.body)['documents_total']; + return jsonDecode(utf8.decode(response.bodyBytes))['documents_total']; } throw const ErrorMessage(ErrorCode.unknown); } @@ -154,9 +161,10 @@ class LabelRepositoryImpl implements LabelRepository { Uri.parse('/api/correspondents/${correspondent.id}/'), headers: {"Content-Type": "application/json"}, body: json.encode(correspondent.toJson()), + encoding: Encoding.getByName("utf-8"), ); if (response.statusCode == 200) { - return Correspondent.fromJson(json.decode(response.body)); + return Correspondent.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.unknown); } @@ -168,9 +176,10 @@ class LabelRepositoryImpl implements LabelRepository { Uri.parse('/api/document_types/${documentType.id}/'), headers: {"Content-Type": "application/json"}, body: json.encode(documentType.toJson()), + encoding: Encoding.getByName("utf-8"), ); if (response.statusCode == 200) { - return DocumentType.fromJson(json.decode(response.body)); + return DocumentType.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.unknown); } @@ -185,9 +194,10 @@ class LabelRepositoryImpl implements LabelRepository { "Content-Type": "application/json", }, body: json.encode(tag.toJson()), + encoding: Encoding.getByName("utf-8"), ); if (response.statusCode == 200) { - return Tag.fromJson(json.decode(response.body)); + return Tag.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.unknown); } @@ -225,7 +235,7 @@ class LabelRepositoryImpl implements LabelRepository { headers: {"Content-Type": "application/json"}, ); if (response.statusCode == 201) { - return StoragePath.fromJson(json.decode(response.body)); + return StoragePath.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw ErrorMessage(ErrorCode.storagePathCreateFailed, httpStatusCode: response.statusCode); } @@ -239,7 +249,7 @@ class LabelRepositoryImpl implements LabelRepository { body: json.encode(path.toJson()), ); if (response.statusCode == 200) { - return StoragePath.fromJson(json.decode(response.body)); + return StoragePath.fromJson(jsonDecode(utf8.decode(response.bodyBytes))); } throw const ErrorMessage(ErrorCode.unknown); } diff --git a/lib/features/login/services/authentication.service.dart b/lib/features/login/services/authentication.service.dart index c2ac4a51..9fe5186d 100644 --- a/lib/features/login/services/authentication.service.dart +++ b/lib/features/login/services/authentication.service.dart @@ -31,7 +31,7 @@ class AuthenticationService { body: {"username": username, "password": password}, ); if (response.statusCode == 200) { - final data = jsonDecode(response.body); + final data = jsonDecode(utf8.decode(response.bodyBytes)); return data['token']; } else if (response.statusCode == 400 && response.body.toLowerCase().contains("no required certificate was sent")) {