diff --git a/hugvey/central_command.py b/hugvey/central_command.py index 11b607b..a330e22 100644 --- a/hugvey/central_command.py +++ b/hugvey/central_command.py @@ -53,6 +53,7 @@ class CentralCommand(object): self.ctx = Context.instance() self.hugveyLock = asyncio.Lock() self.start_time = time.time() + self.languageFiles = {} def loadConfig(self, filename): if hasattr(self, 'config'): @@ -69,6 +70,7 @@ class CentralCommand(object): for lang in self.config['languages']: 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: self.languages[lang['code']] = yaml.load(fp) diff --git a/hugvey/panopticon.py b/hugvey/panopticon.py index d708cb1..0c87265 100644 --- a/hugvey/panopticon.py +++ b/hugvey/panopticon.py @@ -4,6 +4,8 @@ The panopticon provides a way to observe (& control) all running Hugveys trough import logging import tornado +import string +import random import tornado.websocket import tornado.web @@ -13,6 +15,7 @@ from pytz.reference import Central import asyncio import json from urllib.parse import urlparse +from hugvey import central_command logger = logging.getLogger("panopticon") @@ -87,6 +90,41 @@ def getWebSocketHandler(central_command): 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): def __init__(self, central_command, config): @@ -95,9 +133,10 @@ class Panopticon(object): self.application = tornado.web.Application([ (r"/ws", getWebSocketHandler(self.command)), (r"/local/(.*)", tornado.web.StaticFileHandler, - {"path": config['web']['files_dir']}), + {"path": config['web']['files_dir']}), + (r"/upload", getUploadHandler(self.command)), (r"/(.*)", tornado.web.StaticFileHandler, - {"path": web_dir, "default_filename": 'index.html'}), + {"path": web_dir, "default_filename": 'index.html'}), ], debug=True) self.application.listen(config['web']['port']) diff --git a/local/.gitignore b/local/.gitignore deleted file mode 100644 index a5baada..0000000 --- a/local/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -* -!.gitignore - diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 6bd2f48..0000000 --- a/tsconfig.json +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index 406c7fa..af7620e 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -90,8 +90,7 @@ class Panopticon { let req = new XMLHttpRequest(); let graph = this.graph; req.addEventListener("load", function(e){ - console.log('TEST',this); - graph.loadData(JSON.parse(this.response)); + graph.loadData(JSON.parse(this.response), code); // console.log(, e); }); req.open("GET", "/local/" + file); @@ -125,6 +124,7 @@ class Graph{ this.svg = d3.select('#graph'); this.container = d3.select('#container'); this.selectedMsg = null; + this.language_code = null; 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.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); } - saveJson() { + downloadJson() { + if(!this.language_code) { + alert("Make sure to load a language first") + } + var blob = new Blob([this.getJsonString()], {type: 'application/json'}); if(window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveBlob(blob, "pillow_talk.json"); @@ -572,8 +576,31 @@ class Graph{ document.body.removeChild(elem); } } + + saveJson(msg_id, fileInputElement) { + if(!this.language_code) { + alert("Make sure to load a language first") + } + + let formData = new FormData(); - loadData(data) { + 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.updateFromData(); this.build(true);