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.7 KB
Raw Blame
import { Property, createAction } from '@activepieces/pieces-framework';
import { drive as googleDrive } from '@googleapis/drive';
import { googleDriveAuth, createGoogleClient } from '../auth';
import { driveCopyFileOutputSchema } from '../output-schemas';

export const driveCopyFile = createAction({
  auth: googleDriveAuth,
  name: 'drive_copy_file',
  classification: 'WRITE',
  displayName: 'Copy File',
  description: 'Duplicate a file from Google Drive. Returns the new file ID.',
  audience: 'ai',
  aiMetadata: {
    description:
      'Copies an existing Drive file into a target folder under a new name, optionally converting it to a Google Sheet or Doc. Use to clone a file or make an editable Google-format copy; to relocate the original instead of copying use `drive_move_file`. Requires source file ID and destination folder ID. Each call creates a new copy.',
    idempotent: false,
  },
  outputSchema: driveCopyFileOutputSchema,
  props: {
    file_id: Property.ShortText({
      displayName: 'File ID',
      description:
        'The ID of the file to duplicate. Resolve it via `drive_search_files`.',
      required: true,
    }),
    name: Property.ShortText({
      displayName: 'Name',
      description: 'The name of the new file',
      required: true,
    }),
    folder_id: Property.ShortText({
      displayName: 'Folder ID',
      description:
        'The ID of the folder where the file will be duplicated. Resolve it via `drive_search_files`.',
      required: true,
    }),
    convert_to: Property.StaticDropdown({
      displayName: 'Duplicate as',
      description: 'If left unselected the file will be duplicated as it is',
      required: false,
      options: {
        options: [
          {
            label: 'Google Sheets',
            value: 'application/vnd.google-apps.spreadsheet',
          },
          {
            label: 'Google Docs',
            value: 'application/vnd.google-apps.document',
          },
        ],
      },
    }),
  },
  async run(context) {
    const authClient = await createGoogleClient(context.auth);

    const fileId = context.propsValue.file_id;
    const nameForNewFile = context.propsValue.name;
    const parentFolderId = context.propsValue.folder_id;
    const mimeType = context.propsValue.convert_to;

    const drive = googleDrive({ version: 'v3', auth: authClient });

    const requestBody: any = {
      name: nameForNewFile,
      parents: [parentFolderId],
    };

    if (mimeType) {
      requestBody.mimeType = mimeType;
    }

    const response = await drive.files.copy({
      fileId,
      auth: authClient,
      requestBody,
      supportsAllDrives: true,
    });

    if (response.status !== 200) {
      throw new Error('Error duplicating file');
    }

    return response.data;
  },
});