add download link & auto download translated file

This commit is contained in:
Sébastien Thuret 2021-10-25 10:50:55 +02:00
parent b97134ac07
commit 6a304df2e8
No known key found for this signature in database
GPG key ID: 4742E2D66933BB08
3 changed files with 39 additions and 9 deletions

View file

@ -507,11 +507,9 @@ def create_app(args):
id: translate id: translate
type: object type: object
properties: properties:
translatedText: translatedFileUrl:
oneOf: type: string
- type: string description: Translated file url
- type: array
description: Translated text(s)
400: 400:
description: Invalid request description: Invalid request
schema: schema:
@ -588,7 +586,7 @@ def create_app(args):
translated_filename = os.path.basename(translated_file_path) translated_filename = os.path.basename(translated_file_path)
return jsonify( return jsonify(
{ {
"translatedFileUrl": url_for('download_file', filename=translated_filename) "translatedFileUrl": url_for('download_file', filename=translated_filename, _external=True)
} }
) )
except Exception as e: except Exception as e:

View file

@ -35,6 +35,7 @@ document.addEventListener('DOMContentLoaded', function(){
translationType: "text", translationType: "text",
inputFile: false, inputFile: false,
loadingFileTranslation: false, loadingFileTranslation: false,
translatedFileUrl: "",
}, },
mounted: function(){ mounted: function(){
var self = this; var self = this;
@ -317,10 +318,13 @@ document.addEventListener('DOMContentLoaded', function(){
removeFile: function(e) { removeFile: function(e) {
e.preventDefault() e.preventDefault()
this.inputFile = false; this.inputFile = false;
this.translatedFileUrl = "";
this.loadingFileTranslation = false;
}, },
translateFile: function(e) { translateFile: function(e) {
e.preventDefault(); e.preventDefault();
let self = this;
let translateFileRequest = new XMLHttpRequest(); let translateFileRequest = new XMLHttpRequest();
translateFileRequest.open("POST", BaseUrl + "/translate_file", true); translateFileRequest.open("POST", BaseUrl + "/translate_file", true);
@ -333,12 +337,39 @@ document.addEventListener('DOMContentLoaded', function(){
this.loadingFileTranslation = true this.loadingFileTranslation = true
translateFileRequest.onload = () => { translateFileRequest.onload = function() {
if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) { if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) {
this.loadingFileTranslation = false try{
self.loadingFileTranslation = false;
let res = JSON.parse(this.response);
if (res.translatedFileUrl){
self.translatedFileUrl = res.translatedFileUrl;
let link = document.createElement("a");
link.target = "_blank";
link.href = self.translatedFileUrl;
link.click();
}else{
throw new Error(res.error || "Unknown error");
}
}catch(e){
self.error = e.message;
self.loadingFileTranslation = false;
self.inputFile = false;
}
} }
} }
translateFileRequest.onerror = function() {
self.error = "Error while calling /translate_file";
self.loadingFileTranslation = false;
self.inputFile = false;
};
translateFileRequest.send(data); translateFileRequest.send(data);
} }
} }

View file

@ -203,7 +203,8 @@
</div> </div>
</div> </div>
</div> </div>
<button @click="translateFile" v-if="loadingFileTranslation === false" class="btn">Translate</button> <button @click="translateFile" v-if="translatedFileUrl === '' && loadingFileTranslation === false" class="btn">Translate</button>
<a v-if="translatedFileUrl !== ''" :href="translatedFileUrl" class="btn">Download</a>
<div class="progress" v-if="loadingFileTranslation"> <div class="progress" v-if="loadingFileTranslation">
<div class="indeterminate"></div> <div class="indeterminate"></div>
</div> </div>