An open-source cross-platform alternative to AirDrop
refactor: extract bmp to png converter
Tien Do Nam97d34dfa62a9
diff --git a/app/lib/util/image_converter.dart b/app/lib/util/image_converter.dartnew file mode 100644index 00000000..9b4714e8--- /dev/null+++ b/app/lib/util/image_converter.dart@@ -0,0 +1,17 @@+import 'dart:typed_data';++import 'package:image/image.dart';++/// Converts BMP image data to PNG image data.+Future<Uint8List> convertBmpToPng(Uint8List bmp) async {+ final pngImage = await (Command()+ ..decodeBmp(bmp)+ ..encodePng())+ .getBytesThread();++ if (pngImage == null) {+ throw Exception('Failed to convert BMP to PNG');+ }++ return pngImage;+}diff --git a/app/lib/util/native/file_picker.dart b/app/lib/util/native/file_picker.dartindex bc776dc2..c373ed2c 100644--- a/app/lib/util/native/file_picker.dart+++ b/app/lib/util/native/file_picker.dart@@ -6,7 +6,6 @@ import 'package:file_selector/file_selector.dart';import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';import 'package:flutter/services.dart';-import 'package:image/image.dart' as img;import 'package:localsend_app/config/theme.dart';import 'package:localsend_app/gen/strings.g.dart';import 'package:localsend_app/model/cross_file.dart';@@ -15,6 +14,7 @@ import 'package:localsend_app/provider/device_info_provider.dart';import 'package:localsend_app/provider/selection/selected_sending_files_provider.dart';import 'package:localsend_app/util/determine_image_type.dart';import 'package:localsend_app/util/file_path_helper.dart';+import 'package:localsend_app/util/image_converter.dart';import 'package:localsend_app/util/native/android_saf.dart';import 'package:localsend_app/util/native/cross_file_converters.dart';import 'package:localsend_app/util/native/pick_directory_path.dart';@@ -271,27 +271,28 @@ Future<void> _pickClipboard(BuildContext context, Ref ref) async {return;}- var image = await Pasteboard.image;+ final image = await Pasteboard.image;if (image != null) {+ // Adding temporary variable because Dart analyzer somehow doesn't properly downcast Uint8List? to Uint8List+ Uint8List currImage = image;+ String imageType = determineImageType(image);+// On Windows, Pasteboard read image from clipboard as BMP which is large and inefficient. Attempt to convert to PNG- if (determineImageType(image) == 'bmp') {+ if (imageType == 'bmp') {try {- final pngImage = await (img.Command()- ..decodeBmp(image)- ..encodePng())- .getBytesThread();- if (pngImage != null) {- image = pngImage;- }- } catch (err) {+ final pngImage = await convertBmpToPng(image);+ currImage = pngImage;+ imageType = 'png';+ } catch (e) {// Fail to convert to png, proceed with existing bmp}}+final now = DateTime.now();final fileName =- 'clipboard_${now.year}-${now.month.twoDigitString}-${now.day.twoDigitString}_${now.hour.twoDigitString}-${now.minute.twoDigitString}.${determineImageType(image!)}';+ 'clipboard_${now.year}-${now.month.twoDigitString}-${now.day.twoDigitString}_${now.hour.twoDigitString}-${now.minute.twoDigitString}.$imageType';ref.redux(selectedSendingFilesProvider).dispatch(AddBinaryAction(- bytes: image,+ bytes: currImage,fileType: FileType.image,fileName: fileName,));