Skip to content

An open-source cross-platform alternative to AirDrop

fix: FileDto serialization according to v2

Tien Do Nam6525713bf826
diff --git a/lib/model/device.dart b/lib/model/device.dart
index eb4566c1..8b5cc213 100644
--- a/lib/model/device.dart
+++ b/lib/model/device.dart
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'device.freezed.dart';
-part 'device.g.dart';
enum DeviceType {
mobile(Icons.smartphone),
@@ -17,6 +16,7 @@ enum DeviceType {
}
/// Internal device model.
+/// It gets not serialized.
@freezed
class Device with _$Device {
const factory Device({
@@ -27,10 +27,6 @@ class Device with _$Device {
required String fingerprint,
required String alias,
required String? deviceModel,
-
- @JsonKey(unknownEnumValue: DeviceType.desktop) // ignore: invalid_annotation_target
required DeviceType deviceType,
}) = _Device;
-
- factory Device.fromJson(Map<String, Object?> json) => _$DeviceFromJson(json);
}
diff --git a/lib/model/dto/file_dto.dart b/lib/model/dto/file_dto.dart
index caca1e37..83274137 100644
--- a/lib/model/dto/file_dto.dart
+++ b/lib/model/dto/file_dto.dart
@@ -1,30 +1,98 @@
-import 'package:freezed_annotation/freezed_annotation.dart';
+import 'package:collection/collection.dart';
import 'package:localsend_app/model/file_type.dart';
+import 'package:mime/mime.dart';
-part 'file_dto.freezed.dart';
-part 'file_dto.g.dart';
+/// The file DTO that is sent between server and client.
+/// Custom implementation of freezed & json_serializable to handle legacy enums.
+/// The copyWith method is not implemented.
+class FileDto {
+ final String id; // unique inside session
+ final String fileName;
+ final int size;
+ final FileType fileType;
+ final String? hash;
+ final String? preview;
-@freezed
-class FileDto with _$FileDto {
- const factory FileDto({
- required String id, // unique inside session
+ /// This is only used internally to determine if fileType is a mime type or a legacy enum.
+ /// It is not serialized.
+ final bool legacy;
- required String fileName,
+ const FileDto({
+ required this.id,
+ required this.fileName,
+ required this.size,
+ required this.fileType,
+ required this.hash,
+ required this.preview,
+ required this.legacy,
+ });
- required int size,
+ factory FileDto.fromJson(Map<String, Object?> json) => _parseFileDto(json);
- // ignore: invalid_annotation_target
- @JsonKey(unknownEnumValue: FileType.other)
- required FileType fileType,
+ Map<String, dynamic> toJson() => _fileDtoToJson(this);
- /// The hash of the file.
- /// This is used for web-mode as it is unencrypted (HTTP).
- // ignore: invalid_annotation_target
- @JsonKey(includeIfNull: false)
- required String? sha256,
+ @override
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is FileDto &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ fileName == other.fileName &&
+ size == other.size &&
+ fileType == other.fileType &&
+ hash == other.hash &&
+ preview == other.preview &&
+ legacy == other.legacy;
- required String? preview,
- }) = _FileDto;
+ @override
+ int get hashCode => Object.hash(id, fileName, size, fileType, hash, preview, legacy);
+}
+
+/// This deserializer handles both legacy and mime types.
+FileDto _parseFileDto(Map<String, Object?> json) {
+ final String rawFileType = json['fileType'] as String;
+ final FileType fileType;
+ if (rawFileType.contains('/')) {
+ // parse mime
+ if (rawFileType.startsWith('image/')) {
+ fileType = FileType.image;
+ } else if (rawFileType.startsWith('video/')) {
+ fileType = FileType.video;
+ } else if (rawFileType == 'application/pdf') {
+ fileType = FileType.pdf;
+ } else if (rawFileType.startsWith('text/')) {
+ fileType = FileType.text;
+ } else if (rawFileType == 'application/vnd.android.package-archive') {
+ fileType = FileType.apk;
+ } else {
+ fileType = FileType.other;
+ }
+ } else {
+ // parse legacy enum to internal internal enum
+ fileType = FileType.values.firstWhereOrNull((e) => e.name == rawFileType) ?? FileType.other;
+ }
+
+ return FileDto(
+ id: json['id'] as String,
+ fileName: json['fileName'] as String,
+ size: json['size'] as int,
+ fileType: fileType,
+ hash: json['hash'] as String?,
+ preview: json['preview'] as String?,
+ legacy: false,
+ );
+}
- factory FileDto.fromJson(Map<String, Object?> json) => _$FileDtoFromJson(json);
+/// This serializer checks the legacy flag and serializes the file type accordingly.
+Map<String, dynamic> _fileDtoToJson(FileDto instance) {
+ return {
+ 'id': instance.id,
+ 'fileName': instance.fileName,
+ 'size': instance.size,
+ 'fileType': instance.legacy ? instance.fileType.name : lookupMimeType(instance.fileName),
+ if (instance.hash != null)
+ 'hash': instance.hash,
+ if (instance.preview != null)
+ 'preview': instance.preview,
+ };
}
diff --git a/lib/provider/network/send_provider.dart b/lib/provider/network/send_provider.dart
index 8b11b1cb..e29d594a 100644
--- a/lib/provider/network/send_provider.dart
+++ b/lib/provider/network/send_provider.dart
@@ -76,10 +76,11 @@ class SendNotifier extends Notifier<Map<String, SendSessionState>> {
fileName: file.name,
size: file.size,
fileType: file.fileType,
- sha256: null,
+ hash: null,
preview: files.length == 1 && files.first.fileType == FileType.text && files.first.bytes != null
? utf8.decode(files.first.bytes!) // send simple message by embedding it into the preview
: null,
+ legacy: target.version == '1.0',
),
status: FileStatus.queue,
token: null,
@@ -108,7 +109,7 @@ class SendNotifier extends Notifier<Map<String, SendSessionState>> {
protocol: originDevice.https ? ProtocolType.https : ProtocolType.http,
),
files: {
- for (final file in requestState.files.values) file.file.id: file.file,
+ for (final entry in requestState.files.entries) entry.key: entry.value.file,
},
);
diff --git a/lib/provider/network/server/controller/send_controller.dart b/lib/provider/network/server/controller/send_controller.dart
index cda17894..ae731e51 100644
--- a/lib/provider/network/server/controller/send_controller.dart
+++ b/lib/provider/network/server/controller/send_controller.dart
@@ -220,10 +220,11 @@ class SendController {
fileName: file.name,
size: file.size,
fileType: file.fileType,
- sha256: null,
+ hash: null,
preview: files.first.fileType == FileType.text && files.first.bytes != null
? utf8.decode(files.first.bytes!) // send simple message by embedding it into the preview
: null,
+ legacy: false,
),
asset: file.asset,
path: file.path,
diff --git a/lib/util/user_agent_analyzer.dart b/lib/util/user_agent_analyzer.dart
index 33ce6fe4..a3f1f353 100644
--- a/lib/util/user_agent_analyzer.dart
+++ b/lib/util/user_agent_analyzer.dart
@@ -1,6 +1,5 @@
class UserAgentAnalyzer {
String? getBrowser(String userAgent) {
- print('userAgent: $userAgent');
if (userAgent.contains("Firefox")) {
return "Firefox";
} else if (userAgent.contains("Chrome")) {
diff --git a/pubspec.yaml b/pubspec.yaml
index 0c039d99..8209c4ea 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -30,6 +30,7 @@ dependencies:
intl: 0.17.0
json_annotation: 4.8.0
launch_at_startup: 0.2.2
+ mime: 1.0.4
network_info_plus: 3.0.3
open_filex: 4.3.2
package_info_plus: 3.1.0
diff --git a/test/unit/model/dto/prepare_upload_request_dto_test.dart b/test/unit/model/dto/prepare_upload_request_dto_test.dart
index 57b632cb..2d698db0 100644
--- a/test/unit/model/dto/prepare_upload_request_dto_test.dart
+++ b/test/unit/model/dto/prepare_upload_request_dto_test.dart
@@ -1,4 +1,9 @@
+import 'dart:convert';
+
import 'package:localsend_app/model/device.dart';
+import 'package:localsend_app/model/dto/file_dto.dart';
+import 'package:localsend_app/model/dto/info_register_dto.dart';
+import 'package:localsend_app/model/dto/multicast_dto.dart';
import 'package:localsend_app/model/dto/prepare_upload_request_dto.dart';
import 'package:localsend_app/model/file_type.dart';
import 'package:test/test.dart';
@@ -73,5 +78,131 @@ void main() {
expect(parsed.files.length, 1);
expect(parsed.files.values.first.fileType, FileType.other);
});
+
+ test('should parse mime type', () {
+ final dto = {
+ 'info': {
+ 'alias': 'Nice Banana',
+ 'deviceModel': 'Samsung',
+ 'deviceType': 'mobile',
+ },
+ 'files': {
+ 'some id': {
+ 'id': 'some id',
+ 'fileName': 'another image.jpg',
+ 'size': 1234,
+ 'fileType': 'image/jpeg',
+ 'preview': "*preview data*"
+ }
+ }
+ };
+ final parsed = PrepareUploadRequestDto.fromJson(dto);
+ expect(parsed.info.deviceType, DeviceType.mobile);
+ expect(parsed.files.length, 1);
+ expect(parsed.files.values.first.fileType, FileType.image);
+ });
+
+ test('should parse apk mime type', () {
+ final dto = {
+ 'info': {
+ 'alias': 'Nice Banana',
+ 'deviceModel': 'Samsung',
+ 'deviceType': 'mobile',
+ },
+ 'files': {
+ 'some id': {
+ 'id': 'some id',
+ 'fileName': 'myApk.apk',
+ 'size': 1234,
+ 'fileType': 'application/vnd.android.package-archive',
+ }
+ }
+ };
+ final parsed = PrepareUploadRequestDto.fromJson(dto);
+ expect(parsed.info.deviceType, DeviceType.mobile);
+ expect(parsed.files.length, 1);
+ expect(parsed.files.values.first.fileType, FileType.apk);
+ });
+ });
+
+ group('serialize PrepareUploadRequestDto', () {
+ const info = InfoRegisterDto(
+ alias: 'Nice Banana',
+ version: '2.0',
+ deviceModel: 'Samsung',
+ deviceType: DeviceType.mobile,
+ fingerprint: '123',
+ port: 123,
+ protocol: ProtocolType.http,
+ );
+
+ test('should serialize in legacy mode', () {
+ const dto = PrepareUploadRequestDto(
+ info: info,
+ files: {
+ 'some id': FileDto(
+ id: 'some id',
+ fileName: 'another image.jpg',
+ size: 1234,
+ fileType: FileType.image,
+ hash: '*hash*',
+ preview: "*preview data*",
+ legacy: true,
+ ),
+ 'some id 2': FileDto(
+ id: 'some id 2',
+ fileName: 'my apk.apk',
+ size: 1234,
+ fileType: FileType.apk,
+ hash: '*hash*',
+ preview: "*preview data*",
+ legacy: true,
+ ),
+ }
+ );
+ final serialized = _deepSerialize(dto);
+ expect(serialized['info']['deviceType'], 'mobile');
+ expect(serialized['files'].length, 2);
+ expect(serialized['files']['some id']['fileType'], 'image');
+ expect(serialized['files']['some id 2']['fileType'], 'apk');
+ });
+
+ test('should serialize in mime mode', () {
+ const dto = PrepareUploadRequestDto(
+ info: info,
+ files: {
+ 'some id': FileDto(
+ id: 'some id',
+ fileName: 'another image.jpg',
+ size: 1234,
+ fileType: FileType.image,
+ hash: '*hash*',
+ preview: "*preview data*",
+ legacy: false,
+ ),
+ 'some id 2': FileDto(
+ id: 'some id 2',
+ fileName: 'my apk.apk',
+ size: 1234,
+ fileType: FileType.apk,
+ hash: '*hash*',
+ preview: "*preview data*",
+ legacy: false,
+ ),
+ }
+ );
+ final serialized = _deepSerialize(dto);
+
+ expect(serialized['info']['deviceType'], 'mobile');
+ expect(serialized['files'].length, 2);
+ expect(serialized['files']['some id']['fileType'], 'image/jpeg');
+ expect(serialized['files']['some id 2']['fileType'], 'application/vnd.android.package-archive');
+ });
});
}
+
+/// Deep serialize an object to a map.
+/// The toJson method only serializes the first level.
+Map<String, dynamic> _deepSerialize(Object object) {
+ return jsonDecode(jsonEncode(object));
+}