Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.6 KB
Raw Blame
import { Transform } from 'node:stream'
import { ActivepiecesError, ErrorCode } from '@activepieces/core-utils'
import { FileReadToken, FileType } from '@activepieces/shared'
import dayjs from 'dayjs'
import { domainHelper } from '../helper/domain-helper'
import { JwtAudience, JwtSignAlgorithm, jwtUtils } from '../helper/jwt-utils'
import { system } from '../helper/system/system'
import { AppSystemProp } from '../helper/system/system-props'

const executionRetentionInDays = system.getNumberOrThrow(AppSystemProp.EXECUTION_DATA_RETENTION_DAYS)

export const filesService = {
    async verifyReadToken(token: string): Promise<FileReadToken> {
        return jwtUtils.decodeAndVerify<FileReadToken>({
            jwt: token,
            key: await jwtUtils.getJwtSecret(),
            algorithm: JwtSignAlgorithm.HS256,
            audience: JwtAudience.FILE_READ,
        })
    },
    async constructReadUrl(params: ConstructReadUrlParams): Promise<string> {
        const token = await jwtUtils.sign({
            payload: {
                fileId: params.fileId,
                fileType: params.fileType,
            },
            key: await jwtUtils.getJwtSecret(),
            algorithm: JwtSignAlgorithm.HS256,
            expiresInSeconds: dayjs.duration(executionRetentionInDays, 'days').asSeconds(),
            audience: JwtAudience.FILE_READ,
        })
        return domainHelper.getPublicApiUrl({
            path: `v1/files/${params.fileId}?token=${token}`,
        })
    },
}

export function fileTooLargeError(maxBytes: number): ActivepiecesError {
    return new ActivepiecesError({
        code: ErrorCode.FILE_TOO_LARGE,
        params: {
            message: `File exceeds the maximum allowed size of ${maxBytes} bytes`,
            maxBytes,
        },
    })
}

export function enforceByteLimit(maxBytes: number): Transform {
    let total = 0
    return new Transform({
        transform(chunk: Buffer, _encoding, callback) {
            total += chunk.length
            if (total > maxBytes) {
                callback(fileTooLargeError(maxBytes))
                return
            }
            callback(null, chunk)
        },
    })
}

export const fileTransportHeaders = {
    READ_URL: 'x-ap-file-read-url',
    TYPE: 'x-ap-file-type',
    NAME: 'x-ap-file-name',
    ENCODED_NAME: 'x-ap-file-name-encoded',
} as const

export const ENGINE_WRITABLE_FILE_TYPES: ReadonlySet<FileType> = new Set([
    FileType.FLOW_RUN_LOG,
    FileType.FLOW_RUN_LOG_SLICE,
    FileType.FLOW_STEP_FILE,
])

type ConstructReadUrlParams = {
    fileId: string
    fileType?: FileType
    platformId: string | null | undefined
}