Skip to content

Commit

Permalink
🐛 修复未正确获取图片缓存导致的崩溃问题 (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
A-kirami authored Mar 1, 2024
1 parent d2e0285 commit c787e2a
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@imengyu/vue3-context-menu": "^1.3.8",
"@msgpack/msgpack": "3.0.0-beta2",
"@tauri-apps/api": "2.0.0-beta.3",
"@tauri-apps/plugin-fs": "2.0.0-beta.1",
"@tauri-apps/plugin-log": "2.0.0-beta.1",
"@tauri-apps/plugin-os": "2.0.0-beta.1",
"@tauri-apps/plugin-websocket": "2.0.0-beta.1",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

20 changes: 20 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tauri-build = { version = "2.0.0-beta", features = [] }
[dependencies]
tauri = { version = "2.0.0-beta", features = ["macos-private-api", "devtools"] }
tauri-plugin-os = "2.0.0-beta"
tauri-plugin-fs = "2.0.0-beta"
tauri-plugin-log = "2.0.0-beta"
tauri-plugin-websocket = "2.0.0-beta"
serde = { version = "1.0", features = ["derive"] }
Expand Down
6 changes: 5 additions & 1 deletion src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"os:allow-arch",
"os:allow-platform",
"os:allow-os-type",
"os:allow-version"
"os:allow-version",
{
"identifier": "fs:allow-exists",
"allow": [{ "path": "$APPCACHE/**" }]
}
]
}
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn run() {
Ok(())
})
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_websocket::init())
.plugin(
tauri_plugin_log::Builder::new()
Expand Down
9 changes: 9 additions & 0 deletions src/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare global {
const getUserAge: (typeof import('./utils/chat'))['getUserAge']
const getUserAvatar: (typeof import('./utils/chat'))['getUserAvatar']
const getUserNickname: (typeof import('./utils/chat'))['getUserNickname']
const getVersion: (typeof import('./utils/metadata'))['getVersion']
const h: (typeof import('vue'))['h']
const ignorableWatch: (typeof import('@vueuse/core'))['ignorableWatch']
const inject: (typeof import('vue'))['inject']
Expand Down Expand Up @@ -249,11 +250,13 @@ declare global {
const useScrollLock: (typeof import('@vueuse/core'))['useScrollLock']
const useSessionStorage: (typeof import('@vueuse/core'))['useSessionStorage']
const useSessionStore: (typeof import('./stores/session'))['useSessionStore']
const useSettingStore: (typeof import('./stores/setting'))['useSettingStore']
const useShare: (typeof import('@vueuse/core'))['useShare']
const useSlots: (typeof import('vue'))['useSlots']
const useSorted: (typeof import('@vueuse/core'))['useSorted']
const useSpeechRecognition: (typeof import('@vueuse/core'))['useSpeechRecognition']
const useSpeechSynthesis: (typeof import('@vueuse/core'))['useSpeechSynthesis']
const useStateStore: (typeof import('./stores/state'))['useStateStore']
const useStatusStore: (typeof import('./stores/status'))['useStatusStore']
const useStepper: (typeof import('@vueuse/core'))['useStepper']
const useStorage: (typeof import('@vueuse/core'))['useStorage']
Expand Down Expand Up @@ -380,6 +383,7 @@ declare module 'vue' {
readonly getUserAge: UnwrapRef<(typeof import('./utils/chat'))['getUserAge']>
readonly getUserAvatar: UnwrapRef<(typeof import('./utils/chat'))['getUserAvatar']>
readonly getUserNickname: UnwrapRef<(typeof import('./utils/chat'))['getUserNickname']>
readonly getVersion: UnwrapRef<(typeof import('./utils/metadata'))['getVersion']>
readonly h: UnwrapRef<(typeof import('vue'))['h']>
readonly ignorableWatch: UnwrapRef<(typeof import('@vueuse/core'))['ignorableWatch']>
readonly inject: UnwrapRef<(typeof import('vue'))['inject']>
Expand Down Expand Up @@ -579,11 +583,13 @@ declare module 'vue' {
readonly useScrollLock: UnwrapRef<(typeof import('@vueuse/core'))['useScrollLock']>
readonly useSessionStorage: UnwrapRef<(typeof import('@vueuse/core'))['useSessionStorage']>
readonly useSessionStore: UnwrapRef<(typeof import('./stores/session'))['useSessionStore']>
readonly useSettingStore: UnwrapRef<(typeof import('./stores/setting'))['useSettingStore']>
readonly useShare: UnwrapRef<(typeof import('@vueuse/core'))['useShare']>
readonly useSlots: UnwrapRef<(typeof import('vue'))['useSlots']>
readonly useSorted: UnwrapRef<(typeof import('@vueuse/core'))['useSorted']>
readonly useSpeechRecognition: UnwrapRef<(typeof import('@vueuse/core'))['useSpeechRecognition']>
readonly useSpeechSynthesis: UnwrapRef<(typeof import('@vueuse/core'))['useSpeechSynthesis']>
readonly useStateStore: UnwrapRef<(typeof import('./stores/state'))['useStateStore']>
readonly useStatusStore: UnwrapRef<(typeof import('./stores/status'))['useStatusStore']>
readonly useStepper: UnwrapRef<(typeof import('@vueuse/core'))['useStepper']>
readonly useStorage: UnwrapRef<(typeof import('@vueuse/core'))['useStorage']>
Expand Down Expand Up @@ -688,6 +694,7 @@ declare module '@vue/runtime-core' {
readonly getUserAge: UnwrapRef<(typeof import('./utils/chat'))['getUserAge']>
readonly getUserAvatar: UnwrapRef<(typeof import('./utils/chat'))['getUserAvatar']>
readonly getUserNickname: UnwrapRef<(typeof import('./utils/chat'))['getUserNickname']>
readonly getVersion: UnwrapRef<(typeof import('./utils/metadata'))['getVersion']>
readonly h: UnwrapRef<(typeof import('vue'))['h']>
readonly ignorableWatch: UnwrapRef<(typeof import('@vueuse/core'))['ignorableWatch']>
readonly inject: UnwrapRef<(typeof import('vue'))['inject']>
Expand Down Expand Up @@ -887,11 +894,13 @@ declare module '@vue/runtime-core' {
readonly useScrollLock: UnwrapRef<(typeof import('@vueuse/core'))['useScrollLock']>
readonly useSessionStorage: UnwrapRef<(typeof import('@vueuse/core'))['useSessionStorage']>
readonly useSessionStore: UnwrapRef<(typeof import('./stores/session'))['useSessionStore']>
readonly useSettingStore: UnwrapRef<(typeof import('./stores/setting'))['useSettingStore']>
readonly useShare: UnwrapRef<(typeof import('@vueuse/core'))['useShare']>
readonly useSlots: UnwrapRef<(typeof import('vue'))['useSlots']>
readonly useSorted: UnwrapRef<(typeof import('@vueuse/core'))['useSorted']>
readonly useSpeechRecognition: UnwrapRef<(typeof import('@vueuse/core'))['useSpeechRecognition']>
readonly useSpeechSynthesis: UnwrapRef<(typeof import('@vueuse/core'))['useSpeechSynthesis']>
readonly useStateStore: UnwrapRef<(typeof import('./stores/state'))['useStateStore']>
readonly useStatusStore: UnwrapRef<(typeof import('./stores/status'))['useStatusStore']>
readonly useStepper: UnwrapRef<(typeof import('@vueuse/core'))['useStepper']>
readonly useStorage: UnwrapRef<(typeof import('@vueuse/core'))['useStorage']>
Expand Down
5 changes: 5 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ declare module 'vue' {
ASkeleton: (typeof import('ant-design-vue/es'))['Skeleton']
ASwitch: (typeof import('ant-design-vue/es'))['Switch']
ATextarea: (typeof import('ant-design-vue/es'))['Textarea']
Audio: (typeof import('./components/chat/multimedia/Audio.vue'))['default']
Avatar: (typeof import('./components/Avatar.vue'))['default']
ChatAction: (typeof import('./components/ChatAction.vue'))['default']
ChatBox: (typeof import('./components/ChatBox.vue'))['default']
Expand All @@ -43,8 +44,11 @@ declare module 'vue' {
CreateMemberForm: (typeof import('./components/CreateMemberForm.vue'))['default']
CreateUserForm: (typeof import('./components/CreateUserForm.vue'))['default']
HighlightCode: (typeof import('./components/HighlightCode.vue'))['default']
Image: (typeof import('./components/chat/multimedia/Image.vue'))['default']
JumbotronDev: (typeof import('./components/JumbotronDev.vue'))['default']
Link: (typeof import('./components/chat/multimedia/Link.vue'))['default']
Loading: (typeof import('./components/Loading.vue'))['default']
Mention: (typeof import('./components/chat/multimedia/Mention.vue'))['default']
MessageContent: (typeof import('./components/MessageContent.vue'))['default']
RouterLink: (typeof import('vue-router'))['RouterLink']
RouterView: (typeof import('vue-router'))['RouterView']
Expand All @@ -55,6 +59,7 @@ declare module 'vue' {
ThemeSwitch: (typeof import('./components/ThemeSwitch.vue'))['default']
TimeSeparator: (typeof import('./components/TimeSeparator.vue'))['default']
Titlebar: (typeof import('./components/Titlebar.vue'))['default']
Video: (typeof import('./components/chat/multimedia/Video.vue'))['default']
WaveAudioPlayer: (typeof import('./components/WaveAudioPlayer.vue'))['default']
}
}
33 changes: 24 additions & 9 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { invoke } from '@tauri-apps/api/core'
import { appCacheDir, join } from '@tauri-apps/api/path'
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs'

import { CacheFile } from '~/database/model'

interface FileSource {
str?: string
Expand All @@ -23,33 +26,45 @@ export async function createFileCache(
file: string | File,
validateType: string | null = null
): Promise<{ id: string; url: string }> {
const cacheFile = await getCacheFile(file)
if (cacheFile) {
return {
id: cacheFile.id,
url: await getFile(GetType.URL, cacheFile.id),
}
}
const fileSource: FileSource = {}
if (file instanceof File) {
const buffer = await file.arrayBuffer()
const fileSHA256 = await getSHA256(buffer)
const cacheFile = await db.files.where({ sha256: fileSHA256 }).first()
if (cacheFile) {
return {
id: cacheFile.id,
url: await getFile(GetType.URL, cacheFile.id),
}
}
fileSource.binary = Array.from(new Uint8Array(buffer))
} else {
fileSource.str = file
}
const fileId = getUUID()
const { size, sha256 } = await invoke<{ size: number; sha256: string }>('create_cache_file', {
fileSource,
validateType,
})
const fileId = getUUID()
await db.files.add({ id: fileId, name: file instanceof File ? file.name : sha256, size, sha256 })
return {
id: fileId,
url: await getFile(GetType.URL, fileId),
}
}

async function getCacheFile(file: string | File): Promise<CacheFile | undefined> {
if (file instanceof File) {
const buffer = await file.arrayBuffer()
const fileSHA256 = await getSHA256(buffer)
return await db.files.where({ sha256: fileSHA256 }).first()
} else {
const cacheFile = await db.files.where({ id: file }).first()
if (cacheFile && (await exists(`cache/${cacheFile.sha256}`, { baseDir: BaseDirectory.AppCache }))) {
return cacheFile
}
}
}

export const enum GetType {
URL = 'url',
PATH = 'path',
Expand Down

0 comments on commit c787e2a

Please sign in to comment.