From 313f77eefe9c75541cd9c8f30987a47ddff6ad1d Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 19 Jul 2022 23:33:41 +0530 Subject: [PATCH] feat(gui-v2): add ssl file select option, show monaco editor for config edit Signed-off-by: Pranav C --- .../nc-gui-v2/components/monaco/index.vue | 40 ++++ packages/nc-gui-v2/components/monaco/json.vue | 20 -- .../pages/projects/index/create-external.vue | 215 +++++++++++------- packages/nc-gui-v2/utils/fileUtils.ts | 42 ++++ .../nc-gui-v2/utils/projectCreateUtils.ts | 110 +++++---- 5 files changed, 263 insertions(+), 164 deletions(-) create mode 100644 packages/nc-gui-v2/components/monaco/index.vue delete mode 100644 packages/nc-gui-v2/components/monaco/json.vue diff --git a/packages/nc-gui-v2/components/monaco/index.vue b/packages/nc-gui-v2/components/monaco/index.vue new file mode 100644 index 0000000000..3fc97e7325 --- /dev/null +++ b/packages/nc-gui-v2/components/monaco/index.vue @@ -0,0 +1,40 @@ + + + + + diff --git a/packages/nc-gui-v2/components/monaco/json.vue b/packages/nc-gui-v2/components/monaco/json.vue deleted file mode 100644 index b813065307..0000000000 --- a/packages/nc-gui-v2/components/monaco/json.vue +++ /dev/null @@ -1,20 +0,0 @@ - - - - - diff --git a/packages/nc-gui-v2/pages/projects/index/create-external.vue b/packages/nc-gui-v2/pages/projects/index/create-external.vue index e579930582..31ffe7e0b8 100644 --- a/packages/nc-gui-v2/pages/projects/index/create-external.vue +++ b/packages/nc-gui-v2/pages/projects/index/create-external.vue @@ -3,7 +3,6 @@ import { ref } from 'vue' import { useI18n } from 'vue-i18n' import { useToast } from 'vue-toastification' import { Form } from 'ant-design-vue' -import form from '../../../../nc-gui/components/project/spreadsheet/mixins/form' import { navigateTo, useNuxtApp } from '#app' import { extractSdkResponseErrorMsg } from '~/utils/errorUtils' import { @@ -14,7 +13,8 @@ import { projectTitleValidator, sslUsage, } from '~/utils/projectCreateUtils' -import MaterialSymbolsRocketLaunchOutline from '~icons/material-symbols/rocket-launch-outline' + +import { readFile } from '~/utils/fileUtils' const useForm = Form.useForm const name = ref('') @@ -69,10 +69,6 @@ const testConnection = async () => { ...projectDatasource.value.connection, database: getTestDatabaseName(projectDatasource.value), }, - inflection: { - tableName: 'camelize', - columnName: 'camelize', - }, } const result = await $api.utils.testConnection(testConnectionConfig) @@ -90,7 +86,13 @@ const testConnection = async () => { } } -const formState = reactive>({ dataSource: { ...getDefaultConnectionConfig('mysql2') } }) +const formState = reactive>({ + dataSource: { ...getDefaultConnectionConfig('mysql2') }, + inflection: { + tableName: 'camelize', + columnName: 'camelize', + }, +}) const validators = reactive({ 'title': [ @@ -121,12 +123,21 @@ watch( () => formState.title, (v) => (formState.dataSource.connection.database = `${v}_noco`), ) + +const caFileInput = ref() +const keyFileInput = ref() +const certFileInput = ref() + +const onFileSelect = (key: 'ca' | 'cert' | 'key', el: HTMLInputElement) => { + readFile(el, (content) => { + formState.dataSource.connection.ssl[key] = content ?? '' + }) +} diff --git a/packages/nc-gui-v2/utils/fileUtils.ts b/packages/nc-gui-v2/utils/fileUtils.ts index c18edc0c68..7bc2de0d2d 100644 --- a/packages/nc-gui-v2/utils/fileUtils.ts +++ b/packages/nc-gui-v2/utils/fileUtils.ts @@ -5,3 +5,45 @@ const isImage = (name: string, mimetype?: string) => { } export { isImage, imageExt } +// Ref : https://stackoverflow.com/a/12002275 + +// Tested in Mozilla Firefox browser, Chrome +export function readFile(FileElement: HTMLInputElement, CallBackFunction: (content?: any) => void) { + try { + if (!FileElement.files || !FileElement.files.length) { + return CallBackFunction() + } + + const file = FileElement.files[0] + + if (file) { + const reader = new FileReader() + reader.readAsText(file, 'UTF-8') + reader.onload = function (evt) { + CallBackFunction(evt.target?.result) + } + reader.onerror = function () { + CallBackFunction() + } + } + } catch (Exception) { + const fallBack = ieReadFile(FileElement.value) + // eslint-disable-next-line eqeqeq + if (fallBack != false) { + CallBackFunction(fallBack) + } + } +} + +/// Reading files with Internet Explorer +function ieReadFile(filename: string) { + try { + const fso = new ActiveXObject('Scripting.FileSystemObject') + const fh = fso.OpenTextFile(filename, 1) + const contents = fh.ReadAll() + fh.Close() + return contents + } catch (Exception) { + return false + } +} diff --git a/packages/nc-gui-v2/utils/projectCreateUtils.ts b/packages/nc-gui-v2/utils/projectCreateUtils.ts index a1b597d984..8e6514e931 100644 --- a/packages/nc-gui-v2/utils/projectCreateUtils.ts +++ b/packages/nc-gui-v2/utils/projectCreateUtils.ts @@ -29,7 +29,7 @@ export const clientTypes = [ }, { text: 'SQLite', - value: 'sqlite', + value: 'sqlite3', }, ] @@ -42,11 +42,11 @@ const sampleConnectionData = { password: 'password', database: '_test', searchPath: ['public'], - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, mysql2: { host: 'localhost', @@ -54,11 +54,11 @@ const sampleConnectionData = { user: 'root', password: 'password', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, vitess: { host: 'localhost', @@ -66,11 +66,11 @@ const sampleConnectionData = { user: 'root', password: 'password', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, tidb: { host: 'localhost', @@ -78,11 +78,11 @@ const sampleConnectionData = { user: 'root', password: '', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, yugabyte: { host: 'localhost', @@ -90,11 +90,11 @@ const sampleConnectionData = { user: 'postgres', password: '', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, citusdb: { host: 'localhost', @@ -102,11 +102,11 @@ const sampleConnectionData = { user: 'postgres', password: '', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, cockroachdb: { host: 'localhost', @@ -114,11 +114,11 @@ const sampleConnectionData = { user: 'postgres', password: '', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, greenplum: { host: 'localhost', @@ -126,11 +126,11 @@ const sampleConnectionData = { user: 'postgres', password: '', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, mssql: { host: 'localhost', @@ -139,11 +139,11 @@ const sampleConnectionData = { password: 'Password123.', database: '_test', searchPath: ['dbo'], - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, oracledb: { host: 'localhost', @@ -151,11 +151,11 @@ const sampleConnectionData = { user: 'system', password: 'Oracle18', database: '_test', - // ssl: { - // ca: '', - // key: '', - // cert: '', - // }, + ssl: { + ca: '', + key: '', + cert: '', + }, }, sqlite3: { client: 'sqlite3', @@ -187,10 +187,4 @@ export const fieldRequiredValidator = { message: 'Field is required', } -export const sslUsage = { - 'No': 'No', - 'Preferred': 'Preferred', - 'Required': 'pg', - 'Required-CA': 'Required-CA', - 'Required-IDENTITY': 'Required-IDENTITY', -} +export const sslUsage = ['No', 'Preferred', 'Required', 'Required-CA', 'Required-IDENTITY']