Skip to content

Commit 2a40b30

Browse files
authored
Merge pull request #43 from MiniMax-AI-Dev/fix/mime-type-detection
fix(commands): detect MIME type from file extension for images
2 parents 46f460f + 1bef972 commit 2a40b30

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

@@ -73,7 +78,9 @@ export default defineCommand({
7378
} else {
7479
const imgPath = resolve(params.image);
7580
const imgData = readFileSync(imgPath);
76-
ref.image_file = `data:image/jpeg;base64,${imgData.toString('base64')}`;
81+
const ext = extname(imgPath).toLowerCase();
82+
const mime = MIME_TYPES[ext] || 'image/jpeg';
83+
ref.image_file = `data:${mime};base64,${imgData.toString('base64')}`;
7784
}
7885
}
7986

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)