MCPcopy
hub / github.com/simstudioai/sim / handlePdfBuffer

Function handlePdfBuffer

apps/sim/app/api/files/parse/route.ts:914–966  ·  view source on GitHub ↗

* Handle a PDF buffer directly in memory

(
  fileBuffer: Buffer,
  filename: string,
  fileType?: string,
  originalPath?: string,
  maxParsedOutputBytes?: number
)

Source from the content-addressed store, hash-verified

912 * Handle a PDF buffer directly in memory
913 */
914async function handlePdfBuffer(
915 fileBuffer: Buffer,
916 filename: string,
917 fileType?: string,
918 originalPath?: string,
919 maxParsedOutputBytes?: number
920): Promise<ParseResult> {
921 try {
922 logger.info(`Parsing PDF in memory: ${filename}`)
923
924 const result = await parseBufferAsPdf(fileBuffer)
925
926 const content =
927 result.content ||
928 createPdfFallbackMessage(result.metadata?.pageCount || 0, fileBuffer.length, originalPath)
929 const limitedContent = assertParsedContentWithinLimit(content, maxParsedOutputBytes)
930
931 return {
932 success: true,
933 content: limitedContent,
934 filePath: originalPath || filename,
935 metadata: {
936 fileType: fileType || 'application/pdf',
937 size: fileBuffer.length,
938 hash: createHash('md5').update(fileBuffer).digest('hex'),
939 processingTime: 0,
940 },
941 }
942 } catch (error) {
943 if (isPayloadSizeLimitError(error)) throw error
944
945 logger.error('Failed to parse PDF in memory:', error)
946
947 const content = createPdfFailureMessage(
948 0,
949 fileBuffer.length,
950 originalPath || filename,
951 (error as Error).message
952 )
953
954 return {
955 success: true,
956 content,
957 filePath: originalPath || filename,
958 metadata: {
959 fileType: fileType || 'application/pdf',
960 size: fileBuffer.length,
961 hash: createHash('md5').update(fileBuffer).digest('hex'),
962 processingTime: 0,
963 },
964 }
965 }
966}
967
968/**
969 * Handle a CSV buffer directly in memory

Callers 2

handleExternalUrlFunction · 0.85
handleCloudFileFunction · 0.85

Calls 7

isPayloadSizeLimitErrorFunction · 0.90
parseBufferAsPdfFunction · 0.85
createPdfFallbackMessageFunction · 0.85
createPdfFailureMessageFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected