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

View File

@ -70,7 +70,9 @@ const deleteTextAction = (elm, note) => {
const refElm = document.getElementsByClassName(classRef)[0];
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');
}
@ -82,7 +84,9 @@ const deleteAllTextAction = () => {
const createFormData = (entry) => {
let form = new FormData();
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;
}