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:
MyPrototypeWhat 2025-08-01 16:51:11 +08:00
parent 287bab75f6
commit b3aada01d8
3 changed files with 8 additions and 3 deletions

View File

@ -50,7 +50,9 @@ export const ImageGenerationMiddleware: CompletionsMiddleware =
if (!block.file) return null if (!block.file) return null
const binaryData: Uint8Array = await FileManager.readBinaryImage(block.file) const binaryData: Uint8Array = await FileManager.readBinaryImage(block.file)
const mimeType = `${block.file.type}/${block.file.ext.slice(1)}` 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[]) imageFiles = imageFiles.concat(userImages.filter(Boolean) as Blob[])

View File

@ -36,7 +36,7 @@ const ImageViewer: React.FC<ImageViewerProps> = ({ src, style, ...props }) => {
if (!match) throw new Error('无效的 base64 图片格式') if (!match) throw new Error('无效的 base64 图片格式')
const mimeType = match[1] const mimeType = match[1]
const byteArray = Base64.toUint8Array(match[2]) 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 })]) await navigator.clipboard.write([new ClipboardItem({ [mimeType]: blob })])
} else if (src.startsWith('file://')) { } else if (src.startsWith('file://')) {
// 处理本地文件路径 // 处理本地文件路径

View File

@ -62,7 +62,10 @@ const MessageImage: FC<Props> = ({ block }) => {
byteArrays.push(byteArray) 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 })]) await navigator.clipboard.write([new ClipboardItem({ [mimeType]: blob })])
} else { } else {
throw new Error('无效的 base64 图片格式') throw new Error('无效的 base64 图片格式')