MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / tryReadImageFromPath

Function tryReadImageFromPath

source code/utils/imagePaste.ts:353–418  ·  view source on GitHub ↗
(
  text: string,
)

Source from the content-addressed store, hash-verified

351 * @returns Object containing the image path and base64 data, or null if not found
352 */
353export async function tryReadImageFromPath(
354 text: string,
355): Promise<(ImageWithDimensions & { path: string }) | null> {
356 // Strip terminal added spaces or quotes to dragged in paths
357 const cleanedPath = asImageFilePath(text)
358
359 if (!cleanedPath) {
360 return null
361 }
362
363 const imagePath = cleanedPath
364 let imageBuffer
365
366 try {
367 if (isAbsolute(imagePath)) {
368 imageBuffer = getFsImplementation().readFileBytesSync(imagePath)
369 } else {
370 // VSCode Terminal just grabs the text content which is the filename
371 // instead of getting the full path of the file pasted with cmd-v. So
372 // we check if it matches the filename of the image in the clipboard.
373 const clipboardPath = await getImagePathFromClipboard()
374 if (clipboardPath && imagePath === basename(clipboardPath)) {
375 imageBuffer = getFsImplementation().readFileBytesSync(clipboardPath)
376 }
377 }
378 } catch (e) {
379 logError(e as Error)
380 return null
381 }
382 if (!imageBuffer) {
383 return null
384 }
385 if (imageBuffer.length === 0) {
386 logForDebugging(`Image file is empty: ${imagePath}`, { level: 'warn' })
387 return null
388 }
389
390 // BMP is not supported by the API — convert to PNG via Sharp.
391 if (
392 imageBuffer.length >= 2 &&
393 imageBuffer[0] === 0x42 &&
394 imageBuffer[1] === 0x4d
395 ) {
396 const sharp = await getImageProcessor()
397 imageBuffer = await sharp(imageBuffer).png().toBuffer()
398 }
399
400 // Resize if needed to stay under 5MB API limit
401 // Extract extension from path for format hint
402 const ext = extname(imagePath).slice(1).toLowerCase() || 'png'
403 const resized = await maybeResizeAndDownsampleImageBuffer(
404 imageBuffer,
405 imageBuffer.length,
406 ext,
407 )
408 const base64Image = resized.buffer.toString('base64')
409
410 // Detect format from the actual file contents using magic bytes

Callers 1

usePasteHandlerFunction · 0.85

Calls 9

asImageFilePathFunction · 0.85
getFsImplementationFunction · 0.85
logForDebuggingFunction · 0.85
getImageProcessorFunction · 0.85
logErrorFunction · 0.70
toStringMethod · 0.65

Tested by

no test coverage detected