Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/sim/app/api/tools/agiloft/retrieve/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,35 @@ describe('POST /api/tools/agiloft/retrieve', () => {
expect(inputValidationMockFns.mockValidateUrlWithDNS).toHaveBeenCalledTimes(1)
})

it('resolves the real type when Agiloft labels an attachment octet-stream', async () => {
const fileBytes = Buffer.from('PKdocx-bytes', 'utf-8')

inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValueOnce(
mockSecureFetchResponse({
arrayBuffer: fileBytes.buffer.slice(
fileBytes.byteOffset,
fileBytes.byteOffset + fileBytes.byteLength
) as ArrayBuffer,
headers: new Headers({
'content-type': 'application/octet-stream',
'content-disposition': 'attachment; filename="Master Agreement.docx"',
}),
})
)

const response = await POST(createMockRequest('POST', baseBody))
const data = (await response.json()) as { output: { file: { mimeType: string } } }

/**
* Agiloft labels most attachments octet-stream whatever they are. The
* filename disambiguates what the leading bytes cannot: a ZIP header is
* equally a .docx, .xlsx or a plain archive.
*/
expect(data.output.file.mimeType).toBe(
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
})

it('propagates upstream errors', async () => {
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValueOnce(
mockSecureFetchResponse({ ok: false, status: 404, text: 'Record not found' })
Expand Down
12 changes: 10 additions & 2 deletions apps/sim/app/api/tools/agiloft/retrieve/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { checkInternalAuth } from '@/lib/auth/hybrid'
import { secureFetchWithPinnedIP } from '@/lib/core/security/input-validation.server'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { resolveEffectiveMimeType } from '@/lib/uploads/utils/file-utils'
import { isEwRestBody } from '@/tools/agiloft/ewrest'
import {
AGILOFT_MAX_ATTACHMENT_BYTES,
Expand Down Expand Up @@ -128,10 +129,17 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
)
}

/**
* Agiloft labels most attachments application/octet-stream whatever they
* are, so downstream consumers keyed on the header mis-handle them. The
* filename Agiloft sends in Content-Disposition carries the real type.
*/
const mimeType = resolveEffectiveMimeType(contentType, fileName)

logger.info(`[${requestId}] Attachment downloaded successfully`, {
name: fileName,
size: fileBuffer.length,
mimeType: contentType,
mimeType,
})

const base64Data = fileBuffer.toString('base64')
Expand All @@ -141,7 +149,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
output: {
file: {
name: fileName,
mimeType: contentType,
mimeType,
data: base64Data,
size: fileBuffer.length,
},
Expand Down
Loading