Skip to content

Commit

Permalink
fix: media info parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TesteurManiak committed Aug 3, 2023
1 parent 435cd33 commit 53615ff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.1

* Fixed `MediaInfo.fromJson` parsing and added unit tests

## 3.1.0

* Updated Dart SDK constraint to `>=3.0.0 <4.0.0`
Expand Down
44 changes: 22 additions & 22 deletions lib/src/models/media_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ class MediaInfo {
this.data,
});

/// Factory constructor to generate [MediaInfo] from [Map<String, dynamic>].
/// Factory constructor to generate [MediaInfo] from a [Map].
factory MediaInfo.fromJson(Map<String, dynamic> json) {
switch (json) {
case {
'name': final String? name,
'base64': final String? base64,
'data_scheme': final String? dataScheme,
'data': final String data,
}:
return MediaInfo(
fileName: name,
base64: base64,
base64WithScheme: dataScheme,
data: base64Decode(data),
);
default:
throw const FormatException('Invalid media info format');
final name = json['name'];
final dataScheme = json['data_scheme'];
final data = json['data'];

if (name is! String? || dataScheme is! String? || data is! String?) {
throw const FormatException('Invalid media info format');
}

return MediaInfo(
fileName: name,
base64: data,
base64WithScheme: dataScheme,
data: data != null ? base64Decode(data) : null,
);
}

/// Name of the file.
Expand All @@ -43,12 +41,14 @@ class MediaInfo {
/// File's bytes data.
final Uint8List? data;

/// Convert [MediaInfo] to [Map<String, dynamic>] format
Map<String, dynamic> toJson() => {
'name': fileName,
'data': base64,
'data_scheme': base64WithScheme,
};
/// Convert [MediaInfo] to JSON format
Map<String, dynamic> toJson() {
return {
'name': fileName,
'data': base64,
'data_scheme': base64WithScheme,
};
}
}

/// Image's type.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: image_picker_web
description: Flutter Web Plugin to pick Images (as Widget, File or Uint8List)
and Videos (as File or Uint8List)
version: 3.1.0
version: 3.1.1
repository: https://github.com/Ahmadre/image_picker_web

environment:
Expand Down
34 changes: 34 additions & 0 deletions test/src/models/media_info_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:image_picker_web/src/models/media_info.dart';

void main() {
group('MediaInfo', () {
group('fromJson', () {
test('parse full JSON', () {
const validJson = {
'name': 'name',
'data': 'data',
'data_scheme': 'data_scheme',
};

final mediaInfo = MediaInfo.fromJson(validJson);

expect(mediaInfo.fileName, validJson['name']);
expect(mediaInfo.base64, validJson['data']);
expect(mediaInfo.base64WithScheme, validJson['data_scheme']);
});

test('parse partial JSON', () {
const partialJson = {
'name': 'name',
'data': 'data',
};

final mediaInfo = MediaInfo.fromJson(partialJson);

expect(mediaInfo.fileName, partialJson['name']);
expect(mediaInfo.base64, partialJson['data']);
});
});
});
}

0 comments on commit 53615ff

Please sign in to comment.