Fixed text encoding and decoding in edge cases

This commit is contained in:
Maxim Stewart 2020-05-11 15:17:45 -05:00
parent a1cc2a410b
commit e82e80ad39
2 changed files with 22 additions and 12 deletions

View File

@ -1,6 +1,8 @@
# Python imports # Python imports
import os, hashlib, json, base64 import os, hashlib, json, base64
from urllib.parse import unquote # For Python 2 use: from urlparse import unquote
# Flask imports # Flask imports
from flask import Flask, request, render_template, session, send_from_directory, url_for, redirect, escape from flask import Flask, request, render_template, session, send_from_directory, url_for, redirect, escape
from flask_uploads import UploadSet, configure_uploads, ALL from flask_uploads import UploadSet, configure_uploads, ALL
@ -30,8 +32,10 @@ with open(NOTES_PTH) as infile:
notesJson = json.load(infile) notesJson = json.load(infile)
for entry in notesJson: for entry in notesJson:
notesListEncoded.append(entry) notesListEncoded.append(entry)
entryDecoded = str(base64.urlsafe_b64decode( entry.encode("utf-8") ), "utf-8")
notesListDecoded.append(escape(entryDecoded)) decodedStrPart = base64.urlsafe_b64decode(entry.encode('utf-8')).decode('utf-8')
entryDecoded = unquote(decodedStrPart)
notesListDecoded.append(entryDecoded)
except Exception as e: except Exception as e:
print(repr(e)) print(repr(e))
@ -146,12 +150,14 @@ def deleteFile():
def deleteText(): def deleteText():
if request.method == 'POST': if request.method == 'POST':
try: try:
decodedNote = request.values['noteStr'].strip() encodedStr = request.values['noteStr'].strip()
encodedBytes = base64.urlsafe_b64encode(decodedNote.encode("utf-8")) decodedStrPart = base64.urlsafe_b64decode(encodedStr.encode('utf-8')).decode('utf-8')
encodedNote = str(encodedBytes, "utf-8") decodedStr = unquote(decodedStrPart)
notesListEncoded.remove(encodedNote) print("Encoded String:\n\t" + encodedStr)
notesListDecoded.remove(decodedNote) print("Decoded String:\n\t" + decodedStr)
notesListEncoded.remove(encodedStr)
notesListDecoded.remove(decodedStr)
updateNotesFile() updateNotesFile()
msg = "[Success] Deleted entry..." msg = "[Success] Deleted entry..."
@ -182,10 +188,10 @@ def addNote():
text = request.values['entryText'].strip() text = request.values['entryText'].strip()
if request.method == 'POST' and text != '': if request.method == 'POST' and text != '':
try: try:
encodedBytes = base64.urlsafe_b64encode(text.encode("utf-8")) encodedStr = text
encodedStr = str(encodedBytes, "utf-8") decodedStr = base64.urlsafe_b64decode(encodedStr.encode('utf-8')).decode('utf-8')
notesListDecoded.append(text) notesListDecoded.append(decodedStr)
notesListEncoded.append(encodedStr) notesListEncoded.append(encodedStr)
updateNotesFile() updateNotesFile()

View File

@ -70,7 +70,9 @@ const deleteTextAction = (elm, note) => {
const refElm = document.getElementsByClassName(classRef)[0]; const refElm = document.getElementsByClassName(classRef)[0];
refElm.parentElement.removeChild(refElm); refElm.parentElement.removeChild(refElm);
const params = new URLSearchParams('noteStr=' + note) // Encoded special characters then encode to b64
encodedStr = window.btoa(encodeURIComponent(note))
const params = new URLSearchParams('noteStr=' + encodedStr)
doAjax('delete-text', params, 'delete-text'); doAjax('delete-text', params, 'delete-text');
} }
@ -82,7 +84,9 @@ const deleteAllTextAction = () => {
const createFormData = (entry) => { const createFormData = (entry) => {
let form = new FormData(); let form = new FormData();
for (var i = 0; i < entry.length; i++) { for (var i = 0; i < entry.length; i++) {
form.append("entryText", entry); // Encoded special characters then encode to b64
encodedStr = window.btoa(encodeURIComponent(note))
form.append("entryText", encodedStr);
} }
return form; return form;
} }