* Chore: Pretier for consistant formatting - TODO: This PR needs to be updated by Saoud after he runs `npm install` & `npm format:fix` and commits the results of the prettier changes. * Revert prettier config * Run npm install * Fix prettier config and ignore package lock * Run format --------- Co-authored-by: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com>
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import * as vscode from "vscode"
|
|
import fs from "fs/promises"
|
|
import * as path from "path"
|
|
|
|
export async function selectImages(): Promise<string[]> {
|
|
const options: vscode.OpenDialogOptions = {
|
|
canSelectMany: true,
|
|
openLabel: "Select",
|
|
filters: {
|
|
Images: ["png", "jpg", "jpeg", "webp"], // supported by anthropic and openrouter
|
|
},
|
|
}
|
|
|
|
const fileUris = await vscode.window.showOpenDialog(options)
|
|
|
|
if (!fileUris || fileUris.length === 0) {
|
|
return []
|
|
}
|
|
|
|
return await Promise.all(
|
|
fileUris.map(async (uri) => {
|
|
const imagePath = uri.fsPath
|
|
const buffer = await fs.readFile(imagePath)
|
|
const base64 = buffer.toString("base64")
|
|
const mimeType = getMimeType(imagePath)
|
|
const dataUrl = `data:${mimeType};base64,${base64}`
|
|
return dataUrl
|
|
}),
|
|
)
|
|
}
|
|
|
|
function getMimeType(filePath: string): string {
|
|
const ext = path.extname(filePath).toLowerCase()
|
|
switch (ext) {
|
|
case ".png":
|
|
return "image/png"
|
|
case ".jpeg":
|
|
case ".jpg":
|
|
return "image/jpeg"
|
|
case ".webp":
|
|
return "image/webp"
|
|
default:
|
|
throw new Error(`Unsupported file type: ${ext}`)
|
|
}
|
|
}
|