mirror of
https://github.com/haexhub/haex-hub.git
synced 2025-12-17 06:30:50 +01:00
zwischenstand
This commit is contained in:
@ -1,66 +1,76 @@
|
||||
|
||||
import * as schema from "@/../src-tauri/database/schemas/vault";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { platform } from "@tauri-apps/plugin-os";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { drizzle, SqliteRemoteDatabase } from "drizzle-orm/sqlite-proxy";
|
||||
import * as schema from '@/../src-tauri/database/schemas/vault'
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { platform } from '@tauri-apps/plugin-os'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import type { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy'
|
||||
import { drizzle } from 'drizzle-orm/sqlite-proxy'
|
||||
|
||||
interface IVault {
|
||||
name: string;
|
||||
drizzle: SqliteRemoteDatabase<typeof schema>;
|
||||
name: string
|
||||
drizzle: SqliteRemoteDatabase<typeof schema>
|
||||
}
|
||||
interface IOpenVaults {
|
||||
[vaultId: string]: IVault;
|
||||
[vaultId: string]: IVault
|
||||
}
|
||||
|
||||
export const useVaultStore = defineStore("vaultStore", () => {
|
||||
|
||||
export const useVaultStore = defineStore('vaultStore', () => {
|
||||
const currentVaultId = computed<string | undefined>({
|
||||
get: () => getSingleRouteParam(useRouter().currentRoute.value.params.vaultId),
|
||||
get: () =>
|
||||
getSingleRouteParam(useRouter().currentRoute.value.params.vaultId),
|
||||
set: (newVaultId) => {
|
||||
useRouter().currentRoute.value.params.vaultId = newVaultId ?? "";
|
||||
useRouter().currentRoute.value.params.vaultId = newVaultId ?? ''
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const defaultVaultName = ref("HaexHub")
|
||||
const defaultVaultName = ref('HaexHub')
|
||||
const currentVaultName = ref(defaultVaultName.value)
|
||||
|
||||
const read_only = computed<boolean>({
|
||||
get: () => {
|
||||
console.log("query showSidebar", useRouter().currentRoute.value.query.readonly);
|
||||
console.log(
|
||||
'query showSidebar',
|
||||
useRouter().currentRoute.value.query.readonly,
|
||||
)
|
||||
return JSON.parse(
|
||||
getSingleRouteParam(useRouter().currentRoute.value.query.readonly) || "false"
|
||||
);
|
||||
getSingleRouteParam(useRouter().currentRoute.value.query.readonly) ||
|
||||
'false',
|
||||
)
|
||||
},
|
||||
set: (readonly) => {
|
||||
const router = useRouter();
|
||||
const router = useRouter()
|
||||
router.replace({
|
||||
query: {
|
||||
...router.currentRoute.value.query,
|
||||
readonly: JSON.stringify(readonly ? true : false),
|
||||
},
|
||||
});
|
||||
})
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const openVaults = ref<IOpenVaults>({});
|
||||
const openVaults = ref<IOpenVaults>({})
|
||||
|
||||
const currentVault = computed(() => openVaults.value?.[currentVaultId.value ?? ""])
|
||||
const currentVault = computed(
|
||||
() => openVaults.value?.[currentVaultId.value ?? ''],
|
||||
)
|
||||
|
||||
|
||||
|
||||
const openAsync = async ({ path = "", password }: { path: string; password: string }) => {
|
||||
const openAsync = async ({
|
||||
path = '',
|
||||
password,
|
||||
}: {
|
||||
path: string
|
||||
password: string
|
||||
}) => {
|
||||
try {
|
||||
const result = await invoke<string>("open_encrypted_database", {
|
||||
const result = await invoke<string>('open_encrypted_database', {
|
||||
path,
|
||||
key: password,
|
||||
});
|
||||
})
|
||||
|
||||
if (result !== "success") throw new Error(result);
|
||||
if (result !== 'success') throw new Error(result)
|
||||
|
||||
const vaultId = await getVaultIdAsync(path);
|
||||
const seperator = platform() === "windows" ? "\\" : "/";
|
||||
const fileName = path.split(seperator).pop();
|
||||
const vaultId = await getVaultIdAsync(path)
|
||||
const seperator = platform() === 'windows' ? '\\' : '/'
|
||||
const fileName = path.split(seperator).pop()
|
||||
|
||||
openVaults.value = {
|
||||
...openVaults.value,
|
||||
@ -68,53 +78,65 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
name: fileName ?? path,
|
||||
drizzle: drizzle<typeof schema>(
|
||||
async (sql, params: unknown[], method) => {
|
||||
let rows: any = [];
|
||||
let results = [];
|
||||
let rows: unknown[] = []
|
||||
let results: any = []
|
||||
|
||||
// If the query is a SELECT, use the select method
|
||||
if (isSelectQuery(sql)) {
|
||||
console.log("sql_select", sql, params);
|
||||
rows = await invoke("sql_select", { sql, params }).catch((e) => {
|
||||
console.error("SQL select Error:", e, sql, params);
|
||||
return [];
|
||||
});
|
||||
console.log("select", rows);
|
||||
console.log('sql_select', sql, params)
|
||||
rows = await invoke<unknown[]>('sql_select', {
|
||||
sql,
|
||||
params,
|
||||
}).catch((e) => {
|
||||
console.error('SQL select Error:', e, sql, params)
|
||||
return []
|
||||
})
|
||||
console.log('select', rows)
|
||||
} else {
|
||||
// Otherwise, use the execute method
|
||||
rows = await invoke("sql_execute", { sql, params }).catch((e) => {
|
||||
console.error("SQL execute Error:", e, sql, params);
|
||||
return [];
|
||||
});
|
||||
return { rows: [] };
|
||||
rows = await invoke<unknown[]>('sql_execute', {
|
||||
sql,
|
||||
params,
|
||||
}).catch((e) => {
|
||||
console.error('SQL execute Error:', e, sql, params)
|
||||
return []
|
||||
})
|
||||
return { rows: [] }
|
||||
}
|
||||
|
||||
results = method === "all" ? rows : rows[0];
|
||||
results = method === 'all' ? rows : rows[0]
|
||||
|
||||
return { rows: results };
|
||||
return { rows: results }
|
||||
},
|
||||
{ schema: schema, logger: true }
|
||||
{ schema: schema, logger: true },
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const { addVaultAsync } = useLastVaultStore();
|
||||
const { addVaultAsync } = useLastVaultStore()
|
||||
await addVaultAsync({ path })
|
||||
|
||||
return vaultId;
|
||||
return vaultId
|
||||
} catch (error) {
|
||||
console.error("Error openAsync ", error);
|
||||
return false;
|
||||
console.error('Error openAsync ', error)
|
||||
return false
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const refreshDatabaseAsync = async () => {
|
||||
console.log("refreshDatabaseAsync");
|
||||
console.log('refreshDatabaseAsync')
|
||||
/* if (!currentVault.value?.database.close) {
|
||||
return navigateTo(useLocaleRoute()({ name: 'vaultOpen' }));
|
||||
} */
|
||||
};
|
||||
}
|
||||
|
||||
const createAsync = async ({ path, password }: { path: string; password: string }) => {
|
||||
const createAsync = async ({
|
||||
path,
|
||||
password,
|
||||
}: {
|
||||
path: string
|
||||
password: string
|
||||
}) => {
|
||||
/* const existDb = await exists('default.db', {
|
||||
baseDir: BaseDirectory.Resource,
|
||||
}); */
|
||||
@ -122,16 +144,16 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
/* const existDb = await resolveResource('resources/default.db');
|
||||
if (!existDb) throw new Error('Keine Datenbank da');
|
||||
await copyFile(existDb, path); */
|
||||
const result = await invoke("create_encrypted_database", {
|
||||
const result = await invoke('create_encrypted_database', {
|
||||
path,
|
||||
key: password,
|
||||
});
|
||||
console.log("create_encrypted_database", result);
|
||||
return await openAsync({ path, password });
|
||||
};
|
||||
})
|
||||
console.log('create_encrypted_database', result)
|
||||
return await openAsync({ path, password })
|
||||
}
|
||||
|
||||
const closeAsync = async () => {
|
||||
if (!currentVaultId.value) return;
|
||||
if (!currentVaultId.value) return
|
||||
|
||||
/* if (
|
||||
typeof openVaults.value?.[currentVaultId.value]?.database?.close ===
|
||||
@ -140,8 +162,8 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
console.log('close db', openVaults.value?.[currentVaultId.value]);
|
||||
return openVaults.value?.[currentVaultId.value]?.database?.close();
|
||||
} */
|
||||
delete openVaults.value?.[currentVaultId.value];
|
||||
};
|
||||
delete openVaults.value?.[currentVaultId.value]
|
||||
}
|
||||
|
||||
const syncLocaleAsync = async () => {
|
||||
try {
|
||||
@ -154,21 +176,25 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
|
||||
if (currentLocaleRow?.[0]?.value) {
|
||||
const currentLocale = app.$i18n.availableLocales.find(
|
||||
(locale) => locale === currentLocaleRow[0].value
|
||||
(locale) => locale === currentLocaleRow[0].value,
|
||||
)
|
||||
await app.$i18n.setLocale(currentLocale ?? app.$i18n.defaultLocale)
|
||||
} else {
|
||||
await currentVault.value?.drizzle
|
||||
.insert(schema.haexSettings)
|
||||
.values({ id: crypto.randomUUID(), key: 'locale', value: app.$i18n.locale.value })
|
||||
await currentVault.value?.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
key: 'locale',
|
||||
value: app.$i18n.locale.value,
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("ERROR syncLocaleAsync", error)
|
||||
console.log('ERROR syncLocaleAsync', error)
|
||||
}
|
||||
}
|
||||
|
||||
const syncThemeAsync = async () => {
|
||||
const { availableThemes, defaultTheme, currentTheme } = storeToRefs(useUiStore())
|
||||
const { availableThemes, defaultTheme, currentTheme } = storeToRefs(
|
||||
useUiStore(),
|
||||
)
|
||||
const currentThemeRow = await currentVault.value?.drizzle
|
||||
.select()
|
||||
.from(schema.haexSettings)
|
||||
@ -176,7 +202,7 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
|
||||
if (currentThemeRow?.[0]?.value) {
|
||||
const theme = availableThemes.value.find(
|
||||
(theme) => theme.name === currentThemeRow[0].value
|
||||
(theme) => theme.name === currentThemeRow[0].value,
|
||||
)
|
||||
currentTheme.value = theme ?? defaultTheme.value
|
||||
} else {
|
||||
@ -195,7 +221,8 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
.where(eq(schema.haexSettings.key, 'vaultName'))
|
||||
|
||||
if (currentVaultNameRow?.[0]?.value) {
|
||||
currentVaultName.value = currentVaultNameRow.at(0)?.value ?? defaultVaultName.value
|
||||
currentVaultName.value =
|
||||
currentVaultNameRow.at(0)?.value ?? defaultVaultName.value
|
||||
} else {
|
||||
await currentVault.value?.drizzle.insert(schema.haexSettings).values({
|
||||
id: crypto.randomUUID(),
|
||||
@ -206,8 +233,11 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
}
|
||||
|
||||
const updateVaultNameAsync = async (newVaultName?: string | null) => {
|
||||
console.log("set new vaultName", newVaultName)
|
||||
return currentVault.value?.drizzle.update(schema.haexSettings).set({ value: newVaultName ?? defaultVaultName.value }).where(eq(schema.haexSettings.key, "vaultName"))
|
||||
console.log('set new vaultName', newVaultName)
|
||||
return currentVault.value?.drizzle
|
||||
.update(schema.haexSettings)
|
||||
.set({ value: newVaultName ?? defaultVaultName.value })
|
||||
.where(eq(schema.haexSettings.key, 'vaultName'))
|
||||
}
|
||||
|
||||
return {
|
||||
@ -224,24 +254,21 @@ export const useVaultStore = defineStore("vaultStore", () => {
|
||||
syncThemeAsync,
|
||||
syncVaultNameAsync,
|
||||
updateVaultNameAsync,
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
const getVaultIdAsync = async (path: string) => {
|
||||
const encoder = new TextEncoder();
|
||||
const data = encoder.encode(path);
|
||||
const encoder = new TextEncoder()
|
||||
const data = encoder.encode(path)
|
||||
|
||||
const hashBuffer = await crypto.subtle.digest("SHA-256", data);
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
|
||||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); // convert bytes to hex string
|
||||
console.log("vaultId", hashHex);
|
||||
return hashHex;
|
||||
};
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array
|
||||
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join('') // convert bytes to hex string
|
||||
console.log('vaultId', hashHex)
|
||||
return hashHex
|
||||
}
|
||||
|
||||
const isSelectQuery = (sql: string) => {
|
||||
const selectRegex = /^\s*SELECT\b/i;
|
||||
return selectRegex.test(sql);
|
||||
};
|
||||
|
||||
|
||||
|
||||
const selectRegex = /^\s*SELECT\b/i
|
||||
return selectRegex.test(sql)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user