Upload torrents based on socket URI

This commit is contained in:
Drew DeVault 2017-09-08 23:40:59 +09:00
parent f566414259
commit 716bffa3e6
4 changed files with 24 additions and 10 deletions

View File

@ -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 });

View File

@ -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));

View File

@ -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;
}

View File

@ -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/', {
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);