Handle nautilus-sendto links (#2291)

* Handle nautilus-sendto links

* Add handling for thunar sendto links
This commit is contained in:
Janosch Maier 2021-03-26 16:08:58 +01:00 committed by GitHub
parent 63889c4a14
commit 36e33ecfa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -144,8 +144,9 @@ const parseCommandLine = argv => {
const specFilePattern = args['spec-file-pattern'];
const showSpecsInWindow = specMode === 'window';
const resourcePath = path.normalize(path.resolve(path.dirname(path.dirname(__dirname))));
const urlsToOpen = [];
const pathsToOpen = [];
let urlsToOpen = [];
let pathsToOpen = [];
let mailtoLink;
// On Windows and Linux, mailto and file opens are passed in argv. Go through
// the items and pluck out things that look like mailto:, mailspring:, file paths
@ -165,9 +166,20 @@ const parseCommandLine = argv => {
continue;
}
if (arg.startsWith('mailto:') || arg.startsWith('mailspring:')) {
urlsToOpen.push(arg);
// Handle nautilus-sendto links correctly
mailtoLink = extractMailtoLink(arg);
urlsToOpen = urlsToOpen.concat(mailtoLink.urlsToOpen);
pathsToOpen = pathsToOpen.concat(mailtoLink.pathsToOpen);
} else if (arg[0] !== '-' && /[/|\\]/.test(arg)) {
pathsToOpen.push(arg);
if (arg.startsWith('?')) {
// Handle thunar-sendto links correctly by giving them a similar form
// as the nautilus-sendto links by adding a leading `mailto`
mailtoLink = extractMailtoLink('mailto:' + arg);
urlsToOpen = urlsToOpen.concat(mailtoLink.urlsToOpen);
pathsToOpen = pathsToOpen.concat(mailtoLink.pathsToOpen);
} else {
pathsToOpen.push(arg);
}
}
}
@ -193,6 +205,36 @@ const parseCommandLine = argv => {
};
};
const extractMailtoLink = (mailtoLink) => {
console.log(mailtoLink)
// Handle links in the form mailto:test@example.com?attach=file:///path/to/file.txt
// This will handle links e.g. for nautilus-sendto and attach the attachments correctly.
// Attachments currently cannot be attached to mails with a recipient,
// so if a recipient and an attachment is given two mail windows are opened.
let mailCreated = false;
const urlsToOpen = [];
const pathsToOpen = [];
const mailtoUrl = new URL(mailtoLink);
mailtoUrl.searchParams.forEach((value, key) => {
if (key === "attach") {
// We need to strip the leading `file://` in order to detect the files
pathsToOpen.push(value.replace(/^file:\/\//, ""));
mailCreated = true;
}
})
// Check if another draft window should be opened if there is a recipient set
// Prevents duplicate draft window for links such as mailto:?attach=file:///path/to/file.txt
if (!mailCreated || (mailtoUrl.pathname !== '')) {
urlsToOpen.push(mailtoLink);
}
return { urlsToOpen, pathsToOpen }
}
/*
* "Squirrel will spawn your app with command line flags on first run, updates,]
* and uninstalls."