Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(magnet): default app for opening magnet URLs #70

Merged
merged 1 commit into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ mac:
fileAssociations:
ext: torrent
icon: resources/file-moose.icns
protocols:
name: open-magnet
schemes:
- magnet
dmg:
# https://kilianvalkhof:com/2019/electron/notarizing-your-electron-application/:
sign: false
13 changes: 13 additions & 0 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ app.commandLine.appendSwitch("enable-experimental-web-platform-features");
app.name = name;
let win: BrowserWindow;
let path: string;
let magnetUri: string;

if (app.isPackaged) {
unhandled();
Expand Down Expand Up @@ -67,6 +68,9 @@ async function _createWindow() {
if (path) {
params.append("path", encodeURI(path));
}
if (magnetUri) {
params.append("magnet", encodeURI(magnetUri));
}
const query = params.toString();

if (app.isPackaged) {
Expand Down Expand Up @@ -119,3 +123,12 @@ app.on("open-file", (_e, _path) => {
path = _path;
}
});

app.setAsDefaultProtocolClient("magnet");
app.on("open-url", (e, url) => {
if (app.isReady()) {
win.webContents.send("magnet-opened", url);
} else {
magnetUri = url;
}
});
45 changes: 35 additions & 10 deletions renderer/components/Container/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { DownloadingTorrent } from "../../../types/DownloadingTorrent";
import { ipcRenderer, remote } from "electron";
import { Message } from "@components/Message";
import { SelectedCastContext } from "@contexts/SelectedCast";
import { Toast } from "@components/Toast";
import { showToast, Toast } from "@components/Toast";
import { getDefaultColor } from "@utils/theme";
import { parseFileInfo } from "@utils/parseFileInfo";
import fs from "fs";
import MagnetUri from "magnet-uri";
import ParseTorrent from "parse-torrent";

const { searchParams } = new URL(window.location.href);
const path = decodeURI(searchParams.get("path"));
const magnetUri = decodeURI(searchParams.get("magnet"));

export default function () {
const [selectedTorrent, setSelectedTorrent] = useState<DownloadingTorrent>(
Expand Down Expand Up @@ -74,15 +77,15 @@ export default function () {
[store]
);

const loadFile = useCallback((path: string) => {
if (!path) return;
fs.readFile(path, async (err, data) => {
if (err) return;
onFileSelect(await parseFileInfo(data));
});
}, []);

useEffect(() => {
function loadFile(path: string) {
if (!path) return;
fs.readFile(path, async (err, data) => {
if (err) return;
onFileSelect(await parseFileInfo(data));
});
}

loadFile(path);

function loadFileWhenAppIsOpen(e, path: string) {
Expand All @@ -93,7 +96,29 @@ export default function () {
return () => {
ipcRenderer.off("file-opened", loadFileWhenAppIsOpen);
};
}, [loadFile]);
}, []);

useEffect(() => {
function loadMagnetUri(_magnetUri: string) {
if (!_magnetUri) return;
try {
const { name, infoHash }: MagnetUri.Instance = ParseTorrent(_magnetUri);
onFileSelect({ name: name as string, magnet: _magnetUri, infoHash });
} catch (e) {
showToast(`${_magnetUri} is an invalid magnet url.`);
}
}

loadMagnetUri(magnetUri);
function loadMagnetWhenAppIsOpen(e, url: string) {
loadMagnetUri(url);
}
ipcRenderer.on("magnet-opened", loadMagnetWhenAppIsOpen);

return () => {
ipcRenderer.off("magnet-opened", loadMagnetWhenAppIsOpen);
};
}, []);

useEffect(() => {
store.set("torrents", downloads);
Expand Down