Fix API key logic, add some error handling

This commit is contained in:
Piero Toffanin 2021-10-26 17:13:51 -04:00
parent 93b711aa74
commit a82b851a3d
3 changed files with 47 additions and 35 deletions

View file

@ -33,6 +33,14 @@ def get_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):
d = request.get_json()
@ -54,11 +62,7 @@ def get_req_limits(default_limit, api_keys_db, multiplier=1):
req_limit = default_limit
if api_keys_db:
if request.is_json:
json = get_json_dict(request)
api_key = json.get("api_key")
else:
api_key = request.values.get("api_key")
api_key = get_req_api_key()
if 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")
if args.api_keys and args.require_api_key_origin:
if request.is_json:
json = get_json_dict(request)
ak = json.get("api_key")
else:
ak = request.values.get("api_key")
ak = get_req_api_key()
if (
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(
{
"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:

View file

@ -97,6 +97,8 @@ document.addEventListener('DOMContentLoaded', function(){
updated: function(){
M.FormSelect.init(this.$refs.sourceLangDropdown);
M.FormSelect.init(this.$refs.targetLangDropdown);
if (this.$refs.inputTextarea){
if (this.inputText === ""){
this.$refs.inputTextarea.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.translatedTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.translatedTextarea.scrollHeight + 32) + "px";
}
}
if (this.charactersLimit !== -1 && this.inputText.length >= 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)
// but properly display checkmarks on supported browsers.
// 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++){
var el = this.$refs.sourceLangDropdown.children[i];
if (el.value === this.sourceLang){
@ -122,6 +126,9 @@ document.addEventListener('DOMContentLoaded', function(){
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){
@ -131,7 +138,7 @@ document.addEventListener('DOMContentLoaded', function(){
el.removeAttribute('selected');
}
}
}
},
computed: {
requestCode: function(){
@ -361,6 +368,11 @@ document.addEventListener('DOMContentLoaded', function(){
self.loadingFileTranslation = false;
self.inputFile = false;
}
}else{
let res = JSON.parse(this.response);
self.error = res.error || "Unknown error";
self.loadingFileTranslation = false;
self.inputFile = false;
}
}

View file

@ -190,7 +190,7 @@
</div>
<div v-if="inputFile !== false" class="dropzone-content">
<div class="card">
<div class="card-content" style="padding-right: 6px;">
<div class="card-content">
<div class="row mb-0">
<div class="col s12">
[[ inputFile.name ]]