pull:初次提交

This commit is contained in:
Yep_Q
2025-09-08 04:48:28 +08:00
parent 5c0619656d
commit f64f498365
11751 changed files with 1953723 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
import type { INodeProperties } from 'n8n-workflow';
import * as deleteFile from './delete.operation';
import * as get from './get.operation';
import * as getMany from './getMany.operation';
import * as load from './load.operation';
import * as upload from './upload.operation';
export { deleteFile, get, getMany, upload, load };
export const description: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['file'],
},
},
options: [
{
name: 'Delete',
value: 'deleteFile',
description: 'Delete an uploaded file',
action: 'Delete a file',
},
{
name: 'Get',
value: 'get',
description: 'Get a details of an uploaded file',
action: 'Get a file',
},
{
name: 'Get Many',
value: 'getMany',
description: 'Get details of multiple uploaded files',
action: 'Get many files',
},
{
name: 'Load',
value: 'load',
description: 'Load a file into a session',
action: 'Load a file',
},
{
name: 'Upload',
value: 'upload',
description: 'Upload a file into a session',
action: 'Upload a file',
},
],
default: 'getMany',
},
...deleteFile.description,
...get.description,
...getMany.description,
...load.description,
...upload.description,
];

View File

@@ -0,0 +1,40 @@
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { ERROR_MESSAGES } from '../../constants';
import { apiRequest } from '../../transport';
export const description: INodeProperties[] = [
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
required: true,
description: 'ID of the file to delete',
displayOptions: {
show: {
resource: ['file'],
operation: ['deleteFile'],
},
},
},
];
export async function execute(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const fileId = this.getNodeParameter('fileId', index, '') as string;
if (!fileId) {
throw new NodeOperationError(
this.getNode(),
ERROR_MESSAGES.REQUIRED_PARAMETER.replace('{{field}}', 'File ID'),
);
}
await apiRequest.call(this, 'DELETE', `/files/${fileId}`);
return this.helpers.returnJsonArray({ data: { message: 'File deleted successfully' } });
}

View File

@@ -0,0 +1,76 @@
import { NodeOperationError } from 'n8n-workflow';
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { ERROR_MESSAGES } from '../../constants';
import { apiRequest } from '../../transport';
import type { IAirtopResponseWithFiles } from '../../transport/types';
const displayOptions = {
show: {
resource: ['file'],
operation: ['get'],
},
};
export const description: INodeProperties[] = [
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
required: true,
description: 'ID of the file to retrieve',
displayOptions,
},
{
displayName: 'Output Binary File',
name: 'outputBinaryFile',
type: 'boolean',
default: false,
description: 'Whether to output the file in binary format if the file is ready for download',
displayOptions,
},
];
export async function execute(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const fileId = this.getNodeParameter('fileId', index, '') as string;
const outputBinaryFile = this.getNodeParameter('outputBinaryFile', index, false);
if (!fileId) {
throw new NodeOperationError(
this.getNode(),
ERROR_MESSAGES.REQUIRED_PARAMETER.replace('{{field}}', 'File ID'),
);
}
const response = (await apiRequest.call(
this,
'GET',
`/files/${fileId}`,
)) as IAirtopResponseWithFiles;
const { fileName = '', downloadUrl = '', status = '' } = response?.data ?? {};
// Handle binary file output
if (outputBinaryFile && downloadUrl && status === 'available') {
const buffer = (await this.helpers.httpRequest({
url: downloadUrl,
json: false,
encoding: 'arraybuffer',
})) as Buffer;
const file = await this.helpers.prepareBinaryData(buffer, fileName);
return [
{
json: {
...response,
},
binary: { data: file },
},
];
}
return this.helpers.returnJsonArray({ ...response });
}

View File

