Skip to content

Commit

Permalink
fix: open with files somewhat working in linux
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Feb 11, 2024
1 parent 5090bf0 commit 5f846d7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 146 deletions.
125 changes: 0 additions & 125 deletions src-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"npm": "10.1.0",
"ws": "^8.13.0",
"lmdb": "^2.9.2",
"mime-types": "^2.1.35",
"open": "^10.0.3"
"mime-types": "^2.1.35"
}
}
96 changes: 86 additions & 10 deletions src-node/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const NodeConnector = require("./node-connector");
let openModule;
const { exec } = require('child_process');
const fs = require('fs').promises;
const path = require('path');

const UTILS_NODE_CONNECTOR = "ph_utils";
NodeConnector.createNodeConnector(UTILS_NODE_CONNECTOR, exports);

async function isDirectoryAsync(path) {
try {
const stat = await fs.stat(path);
return stat.isDirectory();
} catch (e) {
console.error(`Error accessing path "${path}":`, e);
return false; // Path does not exist or error occurred
}
}

async function getURLContent({url, options}) {
options = options || {
redirect: "follow",
Expand All @@ -23,16 +35,80 @@ async function setLocaleStrings(localStrings) {
exports.Strings = localStrings;
}

async function openURLInDefaultBrowser(url) {
if(!openModule){
openModule = await import('open');
}
if(url.startsWith("http://") || url.startsWith("https://")){
await openModule.default(url);
}
throw new Error("Only HTTP/S protocol is supported", url);
async function openURLInDefaultLinuxBrowser(url) {
return new Promise((resolve, reject)=>{
if(url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")){
const options = { cwd: '/tmp' };
exec(`xdg-open "${url}"`, options, (error) => {
if (error) {
reject(`Error opening URL: ${error}`);
} else {
resolve(`URL opened successfully: ${url}`);
}
});
return;
}
reject("Only HTTP/S protocol is supported:" + url);
});
}

async function xdgOpenDir(dir) {
return new Promise((resolve, reject)=>{
const options = { cwd: '/tmp' };
exec(`xdg-open "${dir}"`, options, (error) => {
if (error) {
reject(`Error opening URL: ${error}`);
} else {
resolve(`path opened successfully: ${dir}`);
}
});
return;
});
}

function openWithLinuxDBUS(fileOrFolderPath) {
return new Promise((resolve, reject)=>{
const dbusSendCommand = `dbus-send --session ` +
`--dest=org.freedesktop.FileManager1 ` +
`--type=method_call ` +
`/org/freedesktop/FileManager1 ` +
`org.freedesktop.FileManager1.ShowItems ` +
`array:string:"file:///${fileOrFolderPath}" string:""`;
const options = { cwd: '/tmp' };
exec(dbusSendCommand, options, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stderr) {
reject(error);
return;
}
resolve();
});
});
}

function showInLinuxFileExplorer(fileOrFolderPath) {
return new Promise((resolve, reject)=>{
openWithLinuxDBUS(fileOrFolderPath)
.then(resolve)
.catch(async ()=>{
// dbus error, happens with appimages deosnt deal with correct versions of dbus libs
// try xdg open
const isDir = await isDirectoryAsync(fileOrFolderPath);
if(isDir){
xdgOpenDir(fileOrFolderPath).then(resolve).catch(reject);
return;
}
// open the parent dir if file
const parentDir = path.dirname(fileOrFolderPath);
xdgOpenDir(parentDir).then(resolve).catch(reject);
});
});
}

exports.getURLContent = getURLContent;
exports.setLocaleStrings = setLocaleStrings;
exports.openURLInDefaultBrowser = openURLInDefaultBrowser;
exports.openURLInDefaultLinuxBrowser = openURLInDefaultLinuxBrowser;
exports.showInLinuxFileExplorer = showInLinuxFileExplorer;
34 changes: 28 additions & 6 deletions src/phoenix/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ async function openURLInPhoenixWindow(url, {
return nativeWindow;
}

function _linuxOpenFileOrFolderLocation(platformPath) {
const NodeUtils = window.NodeUtils;
return new Promise((resolve, reject)=>{
NodeUtils.showInLinuxFileExplorer(platformPath)
.then(resolve)
.catch((err)=>{
console.error("Error NodeUtils.showInLinuxFileExplorer, trying tauri api", err);
window.__TAURI__.tauri
.invoke('show_in_folder', {path: platformPath})
.then(resolve)
.catch((err1)=>{
// linux and appimages bad. Try to show parent dir with xdg open as file open failed
console.error("Failed to open NodeUtils.showInLinuxFileExplorer", err1);
});
});
});
}

Phoenix.app = {
getNodeState: function (cbfn){
cbfn(new Error('Node cannot be run in phoenix browser mode'));
Expand Down Expand Up @@ -328,10 +346,14 @@ Phoenix.app = {
return;
}
const platformPath = Phoenix.fs.getTauriPlatformPath(fullVFSPath);
window.__TAURI__.tauri
.invoke('show_in_folder', {path: platformPath})
.then(resolve)
.catch(reject);
const nodeReady = window.NodeUtils && window.NodeUtils.isNodeReady();
if(Phoenix.platform !== "linux" || !nodeReady) {
window.__TAURI__.tauri
.invoke('show_in_folder', {path: platformPath})
.then(resolve)
.catch(reject);
}
_linuxOpenFileOrFolderLocation(platformPath);
});
},
openURLInDefaultBrowser: function (url, tabIdentifier='_blank'){
Expand All @@ -345,10 +367,10 @@ Phoenix.app = {
return;
}
const NodeUtils = window.NodeUtils;
if(NodeUtils && NodeUtils.isNodeReady()){
if(NodeUtils && NodeUtils.isNodeReady() && Phoenix.platform === "linux"){
// node has first preference as appimages cannot work reliably with tauri open in browser xdg-open
// api across various linux distributions
NodeUtils.openURLInDefaultBrowser(url)
NodeUtils.openURLInDefaultLinuxBrowser(url)
.then(resolve)
.catch(reject);
return;
Expand Down
11 changes: 8 additions & 3 deletions src/utils/NodeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ define(function (require, exports, module) {
return true;
}

function openURLInDefaultBrowser(url) {
return utilsConnector.execPeer("openURLInDefaultBrowser", url);
function openURLInDefaultLinuxBrowser(url) {
return utilsConnector.execPeer("openURLInDefaultLinuxBrowser", url);
}

function showInLinuxFileExplorer(fileOrFolderPath) {
return utilsConnector.execPeer("showInLinuxFileExplorer", fileOrFolderPath);
}

if(NodeConnector.isNodeAvailable()) {
Expand All @@ -67,7 +71,8 @@ define(function (require, exports, module) {

exports.fetchURLText = fetchURLText;
exports.updateNodeLocaleStrings = updateNodeLocaleStrings;
exports.openURLInDefaultBrowser = openURLInDefaultBrowser;
exports.openURLInDefaultLinuxBrowser = openURLInDefaultLinuxBrowser;
exports.showInLinuxFileExplorer = showInLinuxFileExplorer;
exports.isNodeReady = NodeConnector.isNodeReady;

window.NodeUtils = exports;
Expand Down

0 comments on commit 5f846d7

Please sign in to comment.