This commit is contained in:
Sébastien Thuret 2021-10-24 19:14:09 +02:00
parent 4f0d19dbc4
commit 73ebb96171
No known key found for this signature in database
GPG key ID: 4742E2D66933BB08
3 changed files with 23 additions and 16 deletions

View file

@ -1,3 +1,4 @@
import io
import os import os
import tempfile import tempfile
import uuid import uuid
@ -6,7 +7,7 @@ from functools import wraps
import argostranslatefiles import argostranslatefiles
import pkg_resources import pkg_resources
from argostranslatefiles import get_supported_formats 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 import swagger
from flask_swagger_ui import get_swaggerui_blueprint from flask_swagger_ui import get_swaggerui_blueprint
from translatehtml import translate_html from translatehtml import translate_html
@ -548,14 +549,8 @@ def create_app(args):
type: string type: string
description: Error message description: Error message
""" """
if request.is_json: source_lang = request.form.get("source")
json = get_json_dict(request) target_lang = request.form.get("target")
source_lang = json.get("source")
target_lang = json.get("target")
else:
source_lang = request.values.get("source")
target_lang = request.values.get("target")
file = request.files['file'] file = request.files['file']
if not file: if not file:
@ -606,8 +601,16 @@ def create_app(args):
Download a translated file Download a translated file
""" """
filename.split('.').pop(0) 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"]) @app.route("/detect", methods=["POST"])
@access_check @access_check

View file

@ -328,17 +328,18 @@ document.addEventListener('DOMContentLoaded', function(){
let formdata = new FormData(); let formdata = new FormData();
formdata.append("file", this.inputFile); formdata.append("file", this.inputFile);
formdata.append("source", this.sourceLang);
translateFileRequest.send(formdata); formdata.append("target", this.targetLang);
this.loadingFileTranslation = true this.loadingFileTranslation = true
translateFileRequest.onreadystatechange = function () { translateFileRequest.onload = () => {
if (xhr.readyState == 4 && xhr.status == 200) { if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) {
this.loadingFileTranslation = false this.loadingFileTranslation = false
} }
} }
translateFileRequest.send(formdata);
} }
} }
}); });

View file

@ -196,14 +196,17 @@
<p>[[ inputFile.name ]]</p> <p>[[ inputFile.name ]]</p>
</div> </div>
<div class="col s2"> <div class="col s2">
<button @click="removeFile" class="btn-flat"> <button v-if="loadingFileTranslation !== true" @click="removeFile" class="btn-flat">
<i class="material-icons">close</i> <i class="material-icons">close</i>
</button> </button>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<button @click="translateFile" class="btn">Translate</button> <button @click="translateFile" v-if="loadingFileTranslation === false" class="btn">Translate</button>
<div class="progress" v-if="loadingFileTranslation">
<div class="indeterminate"></div>
</div>
</div> </div>
</div> </div>
</div> </div>