Skip to content

Commit

Permalink
fix(llc): do not serialize bytes (#1283)
Browse files Browse the repository at this point in the history
* fix(llc): do not serialize bytes

* chore(llc): update changelog

* fix(ui): cache files picked with photo_manager

* chore(ui): cleanup
  • Loading branch information
imtoori authored Jul 27, 2022
1 parent e9e35d7 commit 89e1609
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
6 changes: 6 additions & 0 deletions packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Upcoming

🐞 Fixed

- Do not serialize `AttachmentFile.bytes`

## 4.4.0

🐞 Fixed
Expand Down
12 changes: 1 addition & 11 deletions packages/stream_chat/lib/src/core/models/attachment_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ class _$Preparing extends Preparing {

@override
Map<String, dynamic> toJson() {
return _$$PreparingToJson(this);
return _$$PreparingToJson(
this,
);
}
}

Expand Down Expand Up @@ -392,7 +394,9 @@ class _$InProgress extends InProgress {

@override
Map<String, dynamic> toJson() {
return _$$InProgressToJson(this);
return _$$InProgressToJson(
this,
);
}
}

Expand All @@ -404,8 +408,8 @@ abstract class InProgress extends UploadState {
factory InProgress.fromJson(Map<String, dynamic> 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;
Expand Down Expand Up @@ -531,7 +535,9 @@ class _$Success extends Success {

@override
Map<String, dynamic> toJson() {
return _$$SuccessToJson(this);
return _$$SuccessToJson(
this,
);
}
}

Expand Down Expand Up @@ -686,7 +692,9 @@ class _$Failed extends Failed {

@override
Map<String, dynamic> toJson() {
return _$$FailedToJson(this);
return _$$FailedToJson(
this,
);
}
}

Expand All @@ -696,7 +704,7 @@ abstract class Failed extends UploadState {

factory Failed.fromJson(Map<String, dynamic> json) = _$Failed.fromJson;

String get error => throw _privateConstructorUsedError;
String get error;
@JsonKey(ignore: true)
_$$FailedCopyWith<_$Failed> get copyWith =>
throw _privateConstructorUsedError;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -369,17 +370,19 @@ class _StreamAttachmentPickerState extends State<StreamAttachmentPicker> {
}

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) {
Expand Down

0 comments on commit 89e1609

Please sign in to comment.