This commit is contained in:
duanfuxiang
2025-01-05 11:51:39 +08:00
commit 0c7ee142cb
215 changed files with 20611 additions and 0 deletions

34
src/utils/image.ts Normal file
View File

@@ -0,0 +1,34 @@
import { MentionableImage } from '../types/mentionable'
export function parseImageDataUrl(dataUrl: string): {
mimeType: string
base64Data: string
} {
const matches = dataUrl.match(/^data:([^;]+);base64,(.+)/)
if (!matches) {
throw new Error('Invalid image data URL format')
}
const [, mimeType, base64Data] = matches
return { mimeType, base64Data }
}
export async function fileToMentionableImage(
file: File,
): Promise<MentionableImage> {
const base64Data = await fileToBase64(file)
return {
type: 'image',
name: file.name,
mimeType: file.type,
data: base64Data,
}
}
function fileToBase64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result as string)
reader.onerror = () => reject(new Error('Failed to read file'))
})
}