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.4 KB
Raw Blame
import { straicoAuth } from '../auth';
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  httpClient,
} from '@activepieces/pieces-common';
import { baseUrlv0 } from '../common/common';

const SUPPORTED_FILE_TYPES = [
  'pdf', 'docx', 'pptx', 'txt', 'xlsx', 'mp3', 'mp4', 
  'html', 'csv', 'json', 'py', 'php', 'js', 'css', 
  'cs', 'swift', 'kt', 'xml', 'ts', 'png', 'jpg', 
  'jpeg', 'webp', 'gif'
];

export const fileUpload = createAction({
  audience: 'both',
  auth: straicoAuth,
  name: 'file_upload',
  displayName: 'Upload File',
  description: 'Upload a file to Straico API for processing.',
  aiMetadata: { description: 'Uploads a document, image, audio or video file to Straico storage and returns a hosted URL. Use it as the preparation step whose URL is then passed into Ask AI as a file url or image url; prefer Create RAG when the file should instead be chunked and indexed for retrieval. The extension must be one of pdf, docx, pptx, txt, xlsx, mp3, mp4, html, csv, json, py, php, js, css, cs, swift, kt, xml, ts, png, jpg, jpeg, webp or gif, otherwise the call fails before any request is sent. Not idempotent: each call stores another copy at a new URL.', idempotent: false },
  props: {
    file: Property.File({
      displayName: 'File',
      required: true,
      description: 'The file to upload. Supported file types: pdf, docx, pptx, txt, xlsx, mp3, mp4, html, csv, json, py, php, js, css, cs, swift, kt, xml, ts, png, jpg, jpeg, webp, gif',
    }),
  },
  async run({ auth, propsValue }) {
    const fileExtension = propsValue.file.filename.split('.').pop()?.toLowerCase();
    if (!fileExtension || !SUPPORTED_FILE_TYPES.includes(fileExtension)) {
      throw new Error(`File type not supported. Supported types are: ${SUPPORTED_FILE_TYPES.join(', ')}`);
    }

    const formData = new FormData();
    formData.append('file', new Blob([propsValue.file.data]), propsValue.file.filename);

    const response = await httpClient.sendRequest<{
      data: {
        url: string;
      };
      success: boolean;
    }>({
      url: `${baseUrlv0}/file/upload`,
      method: HttpMethod.POST,
      authentication: {
        type: AuthenticationType.BEARER_TOKEN,
        token: auth.secret_text,
      },
      body: formData,
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });

    return response.body.data.url;
  },
});