Skip to content

An open-source cross-platform alternative to AirDrop

feat: paste image (#792)

Tien Do Nama93155b3c9c1
diff --git a/app/lib/model/cross_file.dart b/app/lib/model/cross_file.dart
index 177f1834..614cbdae 100644
--- a/app/lib/model/cross_file.dart
+++ b/app/lib/model/cross_file.dart
@@ -27,4 +27,10 @@ class CrossFile with CrossFileMappable {
required this.path,
required this.bytes,
});
+
+ /// Custom toString() to avoid printing the bytes.
+ @override
+ String toString() {
+ return 'CrossFile(name: $name, fileType: $fileType, size: $size, thumbnail: ${thumbnail != null ? thumbnail!.length : 'null'}, asset: $asset, path: $path, bytes: ${bytes != null ? bytes!.length : 'null'})';
+ }
}
diff --git a/app/lib/model/state/send/send_session_state.dart b/app/lib/model/state/send/send_session_state.dart
index bfd54539..3ed2bbdf 100644
--- a/app/lib/model/state/send/send_session_state.dart
+++ b/app/lib/model/state/send/send_session_state.dart
@@ -31,4 +31,12 @@ class SendSessionState with SendSessionStateMappable {
required this.cancelToken,
required this.errorMessage,
});
+
+ /// Custom toString() to avoid printing the bytes.
+ /// The default toString() does not respect the overridden toString() of
+ /// SendingFile.
+ @override
+ String toString() {
+ return 'SendSessionState(sessionId: $sessionId, remoteSessionId: $remoteSessionId, background: $background, status: $status, target: $target, files: $files, startTime: $startTime, endTime: $endTime, cancelToken: $cancelToken, errorMessage: $errorMessage)';
+ }
}
diff --git a/app/lib/model/state/send/sending_file.dart b/app/lib/model/state/send/sending_file.dart
index 82db2e4e..8f8516ad 100644
--- a/app/lib/model/state/send/sending_file.dart
+++ b/app/lib/model/state/send/sending_file.dart
@@ -1,3 +1,5 @@
+import 'dart:typed_data';
+
import 'package:dart_mappable/dart_mappable.dart';
import 'package:localsend_app/model/dto/file_dto.dart';
import 'package:localsend_app/model/file_status.dart';
@@ -10,6 +12,7 @@ class SendingFile with SendingFileMappable {
final FileDto file;
final FileStatus status;
final String? token;
+ final Uint8List? thumbnail;
final AssetEntity? asset; // for thumbnails
final String? path; // android, iOS, desktop
final List<int>? bytes; // web
@@ -19,9 +22,16 @@ class SendingFile with SendingFileMappable {
required this.file,
required this.status,
required this.token,
+ required this.thumbnail,
required this.asset,
required this.path,
required this.bytes,
required this.errorMessage,
});
+
+ /// Custom toString() to avoid printing the bytes.
+ @override
+ String toString() {
+ return 'SendingFile(file: $file, status: $status, token: $token, thumbnail: ${thumbnail != null ? thumbnail!.length : 'null'}, asset: $asset, path: $path, bytes: ${bytes != null ? bytes!.length : 'null'}, errorMessage: $errorMessage)';
+ }
}
diff --git a/app/lib/model/state/send/sending_file.mapper.dart b/app/lib/model/state/send/sending_file.mapper.dart
index db84401b..1e9e5923 100644
--- a/app/lib/model/state/send/sending_file.mapper.dart
+++ b/app/lib/model/state/send/sending_file.mapper.dart
@@ -31,6 +31,9 @@ class SendingFileMapper extends ClassMapperBase<SendingFile> {
Field('status', _$status);
static String? _$token(SendingFile v) => v.token;
static const Field<SendingFile, String> _f$token = Field('token', _$token);
+ static Uint8List? _$thumbnail(SendingFile v) => v.thumbnail;
+ static const Field<SendingFile, Uint8List> _f$thumbnail =
+ Field('thumbnail', _$thumbnail);
static AssetEntity? _$asset(SendingFile v) => v.asset;
static const Field<SendingFile, AssetEntity> _f$asset =
Field('asset', _$asset);
@@ -47,6 +50,7 @@ class SendingFileMapper extends ClassMapperBase<SendingFile> {
#file: _f$file,
#status: _f$status,
#token: _f$token,
+ #thumbnail: _f$thumbnail,
#asset: _f$asset,
#path: _f$path,
#bytes: _f$bytes,
@@ -58,6 +62,7 @@ class SendingFileMapper extends ClassMapperBase<SendingFile> {
file: data.dec(_f$file),
status: data.dec(_f$status),
token: data.dec(_f$token),
+ thumbnail: data.dec(_f$thumbnail),
asset: data.dec(_f$asset),
path: data.dec(_f$path),
bytes: data.dec(_f$bytes),
@@ -118,6 +123,7 @@ abstract class SendingFileCopyWith<$R, $In extends SendingFile, $Out>
{FileDto? file,
FileStatus? status,
String? token,
+ Uint8List? thumbnail,
AssetEntity? asset,
String? path,
List<int>? bytes,
@@ -144,6 +150,7 @@ class _SendingFileCopyWithImpl<$R, $Out>
{FileDto? file,
FileStatus? status,
Object? token = $none,
+ Object? thumbnail = $none,
Object? asset = $none,
Object? path = $none,
Object? bytes = $none,
@@ -152,6 +159,7 @@ class _SendingFileCopyWithImpl<$R, $Out>
if (file != null) #file: file,
if (status != null) #status: status,
if (token != $none) #token: token,
+ if (thumbnail != $none) #thumbnail: thumbnail,
if (asset != $none) #asset: asset,
if (path != $none) #path: path,
if (bytes != $none) #bytes: bytes,
@@ -162,6 +170,7 @@ class _SendingFileCopyWithImpl<$R, $Out>
file: data.get(#file, or: $value.file),
status: data.get(#status, or: $value.status),
token: data.get(#token, or: $value.token),
+ thumbnail: data.get(#thumbnail, or: $value.thumbnail),
asset: data.get(#asset, or: $value.asset),
path: data.get(#path, or: $value.path),
bytes: data.get(#bytes, or: $value.bytes),
diff --git a/app/lib/pages/progress_page.dart b/app/lib/pages/progress_page.dart
index 13bdcfa1..040eb674 100644
--- a/app/lib/pages/progress_page.dart
+++ b/app/lib/pages/progress_page.dart
@@ -1,4 +1,5 @@
import 'dart:async';
+import 'dart:typed_data';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
@@ -24,6 +25,7 @@ import 'package:localsend_app/widget/file_thumbnail.dart';
import 'package:refena_flutter/refena_flutter.dart';
import 'package:routerino/routerino.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
+import 'package:wechat_assets_picker/wechat_assets_picker.dart';
class ProgressPage extends StatefulWidget {
final bool showAppBar;
@@ -244,6 +246,16 @@ class _ProgressPageState extends State<ProgressPage> with Refena {
errorMessage = null;
}
+ final Uint8List? thumbnail;
+ final AssetEntity? asset;
+ if (sendSession != null) {
+ thumbnail = sendSession.files[file.id]!.thumbnail;
+ asset = sendSession.files[file.id]!.asset;
+ } else {
+ thumbnail = null;
+ asset = null;
+ }
+
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5),
child: InkWell(
@@ -255,17 +267,12 @@ class _ProgressPageState extends State<ProgressPage> with Refena {
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
- if (sendSession != null && sendSession.files[file.id]?.asset != null)
- // Special handling for assets
- AssetThumbnail(
- asset: sendSession.files[file.id]!.asset!,
- fileType: file.fileType,
- )
- else
- FilePathThumbnail(
- path: filePath,
- fileType: file.fileType,
- ),
+ SmartFileThumbnail(
+ bytes: thumbnail,
+ asset: asset,
+ path: filePath,
+ fileType: file.fileType,
+ ),
const SizedBox(width: 10),
Expanded(
child: Column(
diff --git a/app/lib/pages/selected_files_page.dart b/app/lib/pages/selected_files_page.dart
index b6748d20..4669bf42 100644
--- a/app/lib/pages/selected_files_page.dart
+++ b/app/lib/pages/selected_files_page.dart
@@ -85,7 +85,7 @@ class SelectedFilesPage extends StatelessWidget {
padding: const EdgeInsets.all(10),
child: Row(
children: [
- CrossFileThumbnail(file),
+ SmartFileThumbnail.fromCrossFile(file),
const SizedBox(width: 10),
Expanded(
child: Column(
diff --git a/app/lib/pages/tabs/send_tab.dart b/app/lib/pages/tabs/send_tab.dart
index bd5aef19..38c25960 100644
--- a/app/lib/pages/tabs/send_tab.dart
+++ b/app/lib/pages/tabs/send_tab.dart
@@ -79,7 +79,7 @@ class _SendTabState extends State<SendTab> with Refena {
icon: option.icon,
label: option.label,
filled: false,
- onTap: () async => ref.dispatchAsync(PickAction(
+ onTap: () async => ref.dispatchAsync(PickFileAction(
option: option,
context: context,
)),
@@ -112,7 +112,7 @@ class _SendTabState extends State<SendTab> with Refena {
children: [
...vm.selectedFiles.map((file) {
return [
- CrossFileThumbnail(file),
+ SmartFileThumbnail.fromCrossFile(file),
const SizedBox(width: 10),
];
}).expand((e) => e),
@@ -141,7 +141,7 @@ class _SendTabState extends State<SendTab> with Refena {
onPressed: () async {
if (options.length == 1) {
// open directly
- await ref.dispatchAsync(PickAction(
+ await ref.dispatchAsync(PickFileAction(
option: options.first,
context: context,
));
diff --git a/app/lib/provider/network/send_provider.dart b/app/lib/provider/network/send_provider.dart
index a03ba805..556bb1ce 100644
--- a/app/lib/provider/network/send_provider.dart
+++ b/app/lib/provider/network/send_provider.dart
@@ -86,6 +86,7 @@ class SendNotifier extends Notifier<Map<String, SendSessionState>> {
),
status: FileStatus.queue,
token: null,
+ thumbnail: file.thumbnail,
asset: file.asset,
path: file.path,
bytes: file.bytes,
diff --git a/app/lib/provider/selection/selected_sending_files_provider.dart b/app/lib/provider/selection/selected_sending_files_provider.dart
index 5a232676..92234717 100644
--- a/app/lib/provider/selection/selected_sending_files_provider.dart
+++ b/app/lib/provider/selection/selected_sending_files_provider.dart
@@ -1,5 +1,6 @@
import 'dart:convert' show utf8;
import 'dart:io';
+import 'dart:typed_data';
import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/model/file_type.dart';
@@ -24,6 +25,7 @@ class SelectedSendingFilesNotifier extends ReduxNotifier<List<CrossFile>> {
List<CrossFile> init() => [];
}
+/// Adds a message.
class AddMessageAction extends ReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> {
final String message;
final int? index;
@@ -52,6 +54,7 @@ class AddMessageAction extends ReduxAction<SelectedSendingFilesNotifier, List<Cr
}
}
+/// Updates a message.
class UpdateMessageAction extends ReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> {
final String message;
final int index;
@@ -69,6 +72,39 @@ class UpdateMessageAction extends ReduxAction<SelectedSendingFilesNotifier, List
}
}
+/// Adds a binary file to the list.
+/// During the sending process, the file will be read from the memory.
+class AddBinaryAction extends ReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> {
+ final Uint8List bytes;
+ final FileType fileType;
+ final String fileName;
+
+ AddBinaryAction({
+ required this.bytes,
+ required this.fileType,
+ required this.fileName,
+ });
+
+ @override
+ List<CrossFile> reduce() {
+ final file = CrossFile(
+ name: fileName,
+ fileType: fileType,
+ size: bytes.length,
+ thumbnail: fileType == FileType.image ? bytes : null,
+ asset: null,
+ path: null,
+ bytes: bytes,
+ );
+
+ return List.unmodifiable([
+ ...state,
+ file,
+ ]);
+ }
+}
+
+/// Adds one or more files to the list.
class AddFilesAction<T> extends AsyncReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> {
final Iterable<T> files;
final Future<CrossFile> Function(T) converter;
@@ -128,6 +164,7 @@ class AddDirectoryAction extends AsyncReduxAction<SelectedSendingFilesNotifier,
}
}
+/// Removes a file at the given [index].
class RemoveSelectedFileAction extends ReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> with GlobalActions {
final int index;
@@ -146,6 +183,7 @@ class RemoveSelectedFileAction extends ReduxAction<SelectedSendingFilesNotifier,
}
}
+/// Removes all files from the list.
class ClearSelectionAction extends ReduxAction<SelectedSendingFilesNotifier, List<CrossFile>> with GlobalActions {
@override
List<CrossFile> reduce() {
diff --git a/app/lib/util/determine_image_type.dart b/app/lib/util/determine_image_type.dart
new file mode 100644
index 00000000..fc0f9051
--- /dev/null
+++ b/app/lib/util/determine_image_type.dart
@@ -0,0 +1,21 @@
+import 'dart:typed_data';
+
+/// Returns the image type (PNG, JPEG, GIF, etc) of the given image data.
+/// Usually based on the first few bytes of the image.
+String determineImageType(Uint8List bytes) {
+ if (bytes.lengthInBytes >= 2) {
+ if (bytes[0] == 0xFF && bytes[1] == 0xD8) {
+ return 'jpeg';
+ }
+ if (bytes[0] == 0x89 && bytes[1] == 0x50) {
+ return 'png';
+ }
+ if (bytes[0] == 0x47 && bytes[1] == 0x49) {
+ return 'gif';
+ }
+ if (bytes[0] == 0x42 && bytes[1] == 0x4D) {
+ return 'bmp';
+ }
+ }
+ return 'png';
+}
diff --git a/app/lib/util/file_path_helper.dart b/app/lib/util/file_path_helper.dart
index 5a04382f..11cbd154 100644
--- a/app/lib/util/file_path_helper.dart
+++ b/app/lib/util/file_path_helper.dart
@@ -51,6 +51,7 @@ extension FilePathStringExt on String {
FileType guessFileType() {
switch (extension) {
+ case 'bmp':
case 'jpg':
case 'jpeg':
case 'heic':
diff --git a/app/lib/util/native/file_picker.dart b/app/lib/util/native/file_picker.dart
index 7f137edd..8c616775 100644
--- a/app/lib/util/native/file_picker.dart
+++ b/app/lib/util/native/file_picker.dart
@@ -6,9 +6,11 @@ import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:localsend_app/gen/strings.g.dart';
+import 'package:localsend_app/model/file_type.dart' as file_type;
import 'package:localsend_app/pages/apk_picker_page.dart';
import 'package:localsend_app/provider/selection/selected_sending_files_provider.dart';
import 'package:localsend_app/theme.dart';
+import 'package:localsend_app/util/determine_image_type.dart';
import 'package:localsend_app/util/native/cross_file_converters.dart';
import 'package:localsend_app/util/native/pick_directory_path.dart';
import 'package:localsend_app/util/native/platform_check.dart';
@@ -88,11 +90,11 @@ enum FilePickerOption {
}
}
-class PickAction extends AsyncGlobalAction {
+class PickFileAction extends AsyncGlobalAction {
final FilePickerOption option;
final BuildContext context;
- PickAction({
+ PickFileAction({
required this.option,
required this.context,
});
@@ -256,6 +258,19 @@ Future<void> _pickClipboard(BuildContext context, Ref ref) async {
return;
}
+ final image = await Pasteboard.image;
+ if (image != null) {
+ final now = DateTime.now();
+ final fileName =
+ 'clipboard_${now.year}-${now.month.twoDigitString}-${now.day.twoDigitString}_${now.hour.twoDigitString}-${now.minute.twoDigitString}.${determineImageType(image)}';
+ ref.redux(selectedSendingFilesProvider).dispatch(AddBinaryAction(
+ bytes: image,
+ fileType: file_type.FileType.image,
+ fileName: fileName,
+ ));
+ return;
+ }
+
if (!context.mounted) {
return;
}
@@ -269,3 +284,7 @@ Future<void> _pickApp(BuildContext context) async {
// Currently, only Android APK
await context.push(() => const ApkPickerPage());
}
+
+extension on int {
+ String get twoDigitString => toString().padLeft(2, '0');
+}
diff --git a/app/lib/widget/dialogs/add_file_dialog.dart b/app/lib/widget/dialogs/add_file_dialog.dart
index cebdb6a9..6e2da934 100644
--- a/app/lib/widget/dialogs/add_file_dialog.dart
+++ b/app/lib/widget/dialogs/add_file_dialog.dart
@@ -62,7 +62,7 @@ class AddFileDialog extends StatelessWidget {
filled: true,
onTap: () async {
context.popUntilRoot();
- await context.ref.dispatchAsync(PickAction(option: option, context: context));
+ await context.ref.dispatchAsync(PickFileAction(option: option, context: context));
},
);
}),
diff --git a/app/lib/widget/file_thumbnail.dart b/app/lib/widget/file_thumbnail.dart
index 292d3fef..ab87e98d 100644
--- a/app/lib/widget/file_thumbnail.dart
+++ b/app/lib/widget/file_thumbnail.dart
@@ -6,27 +6,44 @@ import 'package:localsend_app/model/cross_file.dart';
import 'package:localsend_app/model/file_type.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
-class CrossFileThumbnail extends StatelessWidget {
- final CrossFile file;
+class SmartFileThumbnail extends StatelessWidget {
+ final Uint8List? bytes;
+ final AssetEntity? asset;
+ final String? path;
+ final FileType fileType;
+
+ const SmartFileThumbnail({
+ required this.bytes,
+ required this.asset,
+ required this.path,
+ required this.fileType,
+ });
- const CrossFileThumbnail(this.file);
+ factory SmartFileThumbnail.fromCrossFile(CrossFile file) {
+ return SmartFileThumbnail(
+ bytes: file.thumbnail,
+ asset: file.asset,
+ path: file.path,
+ fileType: file.fileType,
+ );
+ }
@override
Widget build(BuildContext context) {
- if (file.thumbnail != null) {
+ if (bytes != null) {
return MemoryThumbnail(
- bytes: file.thumbnail!,
- fileType: file.fileType,
+ bytes: bytes,
+ fileType: fileType,
);
- } else if (file.asset != null) {
+ } else if (asset != null) {
return AssetThumbnail(
- asset: file.asset!,
- fileType: file.fileType,
+ asset: asset!,
+ fileType: fileType,
);
} else {
return FilePathThumbnail(
- path: file.path,
- fileType: file.fileType,
+ path: path,
+ fileType: fileType,
);
}
}
diff --git a/app/lib/widget/watcher/shortcut_watcher.dart b/app/lib/widget/watcher/shortcut_watcher.dart
index 8c322327..22678a25 100644
--- a/app/lib/widget/watcher/shortcut_watcher.dart
+++ b/app/lib/widget/watcher/shortcut_watcher.dart
@@ -34,7 +34,7 @@ class ShortcutWatcher extends StatelessWidget {
_ExitAppIntent: CallbackAction(onInvoke: (_) => exit(0)),
_PopPageIntent: CallbackAction(onInvoke: (_) async => Navigator.of(Routerino.context).maybePop()),
_PasteIntent: CallbackAction(onInvoke: (_) async {
- await context.ref.dispatchAsync(PickAction(option: FilePickerOption.clipboard, context: context));
+ await context.ref.dispatchAsync(PickFileAction(option: FilePickerOption.clipboard, context: context));
return null;
}),
},