Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Change "Save Torrent File" button to "Copy Magnet Link" for magnet links
Browse files Browse the repository at this point in the history
Fixes: #7772
  • Loading branch information
feross committed Mar 20, 2017
1 parent fad4acf commit e770256
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
3 changes: 2 additions & 1 deletion app/extensions/torrent/locales/en-US/app.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
startPrompt=Start Downloading "{{name}}"?
startPromptUntitled=Start Downloading?
startDownload=Start Download
saveTorrentFile=Save Torrent File
saveTorrentFile=Save Torrent File...
legalNotice=When you start a torrent, its data will be made available to others by means of upload. You are responsible for abiding by your local laws.
missingFilesList=Click "Start Download" to load the torrent file list.
loadingFilesList=Loading the torrent file list...
Expand All @@ -18,3 +18,4 @@ downloadFile=Save File
torrentStatus=Torrent Status
torrentLoadingInfo=Loading torrent info...
torrentLoadingMedia=Loading...
copyMagnetLink=Copy Magnet Link
2 changes: 2 additions & 0 deletions js/webtorrent/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class App extends React.Component {
ix,
name,
torrentId,
torrentIdProtocol,
torrent,
serverUrl,
errorMessage
Expand All @@ -21,6 +22,7 @@ class App extends React.Component {
<TorrentViewer
name={name}
torrentId={torrentId}
torrentIdProtocol={torrentIdProtocol}
torrent={torrent}
serverUrl={serverUrl}
errorMessage={errorMessage}
Expand Down
27 changes: 22 additions & 5 deletions js/webtorrent/components/torrentViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TorrentViewer extends React.Component {
torrent,
serverUrl,
errorMessage,
torrentIdProtocol,
dispatch
} = this.props

Expand All @@ -44,6 +45,24 @@ class TorrentViewer extends React.Component {
mainButtonId = 'startDownload'
}

if (torrentIdProtocol === 'magnet:') {
saveButton = (
<Button
l10nId='copyMagnetLink'
className='whiteButton copyMagnetLink'
onClick={() => dispatch('copyMagnetLink')}
/>
)
} else {
saveButton = (
<Button
l10nId='saveTorrentFile'
className='whiteButton saveTorrentFile'
onClick={() => dispatch('saveTorrentFile')}
/>
)
}

const legalNotice = torrent != null
? <a className='legalNotice' data-l10n-id='poweredByWebTorrent' href='https://webtorrent.io' target='_blank' />
: <div className='legalNotice' data-l10n-id='legalNotice' />
Expand All @@ -57,11 +76,9 @@ class TorrentViewer extends React.Component {
l10nId={mainButtonId}
className='primaryButton mainButton'
disabled={!!torrent}
onClick={() => dispatch('start')} />
<Button
l10nId='saveTorrentFile'
className='whiteButton saveTorrentFile'
onClick={() => dispatch('saveTorrentFile')} />
onClick={() => dispatch('start')}
/>
{saveButton}
</div>
</div>

Expand Down
49 changes: 28 additions & 21 deletions js/webtorrent/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global Blob, URL */

const ipc = window.chrome.ipcRenderer

const clipboardCopy = require('clipboard-copy')
const parseTorrent = require('parse-torrent')
const path = require('path')
const querystring = require('querystring')
Expand All @@ -21,6 +21,7 @@ const store = {
name: null, // Torrent name
ix: null, // Selected file index
torrentId: window.decodeURIComponent(window.location.hash.substring(1)),
torrentIdProtocol: null,
torrent: null,
serverUrl: null,
errorMessage: null
Expand All @@ -32,13 +33,14 @@ init()

function init () {
const parsedUrl = url.parse(store.torrentId)
store.torrentIdProtocol = parsedUrl.protocol

// `ix` param can be set by query param or hash, e.g. ?ix=1 or #ix=1
if (parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') {
store.name = path.basename(parsedUrl.pathname)
store.ix = getIxQuery(parsedUrl)
if (store.ix == null) store.ix = getIxHash(parsedUrl)
} else {
} else if (parsedUrl.protocol === 'magnet:') {
let parsedTorrent
try {
parsedTorrent = parseTorrent(store.torrentId)
Expand All @@ -48,6 +50,8 @@ function init () {
store.name = parsedTorrent.name
store.ix = parsedTorrent.ix
if (store.ix == null) store.ix = getIxHash(parsedUrl)
} else {
return onError(new Error('Invalid torrent identifier'))
}

// Create the client, set up IPC to the WebTorrentRemoteServer
Expand All @@ -63,12 +67,15 @@ function init () {
client.get(store.torrentId, function (err, torrent) {
if (!err) {
store.torrent = torrent
addTorrentEvents(torrent)
initTorrent(torrent)
}
update()
})

// Clean up the client before the window exits
// Clean up the client. Note: Since this does IPC, it's not guaranteed to send
// before the page is closed. But that's okay; webtorrent-remote expects regular
// heartbeats and assumes clients are dead after 30s without one. So basically,
// this is an optimization to destroy the client sooner.
window.addEventListener('beforeunload', function () {
client.destroy()
})
Expand Down Expand Up @@ -105,14 +112,8 @@ function update () {
function onAdded (err, torrent) {
if (err) return onError(err)

// Once torrent's canonical name is available, use it
if (torrent.name) store.name = torrent.name

store.torrent = torrent
addTorrentEvents(torrent)

server = torrent.createServer()
server.listen(onServerListening)
initTorrent(torrent)

update()
}
Expand All @@ -122,10 +123,16 @@ function onServerListening () {
update()
}

function addTorrentEvents (torrent) {
function initTorrent (torrent) {
// Once torrent's canonical name is available, use it
if (torrent.name) store.name = torrent.name

torrent.on('warning', onWarning)
torrent.on('error', onError)

server = torrent.createServer()
server.listen(onServerListening)

// These event listeners aren't strictly required, but it's better to update the
// UI immediately when important events happen instead of waiting for the regular
// update() call that happens on a 1 second interval.
Expand All @@ -140,6 +147,8 @@ function dispatch (action) {
return start()
case 'saveTorrentFile':
return saveTorrentFile()
case 'copyMagnetLink':
return copyMagnetLink()
default:
console.error('Ignoring unknown dispatch type: ' + JSON.stringify(action))
}
Expand All @@ -150,19 +159,17 @@ function start () {
}

function saveTorrentFile () {
let torrentFileName = store.name + '.torrent'
let torrentFileBlobURL = URL.createObjectURL(
new Blob([store.torrent.torrentFile],
{ type: 'application/x-bittorrent' }
))

let a = document.createElement('a')
a.target = '_blank'
a.download = torrentFileName
a.href = torrentFileBlobURL
a.download = true
a.href = store.torrentId
a.click()
}

function copyMagnetLink () {
clipboardCopy(store.torrentId)
}

function onWarning (err) {
console.warn(err.message)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"ad-block": "^2.0.0",
"aphrodite": "^1.0.0",
"async": "^2.0.1",
"clipboard-copy": "^1.0.0",
"electron-localshortcut": "^0.6.0",
"electron-prebuilt": "brave/electron-prebuilt",
"electron-squirrel-startup": "brave/electron-squirrel-startup",
Expand Down

0 comments on commit e770256

Please sign in to comment.