@@ -0,0 +1,97 @@
import {
type IExecuteFunctions,
type INodeExecutionData,
type INodeProperties,
} from 'n8n-workflow';
import { requestAllFiles } from './helpers';
import { wrapData } from '../../../../utils/utilities';
import { apiRequest } from '../../transport';
import type { IAirtopResponse } from '../../transport/types';
const displayOptions = {
show: {
resource: ['file'],
operation: ['getMany'],
},
};
export const description: INodeProperties[] = [
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions,
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: ['file'],
operation: ['getMany'],
returnAll: [false],
},
},
typeOptions: {
minValue: 1,
maxValue: 100,
},
default: 10,
description: 'Max number of results to return',
},
{
displayName: 'Session IDs',
name: 'sessionIds',
type: 'string',
default: '',
description:
'Comma-separated list of <a href="https://docs.airtop.ai/api-reference/airtop-api/sessions/create" target="_blank">Session IDs</a> to filter files by. When empty, all files from all sessions will be returned.',
placeholder: 'e.g. 6aac6f73-bd89-4a76-ab32-5a6c422e8b0b, a13c6f73-bd89-4a76-ab32-5a6c422e8224',
displayOptions,
},
{
displayName: 'Output Files in Single Item',
name: 'outputSingleItem',
type: 'boolean',
default: true,
description:
'Whether to output one item containing all files or output each file as a separate item',
displayOptions,
},
];
export async function execute(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const returnAll = this.getNodeParameter('returnAll', index, false);
const limit = this.getNodeParameter('limit', index, 10);
const sessionIds = this.getNodeParameter('sessionIds', index, '') as string;
const outputSingleItem = this.getNodeParameter('outputSingleItem', index, true) as boolean;
const endpoint = '/files';
let files: IAirtopResponse[] = [];
const responseData = returnAll
? await requestAllFiles.call(this, sessionIds)
: await apiRequest.call(this, 'GET', endpoint, {}, { sessionIds, limit });
if (responseData.data?.files && Array.isArray(responseData.data?.files)) {
files = responseData.data.files;
}
/**
* Returns the files in one of two formats:
* - A single JSON item containing an array of all files (when outputSingleItem = true)
* - Multiple JSON items, one per file
* Data structure reference: https://docs.n8n.io/courses/level-two/chapter-1/#data-structure-of-n8n
*/
if (outputSingleItem) {
return this.helpers.returnJsonArray({ ...responseData });
}
return wrapData(files);
}

View File

