Merge pull request #164 from feross/osx-protocol-handler

OS X: open magnet links in WebTorrent
This commit is contained in:
Feross Aboukhadijeh
2016-03-19 17:16:58 -07:00
3 changed files with 47 additions and 25 deletions

View File

@@ -147,14 +147,36 @@ function buildDarwin (cb) {
'static', 'static',
'WebTorrentFile.icns' 'WebTorrentFile.icns'
) )
var infoPlist = plist.parse(fs.readFileSync(infoPlistPath).toString()) var infoPlist = plist.parse(fs.readFileSync(infoPlistPath, 'utf8'))
infoPlist['CFBundleDocumentTypes'] = [{ infoPlist.CFBundleDocumentTypes = [
CFBundleTypeExtensions: [ 'torrent' ], {
CFBundleTypeName: 'BitTorrent Document', CFBundleTypeExtensions: [ 'torrent' ],
CFBundleTypeRole: 'Editor', CFBundleTypeIconFile: 'WebTorrentFile.icns',
CFBundleTypeIconFile: 'WebTorrentFile.icns' CFBundleTypeName: 'BitTorrent Document',
}] CFBundleTypeRole: 'Editor',
LSHandlerRank: 'Owner',
LSItemContentTypes: [ 'org.bittorrent.torrent' ]
},
{
CFBundleTypeName: 'Any',
CFBundleTypeOSTypes: [ '****' ],
CFBundleTypeRole: 'Editor',
LSHandlerRank: 'Owner',
LSTypeIsPackage: false
}
]
infoPlist.CFBundleURLTypes = [
{
CFBundleTypeRole: 'Editor',
CFBundleURLIconFile: 'WebTorrentFile.icns',
CFBundleURLName: 'BitTorrent Magnet URL',
CFBundleURLSchemes: [ 'magnet' ]
}
]
infoPlist.NSHumanReadableCopyright = 'Copyright © 2014-2016 The WebTorrent Project'
fs.writeFileSync(infoPlistPath, plist.build(infoPlist)) fs.writeFileSync(infoPlistPath, plist.build(infoPlist))
cp.execSync(`cp ${webTorrentFileIconPath} ${resourcesPath}`) cp.execSync(`cp ${webTorrentFileIconPath} ${resourcesPath}`)

View File

@@ -40,13 +40,13 @@ ipc.init()
function onOpen (e, torrentId) { function onOpen (e, torrentId) {
e.preventDefault() e.preventDefault()
console.log(app.ipcReady)
if (app.ipcReady) { if (app.ipcReady) {
openFiles() onReadyOpen()
} else { } else {
app.on('ipcReady', openFiles) app.on('ipcReady', onReadyOpen)
} }
function openFiles () { function onReadyOpen () {
windows.main.send('dispatch', 'openFiles', torrentId) windows.main.send('dispatch', 'onOpen', torrentId)
windows.main.focus()
} }
} }

View File

@@ -87,7 +87,7 @@ function init () {
Cast.init(update) Cast.init(update)
// ...drag and drop a torrent or video file to play or seed // ...drag and drop a torrent or video file to play or seed
dragDrop('body', (files) => dispatch('openFiles', files)) dragDrop('body', (files) => dispatch('onOpen', files))
// ...same thing if you paste a torrent // ...same thing if you paste a torrent
document.addEventListener('paste', onPaste) document.addEventListener('paste', onPaste)
@@ -157,8 +157,8 @@ function dispatch (action, ...args) {
if (['videoMouseMoved', 'playbackJump'].indexOf(action) < 0) { if (['videoMouseMoved', 'playbackJump'].indexOf(action) < 0) {
console.log('dispatch: %s %o', action, args) /* log user interactions, but don't spam */ console.log('dispatch: %s %o', action, args) /* log user interactions, but don't spam */
} }
if (action === 'openFiles') { if (action === 'onOpen') {
openFiles(args[0] /* files */) onOpen(args[0] /* files */)
} }
if (action === 'addTorrent') { if (action === 'addTorrent') {
addTorrent(args[0] /* torrent */) addTorrent(args[0] /* torrent */)
@@ -324,16 +324,16 @@ function updateClientProgress () {
state.dock.progress = progress state.dock.progress = progress
} }
function openFiles (files) { function onOpen (files) {
if (!Array.isArray(files)) files = [ files ] if (!Array.isArray(files)) files = [ files ]
// .torrent file = start downloading the torrent // .torrent file = start downloading the torrent
files.filter(isTorrentFile).forEach(function (torrentFile) { files.filter(isTorrent).forEach(function (torrentFile) {
addTorrent(torrentFile) addTorrent(torrentFile)
}) })
// everything else = seed these files // everything else = seed these files
seed(files.filter(isNotTorrentFile)) seed(files.filter(isNotTorrent))
} }
function onPaste (e) { function onPaste (e) {
@@ -347,14 +347,15 @@ function onPaste (e) {
}) })
} }
function isTorrentFile (file) { function isTorrent (file) {
var name = typeof file === 'string' ? file : file.name var name = typeof file === 'string' ? file : file.name
var extname = path.extname(name).toLowerCase() var isTorrentFile = path.extname(name).toLowerCase() === '.torrent'
return extname === '.torrent' var isMagnet = typeof file === 'string' && /^magnet:/.test(file)
return isTorrentFile || isMagnet
} }
function isNotTorrentFile (file) { function isNotTorrent (file) {
return !isTorrentFile(file) return !isTorrent(file)
} }
// Gets a torrent summary {name, infoHash, status} from state.saved.torrents // Gets a torrent summary {name, infoHash, status} from state.saved.torrents
@@ -462,8 +463,7 @@ function generateTorrentPoster (torrent, torrentSummary) {
torrentPoster(torrent, function (err, buf) { torrentPoster(torrent, function (err, buf) {
if (err) return onWarning(err) if (err) return onWarning(err)
// save it for next time // save it for next time
fs.mkdir(config.CONFIG_POSTER_PATH, function (err) { fs.mkdir(config.CONFIG_POSTER_PATH, function (_) {
if (err) return onWarning(err)
var posterFilePath = path.join(config.CONFIG_POSTER_PATH, torrent.infoHash + '.jpg') var posterFilePath = path.join(config.CONFIG_POSTER_PATH, torrent.infoHash + '.jpg')
fs.writeFile(posterFilePath, buf, function (err) { fs.writeFile(posterFilePath, buf, function (err) {
if (err) return onWarning(err) if (err) return onWarning(err)