Working on file upload

This commit is contained in:
Ruben van de Ven 2019-01-23 22:38:27 +01:00
parent 3cc1586ed2
commit e53753734a
5 changed files with 74 additions and 37 deletions

View file

@ -53,6 +53,7 @@ class CentralCommand(object):
self.ctx = Context.instance() self.ctx = Context.instance()
self.hugveyLock = asyncio.Lock() self.hugveyLock = asyncio.Lock()
self.start_time = time.time() self.start_time = time.time()
self.languageFiles = {}
def loadConfig(self, filename): def loadConfig(self, filename):
if hasattr(self, 'config'): if hasattr(self, 'config'):
@ -69,6 +70,7 @@ class CentralCommand(object):
for lang in self.config['languages']: for lang in self.config['languages']:
lang_filename = os.path.join(self.config['web']['files_dir'], lang['file']) lang_filename = os.path.join(self.config['web']['files_dir'], lang['file'])
self.languageFiles[lang['code']] = lang['file']
with open(lang_filename, 'r') as fp: with open(lang_filename, 'r') as fp:
self.languages[lang['code']] = yaml.load(fp) self.languages[lang['code']] = yaml.load(fp)

View file

@ -4,6 +4,8 @@ The panopticon provides a way to observe (& control) all running Hugveys trough
import logging import logging
import tornado import tornado
import string
import random
import tornado.websocket import tornado.websocket
import tornado.web import tornado.web
@ -13,6 +15,7 @@ from pytz.reference import Central
import asyncio import asyncio
import json import json
from urllib.parse import urlparse from urllib.parse import urlparse
from hugvey import central_command
logger = logging.getLogger("panopticon") logger = logging.getLogger("panopticon")
@ -87,6 +90,41 @@ def getWebSocketHandler(central_command):
return WebSocketHandler return WebSocketHandler
def getUploadHandler(central_command):
class UploadHandler(tornado.web.RequestHandler):
def post(self):
print('upload')
langCode = self.get_argument("language")
langFile = os.path.join(central_command.config['web']['files_dir'] , central_command.languageFiles[langCode])
print(self.request.files['json'][0])
storyData = json.loads(self.request.files['json'][0]['body'])
print(storyData)
if 'audio' in self.request.files:
msgId = self.get_argument("message_id")
audioFile = self.request.files['audio'][0]
original_fname = audioFile['filename']
fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(10))
ext = os.path.splitext(original_fname)[1]
audioFilename = os.path.join(central_command.config['web']['files_dir'], fname + ext)
for i, data in enumerate(storyData):
if data['@id'] != msgId:
continue
storyData[i]['audio'] = {
'file': audioFilename,
'original_name': original_fname
}
with open(audioFilename, 'r') as fp:
logger.info(f'Save {original_fname} to {audioFilename}')
# fp.write(audioFile['body'])
break
with open(langFile, 'r') as fp:
logger.info(f'Save story to {langFile}')
# json.dump(storyData, fp)
self.finish()
return UploadHandler
class Panopticon(object): class Panopticon(object):
def __init__(self, central_command, config): def __init__(self, central_command, config):
@ -96,6 +134,7 @@ class Panopticon(object):
(r"/ws", getWebSocketHandler(self.command)), (r"/ws", getWebSocketHandler(self.command)),
(r"/local/(.*)", tornado.web.StaticFileHandler, (r"/local/(.*)", tornado.web.StaticFileHandler,
{"path": config['web']['files_dir']}), {"path": config['web']['files_dir']}),
(r"/upload", getUploadHandler(self.command)),
(r"/(.*)", tornado.web.StaticFileHandler, (r"/(.*)", tornado.web.StaticFileHandler,
{"path": web_dir, "default_filename": 'index.html'}), {"path": web_dir, "default_filename": 'index.html'}),
], debug=True) ], debug=True)

3
local/.gitignore vendored
View file

@ -1,3 +0,0 @@
*
!.gitignore

View file

@ -1,28 +0,0 @@
{
"compilerOptions": {
"declaration": true,
"emitDecoratorMetadata": false,
"experimentalDecorators": false,
"module": "es2015",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": false,
"noImplicitAny": false,
"noImplicitReturns": false,
"outDir": "www/js",
"removeComments": false,
"sourceMap": true,
"strictNullChecks": false,
"target": "es2015",
"watch": true,
// this enables stricter inference for data properties on `this`
"strict": true,
"plugins": [
{
"name": "tslint-language-service"
}
],
"rootDir": "www"
},
"compileOnSave": true,
"buildOnSave": true
}

View file

@ -90,8 +90,7 @@ class Panopticon {
let req = new XMLHttpRequest(); let req = new XMLHttpRequest();
let graph = this.graph; let graph = this.graph;
req.addEventListener("load", function(e){ req.addEventListener("load", function(e){
console.log('TEST',this); graph.loadData(JSON.parse(this.response), code);
graph.loadData(JSON.parse(this.response));
// console.log(, e); // console.log(, e);
}); });
req.open("GET", "/local/" + file); req.open("GET", "/local/" + file);
@ -125,6 +124,7 @@ class Graph{
this.svg = d3.select('#graph'); this.svg = d3.select('#graph');
this.container = d3.select('#container'); this.container = d3.select('#container');
this.selectedMsg = null; this.selectedMsg = null;
this.language_code = null;
this.messages = []; // initialise empty array. For the simulation, make sure we keep the same array object this.messages = []; // initialise empty array. For the simulation, make sure we keep the same array object
this.directions = []; // initialise empty array. For the simulation, make sure we keep the same array object this.directions = []; // initialise empty array. For the simulation, make sure we keep the same array object
this.conditions = []; // initialise empty array. For the simulation, make sure we keep the same array object this.conditions = []; // initialise empty array. For the simulation, make sure we keep the same array object
@ -558,7 +558,11 @@ class Graph{
return JSON.stringify(d); return JSON.stringify(d);
} }
saveJson() { downloadJson() {
if(!this.language_code) {
alert("Make sure to load a language first")
}
var blob = new Blob([this.getJsonString()], {type: 'application/json'}); var blob = new Blob([this.getJsonString()], {type: 'application/json'});
if(window.navigator.msSaveOrOpenBlob) { if(window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, "pillow_talk.json"); window.navigator.msSaveBlob(blob, "pillow_talk.json");
@ -573,7 +577,30 @@ class Graph{
} }
} }
loadData(data) { saveJson(msg_id, fileInputElement) {
if(!this.language_code) {
alert("Make sure to load a language first")
}
let formData = new FormData();
formData.append("language", this.language_code);
if(msg_id) {
formData.append("message_id", msg_id);
formData.append("audio", fileInputElement.files[0]);
}
let blob = new Blob([this.getJsonString()], { type: "application/json"});
formData.append("json", blob);
console.log(formData);
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:8888/upload");
request.send(formData);
}
loadData(data, language_code) {
this.language_code = language_code;
this.data = data; this.data = data;
this.updateFromData(); this.updateFromData();
this.build(true); this.build(true);