From 89e160969d2d8be662f3f4f939eb2e4e1c56282d Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 27 Jul 2022 10:36:11 +0200 Subject: [PATCH] fix(llc): do not serialize bytes (#1283) * fix(llc): do not serialize bytes * chore(llc): update changelog * fix(ui): cache files picked with photo_manager * chore(ui): cleanup --- packages/stream_chat/CHANGELOG.md | 6 +++++ .../lib/src/core/models/attachment_file.dart | 12 +--------- .../core/models/attachment_file.freezed.dart | 22 +++++++++++++------ .../src/core/models/attachment_file.g.dart | 2 -- .../stream_attachment_picker.dart | 17 ++++++++------ 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index b934eeb1..fa4d18ab 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +🐞 Fixed + +- Do not serialize `AttachmentFile.bytes` + ## 4.4.0 🐞 Fixed diff --git a/packages/stream_chat/lib/src/core/models/attachment_file.dart b/packages/stream_chat/lib/src/core/models/attachment_file.dart index 1684e005..edce0e1a 100644 --- a/packages/stream_chat/lib/src/core/models/attachment_file.dart +++ b/packages/stream_chat/lib/src/core/models/attachment_file.dart @@ -51,7 +51,7 @@ class AttachmentFile { /// Byte data for this file. Particularly useful if you want to manipulate /// its data or easily upload to somewhere else. - @JsonKey(toJson: _toString, fromJson: _fromString) + @JsonKey(ignore: true) final Uint8List? bytes; /// The file size in bytes. @@ -124,13 +124,3 @@ class UploadState with _$UploadState { /// Returns true if state is [Failed] bool get isFailed => this is Failed; } - -Uint8List? _fromString(String? bytes) { - if (bytes == null) return null; - return Uint8List.fromList(bytes.codeUnits); -} - -String? _toString(Uint8List? bytes) { - if (bytes == null) return null; - return String.fromCharCodes(bytes); -} diff --git a/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart b/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart index 2ea50924..f0ccc584 100644 --- a/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart +++ b/packages/stream_chat/lib/src/core/models/attachment_file.freezed.dart @@ -224,7 +224,9 @@ class _$Preparing extends Preparing { @override Map toJson() { - return _$$PreparingToJson(this); + return _$$PreparingToJson( + this, + ); } } @@ -392,7 +394,9 @@ class _$InProgress extends InProgress { @override Map toJson() { - return _$$InProgressToJson(this); + return _$$InProgressToJson( + this, + ); } } @@ -404,8 +408,8 @@ abstract class InProgress extends UploadState { factory InProgress.fromJson(Map json) = _$InProgress.fromJson; - int get uploaded => throw _privateConstructorUsedError; - int get total => throw _privateConstructorUsedError; + int get uploaded; + int get total; @JsonKey(ignore: true) _$$InProgressCopyWith<_$InProgress> get copyWith => throw _privateConstructorUsedError; @@ -531,7 +535,9 @@ class _$Success extends Success { @override Map toJson() { - return _$$SuccessToJson(this); + return _$$SuccessToJson( + this, + ); } } @@ -686,7 +692,9 @@ class _$Failed extends Failed { @override Map toJson() { - return _$$FailedToJson(this); + return _$$FailedToJson( + this, + ); } } @@ -696,7 +704,7 @@ abstract class Failed extends UploadState { factory Failed.fromJson(Map json) = _$Failed.fromJson; - String get error => throw _privateConstructorUsedError; + String get error; @JsonKey(ignore: true) _$$FailedCopyWith<_$Failed> get copyWith => throw _privateConstructorUsedError; diff --git a/packages/stream_chat/lib/src/core/models/attachment_file.g.dart b/packages/stream_chat/lib/src/core/models/attachment_file.g.dart index 6b657c2a..fe02433e 100644 --- a/packages/stream_chat/lib/src/core/models/attachment_file.g.dart +++ b/packages/stream_chat/lib/src/core/models/attachment_file.g.dart @@ -11,14 +11,12 @@ AttachmentFile _$AttachmentFileFromJson(Map json) => size: json['size'] as int?, path: json['path'] as String?, name: json['name'] as String?, - bytes: _fromString(json['bytes'] as String?), ); Map _$AttachmentFileToJson(AttachmentFile instance) => { 'path': instance.path, 'name': instance.name, - 'bytes': _toString(instance.bytes), 'size': instance.size, }; diff --git a/packages/stream_chat_flutter/lib/src/v4/message_input/stream_attachment_picker.dart b/packages/stream_chat_flutter/lib/src/v4/message_input/stream_attachment_picker.dart index 9f10bae7..2473cf0a 100644 --- a/packages/stream_chat_flutter/lib/src/v4/message_input/stream_attachment_picker.dart +++ b/packages/stream_chat_flutter/lib/src/v4/message_input/stream_attachment_picker.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:path_provider/path_provider.dart'; import 'package:photo_manager/photo_manager.dart'; import 'package:stream_chat_flutter/src/extension.dart'; import 'package:stream_chat_flutter/src/media_list_view.dart'; @@ -369,17 +370,19 @@ class _StreamAttachmentPickerState extends State { } void _addAssetAttachment(AssetEntity medium) async { - final mediaFile = await medium.originFile.timeout( - const Duration(seconds: 5), - onTimeout: () => medium.originFile, - ); + final mediaFile = await medium.originFile; if (mediaFile == null) return; + final tempDir = await getTemporaryDirectory(); + + final cachedFile = await mediaFile + .copy('${tempDir.path}/${mediaFile.path.split('/').last}'); + final file = AttachmentFile( - path: mediaFile.path, - size: await mediaFile.length(), - bytes: mediaFile.readAsBytesSync(), + path: cachedFile.path, + size: await cachedFile.length(), + bytes: cachedFile.readAsBytesSync(), ); if (file.size! > widget.maxAttachmentSize) {