mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(cli): preserve source extensions for generated media (#1650)
This commit is contained in:
@@ -116,6 +116,19 @@ describe('generatedImages', () => {
|
||||
clearGeneratedImages()
|
||||
})
|
||||
|
||||
it('preserves the source extension when a custom file name omits one', () => {
|
||||
const file = registerGeneratedImage({
|
||||
id: 'test-zip-with-title',
|
||||
path: '/tmp/plan-a.zip',
|
||||
fileName: 'Cursor Plan A Markdown 导出',
|
||||
mimeType: 'application/octet-stream',
|
||||
bytes: Buffer.from('PK\\x03\\x04')
|
||||
})
|
||||
|
||||
expect(file.fileName).toBe('Cursor Plan A Markdown 导出.zip')
|
||||
clearGeneratedImages()
|
||||
})
|
||||
|
||||
it('snapshots image bytes at registration time', () => {
|
||||
const source = Buffer.from('original image bytes')
|
||||
const image = registerGeneratedImage({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { basename } from 'node:path'
|
||||
import { basename, extname } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { open } from 'node:fs/promises'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
@@ -277,6 +277,14 @@ export function isInlineMediaMimeType(mimeType: string): boolean {
|
||||
return mimeType.startsWith('image/') || mimeType.startsWith('video/') || mimeType.startsWith('audio/')
|
||||
}
|
||||
|
||||
function preserveSourceExtension(fileName: string, path: string): string {
|
||||
const sourceExtension = extname(basename(path))
|
||||
if (!sourceExtension || extname(fileName)) {
|
||||
return fileName
|
||||
}
|
||||
return `${fileName}${sourceExtension}`
|
||||
}
|
||||
|
||||
export function detectDisplayMediaMimeType(bytes: Uint8Array): string {
|
||||
const imageMimeType = detectImageMimeType(bytes)
|
||||
if (imageMimeType) return imageMimeType
|
||||
@@ -312,9 +320,10 @@ export function registerGeneratedImage(args: { id: string; path: string; mimeTyp
|
||||
generatedImageBytes -= previous.content.byteLength
|
||||
}
|
||||
|
||||
const fallbackFileName = basename(args.path) || `${args.id}.png`
|
||||
const metadata: GeneratedImageMetadata = {
|
||||
id: args.id,
|
||||
fileName: args.fileName || basename(args.path) || `${args.id}.png`,
|
||||
fileName: preserveSourceExtension(args.fileName || fallbackFileName, args.path),
|
||||
content,
|
||||
mimeType: args.mimeType,
|
||||
createdAt: Date.now()
|
||||
|
||||
Reference in New Issue
Block a user