diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-06-02 13:40:43 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-06-02 13:40:43 +0200 |
| commit | 015c9b3e7ec8b20308fc604d925a6e9c188aa42e (patch) | |
| tree | 54dab9950e6dd73e1333f2e080cb123d4c1d9c1e /app | |
| parent | a6a529a5757417906f6df60ca7557fac3a380966 (diff) | |
pull in proxy from stone island
Diffstat (limited to 'app')
| -rw-r--r-- | app/client/common/fileList.component.js | 2 | ||||
| -rw-r--r-- | app/client/dataset/dataset.actions.js | 65 | ||||
| -rw-r--r-- | app/client/dataset/dataset.component.js | 60 | ||||
| -rw-r--r-- | app/server/bridge.js | 2 | ||||
| -rw-r--r-- | app/server/proxy.js | 178 |
5 files changed, 251 insertions, 56 deletions
diff --git a/app/client/common/fileList.component.js b/app/client/common/fileList.component.js index ece77ae..87384b6 100644 --- a/app/client/common/fileList.component.js +++ b/app/client/common/fileList.component.js @@ -12,7 +12,7 @@ class FileList extends Component { } render(){ const { files, linkFiles, title, onClick, className="" } = this.props - if (!files.length) return null + if (!files || !files.length) return null let fields = this.props.fields || defaultFields if (typeof fields === 'string') { fields = new Set(fields.split(' ')) diff --git a/app/client/dataset/dataset.actions.js b/app/client/dataset/dataset.actions.js index f42e15c..983d586 100644 --- a/app/client/dataset/dataset.actions.js +++ b/app/client/dataset/dataset.actions.js @@ -1,12 +1,71 @@ // import socket from '../socket' import types from '../types' +import actions from '../actions' +import { parser } from '../api' + +export const createOrUpdateFolder = (module, folder) => dispatch => { + if (! folder || ! folder.id) { + actions.folder.create({ + // username... should get added inside the API + module: module.name, + datatype: module.datatype, + activity: 'dataset', + name + }).then(folder => { + // set current folder + }) + } + else { + actions.folder.update({ + id: folder.id, + module: module.name, + datatype: module.datatype, + activity: 'dataset', + name + }) + } +} + +export const uploadFile = (module, folder, file) => dispatch => { + const fd = new FormData() + fd.append('file', file) + actions.folder.upload(fd, { + id: folder.id, + module: module.name, + activity: 'file', + epoch: 0, + processed: false, + generated: false, + }) +} + +export const fetchURL = (module, folder, url) => { + // name url + // mime datatype + // duration analysis + // size activity + // opt created_at updated_at + parser.parse(url, media => { + if (!media) return + console.log('media', media) + actions.file.create({ + folder_id: folder.id, + module: module.name, + activity: 'url', + duration: parseInt(media.duration) || 0, + epoch: 0, + processed: false, + generated: false, + opt: media, + url + }) + }) +} + export const uploadFiles = (files) => { return { type: types.dataset.upload_files } } -export const fetchURL = (url) => { - return { type: types.dataset.fetch_url } -} // export const uploadFiles = (files) => { // return dispatch => { diff --git a/app/client/dataset/dataset.component.js b/app/client/dataset/dataset.component.js index 45f88dc..ebea8c9 100644 --- a/app/client/dataset/dataset.component.js +++ b/app/client/dataset/dataset.component.js @@ -2,7 +2,7 @@ import { h, Component } from 'preact' import { bindActionCreators } from 'redux' import { connect } from 'react-redux' -import { actions, parser } from '../api' +import * as datasetActions from './dataset.actions' import Group from '../common/group.component' import Param from '../common/param.component' @@ -19,59 +19,16 @@ class Dataset extends Component { this.pickFile = this.pickFile.bind(this) } handleName(name) { - const { module, folder } = this.props - if (! folder.id) { - this.props.actions.folder.create({ - // username... should get added inside the API - module: module.name, - datatype: module.datatype, - activity: 'dataset', - name - }) - } else { - this.props.actions.folder.update({ - id: folder.id, - module: module.name, - datatype: module.datatype, - activity: 'dataset', - name - }) - } + const { module, folder, actions } = this.props + actions.dataset.createOrUpdateFolder(module, folder) } handleUpload(file) { - const { module, folder } = this.props - const fd = new FormData() - fd.append('file', file) - this.props.actions.folder.upload(fd, { - id: folder.id, - module: module.name, - activity: 'file', - epoch: 0, - processed: false, - generated: false, - }) + const { module, folder, actions } = this.props + actions.dataset.uploadFile(module, folder, file) } handleURL(url) { - // name url - // mime datatype - // duration analysis - // size activity - // opt created_at updated_at - parser.parse(url, media => { - if (!media) return - console.log('media', media) - this.props.actions.file.create({ - folder_id: this.props.folder.id, - module: this.props.module.name, - activity: 'url', - duration: parseInt(media.duration) || 0, - epoch: 0, - processed: false, - generated: false, - opt: media, - url - }) - }) + const { module, folder, actions } = this.props + actions.dataset.fetchURL(module, folder, url) } pickFile(file){ console.log('pick', file) @@ -139,8 +96,7 @@ const mapStateToProps = state => state.dataset const mapDispatchToProps = (dispatch, ownProps) => ({ actions: { - folder: bindActionCreators(actions.folder, dispatch), - file: bindActionCreators(actions.file, dispatch), + dataset: bindActionCreators(datasetActions, dispatch), } }) diff --git a/app/server/bridge.js b/app/server/bridge.js index 9405dad..c2deb20 100644 --- a/app/server/bridge.js +++ b/app/server/bridge.js @@ -1,4 +1,5 @@ import { server, io } from './site' +import * as proxy from './proxy' let relay_connected = false @@ -10,6 +11,7 @@ export const relay = (() => { if (process.env.EXPRESS_CONNECTS_TO_RELAY === 'true') { console.log('Connecting to relay on ' + process.env.RELAY_REMOTE) relay = require('socket.io-client').connect(process.env.RELAY_REMOTE) + proxy.attach(server) bind_relay(relay) } else { relay = io.of('/relay') diff --git a/app/server/proxy.js b/app/server/proxy.js new file mode 100644 index 0000000..cebffb0 --- /dev/null +++ b/app/server/proxy.js @@ -0,0 +1,178 @@ +var http = require('http') +var https = require('https') +var path = require('path') +var fs = require('fs') +var url = require('url') + +var PORT = 9090 +var cache = {} + +var DEBUG = !!process.env.DEBUG_PROXY +// var USE_CACHE = true + +export const attach = server => { + console.log('attaching proxy') + var evs = server.listeners('request').slice(0) + server.removeAllListeners('request') + server.on('request', function(req, res) { + if (0 === req.url.indexOf('/api')) { + serve(req, res) + } else { + for (var i = 0; i < evs.length; i++) { + evs[i].call(server, req, res) + } + } + }) +} + +function serve(req, res) { + + if (DEBUG) { + console.log("\n\n___________________________") + } + + console.log(req.method, req.url) + + // if (USE_CACHE && req.method == 'GET' && req.url in cache) { + // res.writeHeader(200, { 'Content-type': 'application/json' }) + // res.end(cache[req.url]) + // return + // } + + // console.log(req.headers) + + var options = {} + options.headers = get_headers(req.headers) + options.method = req.method + options.port = process.env.SECURE_PROXY ? 443 : 80 + options.hostname = process.env.RELAY_REMOTE + options.path = req.url + + if (DEBUG) { + console.log(options.headers) + console.log(req.url) + } + + req.pause() + var connector = (process.env.SECURE_PROXY ? https : http).request(options, server_res => { + console.log('>> GOT', server_res.statusCode) + + server_res.pause() + res.writeHeader(server_res.statusCode, server_res.headers) + server_res.pipe(res) + + // if (! options.headers['x-yoox-cart-token']) { + // cache[req.url] = '' + // server_res.on('data', function(s){ + // cache[req.url] += s.toString() + // }) + // } + + if (DEBUG) { + server_res.on('data', s => console.log(s.toString())) + } + + server_res.resume() + }) + connector.on('error', e => console.error(e)) + if (DEBUG) { + req.on('data', s => console.log('>>', s.toString())) + } + req.on('error', s => { console.log('/!\\ ERROR /!\\'); console.log(s) }) + req.pipe(connector) + req.resume() + +} + +/* + headers: { + 'x-yoox-appname': auth.appname, + 'x-yoox-account-token': auth.access_token, + 'x-yoox-device': auth.device, + 'x-yoox-api-key': auth.apikey, + 'x-yoox-cart-token': cart.token, + }, +*/ + +function get_headers (h){ + var hh = {} + // 'appname account-token device api-key cart-token'.split(' ').forEach(function(s){ + // var key = 'x-yoox-' + s + // if (key in h) hh[key] = h[key] + // }) + 'cookie'.split(' ').forEach(key => { + if (key in h) hh[key] = h[key] + }) + hh['Content-Type'] = 'application/json' // h['content-type'] + if ('content-length' in h) hh['Content-Length'] = h['content-length'] + // if ('connection' in h) hh['Connection'] = h['connection'] + return hh +} + +// var mimes = { +// 'gif': 'image/gif', +// 'png': 'image/png', +// 'jpg': 'image/jpeg', +// 'jpeg': 'image/jpeg', +// 'html': 'text/html', +// 'js': 'application/javascript', +// 'css': 'text/css', +// 'woff': 'application/font-woff', +// 'ttf': 'application/font-sfnt', +// 'otf': 'application/font-sfnt', +// 'svg': 'image/svg+xml', +// } + +// function stream (req, res) { +// var url = req.url.split('?')[0] +// var ext_partz = url.toLowerCase().split('.'), ext = ext_partz[ext_partz.length-1] +// var mime = mimes[ext] || 'application/octet-stream' + +// if (! url || url == '/') { +// url = 'index.html' +// mime = 'text/html' +// } +// if (url == '/cordova.js' || url == '/favicon.ico') { +// res.writeHead(200, 'OK', { 'Content-type': 'application/javascript' }) +// res.end('{}') +// return +// } + +// var file = path.resolve('./StoneIsland/www/' + url) +// fs.stat(file, function(err, stats) { +// if (! stats) { +// // console.log(404) +// res.writeHead(404) +// res.end('404') +// return +// } + +// var headers = { +// 'Content-Length': stats.size, +// 'Content-Type': mime, +// } + +// res.writeHead(200, 'OK', headers) + +// var stream = fs.createReadStream(file, { start: 0, end: stats.size }) +// stream.on('open', function() { +// stream.pipe(res) +// }) +// stream.on('error', function(err) { +// console.log(err) +// stream.destroy() +// res.end() +// }) +// stream.on('close', function(){ +// stream.destroy() +// res.end() +// }) +// /* +// req.connection.setMaxListeners(50) +// req.connection.on('close', function(){ +// stream.destroy() +// res.end() +// }) +// */ +// }) +// } |
