Skip to content

Commit 1bef972

Browse files
Elaine YINclaude
authored andcommitted
fix(commands): detect MIME type from file extension for images
video --first-frame and image --subject-ref hardcoded image/jpeg for all local files. PNG/WebP files sent with wrong MIME could be rejected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 920273d commit 1bef972

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/commands/image/generate.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import type { Config } from '../../config/schema';
77
import type { GlobalFlags } from '../../types/flags';
88
import type { ImageRequest, ImageResponse } from '../../types/api';
99
import { mkdirSync, existsSync, readFileSync } from 'fs';
10-
import { join, resolve } from 'path';
10+
import { join, resolve, extname } from 'path';
11+
12+
const MIME_TYPES: Record<string, string> = {
13+
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
14+
'.png': 'image/png', '.webp': 'image/webp',
15+
};
1116
import { isInteractive } from '../../utils/env';
1217
import { promptText, failIfMissing } from '../../utils/prompt';
1318

@@ -69,7 +74,9 @@ export default defineCommand({
6974
} else {
7075
const imgPath = resolve(params.image);
7176
const imgData = readFileSync(imgPath);
72-
ref.image_file = `data:image/jpeg;base64,${imgData.toString('base64')}`;
77+
const ext = extname(imgPath).toLowerCase();
78+
const mime = MIME_TYPES[ext] || 'image/jpeg';
79+
ref.image_file = `data:${mime};base64,${imgData.toString('base64')}`;
7380
}
7481
}
7582

src/commands/video/generate.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import type { Config } from '../../config/schema';
1010
import type { GlobalFlags } from '../../types/flags';
1111
import type { VideoRequest, VideoResponse, VideoTaskResponse, FileRetrieveResponse } from '../../types/api';
1212
import { readFileSync } from 'fs';
13+
import { extname } from 'path';
14+
15+
const MIME_TYPES: Record<string, string> = {
16+
'.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
17+
'.png': 'image/png', '.webp': 'image/webp',
18+
};
1319
import { isInteractive } from '../../utils/env';
1420
import { promptText, failIfMissing } from '../../utils/prompt';
1521

@@ -63,7 +69,9 @@ export default defineCommand({
6369
body.first_frame_image = framePath;
6470
} else {
6571
const imgData = readFileSync(framePath);
66-
body.first_frame_image = `data:image/jpeg;base64,${imgData.toString('base64')}`;
72+
const ext = extname(framePath).toLowerCase();
73+
const mime = MIME_TYPES[ext] || 'image/jpeg';
74+
body.first_frame_image = `data:${mime};base64,${imgData.toString('base64')}`;
6775
}
6876
}
6977

0 commit comments

Comments
 (0)