initial import
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
import { KDrive } from './nodes/KDrive/KDrive.node';
|
||||
import { KDriveCredentials } from './nodes/KDrive/KDriveCredentials.api';
|
||||
|
||||
export { KDrive, KDriveCredentials };
|
||||
@@ -0,0 +1,87 @@
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
IHttpRequestOptions,
|
||||
ILoadOptionsFunctions,
|
||||
INodePropertyOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { OptionsWithUri } from 'request';
|
||||
|
||||
/**
|
||||
* Make an API request to kDrive API
|
||||
*/
|
||||
export async function kdriveApiRequest(
|
||||
this: IExecuteFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
body: IDataObject = {},
|
||||
credentials: IDataObject,
|
||||
returnFullResponse: boolean = false,
|
||||
): Promise<any> {
|
||||
const options: OptionsWithUri = {
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method,
|
||||
uri: `https://api.infomaniak.com${endpoint}`,
|
||||
body,
|
||||
json: true,
|
||||
};
|
||||
|
||||
// Add authentication
|
||||
if (credentials.authentication === 'apiKey') {
|
||||
options.headers!['Authorization'] = `Bearer ${credentials.apiKey}`;
|
||||
}
|
||||
|
||||
// Handle query parameters for GET requests
|
||||
if (method === 'GET' && Object.keys(body).length > 0) {
|
||||
options.qs = body;
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
// Handle form data for file uploads
|
||||
if (endpoint.includes('/upload') && method === 'POST') {
|
||||
options.formData = body;
|
||||
delete options.headers!['Content-Type'];
|
||||
options.json = false;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await this.helpers.request!(options);
|
||||
|
||||
if (returnFullResponse) {
|
||||
return response;
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
handleApiError.call(this, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle API errors
|
||||
*/
|
||||
export function handleApiError(this: IExecuteFunctions, error: any): void {
|
||||
let errorMessage = 'Unknown error occurred';
|
||||
|
||||
if (error.response) {
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
if (error.response.body && error.response.body.message) {
|
||||
errorMessage = error.response.body.message;
|
||||
} else if (error.response.body && typeof error.response.body === 'string') {
|
||||
errorMessage = error.response.body;
|
||||
} else {
|
||||
errorMessage = `API Error: ${error.response.statusCode} - ${error.response.statusMessage}`;
|
||||
}
|
||||
} else if (error.message) {
|
||||
// The request was made but no response was received
|
||||
errorMessage = error.message;
|
||||
}
|
||||
|
||||
throw new Error(`kDrive API Error: ${errorMessage}`);
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
import {
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
IDataObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
kdriveApiRequest,
|
||||
handleApiError,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class KDrive implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'kDrive',
|
||||
name: 'kDrive',
|
||||
icon: 'file:kdrive.svg',
|
||||
group: ['fileManagement'],
|
||||
version: 1,
|
||||
description: 'Interact with Infomaniak kDrive API',
|
||||
defaults: {
|
||||
name: 'kDrive',
|
||||
color: '#007BFF',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'kdriveApi',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: ['apiKey'],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'API Key',
|
||||
value: 'apiKey',
|
||||
},
|
||||
],
|
||||
default: 'apiKey',
|
||||
description: 'Authentication method',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Drive',
|
||||
value: 'drive',
|
||||
},
|
||||
{
|
||||
name: 'File',
|
||||
value: 'file',
|
||||
},
|
||||
{
|
||||
name: 'Directory',
|
||||
value: 'directory',
|
||||
},
|
||||
],
|
||||
default: 'file',
|
||||
description: 'Resource to operate on',
|
||||
},
|
||||
// Drive operations
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['drive'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'List Drives',
|
||||
value: 'listDrives',
|
||||
description: 'List all accessible drives',
|
||||
},
|
||||
{
|
||||
name: 'Get Drive Info',
|
||||
value: 'getDriveInfo',
|
||||
description: 'Get information about a specific drive',
|
||||
},
|
||||
],
|
||||
default: 'listDrives',
|
||||
},
|
||||
// File operations
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'List Files',
|
||||
value: 'listFiles',
|
||||
description: 'List files in a directory',
|
||||
},
|
||||
{
|
||||
name: 'Get File Info',
|
||||
value: 'getFileInfo',
|
||||
description: 'Get information about a file',
|
||||
},
|
||||
{
|
||||
name: 'Upload File',
|
||||
value: 'uploadFile',
|
||||
description: 'Upload a file',
|
||||
},
|
||||
{
|
||||
name: 'Download File',
|
||||
value: 'downloadFile',
|
||||
description: 'Download a file',
|
||||
},
|
||||
{
|
||||
name: 'Delete File',
|
||||
value: 'deleteFile',
|
||||
description: 'Delete a file (move to trash)',
|
||||
},
|
||||
{
|
||||
name: 'Search Files',
|
||||
value: 'searchFiles',
|
||||
description: 'Search for files',
|
||||
},
|
||||
{
|
||||
name: 'Get File Versions',
|
||||
value: 'getFileVersions',
|
||||
description: 'Get file versions',
|
||||
},
|
||||
],
|
||||
default: 'listFiles',
|
||||
},
|
||||
// Directory operations
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directory'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create Directory',
|
||||
value: 'createDirectory',
|
||||
description: 'Create a new directory',
|
||||
},
|
||||
{
|
||||
name: 'Create File',
|
||||
value: 'createFile',
|
||||
description: 'Create a new file',
|
||||
},
|
||||
],
|
||||
default: 'createDirectory',
|
||||
},
|
||||
// Drive ID
|
||||
{
|
||||
displayName: 'Drive ID',
|
||||
name: 'driveId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file', 'directory'],
|
||||
operation: ['listFiles', 'getFileInfo', 'uploadFile', 'downloadFile', 'deleteFile', 'searchFiles', 'getFileVersions', 'createDirectory', 'createFile'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the drive',
|
||||
},
|
||||
// File ID for file operations
|
||||
{
|
||||
displayName: 'File ID',
|
||||
name: 'fileId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['getFileInfo', 'downloadFile', 'deleteFile', 'getFileVersions'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the file',
|
||||
},
|
||||
{
|
||||
displayName: 'Parent Directory ID',
|
||||
name: 'parentDirectoryId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['listFiles'],
|
||||
},
|
||||
},
|
||||
default: 'root',
|
||||
description: 'The ID of the parent directory (use "root" for root directory)',
|
||||
},
|
||||
// Directory ID for directory operations
|
||||
{
|
||||
displayName: 'Parent Directory ID',
|
||||
name: 'parentDirectoryId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directory'],
|
||||
operation: ['createDirectory', 'createFile'],
|
||||
},
|
||||
},
|
||||
default: 'root',
|
||||
description: 'The ID of the parent directory (use "root" for root directory)',
|
||||
},
|
||||
// Directory name for create directory
|
||||
{
|
||||
displayName: 'Directory Name',
|
||||
name: 'directoryName',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directory'],
|
||||
operation: ['createDirectory'],
|
||||
},
|
||||
},
|
||||
description: 'Name of the new directory',
|
||||
},
|
||||
// File name for create file
|
||||
{
|
||||
displayName: 'File Name',
|
||||
name: 'fileName',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directory'],
|
||||
operation: ['createFile'],
|
||||
},
|
||||
},
|
||||
description: 'Name of the new file',
|
||||
},
|
||||
// File content for create file
|
||||
{
|
||||
displayName: 'File Content',
|
||||
name: 'fileContent',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
alwaysOpenEditWindow: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directory'],
|
||||
operation: ['createFile'],
|
||||
},
|
||||
},
|
||||
description: 'Content of the new file',
|
||||
},
|
||||
// File data for upload
|
||||
{
|
||||
displayName: 'File Data',
|
||||
name: 'fileData',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
alwaysOpenEditWindow: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['uploadFile'],
|
||||
},
|
||||
},
|
||||
description: 'File data to upload (base64 encoded)',
|
||||
},
|
||||
{
|
||||
displayName: 'File Name',
|
||||
name: 'uploadFileName',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['uploadFile'],
|
||||
},
|
||||
},
|
||||
description: 'Name of the file to upload',
|
||||
},
|
||||
{
|
||||
displayName: 'Parent Directory ID',
|
||||
name: 'uploadParentDirectoryId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['uploadFile'],
|
||||
},
|
||||
},
|
||||
default: 'root',
|
||||
description: 'The ID of the parent directory for upload (use "root" for root directory)',
|
||||
},
|
||||
// Search parameters
|
||||
{
|
||||
displayName: 'Search Query',
|
||||
name: 'searchQuery',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['searchFiles'],
|
||||
},
|
||||
},
|
||||
description: 'Search query for files',
|
||||
},
|
||||
{
|
||||
displayName: 'Search In Trash',
|
||||
name: 'searchInTrash',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['file'],
|
||||
operation: ['searchFiles'],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'Whether to search in trash',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
|
||||
const resource = this.getNodeParameter('resource', 0) as string;
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
|
||||
const credentials = await this.getCredentials('kdriveApi');
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
try {
|
||||
if (resource === 'drive') {
|
||||
if (operation === 'listDrives') {
|
||||
const response = await kdriveApiRequest.call(this, 'GET', '/2/drive', {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'getDriveInfo') {
|
||||
const driveId = this.getNodeParameter('driveId', i) as string;
|
||||
const response = await kdriveApiRequest.call(this, 'GET', `/2/drive/${driveId}/settings`, {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
}
|
||||
} else if (resource === 'file') {
|
||||
const driveId = this.getNodeParameter('driveId', i) as string;
|
||||
|
||||
if (operation === 'listFiles') {
|
||||
const parentDirectoryId = this.getNodeParameter('parentDirectoryId', i) as string;
|
||||
const endpoint = parentDirectoryId === 'root'
|
||||
? `/3/drive/${driveId}/files/root/files`
|
||||
: `/3/drive/${driveId}/files/${parentDirectoryId}/files`;
|
||||
const response = await kdriveApiRequest.call(this, 'GET', endpoint, {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'getFileInfo') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
const response = await kdriveApiRequest.call(this, 'GET', `/3/drive/${driveId}/files/${fileId}`, {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'uploadFile') {
|
||||
const fileData = this.getNodeParameter('fileData', i) as string;
|
||||
const fileName = this.getNodeParameter('uploadFileName', i) as string;
|
||||
const parentDirectoryId = this.getNodeParameter('uploadParentDirectoryId', i) as string;
|
||||
|
||||
const formData: IDataObject = {
|
||||
file: fileData,
|
||||
filename: fileName,
|
||||
parent_id: parentDirectoryId === 'root' ? 'root' : parentDirectoryId,
|
||||
};
|
||||
|
||||
const response = await kdriveApiRequest.call(this, 'POST', `/3/drive/${driveId}/upload`, formData, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'downloadFile') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
const response = await kdriveApiRequest.call(this, 'GET', `/2/drive/${driveId}/files/${fileId}/download`, {}, credentials, true);
|
||||
returnData.push({ binary: response });
|
||||
} else if (operation === 'deleteFile') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
const response = await kdriveApiRequest.call(this, 'DELETE', `/2/drive/${driveId}/files/${fileId}`, {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'searchFiles') {
|
||||
const searchQuery = this.getNodeParameter('searchQuery', i) as string;
|
||||
const searchInTrash = this.getNodeParameter('searchInTrash', i) as boolean;
|
||||
|
||||
const params: IDataObject = {
|
||||
query: searchQuery,
|
||||
in_trash: searchInTrash,
|
||||
};
|
||||
|
||||
const response = await kdriveApiRequest.call(this, 'GET', `/3/drive/${driveId}/files/search`, params, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'getFileVersions') {
|
||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||
const response = await kdriveApiRequest.call(this, 'GET', `/3/drive/${driveId}/files/${fileId}/versions`, {}, credentials);
|
||||
returnData.push({ json: response });
|
||||
}
|
||||
} else if (resource === 'directory') {
|
||||
const driveId = this.getNodeParameter('driveId', i) as string;
|
||||
|
||||
if (operation === 'createDirectory') {
|
||||
const parentDirectoryId = this.getNodeParameter('parentDirectoryId', i) as string;
|
||||
const directoryName = this.getNodeParameter('directoryName', i) as string;
|
||||
|
||||
const body: IDataObject = {
|
||||
name: directoryName,
|
||||
parent_id: parentDirectoryId === 'root' ? 'root' : parentDirectoryId,
|
||||
};
|
||||
|
||||
const response = await kdriveApiRequest.call(this, 'POST', `/3/drive/${driveId}/files/${parentDirectoryId === 'root' ? 'root' : parentDirectoryId}/directory`, body, credentials);
|
||||
returnData.push({ json: response });
|
||||
} else if (operation === 'createFile') {
|
||||
const parentDirectoryId = this.getNodeParameter('parentDirectoryId', i) as string;
|
||||
const fileName = this.getNodeParameter('fileName', i) as string;
|
||||
const fileContent = this.getNodeParameter('fileContent', i) as string;
|
||||
|
||||
const body: IDataObject = {
|
||||
name: fileName,
|
||||
content: fileContent,
|
||||
parent_id: parentDirectoryId === 'root' ? 'root' : parentDirectoryId,
|
||||
};
|
||||
|
||||
const response = await kdriveApiRequest.call(this, 'POST', `/3/drive/${driveId}/files/${parentDirectoryId === 'root' ? 'root' : parentDirectoryId}/file`, body, credentials);
|
||||
returnData.push({ json: response });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({ json: { error: error.message } });
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import {
|
||||
ICredentialType,
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export class KDriveCredentials implements ICredentialType {
|
||||
name = 'kdriveApi';
|
||||
displayName = 'kDrive API';
|
||||
documentationUrl = 'https://developer.infomaniak.com';
|
||||
properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'API Key',
|
||||
name: 'apiKey',
|
||||
type: 'string',
|
||||
typeOptions: {
|
||||
password: true,
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
description: 'Your kDrive API key',
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<rect width="24" height="24" rx="4" fill="#007BFF"/>
|
||||
<path d="M17 10H7L12 5L17 10Z" fill="white"/>
|
||||
<path d="M7 14H17L12 19L7 14Z" fill="white"/>
|
||||
<path d="M12 12H7" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M17 12H12" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 497 B |
Reference in New Issue
Block a user