mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 12:51:26 +08:00
fix: ensure Blob creation uses a copy of byte arrays for image handling
- Updated Blob creation in ImageGenerationMiddleware, ImageViewer, and MessageImage components to use `slice()` on byte arrays, preventing potential mutations of the original data.
This commit is contained in:
parent
287bab75f6
commit
b3aada01d8
@ -50,7 +50,9 @@ export const ImageGenerationMiddleware: CompletionsMiddleware =
|
||||
if (!block.file) return null
|
||||
const binaryData: Uint8Array = await FileManager.readBinaryImage(block.file)
|
||||
const mimeType = `${block.file.type}/${block.file.ext.slice(1)}`
|
||||
return await toFile(new Blob([binaryData]), block.file.origin_name || 'image.png', { type: mimeType })
|
||||
return await toFile(new Blob([binaryData.slice()]), block.file.origin_name || 'image.png', {
|
||||
type: mimeType
|
||||
})
|
||||
})
|
||||
)
|
||||
imageFiles = imageFiles.concat(userImages.filter(Boolean) as Blob[])
|
||||
|
||||
@ -36,7 +36,7 @@ const ImageViewer: React.FC<ImageViewerProps> = ({ src, style, ...props }) => {
|
||||
if (!match) throw new Error('无效的 base64 图片格式')
|
||||
const mimeType = match[1]
|
||||
const byteArray = Base64.toUint8Array(match[2])
|
||||
const blob = new Blob([byteArray], { type: mimeType })
|
||||
const blob = new Blob([byteArray.slice()], { type: mimeType })
|
||||
await navigator.clipboard.write([new ClipboardItem({ [mimeType]: blob })])
|
||||
} else if (src.startsWith('file://')) {
|
||||
// 处理本地文件路径
|
||||
|
||||
@ -62,7 +62,10 @@ const MessageImage: FC<Props> = ({ block }) => {
|
||||
byteArrays.push(byteArray)
|
||||
}
|
||||
|
||||
const blob = new Blob(byteArrays, { type: mimeType })
|
||||
const blob = new Blob(
|
||||
byteArrays.map((b) => b.slice()),
|
||||
{ type: mimeType }
|
||||
)
|
||||
await navigator.clipboard.write([new ClipboardItem({ [mimeType]: blob })])
|
||||
} else {
|
||||
throw new Error('无效的 base64 图片格式')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user