Skip to content

An open-source cross-platform alternative to AirDrop

refactor: drop open_dir dependency

Tien Do Nam22e5f1cbaac4
diff --git a/app/lib/util/native/open_folder.dart b/app/lib/util/native/open_folder.dart
index 1f05a6af..dfaef8f9 100644
--- a/app/lib/util/native/open_folder.dart
+++ b/app/lib/util/native/open_folder.dart
@@ -1,8 +1,9 @@
+import 'dart:io';
+
import 'package:flutter/foundation.dart';
import 'package:localsend_app/util/native/channel/android_channel.dart' as android_channel;
import 'package:localsend_app/util/native/platform_check.dart';
import 'package:logging/logging.dart';
-import 'package:open_dir/open_dir.dart';
import 'package:open_filex/open_filex.dart';
final _logger = Logger('OpenFolder');
@@ -24,16 +25,56 @@ Future<void> openFolder({
folderPath = folderPath.replaceAll('/', '\\');
}
- final result = await OpenDir().openNativeDir(path: folderPath, highlightedFileName: fileName);
+ final result = await _showInFileManager(folderPath: folderPath, fileName: fileName);
_logger.info('Open folder result: $result, path: $folderPath, file: $fileName');
- } else {
- // only open folder
-
- if (!folderPath.endsWith('/')) {
- folderPath = '$folderPath/';
+ if (result) {
+ return;
}
+ // fall through: open the folder without selecting the file
+ }
+
+ if (!folderPath.endsWith('/')) {
+ folderPath = '$folderPath/';
+ }
- final result = await OpenFilex.open(folderPath);
- _logger.info('Open folder result: ${result.message}, path: $folderPath');
+ final result = await OpenFilex.open(folderPath);
+ _logger.info('Open folder result: ${result.message}, path: $folderPath');
+}
+
+/// Opens [folderPath] in the platform's file manager with [fileName] selected.
+/// Arguments are passed as a list (no shell), so file names received from remote devices cannot inject commands.
+Future<bool> _showInFileManager({required String folderPath, required String fileName}) async {
+ final separator = defaultTargetPlatform == TargetPlatform.windows ? '\\' : '/';
+ final filePath = folderPath.endsWith(separator) ? '$folderPath$fileName' : '$folderPath$separator$fileName';
+ if (!File(filePath).existsSync()) {
+ return false;
+ }
+
+ try {
+ switch (defaultTargetPlatform) {
+ case TargetPlatform.macOS:
+ final result = await Process.run('open', ['-R', filePath]);
+ return result.exitCode == 0;
+ case TargetPlatform.windows:
+ // explorer.exe exit codes are meaningless (it usually returns 1 even on success)
+ await Process.run('explorer.exe', ['/select,', filePath]);
+ return true;
+ case TargetPlatform.linux:
+ final result = await Process.run('dbus-send', [
+ '--session',
+ '--print-reply',
+ '--dest=org.freedesktop.FileManager1',
+ '/org/freedesktop/FileManager1',
+ 'org.freedesktop.FileManager1.ShowItems',
+ 'array:string:${Uri.file(filePath).toString()}',
+ 'string:',
+ ]);
+ return result.exitCode == 0;
+ default:
+ return false;
+ }
+ } catch (e) {
+ _logger.warning('Failed to show file in file manager', e);
+ return false;
}
}
diff --git a/app/linux/flutter/generated_plugin_registrant.cc b/app/linux/flutter/generated_plugin_registrant.cc
index d4c9863b..00e865c3 100644
--- a/app/linux/flutter/generated_plugin_registrant.cc
+++ b/app/linux/flutter/generated_plugin_registrant.cc
@@ -10,7 +10,6 @@
#include <dynamic_color/dynamic_color_plugin.h>
#include <file_selector_linux/file_selector_plugin.h>
#include <gtk/gtk_plugin.h>
-#include <open_dir_linux/open_dir_linux_plugin.h>
#include <pasteboard/pasteboard_plugin.h>
#include <screen_retriever_linux/screen_retriever_linux_plugin.h>
#include <tray_manager/tray_manager_plugin.h>
@@ -32,9 +31,6 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) gtk_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "GtkPlugin");
gtk_plugin_register_with_registrar(gtk_registrar);
- g_autoptr(FlPluginRegistrar) open_dir_linux_registrar =
- fl_plugin_registry_get_registrar_for_plugin(registry, "OpenDirLinuxPlugin");
- open_dir_linux_plugin_register_with_registrar(open_dir_linux_registrar);
g_autoptr(FlPluginRegistrar) pasteboard_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "PasteboardPlugin");
pasteboard_plugin_register_with_registrar(pasteboard_registrar);
diff --git a/app/linux/flutter/generated_plugins.cmake b/app/linux/flutter/generated_plugins.cmake
index cb61d44c..abb3914a 100644
--- a/app/linux/flutter/generated_plugins.cmake
+++ b/app/linux/flutter/generated_plugins.cmake
@@ -7,7 +7,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
dynamic_color
file_selector_linux
gtk
- open_dir_linux
pasteboard
screen_retriever_linux
tray_manager
diff --git a/app/macos/Flutter/GeneratedPluginRegistrant.swift b/app/macos/Flutter/GeneratedPluginRegistrant.swift
index e2c19611..dd090376 100644
--- a/app/macos/Flutter/GeneratedPluginRegistrant.swift
+++ b/app/macos/Flutter/GeneratedPluginRegistrant.swift
@@ -14,7 +14,6 @@ import file_selector_macos
import gal
import in_app_purchase_storekit
import network_info_plus
-import open_dir_macos
import package_info_plus
import pasteboard
import photo_manager
@@ -37,7 +36,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
GalPlugin.register(with: registry.registrar(forPlugin: "GalPlugin"))
InAppPurchasePlugin.register(with: registry.registrar(forPlugin: "InAppPurchasePlugin"))
NetworkInfoPlusPlugin.register(with: registry.registrar(forPlugin: "NetworkInfoPlusPlugin"))
- OpenDirMacosPlugin.register(with: registry.registrar(forPlugin: "OpenDirMacosPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PasteboardPlugin.register(with: registry.registrar(forPlugin: "PasteboardPlugin"))
PhotoManagerPlugin.register(with: registry.registrar(forPlugin: "PhotoManagerPlugin"))
diff --git a/app/macos/Podfile.lock b/app/macos/Podfile.lock
index ad92fff7..10838cc8 100644
--- a/app/macos/Podfile.lock
+++ b/app/macos/Podfile.lock
@@ -21,8 +21,6 @@ PODS:
- FlutterMacOS
- network_info_plus (0.0.1):
- FlutterMacOS
- - open_dir_macos (0.0.1):
- - FlutterMacOS
- package_info_plus (0.0.1):
- FlutterMacOS
- pasteboard (0.0.1):
@@ -63,7 +61,6 @@ DEPENDENCIES:
- gal (from `Flutter/ephemeral/.symlinks/plugins/gal/darwin`)
- in_app_purchase_storekit (from `Flutter/ephemeral/.symlinks/plugins/in_app_purchase_storekit/darwin`)
- network_info_plus (from `Flutter/ephemeral/.symlinks/plugins/network_info_plus/macos`)
- - open_dir_macos (from `Flutter/ephemeral/.symlinks/plugins/open_dir_macos/macos`)
- package_info_plus (from `Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos`)
- pasteboard (from `Flutter/ephemeral/.symlinks/plugins/pasteboard/macos`)
- photo_manager (from `Flutter/ephemeral/.symlinks/plugins/photo_manager/darwin`)
@@ -102,8 +99,6 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/in_app_purchase_storekit/darwin
network_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/network_info_plus/macos
- open_dir_macos:
- :path: Flutter/ephemeral/.symlinks/plugins/open_dir_macos/macos
package_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos
pasteboard:
@@ -141,7 +136,6 @@ SPEC CHECKSUMS:
gal: baecd024ebfd13c441269ca7404792a7152fde89
in_app_purchase_storekit: 22cca7d08eebca9babdf4d07d0baccb73325d3c8
network_info_plus: 21d1cd6a015ccb2fdff06a1fbfa88d54b4e92f61
- open_dir_macos: 79810d7921d777f8732bf6358b3a0839a8fad9b5
package_info_plus: f0052d280d17aa382b932f399edf32507174e870
pasteboard: 278d8100149f940fb795d6b3a74f0720c890ecb7
photo_manager: c303e4476f6c220274119f0212a7753e45edd4a2
diff --git a/app/pubspec.yaml b/app/pubspec.yaml
index 947a6355..33d2ce83 100644
--- a/app/pubspec.yaml
+++ b/app/pubspec.yaml
@@ -43,7 +43,6 @@ dependencies:
moform: 0.2.11
nanoid2: 2.0.1
network_info_plus: 7.0.0
- open_dir: 0.0.2+1
open_filex: 4.7.0
package_info_plus: 9.0.1
pasteboard: 0.3.0
diff --git a/app/windows/flutter/generated_plugin_registrant.cc b/app/windows/flutter/generated_plugin_registrant.cc
index e1b56e62..2f9adc5b 100644
--- a/app/windows/flutter/generated_plugin_registrant.cc
+++ b/app/windows/flutter/generated_plugin_registrant.cc
@@ -11,7 +11,6 @@
#include <dynamic_color/dynamic_color_plugin_c_api.h>
#include <file_selector_windows/file_selector_windows.h>
#include <gal/gal_plugin_c_api.h>
-#include <open_dir_windows/open_dir_windows_plugin_c_api.h>
#include <pasteboard/pasteboard_plugin.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <screen_retriever_windows/screen_retriever_windows_plugin_c_api.h>
@@ -32,8 +31,6 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("FileSelectorWindows"));
GalPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("GalPluginCApi"));
- OpenDirWindowsPluginCApiRegisterWithRegistrar(
- registry->GetRegistrarForPlugin("OpenDirWindowsPluginCApi"));
PasteboardPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PasteboardPlugin"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
diff --git a/app/windows/flutter/generated_plugins.cmake b/app/windows/flutter/generated_plugins.cmake
index d9e680f0..130b2b99 100644
--- a/app/windows/flutter/generated_plugins.cmake
+++ b/app/windows/flutter/generated_plugins.cmake
@@ -8,7 +8,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
dynamic_color
file_selector_windows
gal
- open_dir_windows
pasteboard
permission_handler_windows
screen_retriever_windows
diff --git a/pubspec.lock b/pubspec.lock
index 0254dc93..d104c6dd 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -1022,46 +1022,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "9.5.0"
- open_dir:
- dependency: transitive
- description:
- name: open_dir
- sha256: a4884b00e5e5795a9b4b3d582ac6a66e9196795ed760dbc3c63b4837c70c5901
- url: "https://pub.dev"
- source: hosted
- version: "0.0.2+1"
- open_dir_linux:
- dependency: transitive
- description:
- name: open_dir_linux
- sha256: "566cd9e02403971be06af35e1abc8057a4f3f98888c1226042e96a2af333b8bc"
- url: "https://pub.dev"
- source: hosted
- version: "0.0.2+1"
- open_dir_macos:
- dependency: transitive
- description:
- name: open_dir_macos
- sha256: "51fdc8c3a06c9d571b599b5901045ada23d1440b24c3052c0a66cf3ee4ac901b"
- url: "https://pub.dev"
- source: hosted
- version: "0.0.2"
- open_dir_platform_interface:
- dependency: transitive
- description:
- name: open_dir_platform_interface
- sha256: ca189abb02d8e3320f9b2493b6d58e3a33f393d5eb4ccbbef02e0bc0fd393872
- url: "https://pub.dev"
- source: hosted
- version: "0.0.2"
- open_dir_windows:
- dependency: transitive
- description:
- name: open_dir_windows
- sha256: ec48df32ce61adb6f6cede0330d13b0d89714d2ee2df198f32ecd520e3a5d250
- url: "https://pub.dev"
- source: hosted
- version: "0.0.2+1"
open_filex:
dependency: transitive
description: