import React, { Component } from 'react'; import { findDOMNode } from 'react-dom'; import { connect } from 'react-redux'; import { push } from 'react-router-redux'; import { Progress, Card, CardBlock, CardTitle, CardText, FormGroup, Label, Input } from 'reactstrap'; import ToggleContainer from './toggle_container'; import fetch from 'isomorphic-fetch'; import bencode from 'bencode'; import ws_send from '../socket'; import { convertToBitrate } from '../bitrate'; class Throttle extends Component { constructor() { super(); this.state = { strategy: "global", unit: "MiB/s", limit: 1 }; this.setStrategy = this.setStrategy.bind(this); } invokeChange() { const { onChange } = this.props; if (!onChange) { return; } const { strategy, limit, unit } = this.state; switch (strategy) { case "global": onChange(null); break; case "unlimited": onChange(-1); break; default: onChange(convertToBitrate(limit, unit)); break; } } setStrategy(strategy) { this.setState({ strategy }); this.invokeChange(); } setLimit(limit) { if (limit <= 0) { this.setState({ limit: this.state.limit }); return; } this.setState({ limit }); this.invokeChange(); } setUnit(unit) { this.setState({ unit }); this.invokeChange(); } render() { const { legend, prop } = this.props; return (
{legend} {this.state.strategy === "custom" &&
this.setLimit(parseFloat(e.target.value))} /> this.setUnit(e.target.value)} >
}
); } } class AddTorrent extends Component { constructor() { super(); this.state = { loading: false, customize: false, file: null, torrent: null, files: [], startImmediately: true, uploadThrottle: -1, downloadThrottle: -1, priority: 3, }; } async handleTransferOffer(offer, file) { const headers = new Headers(); headers.append("Authorization", "Bearer " + offer.token); try { const resp = await fetch('http://localhost:8412/', { method: 'POST', body: file, headers: headers }); } catch (ex) { // TODO: something more useful console.log(ex); } } applyOptions(id) { const { priority, uploadThrottle, downloadThrottle } = this.state; const { dispatch } = this.props; const customize = // TODO: File options priority !== 3 || uploadThrottle !== -1 || downloadThrottle !== -1; if (!customize) { dispatch(push(`/torrents/${id}`)); return; } ws_send("UPDATE_RESOURCE", { resource: { id, priority, throttle_up: uploadThrottle, throttle_down: downloadThrottle } }, async done => { if (this.state.startImmediately) { ws_send("RESUME_TORRENT", { id }); } dispatch(push(`/torrents/${id}`)); }); } uploadFile() { this.setState({ loading: true }); const { 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 => { switch (offer.type) { case "TRANSFER_OFFER": return await this.handleTransferOffer(offer, file); case "RESOURCES_EXTANT": const [id] = offer.ids; this.applyOptions.bind(this)(id); break; } }); } processTorrent(torrent) { const { info } = torrent; if (!info.files) { this.setState({ files: [ { name: info.name, length: info.length } ], torrent }); } else { // TODO } } handleFile(e) { const file = e.target.files[0]; const reader = new FileReader(); reader.onload = e => { try { const torrent = bencode.decode(reader.result, 'utf8'); this.processTorrent.bind(this)(torrent); } catch (ex) { // TODO: something meaningful console.log(ex); } }; reader.readAsArrayBuffer(file); this.setState({ file }); } renderOptions() { return ( this.setState({ priority: parseInt(e.target.value)}) } > this.setState({ downloadThrottle: limit })} /> this.setState({ uploadThrottle: limit })} /> ); } renderTorrent() { const { torrent, file, files, loading } = this.state; const details = { "comment": d => d, "creation date": d => new Date(d * 1000).toDateString(), "created by": d => d }; return ( {file.name}
{Object.keys(details).map(key => torrent[key] && (
{key}
{details[key](torrent[key])}
) )}
{this.renderOptions.bind(this)()} TODO {loading ? : null}
); } render() { return (

Add torrent

{this.state.torrent && this.renderTorrent.bind(this)()}
{this.state.torrent ?
: }
); } } export default connect()(AddTorrent);