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.8 KB
Raw Blame

import {
  DedupeStrategy,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import {
  AppConnectionValueForAuthProperty,
  PiecePropValueSchema,
  Property,
  StaticPropsValue,
  TriggerStrategy,
  createTrigger,
} from '@activepieces/pieces-framework';
import { cloudinaryAuth } from '../common/auth';
import { makeRequest } from '../common/client';
import dayjs from 'dayjs';
import { newResourceOutputSchema } from '../output-schemas';

const props = {
  asset_folder: Property.ShortText({
    displayName: 'Asset Folder',
    description: 'The Cloudinary folder to watch for new resources. Leave empty to watch the entire account.',
    required: false,
  }),
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof cloudinaryAuth>, StaticPropsValue<typeof props>> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue }) => {
    let endpoint = '/resources/image'; // Default to all resources
    
    if (propsValue.asset_folder && propsValue.asset_folder.trim()) {
      endpoint = `/resources/by_asset_folder?asset_folder=${encodeURIComponent(propsValue.asset_folder.trim())}`;
    }
    
    const resources = await makeRequest(auth, HttpMethod.GET, endpoint);
    
    return (resources.resources || []).map((item: any) => ({
      epochMilliSeconds: dayjs(item.created_at).valueOf(),
      data: item,
    }));
  },
};

export const newResourceInFolder = createTrigger({
  auth: cloudinaryAuth,
  name: 'new_resource',
  outputSchema: newResourceOutputSchema,
  classification: 'READ',
  displayName: 'New Resource',
  description: 'Triggers when a new image, video, or file is uploaded to a specific folder or account in Cloudinary.',
  aiMetadata: {
    description: 'Fires when a new asset is uploaded to Cloudinary, scoped to a specific asset folder or the whole account when no folder is given. Each event represents one newly created asset and its metadata.',
  },
  props,
  sampleData: {
    "asset_id": "bcace221f5b11685dd6effb9d69d5ec3",
    "public_id": "signature_snc5z4",
    "format": "png",
    "version": 1752843735,
    "resource_type": "image",
    "type": "upload",
    "created_at": "2025-07-18T13:02:15Z",
    "bytes": 13685,
    "width": 667,
    "height": 276,
    "asset_folder": "Testr",
    "display_name": "signature_snc5z4",
    "url": "http://res.cloudinary.com/dndacs7ddse/image/upload/v1752843735/signature_snc5z4.png",
    "secure_url": "https://res.cloudinary.com/dndacs7sdde/image/upload/v1752843735/signature_snc5z4.png"
  },
  type: TriggerStrategy.POLLING,
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },
  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },
  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});