Files
cherry-studio/src/renderer/src/services/ImageStorage.ts
T
自由的世界人 2989913f14 feat: add model provider logo upload (#4408)
* feat: add model provider logo upload

* Update index.tsx

* fix: upload image delete
2025-04-09 23:52:42 +08:00

51 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import db from '@renderer/databases'
import { convertToBase64 } from '@renderer/utils'
const IMAGE_PREFIX = 'image://'
export default class ImageStorage {
static async set(key: string, value: File | string) {
const id = IMAGE_PREFIX + key
try {
if (typeof value === 'string') {
// stringemoji
if (await db.settings.get(id)) {
db.settings.update(id, { value })
return
}
await db.settings.add({ id, value })
} else {
// file image
const base64Image = await convertToBase64(value)
if (typeof base64Image === 'string') {
if (await db.settings.get(id)) {
db.settings.update(id, { value: base64Image })
return
}
await db.settings.add({ id, value: base64Image })
}
}
} catch (error) {
console.error('Error storing the image', error)
}
}
static async get(key: string): Promise<string> {
const id = IMAGE_PREFIX + key
return (await db.settings.get(id))?.value
}
static async remove(key: string): Promise<void> {
const id = IMAGE_PREFIX + key
try {
const record = await db.settings.get(id)
if (record) {
await db.settings.delete(id)
}
} catch (error) {
console.error('Error removing the image', error)
throw error
}
}
}