Skip to content

Commit

Permalink
fix(console): encode binary files as base64, and text files as utf8 (#…
Browse files Browse the repository at this point in the history
…6966)

The console's UI component for a file browser will use utf8 to manage
text files, and base64 to manage the rest of the files.

It's still pending to provide a better solution.
  • Loading branch information
skyrpex authored Jul 31, 2024
1 parent b200bde commit 1d078fb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/wing-console/console/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"react-dom": "^18.3.1",
"react-markdown": "^9.0.1",
"react-use": "^17.5.0",
"textextensions": "^6.11.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
27 changes: 14 additions & 13 deletions apps/wing-console/console/ui/src/use-download-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import JSZip from "jszip";
import textextensions from "textextensions";

export interface File {
filename: string;
Expand All @@ -7,20 +8,20 @@ export interface File {

// TODO - once mime-types is added to the sdk, use it to determine the encoding
export const getFileEncoding = (file: string): "base64" | "utf8" => {
const type = file.split(".").pop();
switch (type) {
case "txt":
case "json":
case "js":
case "html":
case "css":
case "md": {
return "utf8";
}
default: {
return "base64";
}
const basename = file.split("/").pop() ?? file;
if (!basename.includes(".")) {
return "utf8";
}
const extension = basename.split(".").pop();
if (!extension) {
return "utf8";
}

if (textextensions.includes(extension)) {
return "utf8";
}

return "base64";
};

const getEncodedUrl = (file: string, content: string) => {
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1d078fb

Please sign in to comment.