From 38d8973a2367058c8b28d015dbce21ca951480c3 Mon Sep 17 00:00:00 2001 From: szschaler Date: Tue, 30 Jan 2024 17:49:39 +0000 Subject: [PATCH] Correctly decode UTF-8 from Base64 (and also encode UTF-8 when sending to action functions) --- platform/src/FileHandler.js | 12 ++++++++++-- platform/src/Utility.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/platform/src/FileHandler.js b/platform/src/FileHandler.js index 84dc0d7..56ed811 100644 --- a/platform/src/FileHandler.js +++ b/platform/src/FileHandler.js @@ -10,6 +10,11 @@ class FileHandler { } + base64ToBytes(base64) { + const binString = window.atob(base64); + return Uint8Array.from(binString, (m) => m.codePointAt(0)); + } + fetchFile(url, isPrivate){ if (isPrivate){ @@ -19,14 +24,17 @@ class FileHandler { if (requestUrl != null) { var xhr = new XMLHttpRequest(); xhr.open("GET", requestUrl, false); + xhr.setRequestHeader("Accept", "application/json; charset=UTF-8"); xhr.withCredentials = true; xhr.send(); if (xhr.status === 200) { let response = JSON.parse(xhr.responseText); - - return { content: window.atob(response.data.content), sha: response.data.sha }; + + let contents = new TextDecoder().decode(this.base64ToBytes(response.data.content)); + + return { content: contents, sha: response.data.sha }; } else { return null; diff --git a/platform/src/Utility.js b/platform/src/Utility.js index 21df2a8..f0f0322 100644 --- a/platform/src/Utility.js +++ b/platform/src/Utility.js @@ -45,7 +45,7 @@ export function jsonRequest(url, json, useCredentials=false){ var xhr = new XMLHttpRequest(); xhr.open("POST", url); - xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8"); xhr.withCredentials = useCredentials; xhr.onload = function () {