From 73ebb96171177bf20781544584550272b439ab5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Thuret?= Date: Sun, 24 Oct 2021 19:14:09 +0200 Subject: [PATCH] fix --- app/app.py | 23 +++++++++++++---------- app/static/js/app.js | 9 +++++---- app/templates/index.html | 7 +++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/app.py b/app/app.py index 3ec42ff..e19cf6c 100644 --- a/app/app.py +++ b/app/app.py @@ -1,3 +1,4 @@ +import io import os import tempfile import uuid @@ -6,7 +7,7 @@ from functools import wraps import argostranslatefiles import pkg_resources from argostranslatefiles import get_supported_formats -from flask import Flask, abort, jsonify, render_template, request, url_for, send_from_directory +from flask import Flask, abort, jsonify, render_template, request, url_for, send_from_directory, send_file from flask_swagger import swagger from flask_swagger_ui import get_swaggerui_blueprint from translatehtml import translate_html @@ -548,14 +549,8 @@ def create_app(args): type: string description: Error message """ - if request.is_json: - json = get_json_dict(request) - source_lang = json.get("source") - target_lang = json.get("target") - else: - source_lang = request.values.get("source") - target_lang = request.values.get("target") - + source_lang = request.form.get("source") + target_lang = request.form.get("target") file = request.files['file'] if not file: @@ -606,8 +601,16 @@ def create_app(args): Download a translated file """ filename.split('.').pop(0) + filepath = os.path.join(tempfile.gettempdir(), filename) - return send_from_directory(directory=tempfile.gettempdir(), filename=filename) + return_data = io.BytesIO() + with open(filepath, 'rb') as fo: + return_data.write(fo.read()) + return_data.seek(0) + + os.remove(filepath) + + return send_file(return_data, attachment_filename=filename) @app.route("/detect", methods=["POST"]) @access_check diff --git a/app/static/js/app.js b/app/static/js/app.js index 149f256..c92c51b 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -328,17 +328,18 @@ document.addEventListener('DOMContentLoaded', function(){ let formdata = new FormData(); formdata.append("file", this.inputFile); - - translateFileRequest.send(formdata); + formdata.append("source", this.sourceLang); + formdata.append("target", this.targetLang); this.loadingFileTranslation = true - translateFileRequest.onreadystatechange = function () { - if (xhr.readyState == 4 && xhr.status == 200) { + translateFileRequest.onload = () => { + if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) { this.loadingFileTranslation = false } } + translateFileRequest.send(formdata); } } }); diff --git a/app/templates/index.html b/app/templates/index.html index dad21ae..37f1144 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -196,14 +196,17 @@

[[ inputFile.name ]]

-
- + +
+
+