From 716bffa3e677e38f0d17d41c883ea134a409c146 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 8 Sep 2017 23:40:59 +0900 Subject: [PATCH] Upload torrents based on socket URI --- src/actions/socket.js | 3 +++ src/index.js | 3 ++- src/reducers/socket.js | 9 ++++++--- src/ui/add_torrent.js | 19 +++++++++++++------ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/actions/socket.js b/src/actions/socket.js index 533719d..09c2c79 100644 --- a/src/actions/socket.js +++ b/src/actions/socket.js @@ -5,6 +5,9 @@ export const SOCKET_STATE = { }; export const SOCKET_UPDATE = "SOCKET_UPDATE"; +export const SOCKET_URI = "SOCKET_URI"; export const socket_update = (state, reason=null) => ({ type: SOCKET_UPDATE, state, reason }); + +export const socket_uri = uri => ({ type: SOCKET_URI, uri }); diff --git a/src/index.js b/src/index.js index 614f74a..956c363 100644 --- a/src/index.js +++ b/src/index.js @@ -10,13 +10,14 @@ import store, { create, history } from './store'; import scss from '../scss/main.scss'; import { ws_init } from './socket'; import { filter_subscribe } from './actions/filter_subscribe'; -import { socket_update, SOCKET_STATE } from './actions/socket'; +import { socket_uri, socket_update, SOCKET_STATE } from './actions/socket'; import Nav from './ui/navigation'; import Main from './ui/main'; import Connection from './ui/connection'; export function initialize(uri) { + store.dispatch(socket_uri(uri)); store.dispatch(socket_update(SOCKET_STATE.CONNECTING)); ws_init(uri, () => { store.dispatch(socket_update(SOCKET_STATE.CONNECTED)); diff --git a/src/reducers/socket.js b/src/reducers/socket.js index 378326e..3c69e38 100644 --- a/src/reducers/socket.js +++ b/src/reducers/socket.js @@ -1,13 +1,16 @@ -import { SOCKET_STATE, SOCKET_UPDATE } from '../actions/socket'; +import { SOCKET_STATE, SOCKET_UPDATE, SOCKET_URI } from '../actions/socket'; export default function socket(_state = { state: SOCKET_STATE.DISCONNECTED, - reason: null + reason: null, + uri: null }, action) { - const { state, reason } = action; + const { state, reason, uri } = action; switch (action.type) { case SOCKET_UPDATE: return { ..._state, state, reason }; + case SOCKET_URI: + return { ..._state, uri }; default: return _state; } diff --git a/src/ui/add_torrent.js b/src/ui/add_torrent.js index f47203a..656cd32 100644 --- a/src/ui/add_torrent.js +++ b/src/ui/add_torrent.js @@ -168,12 +168,19 @@ class AddTorrent extends Component { async handleTransferOffer(offer, file) { const headers = new Headers(); headers.append("Authorization", "Bearer " + offer.token); + const { socket } = this.props; + console.log(socket); + const a = document.createElement('a'); + a.href = socket.uri; + const url = (a.protocol === "ws:" ? "http://" : "https://") + a.host; try { - const resp = await fetch('http://localhost:8412/', { - method: 'POST', - body: file, - headers: headers - }); + const resp = await fetch(url, + { + method: 'POST', + body: file, + headers: headers + } + ); } catch (ex) { // TODO: something more useful console.log(ex); @@ -457,4 +464,4 @@ class AddTorrent extends Component { } } -export default connect()(AddTorrent); +export default connect(s => ({ socket: s.socket }))(AddTorrent);