diff --git a/src/index.js b/src/index.js index 1e08329..9e349e8 100644 --- a/src/index.js +++ b/src/index.js @@ -33,3 +33,7 @@ ws_init(() => { , root); }); + +navigator.registerProtocolHandler("magnet", + window.location.origin + "/add-torrent/%s", + "Open magnet link with receptor"); diff --git a/src/ui/add_torrent.js b/src/ui/add_torrent.js index f525ad4..1845ce9 100644 --- a/src/ui/add_torrent.js +++ b/src/ui/add_torrent.js @@ -144,6 +144,8 @@ class AddTorrent extends Component { loading: false, customize: false, file: null, + magnet: null, + useMagnet: false, torrent: null, files: [], startImmediately: true, @@ -153,6 +155,14 @@ class AddTorrent extends Component { }; } + componentDidMount() { + let { magnet } = this.props.match.params; + if (magnet) { + magnet = decodeURIComponent(magnet); + this.setState({ magnet, useMagnet: true }); + } + } + async handleTransferOffer(offer, file) { const headers = new Headers(); headers.append("Authorization", "Bearer " + offer.token); @@ -200,16 +210,13 @@ class AddTorrent extends Component { uploadFile() { this.setState({ loading: true }); - const { file, startImmediately } = this.state; + const { magnet, file, startImmediately } = this.state; const { dispatch } = this.props; const customize = // TODO: File options this.state.priority !== 3 || this.state.uploadThrottle !== -1 || this.state.downloadThrottle !== -1; - ws_send("UPLOAD_TORRENT", { - size: file.size, - start: startImmediately && !customize - }, async offer => { + const handleOffer = async offer => { switch (offer.type) { case "TRANSFER_OFFER": return await this.handleTransferOffer(offer, file); @@ -218,7 +225,18 @@ class AddTorrent extends Component { this.applyOptions.bind(this)(id); break; } - }); + }; + if (magnet) { + ws_send("UPLOAD_MAGNET", { + uri: magnet, + start: startImmediately && !customize + }, handleOffer); + } else { + ws_send("UPLOAD_TORRENT", { + size: file.size, + start: startImmediately && !customize + }, handleOffer); + } } processTorrent(torrent) { @@ -303,7 +321,7 @@ class AddTorrent extends Component { } renderTorrent() { - const { torrent, file, files, loading } = this.state; + const { magnet, torrent, file, files, loading } = this.state; const details = { "comment": d => d, @@ -313,35 +331,39 @@ class AddTorrent extends Component { return ( - - {file.name} - -
- {Object.keys(details).map(key => - torrent[key] && ( -
-
{key}
-
{details[key](torrent[key])}
-
- ) - )} -
-
-
+ {torrent && + + {magnet && "Magnet link" || file.name} + +
+ {Object.keys(details).map(key => + torrent[key] && ( +
+
{key}
+
{details[key](torrent[key])}
+
+ ) + )} +
+
+
+ } {this.renderOptions.bind(this)()} - - - - TODO - - - + {torrent && + + + + TODO + + + + } {loading ? @@ -360,7 +382,14 @@ class AddTorrent extends Component { return (

Add torrent

- {this.state.torrent && this.renderTorrent.bind(this)()} + {this.state.magnet && this.state.useMagnet && +

{this.state.magnet}

+ } + {(this.state.torrent || this.state.magnet) && this.renderTorrent.bind(this)()}
- {this.state.torrent ? + {!this.state.useMagnet && (this.state.torrent ?
Select a different torrent?
: - - } +
+ +
+ - or - +
+ + this.setState({ magnet: e.target.value })} + /> + + +
+ )}
); diff --git a/src/ui/main.js b/src/ui/main.js index 9b8c8ee..dc206f9 100644 --- a/src/ui/main.js +++ b/src/ui/main.js @@ -13,7 +13,8 @@ export default class Main extends Component {
- + +