Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: update filer reader result (#11638)

boojack529aabfa1345
diff --git a/frontend/src/components/AlterSchemaPrepForm/DatabaseGroupPrevEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/DatabaseGroupPrevEditorModal.vue
index 85fbc636a5..11885d0c8f 100644
--- a/frontend/src/components/AlterSchemaPrepForm/DatabaseGroupPrevEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/DatabaseGroupPrevEditorModal.vue
@@ -76,12 +76,11 @@ import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { MonacoEditor } from "@/components/MonacoEditor";
import ActionConfirmModal from "@/components/SchemaEditorV1/Modals/ActionConfirmModal.vue";
-import { useDBGroupStore, useNotificationStore } from "@/store";
+import { pushNotification, useDBGroupStore } from "@/store";
import type { ComposedDatabaseGroup, ComposedSchemaGroup } from "@/types";
+import { MAX_UPLOAD_FILE_SIZE_MB, readFileAsArrayBuffer } from "@/utils";
import { generateDatabaseGroupIssueRoute } from "@/utils/databaseGroup/issue";
-const MAX_UPLOAD_FILE_SIZE_MB = 1;
-
interface LocalState {
editStatement: string;
showActionConfirmModal: boolean;
@@ -111,7 +110,6 @@ const state = reactive<LocalState>({
editStatement: "",
showActionConfirmModal: false,
});
-const notificationStore = useNotificationStore();
const allowPreviewIssue = computed(() => {
return state.editStatement !== "";
@@ -158,7 +156,7 @@ const dismissModal = () => {
}
};
-const handleUploadFile = (e: Event) => {
+const handleUploadFile = async (e: Event) => {
const target = e.target as HTMLInputElement;
const file = (target.files || [])[0];
const cleanup = () => {
@@ -173,7 +171,7 @@ const handleUploadFile = (e: Event) => {
return cleanup();
}
if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
- notificationStore.pushNotification({
+ pushNotification({
module: "bytebase",
style: "CRITICAL",
title: t("issue.upload-sql-file-max-size-exceeded", {
@@ -182,21 +180,21 @@ const handleUploadFile = (e: Event) => {
});
return cleanup();
}
- const fr = new FileReader();
- fr.onload = () => {
- const sql = fr.result as string;
- state.editStatement = sql;
- };
- fr.onerror = () => {
- notificationStore.pushNotification({
+
+ try {
+ const { arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ const statement = decoder.decode(arrayBuffer);
+ state.editStatement = statement;
+ } catch (error) {
+ pushNotification({
module: "bytebase",
style: "WARN",
title: `Read file error`,
- description: String(fr.error),
+ description: String(error),
});
- return;
- };
- fr.readAsText(file);
+ }
cleanup();
};
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index a08766ccea..a3b23d8bd3 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -152,7 +152,13 @@ import { Engine } from "@/types/proto/v1/common";
import type { DatabaseMetadata } from "@/types/proto/v1/database_service";
import { DatabaseMetadataView } from "@/types/proto/v1/database_service";
import { TenantMode } from "@/types/proto/v1/project_service";
-import { TinyTimer, defer, extractProjectResourceName } from "@/utils";
+import {
+ MAX_UPLOAD_FILE_SIZE_MB,
+ TinyTimer,
+ defer,
+ extractProjectResourceName,
+ readFileAsArrayBuffer,
+} from "@/utils";
import { MonacoEditor } from "../MonacoEditor";
import { provideSQLCheckContext } from "../SQLCheck";
import type { EditTarget, GenerateDiffDDLResult } from "../SchemaEditorLite";
@@ -162,8 +168,6 @@ import SchemaEditorLite, {
import MaskSpinner from "../misc/MaskSpinner.vue";
import SchemaEditorSQLCheckButton from "./SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue";
-const MAX_UPLOAD_FILE_SIZE_MB = 1;
-
type TabType = "raw-sql" | "schema-editor";
interface LocalState {
@@ -403,7 +407,7 @@ const generateDiffDDLMap = async (silent: boolean) => {
return statementMap;
};
-const handleUploadFile = (e: Event) => {
+const handleUploadFile = async (e: Event) => {
const target = e.target as HTMLInputElement;
const file = (target.files || [])[0];
const cleanup = () => {
@@ -418,7 +422,7 @@ const handleUploadFile = (e: Event) => {
return cleanup();
}
if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
- notificationStore.pushNotification({
+ pushNotification({
module: "bytebase",
style: "CRITICAL",
title: t("issue.upload-sql-file-max-size-exceeded", {
@@ -427,22 +431,20 @@ const handleUploadFile = (e: Event) => {
});
return cleanup();
}
- const fr = new FileReader();
- fr.onload = () => {
- const sql = fr.result as string;
- state.editStatement = sql;
- };
- fr.onerror = () => {
- notificationStore.pushNotification({
+
+ try {
+ const { arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ state.editStatement = decoder.decode(arrayBuffer);
+ } catch (error) {
+ pushNotification({
module: "bytebase",
style: "WARN",
title: `Read file error`,
- description: String(fr.error),
+ description: String(error),
});
- return;
- };
- fr.readAsText(file);
-
+ }
cleanup();
};
diff --git a/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/RawSQLForm.vue b/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/RawSQLForm.vue
index 7ba16a5808..81ef549166 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/RawSQLForm.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/AddChangePanel/form/RawSQLForm.vue
@@ -12,11 +12,18 @@
<script setup lang="ts">
import { computed } from "vue";
-import { useLocalSheetStore } from "@/store";
-import { getSheetStatement, readFileAsync, setSheetStatement } from "@/utils";
+import { useI18n } from "vue-i18n";
+import { pushNotification, useLocalSheetStore } from "@/store";
+import {
+ MAX_UPLOAD_FILE_SIZE_MB,
+ getSheetStatement,
+ readFileAsArrayBuffer,
+ setSheetStatement,
+} from "@/utils";
import RawSQLEditor from "../../RawSQLEditor";
import { useAddChangeContext } from "../context";
+const { t } = useI18n();
const { changeFromRawSQL: change } = useAddChangeContext();
const sheet = computed(() => {
@@ -33,12 +40,45 @@ const statement = computed({
});
const handleUploadEvent = async (e: Event) => {
+ const target = e.target as HTMLInputElement;
+ const file = (target.files || [])[0];
+ const cleanup = () => {
+ // Note that once selected a file, selecting the same file again will not
+ // trigger <input type="file">'s change event.
+ // So we need to do some cleanup stuff here.
+ target.files = null;
+ target.value = "";
+ };
+
+ if (!file) {
+ return cleanup();
+ }
+ if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("issue.upload-sql-file-max-size-exceeded", {
+ size: `${MAX_UPLOAD_FILE_SIZE_MB}MB`,
+ }),
+ });
+ return cleanup();
+ }
+
try {
- const { filename, content } = await readFileAsync(e, 100);
- statement.value = content;
+ const { filename, arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ statement.value = decoder.decode(arrayBuffer);
sheet.value.title = filename;
- } finally {
- // Nothing
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: `Read file error`,
+ description: String(error),
+ });
}
+
+ cleanup();
};
</script>
diff --git a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
index fd8b4ef2d4..3035f89c07 100644
--- a/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
+++ b/frontend/src/components/Changelist/ChangelistDetail/RawSQLPanel/RawSQLPanel.vue
@@ -72,9 +72,10 @@ import { pushNotification, useSheetV1Store } from "@/store";
import { Sheet } from "@/types/proto/v1/sheet_service";
import {
getSheetStatement,
- readFileAsync,
setSheetStatement,
getStatementSize,
+ MAX_UPLOAD_FILE_SIZE_MB,
+ readFileAsArrayBuffer,
} from "@/utils";
import RawSQLEditor from "../RawSQLEditor";
import { useChangelistDetailContext } from "../context";
@@ -136,12 +137,45 @@ const reset = () => {
};
const handleUploadEvent = async (e: Event) => {
+ const target = e.target as HTMLInputElement;
+ const file = (target.files || [])[0];
+ const cleanup = () => {
+ // Note that once selected a file, selecting the same file again will not
+ // trigger <input type="file">'s change event.
+ // So we need to do some cleanup stuff here.
+ target.files = null;
+ target.value = "";
+ };
+
+ if (!file) {
+ return cleanup();
+ }
+ if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("issue.upload-sql-file-max-size-exceeded", {
+ size: `${MAX_UPLOAD_FILE_SIZE_MB}MB`,
+ }),
+ });
+ return cleanup();
+ }
+
try {
- const { content } = await readFileAsync(e, 100);
- statement.value = content;
- } finally {
- // Nothing
+ const { arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ statement.value = decoder.decode(arrayBuffer);
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: `Read file error`,
+ description: String(error),
+ });
}
+
+ cleanup();
};
const doSaveChange = async () => {
diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
index ece190b9cd..8c21b98a37 100644
--- a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
@@ -254,8 +254,9 @@ import {
getStatementSize,
isDatabaseChangeRelatedIssue,
isDatabaseDataExportIssue,
+ MAX_UPLOAD_FILE_SIZE_MB,
+ readFileAsArrayBuffer,
} from "@/utils";
-import { readFileAsync } from "@/utils";
import { useSQLAdviceMarkers } from "../useSQLAdviceMarkers";
import FormatOnSaveCheckbox from "./FormatOnSaveCheckbox.vue";
import type { EditState } from "./useTempEditState";
@@ -581,36 +582,111 @@ const showOverwriteConfirmDialog = () => {
});
};
-const handleUploadAndOverwrite = async (event: Event) => {
+const handleUploadAndOverwrite = async (e: Event) => {
if (state.isUploadingFile) {
return;
}
try {
state.isUploadingFile = true;
await showOverwriteConfirmDialog();
- const { filename, content: statement } = await readFileAsync(event, 100);
- state.isEditing = true;
- state.statement = statement;
- handleStatementChange(statement);
- if (sheet.value) {
- sheet.value.title = filename;
+
+ const target = e.target as HTMLInputElement;
+ const file = (target.files || [])[0];
+ const cleanup = () => {
+ // Note that once selected a file, selecting the same file again will not
+ // trigger <input type="file">'s change event.
+ // So we need to do some cleanup stuff here.
+ target.files = null;
+ target.value = "";
+ };
+
+ if (!file) {
+ return cleanup();
+ }
+ if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("issue.upload-sql-file-max-size-exceeded", {
+ size: `${MAX_UPLOAD_FILE_SIZE_MB}MB`,
+ }),
+ });
+ return cleanup();
+ }
+
+ try {
+ const { filename, arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ const statement = decoder.decode(arrayBuffer);
+ state.isEditing = true;
+ state.statement = statement;
+ handleStatementChange(statement);
+ if (sheet.value) {
+ sheet.value.title = filename;
+ }
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: `Read file error`,
+ description: String(error),
+ });
}
+ cleanup();
resetTempEditState();
} finally {
state.isUploadingFile = false;
}
};
-const handleUploadFile = async (event: Event) => {
+const handleUploadFile = async (e: Event) => {
try {
state.isUploadingFile = true;
- const { filename, content: statement } = await readFileAsync(event, 100);
- handleStatementChange(statement);
- if (sheet.value) {
- sheet.value.title = filename;
+ const target = e.target as HTMLInputElement;
+ const file = (target.files || [])[0];
+ const cleanup = () => {
+ // Note that once selected a file, selecting the same file again will not
+ // trigger <input type="file">'s change event.
+ // So we need to do some cleanup stuff here.
+ target.files = null;
+ target.value = "";
+ };
+
+ if (!file) {
+ return cleanup();
+ }
+ if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("issue.upload-sql-file-max-size-exceeded", {
+ size: `${MAX_UPLOAD_FILE_SIZE_MB}MB`,
+ }),
+ });
+ return cleanup();
+ }
+
+ try {
+ const { filename, arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ const statement = decoder.decode(arrayBuffer);
+ handleStatementChange(statement);
+ if (sheet.value) {
+ sheet.value.title = filename;
+ }
+ } catch (error) {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: `Read file error`,
+ description: String(error),
+ });
}
+ cleanup();
resetTempEditState();
} finally {
state.isUploadingFile = false;
diff --git a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
index 2d31171a44..5a47e55299 100644
--- a/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
+++ b/frontend/src/components/SyncDatabaseSchema/RawSQLEditor.vue
@@ -81,7 +81,7 @@ import { useI18n } from "vue-i18n";
import { MonacoEditor } from "@/components/MonacoEditor";
import DownloadSheetButton from "@/components/Sheet/DownloadSheetButton.vue";
import { ProjectSelect } from "@/components/v2";
-import { useNotificationStore, useSheetV1Store } from "@/store";
+import { pushNotification, useSheetV1Store } from "@/store";
import {
DEFAULT_PROJECT_V1_NAME,
UNKNOWN_ID,
@@ -93,11 +93,11 @@ import {
extractSheetUID,
getStatementSize,
getSheetStatement,
+ readFileAsArrayBuffer,
+ MAX_UPLOAD_FILE_SIZE_MB,
} from "@/utils";
import type { RawSQLState } from "./types";
-const MAX_UPLOAD_FILE_SIZE_MB = 1;
-
interface LocalState {
projectId?: string;
engine: Engine;
@@ -118,7 +118,6 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const notificationStore = useNotificationStore();
const sheetStore = useSheetV1Store();
const state = reactive<LocalState>({
projectId: props.projectId,
@@ -177,7 +176,7 @@ const handleEngineChange = () => {
});
};
-const handleUploadFile = (e: Event) => {
+const handleUploadFile = async (e: Event) => {
const target = e.target as HTMLInputElement;
const file = (target.files || [])[0];
const cleanup = () => {
@@ -192,7 +191,7 @@ const handleUploadFile = (e: Event) => {
return cleanup();
}
if (file.size > MAX_UPLOAD_FILE_SIZE_MB * 1024 * 1024) {
- notificationStore.pushNotification({
+ pushNotification({
module: "bytebase",
style: "CRITICAL",
title: t("issue.upload-sql-file-max-size-exceeded", {
@@ -201,9 +200,12 @@ const handleUploadFile = (e: Event) => {
});
return cleanup();
}
- const fr = new FileReader();
- fr.onload = async () => {
- const statement = fr.result as string;
+
+ try {
+ const { arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ const statement = decoder.decode(arrayBuffer);
const sheet = await sheetStore.createSheet(DEFAULT_PROJECT_V1_NAME, {
title: file.name,
content: new TextEncoder().encode(statement),
@@ -211,17 +213,14 @@ const handleUploadFile = (e: Event) => {
const sheetId = Number(extractSheetUID(sheet.name));
await handleStatementChange(statement);
update(sheetId);
- };
- fr.onerror = () => {
- notificationStore.pushNotification({
+ } catch (error) {
+ pushNotification({
module: "bytebase",
style: "WARN",
title: `Read file error`,
- description: String(fr.error),
+ description: String(error),
});
- return;
- };
- fr.readAsText(file);
+ }
cleanup();
};
diff --git a/frontend/src/components/misc/DroppableTextarea.vue b/frontend/src/components/misc/DroppableTextarea.vue
index 9e90a7eac4..811bc924fa 100644
--- a/frontend/src/components/misc/DroppableTextarea.vue
+++ b/frontend/src/components/misc/DroppableTextarea.vue
@@ -57,6 +57,7 @@ import { pushNotification } from "@/store";
import { BBSpin } from "@/bbkit";
import { onMounted } from "vue";
import { NInput } from "naive-ui";
+import { readFileAsArrayBuffer } from "@/utils";
type LocalState = {
value: string | undefined;
@@ -101,7 +102,7 @@ watch(
(value) => emit("update:value", value)
);
-const onDrop = (files: File[] | FileList | null) => {
+const onDrop = async (files: File[] | FileList | null) => {
// called when files are dropped on zone
const file = head(files);
if (file) {
@@ -114,23 +115,23 @@ const onDrop = (files: File[] | FileList | null) => {
});
return;
}
- const fr = new FileReader();
- fr.addEventListener("load", (e) => {
- emit("update:value", fr.result as string);
- state.reading = false;
- });
- fr.addEventListener("error", () => {
+
+ state.reading = true;
+ try {
+ const { arrayBuffer } = await readFileAsArrayBuffer(file);
+ // TODO(steven): let user choose encoding.
+ const decoder = new TextDecoder("utf-8");
+ const statement = decoder.decode(arrayBuffer);
+ emit("update:value", statement);
+ } catch (error) {
pushNotification({
module: "bytebase",
style: "WARN",
title: `Read file error`,
- description: String(fr.error),
+ description: String(error),
});
- state.reading = false;
- return;
- });
+ }
state.reading = true;
- fr.readAsText(file);
}
};
diff --git a/frontend/src/utils/dom.ts b/frontend/src/utils/dom.ts
index a5fa28c990..ceb6817ef2 100644
--- a/frontend/src/utils/dom.ts
+++ b/frontend/src/utils/dom.ts
@@ -1,6 +1,3 @@
-import { t } from "@/plugins/i18n";
-import { pushNotification } from "@/store";
-
export const isDescendantOf = (
node: Element | null | undefined,
selector: string
@@ -27,69 +24,6 @@ export const findAncestor = (
return undefined;
};
-export const readFileAsync = (
- event: Event,
- maxFileSizeMB: number
-): Promise<{
- filename: string;
- content: string;
-}> => {
- return new Promise((resolve, reject) => {
- const target = event.target as HTMLInputElement;
- const file = (target.files || [])[0];
- const cleanup = () => {
- // Note that once selected a file, selecting the same file again will not
- // trigger <input type="file">'s change event.
- // So we need to do some cleanup stuff here.
- target.files = null;
- target.value = "";
- };
-
- if (!file) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: "File not found",
- });
- cleanup();
- reject();
- return;
- }
- if (file.size > maxFileSizeMB * 1024 * 1024) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: t("issue.upload-sql-file-max-size-exceeded", {
- size: `${maxFileSizeMB}MB`,
- }),
- });
- cleanup();
- reject();
- return;
- }
-
- const fr = new FileReader();
- fr.onload = async () => {
- const content = fr.result as string;
- resolve({
- filename: file.name,
- content: content,
- });
- };
- fr.onerror = () => {
- pushNotification({
- module: "bytebase",
- style: "WARN",
- title: "Read file error",
- description: String(fr.error),
- });
- reject();
- };
- fr.readAsText(file);
- cleanup();
- });
-};
-
export const getScrollParent = (
element: HTMLElement | null | undefined
): HTMLElement => {
diff --git a/frontend/src/utils/sheet.ts b/frontend/src/utils/sheet.ts
index c44cb5a97c..28816dde0a 100644
--- a/frontend/src/utils/sheet.ts
+++ b/frontend/src/utils/sheet.ts
@@ -1,5 +1,30 @@
import Long from "long";
+// MAX_UPLOAD_FILE_SIZE_MB is the maximum size of the file that can be uploaded in MB.
+export const MAX_UPLOAD_FILE_SIZE_MB = 1;
+
export const getStatementSize = (statement: string): Long => {
return Long.fromNumber(new TextEncoder().encode(statement).length);
};
+
+export const readFileAsArrayBuffer = (
+ file: File
+): Promise<{
+ filename: string;
+ arrayBuffer: ArrayBuffer;
+}> => {
+ return new Promise((resolve, reject) => {
+ const fr = new FileReader();
+ fr.onload = async () => {
+ const arrayBuffer = fr.result as ArrayBuffer;
+ resolve({
+ filename: file.name,
+ arrayBuffer: arrayBuffer,
+ });
+ };
+ fr.onerror = () => {
+ reject(String(fr.error));
+ };
+ fr.readAsArrayBuffer(file);
+ });
+};