From c4a74c526d26e65d4f2bc4a005548fa23cf6e843 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 01:31:11 -0700 Subject: [PATCH 1/5] tweak registerProtocolHandler API --- main/register-protocol-handler.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/main/register-protocol-handler.js b/main/register-protocol-handler.js index 9b46f6b1..88e7cf5b 100644 --- a/main/register-protocol-handler.js +++ b/main/register-protocol-handler.js @@ -2,7 +2,7 @@ var config = require('../config') module.exports = function () { if (process.platform === 'win32') { - registerProtocolHandler('magnet', 'BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath) + registerProtocolHandler('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath) registerFileHandler('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath) } } @@ -12,13 +12,15 @@ module.exports = function () { * registry: * * HKEY_CLASSES_ROOT - * $PROTOCOL - * (Default) = "URL:$NAME" - * URL Protocol = "" - * shell - * open - * command - * (Default) = "$COMMAND" "%1" + * $PROTOCOL + * (Default) = "$NAME" + * URL Protocol = "" + * DefaultIcon + * (Default) = "$ICON" + * shell + * open + * command + * (Default) = "$COMMAND" "%1" * * Source: https://msdn.microsoft.com/en-us/library/aa767914.aspx * @@ -34,7 +36,7 @@ function registerProtocolHandler (protocol, name, icon, command) { hive: Registry.HKCU, // HKEY_CURRENT_USER key: '\\Software\\Classes\\' + protocol }) - protocolKey.set('', Registry.REG_SZ, 'URL:' + name, callback) + protocolKey.set('', Registry.REG_SZ, name, callback) protocolKey.set('URL Protocol', Registry.REG_SZ, '', callback) var iconKey = new Registry({ From 5cb0e6940fdfce804f845c67f0108d2ed420dc11 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 02:48:19 -0700 Subject: [PATCH 2/5] register-handlers.js --- main/{register-protocol-handler.js => register-handlers.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main/{register-protocol-handler.js => register-handlers.js} (100%) diff --git a/main/register-protocol-handler.js b/main/register-handlers.js similarity index 100% rename from main/register-protocol-handler.js rename to main/register-handlers.js From 9f46da54775b93be68f92a106aaa6437f8e67f4b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 03:19:32 -0700 Subject: [PATCH 3/5] Linux: Register .torrent file/magnet link handlers --- config.js | 4 +++- main/index.js | 2 +- main/register-handlers.js | 27 +++++++++++++++++++++++---- static/webtorrent.desktop | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 static/webtorrent.desktop diff --git a/config.js b/config.js index 7ded0738..2ff8438a 100644 --- a/config.js +++ b/config.js @@ -15,6 +15,8 @@ module.exports = { IS_PRODUCTION: isProduction(), + STATIC_PATH: pathToStatic(), + SOUND_ADD: 'file://' + path.join(__dirname, 'static', 'sound', 'add.wav'), SOUND_DELETE: 'file://' + path.join(__dirname, 'static', 'sound', 'delete.wav'), SOUND_DISABLE: 'file://' + path.join(__dirname, 'static', 'sound', 'disable.wav'), @@ -36,7 +38,7 @@ function isProduction () { return !/\\electron\.exe$/.test(process.execPath) } if (process.platform === 'linux') { - // TODO + return !/\/electron$/.test(process.execPath) } } diff --git a/main/index.js b/main/index.js index 9a6cae7d..d95109ed 100644 --- a/main/index.js +++ b/main/index.js @@ -5,7 +5,7 @@ var app = electron.app var config = require('../config') var ipc = require('./ipc') var menu = require('./menu') -var registerProtocolHandler = require('./register-protocol-handler') +var registerProtocolHandler = require('./register-handlers') var shortcuts = require('./shortcuts') var windows = require('./windows') diff --git a/main/register-handlers.js b/main/register-handlers.js index 88e7cf5b..14db50de 100644 --- a/main/register-handlers.js +++ b/main/register-handlers.js @@ -2,9 +2,28 @@ var config = require('../config') module.exports = function () { if (process.platform === 'win32') { - registerProtocolHandler('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath) - registerFileHandler('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath) + registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath) + registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath) } + if (process.platform === 'linux') { + installDesktopFile() + } +} + +function installDesktopFile () { + var fs = require('fs') + var path = require('path') + var os = require('os') + + var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop') + var desktopFile = fs.readFileSync(templatePath, 'utf8') + + desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) + desktopFile = desktopFile.replace(/\$APP_PATH/g, path.dirname(process.execPath)) + desktopFile = desktopFile.replace(/\$EXEC_PATH/g, process.execPath) + + var desktopFilePath = path.join(os.homedir(), '.local', 'share', 'applications', 'webtorrent.desktop') + fs.writeFileSync(desktopFilePath, desktopFile) } /** @@ -29,7 +48,7 @@ module.exports = function () { * "HKEY_CLASSES_ROOT" anyway, and can be written by unprivileged users. */ -function registerProtocolHandler (protocol, name, icon, command) { +function registerProtocolHandlerWin32 (protocol, name, icon, command) { var Registry = require('winreg') var protocolKey = new Registry({ @@ -56,7 +75,7 @@ function registerProtocolHandler (protocol, name, icon, command) { } } -function registerFileHandler (ext, id, name, icon, command) { +function registerFileHandlerWin32 (ext, id, name, icon, command) { var Registry = require('winreg') var extKey = new Registry({ diff --git a/static/webtorrent.desktop b/static/webtorrent.desktop new file mode 100644 index 00000000..41ffe369 --- /dev/null +++ b/static/webtorrent.desktop @@ -0,0 +1,16 @@ +[Desktop Entry] +Name=$APP_NAME +Version=1.0 +GenericName=BitTorrent Client +X-GNOME-FullName=$APP_NAME +Comment=Download and share files over BitTorrent +Encoding=UTF-8 +Type=Application +Icon=webtorrent +Terminal=false +Path=$APP_PATH +Exec=$EXEC_PATH %U +TryExec=$EXEC_PATH +StartupNotify=false +Categories=Network;FileTransfer;P2P; +MimeType=application/x-bittorrent;x-scheme-handler/magnet; From 8ae727db035630bf56609f069966370332da67ed Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 03:29:02 -0700 Subject: [PATCH 4/5] rely on electron's automatic ASAR handling --- config.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/config.js b/config.js index 2ff8438a..1fb24458 100644 --- a/config.js +++ b/config.js @@ -3,8 +3,8 @@ var path = require('path') module.exports = { APP_COPYRIGHT: 'Copyright © 2014-2016 The WebTorrent Project', - APP_FILE_ICON: path.join(pathToStatic(), 'WebTorrentFile'), - APP_ICON: path.join(pathToStatic(), 'WebTorrent'), + APP_FILE_ICON: path.join(__dirname, 'static', 'WebTorrentFile'), + APP_ICON: path.join(__dirname, 'static', 'WebTorrent'), APP_NAME: 'WebTorrent', CONFIG_PATH: applicationConfigPath('WebTorrent'), @@ -15,7 +15,7 @@ module.exports = { IS_PRODUCTION: isProduction(), - STATIC_PATH: pathToStatic(), + STATIC_PATH: path.join(__dirname, 'static'), SOUND_ADD: 'file://' + path.join(__dirname, 'static', 'sound', 'add.wav'), SOUND_DELETE: 'file://' + path.join(__dirname, 'static', 'sound', 'delete.wav'), @@ -41,9 +41,3 @@ function isProduction () { return !/\/electron$/.test(process.execPath) } } - -function pathToStatic () { - return isProduction() - ? path.join(process.resourcesPath, 'app.asar.unpacked', 'static') - : path.join(__dirname, 'static') -} From c11ebf30bdab94435c5dcce8e209aacead65f06b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 03:30:39 -0700 Subject: [PATCH 5/5] Only use 'app.asar.unpacked' path where it is needed --- main/register-handlers.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main/register-handlers.js b/main/register-handlers.js index 14db50de..cce2e20d 100644 --- a/main/register-handlers.js +++ b/main/register-handlers.js @@ -1,9 +1,9 @@ -var config = require('../config') - module.exports = function () { if (process.platform === 'win32') { - registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath) - registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath) + var path = require('path') + var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') + registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, process.execPath) + registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, process.execPath) } if (process.platform === 'linux') { installDesktopFile() @@ -11,6 +11,7 @@ module.exports = function () { } function installDesktopFile () { + var config = require('../config') var fs = require('fs') var path = require('path') var os = require('os')