mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-08-21 22:00:15 +02:00
Fix API key logic, add some error handling
This commit is contained in:
parent
93b711aa74
commit
a82b851a3d
22
app/app.py
22
app/app.py
@ -33,6 +33,14 @@ def get_upload_dir():
|
|||||||
|
|
||||||
return upload_dir
|
return upload_dir
|
||||||
|
|
||||||
|
def get_req_api_key():
|
||||||
|
if request.is_json:
|
||||||
|
json = get_json_dict(request)
|
||||||
|
ak = json.get("api_key")
|
||||||
|
else:
|
||||||
|
ak = request.values.get("api_key")
|
||||||
|
|
||||||
|
return ak
|
||||||
|
|
||||||
def get_json_dict(request):
|
def get_json_dict(request):
|
||||||
d = request.get_json()
|
d = request.get_json()
|
||||||
@ -54,11 +62,7 @@ def get_req_limits(default_limit, api_keys_db, multiplier=1):
|
|||||||
req_limit = default_limit
|
req_limit = default_limit
|
||||||
|
|
||||||
if api_keys_db:
|
if api_keys_db:
|
||||||
if request.is_json:
|
api_key = get_req_api_key()
|
||||||
json = get_json_dict(request)
|
|
||||||
api_key = json.get("api_key")
|
|
||||||
else:
|
|
||||||
api_key = request.values.get("api_key")
|
|
||||||
|
|
||||||
if api_key:
|
if api_key:
|
||||||
db_req_limit = api_keys_db.lookup(api_key)
|
db_req_limit = api_keys_db.lookup(api_key)
|
||||||
@ -162,11 +166,7 @@ def create_app(args):
|
|||||||
abort(403, description="Too many request limits violations")
|
abort(403, description="Too many request limits violations")
|
||||||
|
|
||||||
if args.api_keys and args.require_api_key_origin:
|
if args.api_keys and args.require_api_key_origin:
|
||||||
if request.is_json:
|
ak = get_req_api_key()
|
||||||
json = get_json_dict(request)
|
|
||||||
ak = json.get("api_key")
|
|
||||||
else:
|
|
||||||
ak = request.values.get("api_key")
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
api_keys_db.lookup(ak) is None and request.headers.get("Origin") != args.require_api_key_origin
|
api_keys_db.lookup(ak) is None and request.headers.get("Origin") != args.require_api_key_origin
|
||||||
@ -608,7 +608,7 @@ def create_app(args):
|
|||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedFileUrl": url_for('download_file', filename=translated_filename, _external=True)
|
"translatedFileUrl": url_for('download_file', filename=translated_filename, _external=True, api_key=get_req_api_key())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -97,6 +97,8 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
updated: function(){
|
updated: function(){
|
||||||
M.FormSelect.init(this.$refs.sourceLangDropdown);
|
M.FormSelect.init(this.$refs.sourceLangDropdown);
|
||||||
M.FormSelect.init(this.$refs.targetLangDropdown);
|
M.FormSelect.init(this.$refs.targetLangDropdown);
|
||||||
|
|
||||||
|
if (this.$refs.inputTextarea){
|
||||||
if (this.inputText === ""){
|
if (this.inputText === ""){
|
||||||
this.$refs.inputTextarea.style.height = this.inputTextareaHeight + "px";
|
this.$refs.inputTextarea.style.height = this.inputTextareaHeight + "px";
|
||||||
this.$refs.translatedTextarea.style.height = this.inputTextareaHeight + "px";
|
this.$refs.translatedTextarea.style.height = this.inputTextareaHeight + "px";
|
||||||
@ -105,6 +107,7 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
this.$refs.inputTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.inputTextarea.scrollHeight + 32) + "px";
|
this.$refs.inputTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.inputTextarea.scrollHeight + 32) + "px";
|
||||||
this.$refs.translatedTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.translatedTextarea.scrollHeight + 32) + "px";
|
this.$refs.translatedTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.translatedTextarea.scrollHeight + 32) + "px";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){
|
if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){
|
||||||
this.inputText = this.inputText.substring(0, this.charactersLimit);
|
this.inputText = this.inputText.substring(0, this.charactersLimit);
|
||||||
@ -113,6 +116,7 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
// Update "selected" attribute (to overcome a vue.js limitation)
|
// Update "selected" attribute (to overcome a vue.js limitation)
|
||||||
// but properly display checkmarks on supported browsers.
|
// but properly display checkmarks on supported browsers.
|
||||||
// Also change the <select> width value depending on the <option> length
|
// Also change the <select> width value depending on the <option> length
|
||||||
|
if (this.$refs.sourceLangDropdown){
|
||||||
for (var i = 0; i < this.$refs.sourceLangDropdown.children.length; i++){
|
for (var i = 0; i < this.$refs.sourceLangDropdown.children.length; i++){
|
||||||
var el = this.$refs.sourceLangDropdown.children[i];
|
var el = this.$refs.sourceLangDropdown.children[i];
|
||||||
if (el.value === this.sourceLang){
|
if (el.value === this.sourceLang){
|
||||||
@ -122,6 +126,9 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
el.removeAttribute('selected');
|
el.removeAttribute('selected');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.$refs.targetLangDropdown){
|
||||||
for (var i = 0; i < this.$refs.targetLangDropdown.children.length; i++){
|
for (var i = 0; i < this.$refs.targetLangDropdown.children.length; i++){
|
||||||
var el = this.$refs.targetLangDropdown.children[i];
|
var el = this.$refs.targetLangDropdown.children[i];
|
||||||
if (el.value === this.targetLang){
|
if (el.value === this.targetLang){
|
||||||
@ -131,7 +138,7 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
el.removeAttribute('selected');
|
el.removeAttribute('selected');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
requestCode: function(){
|
requestCode: function(){
|
||||||
@ -361,6 +368,11 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||||||
self.loadingFileTranslation = false;
|
self.loadingFileTranslation = false;
|
||||||
self.inputFile = false;
|
self.inputFile = false;
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
let res = JSON.parse(this.response);
|
||||||
|
self.error = res.error || "Unknown error";
|
||||||
|
self.loadingFileTranslation = false;
|
||||||
|
self.inputFile = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div v-if="inputFile !== false" class="dropzone-content">
|
<div v-if="inputFile !== false" class="dropzone-content">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-content" style="padding-right: 6px;">
|
<div class="card-content">
|
||||||
<div class="row mb-0">
|
<div class="row mb-0">
|
||||||
<div class="col s12">
|
<div class="col s12">
|
||||||
[[ inputFile.name ]]
|
[[ inputFile.name ]]
|
||||||
|
Loading…
Reference in New Issue
Block a user