@@ -0,0 +1,248 @@
import pick from 'lodash/pick';
import type { IExecuteFunctions } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
import { BASE_URL, ERROR_MESSAGES, OPERATION_TIMEOUT } from '../../constants';
import { apiRequest } from '../../transport';
import type { IAirtopResponseWithFiles, IAirtopFileInputRequest } from '../../transport/types';
/**
* Fetches all files from the Airtop API using pagination
* @param this - The execution context providing access to n8n functionality
* @param sessionIds - Comma-separated string of session IDs to filter files by
* @returns Promise resolving to a response object containing the complete array of files
*/
export async function requestAllFiles(
this: IExecuteFunctions,
sessionIds: string,
): Promise<IAirtopResponseWithFiles> {
const endpoint = '/files';
let hasMore = true;
let currentOffset = 0;
const limit = 100;
const files: IAirtopResponseWithFiles['data']['files'] = [];
let responseData: IAirtopResponseWithFiles;
while (hasMore) {
// request files
responseData = (await apiRequest.call(
this,
'GET',
endpoint,
{},
{ offset: currentOffset, limit, sessionIds },
)) as IAirtopResponseWithFiles;
// add files to the array
if (responseData.data?.files && Array.isArray(responseData.data?.files)) {
files.push(...responseData.data.files);
}
// check if there are more files
hasMore = Boolean(responseData.data?.pagination?.hasMore);
currentOffset += limit;
}
return {
data: {
files,
pagination: {
hasMore,
},
},
};
}
/**
* Polls the Airtop API until a file reaches "available" status or times out
* @param this - The execution context providing access to n8n functionality
* @param fileId - The unique identifier of the file to poll
* @param timeout - Maximum time in milliseconds to wait before failing (defaults to OPERATION_TIMEOUT)
* @param intervalSeconds - Time in seconds to wait between polling attempts (defaults to 1)
* @returns Promise resolving to the file ID when the file is available
* @throws NodeApiError if the operation times out or API request fails
*/
export async function pollFileUntilAvailable(
this: IExecuteFunctions,
fileId: string,
timeout = OPERATION_TIMEOUT,
intervalSeconds = 1,
): Promise<string> {
let fileStatus = '';
const startTime = Date.now();
while (fileStatus !== 'available') {
const elapsedTime = Date.now() - startTime;
if (elapsedTime >= timeout) {
throw new NodeApiError(this.getNode(), {
message: ERROR_MESSAGES.TIMEOUT_REACHED,
code: 500,
});
}
const response = await apiRequest.call(this, 'GET', `/files/${fileId}`);
fileStatus = response.data?.status as string;
// Wait before the next polling attempt
await new Promise((resolve) => setTimeout(resolve, intervalSeconds * 1000));
}
return fileId;
}
/**
* Creates a file entry in Airtop, uploads the file content, and waits until processing completes
* @param this - The execution context providing access to n8n functionality
* @param fileName - Name to assign to the uploaded file
* @param fileBuffer - Buffer containing the binary file data to upload
* @param fileType - Classification of the file in Airtop (e.g., 'customer_upload')
* @param pollingFunction - Function to use for checking file availability (defaults to pollFileUntilAvailable)
* @returns Promise resolving to the file ID once upload is complete and file is available
* @throws NodeApiError if file creation, upload, or polling fails
*/
export async function createAndUploadFile(
this: IExecuteFunctions,
fileName: string,
fileBuffer: Buffer,
fileType: string,
pollingFunction = pollFileUntilAvailable,
): Promise<string> {
// Create file entry
const createResponse = await apiRequest.call(this, 'POST', '/files', { fileName, fileType });
const fileId = createResponse.data?.id;
const uploadUrl = createResponse.data?.uploadUrl as string;
if (!fileId || !uploadUrl) {
throw new NodeApiError(this.getNode(), {
message: 'Failed to create file entry: missing file ID or upload URL',
code: 500,
});
}
// Upload the file
await this.helpers.httpRequest({
method: 'PUT',
url: uploadUrl,
body: fileBuffer,
headers: {
'Content-Type': 'application/octet-stream',
},
});
// Poll until the file is available
return await pollingFunction.call(this, fileId as string);
}
/**
* Waits for a file to be ready in a session by polling file's information
* @param this - The execution context providing access to n8n functionality
* @param sessionId - ID of the session to check for file availability
* @param fileId - ID of the file
* @param timeout - Maximum time in milliseconds to wait before failing (defaults to OPERATION_TIMEOUT)
* @returns Promise that resolves when a file in the session becomes available
* @throws NodeApiError if the timeout is reached before a file becomes available
*/
export async function waitForFileInSession(
this: IExecuteFunctions,
sessionId: string,
fileId: string,
timeout = OPERATION_TIMEOUT,
): Promise<void> {
const url = `${BASE_URL}/files/${fileId}`;
const isFileInSession = async (): Promise<boolean> => {
const fileInfo = (await apiRequest.call(this, 'GET', url)) as IAirtopResponseWithFiles;
return Boolean(fileInfo.data?.sessionIds?.includes(sessionId));
};
const startTime = Date.now();
while (!(await isFileInSession())) {
const elapsedTime = Date.now() - startTime;
// throw error if timeout is reached
if (elapsedTime >= timeout) {
throw new NodeApiError(this.getNode(), {
message: ERROR_MESSAGES.TIMEOUT_REACHED,
code: 500,
});
}
// wait 1 second before checking again
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
/**
* Associates a file with a session and waits until the file is ready for use
* @param this - The execution context providing access to n8n functionality
* @param fileId - ID of the file to associate with the session
* @param sessionId - ID of the session to add the file to
* @param pollingFunction - Function to use for checking file availability in session (defaults to waitForFileInSession)
* @returns Promise that resolves when the file is ready for use in the session
*/
export async function pushFileToSession(
this: IExecuteFunctions,
fileId: string,
sessionId: string,
pollingFunction = waitForFileInSession,
): Promise<void> {
// Push file into session
await apiRequest.call(this, 'POST', `/files/${fileId}/push`, { sessionIds: [sessionId] });
await pollingFunction.call(this, sessionId, fileId);
}
/**
* Activates a file upload input in a specific window within a session
* @param this - The execution context providing access to n8n functionality
* @param fileId - ID of the file to use for the input
* @param windowId - ID of the window where the file input will be triggered
* @param sessionId - ID of the session containing the window
* @returns Promise that resolves when the file input has been triggered
*/
export async function triggerFileInput(
this: IExecuteFunctions,
request: IAirtopFileInputRequest,
): Promise<void> {
await apiRequest.call(
this,
'POST',
`/sessions/${request.sessionId}/windows/${request.windowId}/file-input`,
pick(request, ['fileId', 'elementDescription', 'includeHiddenElements']),
);
}
/**
* Creates a file Buffer from either a URL or binary data
* This function supports two source types:
* - URL: Downloads the file from the specified URL and returns it as a Buffer
* - Binary: Retrieves binary data from the workflow's binary data storage
*
* @param this - The execution context providing access to n8n functionality
* @param source - Source type, either 'url' or 'binary'
* @param value - Either a URL string or binary data property name depending on source type
* @param itemIndex - Index of the workflow item to get binary data from (when source is 'binary')
* @returns Promise resolving to a Buffer containing the file data
* @throws NodeApiError if the source type is unsupported or retrieval fails
*/
export async function createFileBuffer(
this: IExecuteFunctions,
source: string,
value: string,
itemIndex: number,
): Promise<Buffer> {
if (source === 'url') {
const buffer = (await this.helpers.httpRequest({
url: value,
json: false,
encoding: 'arraybuffer',
})) as Buffer;
return buffer;
}
if (source === 'binary') {
const binaryData = await this.helpers.getBinaryDataBuffer(itemIndex, value);
return binaryData;
}
throw new NodeApiError(this.getNode(), {
message: `Unsupported source type: ${source}. Please use 'url' or 'binary'`,
code: 500,
});
}

View File

@@ -0,0 +1,85 @@
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { pushFileToSession, triggerFileInput } from './helpers';
import {
sessionIdField,
windowIdField,
elementDescriptionField,
includeHiddenElementsField,
} from '../common/fields';
const displayOptions = {
show: {
resource: ['file'],
operation: ['load'],
},
};
export const description: INodeProperties[] = [
{
...sessionIdField,
description: 'The session ID to load the file into',
displayOptions,
},
{
...windowIdField,
description: 'The window ID to trigger the file input in',
displayOptions,
},
{
displayName: 'File ID',
name: 'fileId',
type: 'string',
default: '',
required: true,
description: 'ID of the file to load into the session',
displayOptions,
},
{
...elementDescriptionField,
description: 'Optional description of the file input to interact with',
placeholder: 'e.g. the file upload selection box',
displayOptions,
},
{
...includeHiddenElementsField,
displayOptions,
},
];
export async function execute(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const fileId = this.getNodeParameter('fileId', index, '') as string;
const sessionId = this.getNodeParameter('sessionId', index, '') as string;
const windowId = this.getNodeParameter('windowId', index, '') as string;
const elementDescription = this.getNodeParameter('elementDescription', index, '') as string;
const includeHiddenElements = this.getNodeParameter(
'includeHiddenElements',
index,
false,
) as boolean;
try {
await pushFileToSession.call(this, fileId, sessionId);
await triggerFileInput.call(this, {
fileId,
windowId,
sessionId,
elementDescription,
includeHiddenElements,
});
return this.helpers.returnJsonArray({
sessionId,
windowId,
data: {
message: 'File loaded successfully',
},
});
} catch (error) {
throw new NodeOperationError(this.getNode(), error as Error);
}
}

View File

@@ -0,0 +1,201 @@
import type { IExecuteFunctions, INodeExecutionData, INodeProperties } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import {
createAndUploadFile,
pushFileToSession,
triggerFileInput,
createFileBuffer,
} from './helpers';
import { validateRequiredStringField } from '../../GenericFunctions';
import {
sessionIdField,
windowIdField,
elementDescriptionField,
includeHiddenElementsField,
} from '../common/fields';
const displayOptions = {
show: {
resource: ['file'],
operation: ['upload'],
},
};
export const description: INodeProperties[] = [
{
...sessionIdField,
description: 'The session ID to load the file into',
displayOptions,
},
{
...windowIdField,
description: 'The window ID to trigger the file input in',
displayOptions,
},
{
displayName: 'File Name',
name: 'fileName',
type: 'string',
default: '',
required: true,
description:
'Name for the file to upload. For a session, all files loaded should have <b>unique names</b>.',
displayOptions,
},
{
displayName: 'File Type',
name: 'fileType',
type: 'options',
options: [
{
name: 'Browser Download',
value: 'browser_download',
},
{
name: 'Screenshot',
value: 'screenshot',
},
{
name: 'Video',
value: 'video',
},
{
name: 'Customer Upload',
value: 'customer_upload',
},
],
default: 'customer_upload',
description: "Choose the type of file to upload. Defaults to 'Customer Upload'.",
displayOptions,
},
{
displayName: 'Source',
name: 'source',
type: 'options',
options: [
{
name: 'URL',
value: 'url',
},
{
name: 'Binary',
value: 'binary',
},
],
default: 'url',
description: 'Source of the file to upload',
displayOptions,
},
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
required: true,
displayOptions: {
show: {
source: ['binary'],
...displayOptions.show,
},
},
description: 'Name of the binary property containing the file data',
},
{
displayName: 'URL',
name: 'url',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
source: ['url'],
...displayOptions.show,
},
},
description: 'URL from where to fetch the file to upload',
},
{
displayName: 'Trigger File Input',
name: 'triggerFileInputParameter',
type: 'boolean',
default: true,
description:
'Whether to automatically trigger the file input dialog in the current window. If disabled, the file will only be uploaded to the session without opening the file input dialog.',
displayOptions,
},
{
...elementDescriptionField,
description: 'Optional description of the file input to interact with',
placeholder: 'e.g. the file upload selection box',
displayOptions: {
show: {
triggerFileInputParameter: [true],
...displayOptions.show,
},
},
},
{
...includeHiddenElementsField,
displayOptions: {
show: {
triggerFileInputParameter: [true],
...displayOptions.show,
},
},
},
];
export async function execute(
this: IExecuteFunctions,
index: number,
): Promise<INodeExecutionData[]> {
const sessionId = validateRequiredStringField.call(this, index, 'sessionId', 'Session ID');
const windowId = validateRequiredStringField.call(this, index, 'windowId', 'Window ID');
const fileName = this.getNodeParameter('fileName', index, '') as string;
const fileType = this.getNodeParameter('fileType', index, 'customer_upload') as string;
const source = this.getNodeParameter('source', index, 'url') as string;
const url = this.getNodeParameter('url', index, '') as string;
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index, '');
const triggerFileInputParameter = this.getNodeParameter(
'triggerFileInputParameter',
index,
true,
) as boolean;
const elementDescription = this.getNodeParameter('elementDescription', index, '') as string;
const includeHiddenElements = this.getNodeParameter(
'includeHiddenElements',
index,
false,
) as boolean;
// Get the file content based on source type
const fileValue = source === 'url' ? url : binaryPropertyName;
try {
const fileBuffer = await createFileBuffer.call(this, source, fileValue, index);
const fileId = await createAndUploadFile.call(this, fileName, fileBuffer, fileType);
// Push file to session
await pushFileToSession.call(this, fileId, sessionId);
if (triggerFileInputParameter) {
await triggerFileInput.call(this, {
fileId,
windowId,
sessionId,
elementDescription,
includeHiddenElements,
});
}
return this.helpers.returnJsonArray({
sessionId,
windowId,
data: {
fileId,
message: 'File uploaded successfully',
},
});
} catch (error) {
throw new NodeOperationError(this.getNode(), error as Error);
}
}