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

OS X: open magnet links in WebTorrent #164

Merged
merged 3 commits into from
Mar 20, 2016
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
38 changes: 30 additions & 8 deletions bin/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,36 @@ function buildDarwin (cb) {
'static',
'WebTorrentFile.icns'
)
var infoPlist = plist.parse(fs.readFileSync(infoPlistPath).toString())

infoPlist['CFBundleDocumentTypes'] = [{
CFBundleTypeExtensions: [ 'torrent' ],
CFBundleTypeName: 'BitTorrent Document',
CFBundleTypeRole: 'Editor',
CFBundleTypeIconFile: 'WebTorrentFile.icns'
}]
var infoPlist = plist.parse(fs.readFileSync(infoPlistPath, 'utf8'))

infoPlist.CFBundleDocumentTypes = [
{
CFBundleTypeExtensions: [ 'torrent' ],
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))
cp.execSync(`cp ${webTorrentFileIconPath} ${resourcesPath}`)
Expand Down
10 changes: 5 additions & 5 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ ipc.init()

function onOpen (e, torrentId) {
e.preventDefault()
console.log(app.ipcReady)
if (app.ipcReady) {
openFiles()
onReadyOpen()
} else {
app.on('ipcReady', openFiles)
app.on('ipcReady', onReadyOpen)
}
function openFiles () {
windows.main.send('dispatch', 'openFiles', torrentId)
function onReadyOpen () {
windows.main.send('dispatch', 'onOpen', torrentId)
windows.main.focus()
}
}
26 changes: 13 additions & 13 deletions renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function init () {
Cast.init(update)

// ...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
document.addEventListener('paste', onPaste)
Expand Down Expand Up @@ -157,8 +157,8 @@ function dispatch (action, ...args) {
if (['videoMouseMoved', 'playbackJump'].indexOf(action) < 0) {
console.log('dispatch: %s %o', action, args) /* log user interactions, but don't spam */
}
if (action === 'openFiles') {
openFiles(args[0] /* files */)
if (action === 'onOpen') {
onOpen(args[0] /* files */)
}
if (action === 'addTorrent') {
addTorrent(args[0] /* torrent */)
Expand Down Expand Up @@ -324,16 +324,16 @@ function updateClientProgress () {
state.dock.progress = progress
}

function openFiles (files) {
function onOpen (files) {
if (!Array.isArray(files)) files = [ files ]

// .torrent file = start downloading the torrent
files.filter(isTorrentFile).forEach(function (torrentFile) {
files.filter(isTorrent).forEach(function (torrentFile) {
addTorrent(torrentFile)
})

// everything else = seed these files
seed(files.filter(isNotTorrentFile))
seed(files.filter(isNotTorrent))
}

function onPaste (e) {
Expand All @@ -347,14 +347,15 @@ function onPaste (e) {
})
}

function isTorrentFile (file) {
function isTorrent (file) {
var name = typeof file === 'string' ? file : file.name
var extname = path.extname(name).toLowerCase()
return extname === '.torrent'
var isTorrentFile = path.extname(name).toLowerCase() === '.torrent'
var isMagnet = typeof file === 'string' && /^magnet:/.test(file)
return isTorrentFile || isMagnet
}

function isNotTorrentFile (file) {
return !isTorrentFile(file)
function isNotTorrent (file) {
return !isTorrent(file)
}

// Gets a torrent summary {name, infoHash, status} from state.saved.torrents
Expand Down Expand Up @@ -462,8 +463,7 @@ function generateTorrentPoster (torrent, torrentSummary) {
torrentPoster(torrent, function (err, buf) {
if (err) return onWarning(err)
// save it for next time
fs.mkdir(config.CONFIG_POSTER_PATH, function (err) {
if (err) return onWarning(err)
fs.mkdir(config.CONFIG_POSTER_PATH, function (_) {
var posterFilePath = path.join(config.CONFIG_POSTER_PATH, torrent.infoHash + '.jpg')
fs.writeFile(posterFilePath, buf, function (err) {
if (err) return onWarning(err)
Expand Down