forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-saver.ts
More file actions
97 lines (82 loc) · 2.83 KB
/
Copy pathimage-saver.ts
File metadata and controls
97 lines (82 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Image Saving Utility
*
* Handles saving generated images to disk and returning file paths.
*/
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
/**
* Default directory for saving generated images.
* Uses ~/.opencode/generated-images/
*/
function getImageOutputDir(): string {
const homeDir = os.homedir();
const outputDir = path.join(homeDir, '.opencode', 'generated-images');
// Create directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
return outputDir;
}
/**
* Generate a unique filename for the image.
*/
function generateImageFilename(mimeType: string): string {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const random = Math.random().toString(36).substring(2, 8);
// Determine extension from mime type
let ext = 'png';
if (mimeType.includes('jpeg') || mimeType.includes('jpg')) {
ext = 'jpg';
} else if (mimeType.includes('gif')) {
ext = 'gif';
} else if (mimeType.includes('webp')) {
ext = 'webp';
}
return `image-${timestamp}-${random}.${ext}`;
}
/**
* Save base64 image data to disk and return the file path.
*
* @param base64Data - The base64-encoded image data
* @param mimeType - The MIME type of the image (e.g., "image/jpeg")
* @returns The absolute path to the saved image file
*/
export function saveImageToDisk(base64Data: string, mimeType: string): string {
try {
const outputDir = getImageOutputDir();
const filename = generateImageFilename(mimeType);
const filePath = path.join(outputDir, filename);
// Decode base64 and write to file
const buffer = Buffer.from(base64Data, 'base64');
fs.writeFileSync(filePath, buffer);
return filePath;
} catch (error) {
// If saving fails, return empty string (caller will fall back to base64)
console.error('[image-saver] Failed to save image:', error);
return '';
}
}
/**
* Process inlineData and return either a file path or base64 data URL.
* Attempts to save to disk first, falls back to base64 if saving fails.
*
* @param inlineData - Object containing mimeType and base64 data
* @returns Markdown image string with either file path or data URL
*/
export function processImageData(inlineData: { mimeType?: string; data?: string }): string | null {
const mimeType = inlineData.mimeType || 'image/png';
const data = inlineData.data;
if (!data) {
return null;
}
// Try to save to disk first
const filePath = saveImageToDisk(data, mimeType);
if (filePath) {
// Successfully saved - return file path with open command hint
return `\n\nImage saved to: \`${filePath}\`\n\nTo view: \`open "${filePath}"\``;
}
// Fall back to base64 data URL
return ``;
}