]> OzVa Git service - ozva-cloud/commitdiff
feat: webui editing support multiple encodings (#197)
authorsigoden <sigoden@gmail.com>
Fri, 17 Mar 2023 03:22:21 +0000 (11:22 +0800)
committerGitHub <noreply@github.com>
Fri, 17 Mar 2023 03:22:21 +0000 (11:22 +0800)
assets/index.js

index e3c675796f6e1e16bcf6103e6822e9de5ee2f538..9f10fc4f8b33bc57a3a02df8f5023ab9114aec5c 100644 (file)
@@ -535,8 +535,15 @@ async function setupEditPage() {
   try {
     const res = await fetch(baseUrl());
     await assertResOK(res);
-    const text = await res.text();
-    $editor.value = text;
+    const encoding = getEncoding(res.headers.get("content-type"));
+    if (encoding === "utf-8") {
+      $editor.value = await res.text();
+    } else {
+      const bytes = await res.arrayBuffer();
+      const dataView = new DataView(bytes)
+      const decoder = new TextDecoder(encoding)
+      $editor.value = decoder.decode(dataView);
+    }
   } catch (err) {
     alert(`Failed get file, ${err.message}`);
   }
@@ -796,3 +803,14 @@ async function assertResOK(res) {
     throw new Error(await res.text())
   }
 }
+
+function getEncoding(contentType) {
+    const charset = contentType?.split(";")[1];
+    if (/charset/i.test(charset)) {
+      let encoding = charset.split("=")[1];
+      if (encoding) {
+        return encoding.toLowerCase()
+      }
+    }
+    return 'utf-8'
+}
\ No newline at end of file