mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-25 09:21:04 +00:00
Fix API key logic, add some error handling
This commit is contained in:
parent
93b711aa74
commit
a82b851a3d
3 changed files with 47 additions and 35 deletions
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,13 +97,16 @@ 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.inputText === ""){
|
|
||||||
this.$refs.inputTextarea.style.height = this.inputTextareaHeight + "px";
|
if (this.$refs.inputTextarea){
|
||||||
this.$refs.translatedTextarea.style.height = this.inputTextareaHeight + "px";
|
if (this.inputText === ""){
|
||||||
}else{
|
this.$refs.inputTextarea.style.height = this.inputTextareaHeight + "px";
|
||||||
this.$refs.inputTextarea.style.height = this.$refs.translatedTextarea.style.height = "1px";
|
this.$refs.translatedTextarea.style.height = this.inputTextareaHeight + "px";
|
||||||
this.$refs.inputTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.inputTextarea.scrollHeight + 32) + "px";
|
}else{
|
||||||
this.$refs.translatedTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.translatedTextarea.scrollHeight + 32) + "px";
|
this.$refs.inputTextarea.style.height = this.$refs.translatedTextarea.style.height = "1px";
|
||||||
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){
|
if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){
|
||||||
|
@ -113,25 +116,29 @@ 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
|
||||||
for (var i = 0; i < this.$refs.sourceLangDropdown.children.length; i++){
|
if (this.$refs.sourceLangDropdown){
|
||||||
var el = this.$refs.sourceLangDropdown.children[i];
|
for (var i = 0; i < this.$refs.sourceLangDropdown.children.length; i++){
|
||||||
if (el.value === this.sourceLang){
|
var el = this.$refs.sourceLangDropdown.children[i];
|
||||||
el.setAttribute('selected', '');
|
if (el.value === this.sourceLang){
|
||||||
this.$refs.sourceLangDropdown.style.width = getTextWidth(el.text) + 24 + 'px';
|
el.setAttribute('selected', '');
|
||||||
}else{
|
this.$refs.sourceLangDropdown.style.width = getTextWidth(el.text) + 24 + 'px';
|
||||||
el.removeAttribute('selected');
|
}else{
|
||||||
}
|
el.removeAttribute('selected');
|
||||||
}
|
}
|
||||||
for (var i = 0; i < this.$refs.targetLangDropdown.children.length; i++){
|
|
||||||
var el = this.$refs.targetLangDropdown.children[i];
|
|
||||||
if (el.value === this.targetLang){
|
|
||||||
el.setAttribute('selected', '');
|
|
||||||
this.$refs.targetLangDropdown.style.width = getTextWidth(el.text) + 24 + 'px';
|
|
||||||
}else{
|
|
||||||
el.removeAttribute('selected');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.$refs.targetLangDropdown){
|
||||||
|
for (var i = 0; i < this.$refs.targetLangDropdown.children.length; i++){
|
||||||
|
var el = this.$refs.targetLangDropdown.children[i];
|
||||||
|
if (el.value === this.targetLang){
|
||||||
|
el.setAttribute('selected', '');
|
||||||
|
this.$refs.targetLangDropdown.style.width = getTextWidth(el.text) + 24 + 'px';
|
||||||
|
}else{
|
||||||
|
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 a new issue