mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Fix OneDrive-Node
This commit is contained in:
parent
ff3fa559ba
commit
bc7dfd97de
|
@ -1,4 +1,4 @@
|
||||||
import { INodeProperties } from "n8n-workflow";
|
import { INodeProperties } from 'n8n-workflow';
|
||||||
|
|
||||||
export const fileOperations = [
|
export const fileOperations = [
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,75 +0,0 @@
|
||||||
import { INodeProperties } from "n8n-workflow";
|
|
||||||
|
|
||||||
export const folderOperations = [
|
|
||||||
{
|
|
||||||
displayName: 'Operation',
|
|
||||||
name: 'operation',
|
|
||||||
type: 'options',
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
resource: [
|
|
||||||
'folder',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'Get Children',
|
|
||||||
value: 'getChildren',
|
|
||||||
description: 'Get items inside a folder',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Search',
|
|
||||||
value: 'search',
|
|
||||||
description: 'Search a folder',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
default: 'getChildren',
|
|
||||||
description: 'The operation to perform.',
|
|
||||||
},
|
|
||||||
] as INodeProperties[];
|
|
||||||
|
|
||||||
export const folderFields = [
|
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
|
||||||
/* folder:getChildren */
|
|
||||||
/* -------------------------------------------------------------------------- */
|
|
||||||
{
|
|
||||||
displayName: 'Folder ID',
|
|
||||||
name: 'folderId',
|
|
||||||
type: 'string',
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
operation: [
|
|
||||||
'getChildren',
|
|
||||||
],
|
|
||||||
resource: [
|
|
||||||
'folder',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
default: '',
|
|
||||||
description: 'Folder ID',
|
|
||||||
},
|
|
||||||
/* -------------------------------------------------------------------------- */
|
|
||||||
/* folder:search */
|
|
||||||
/* -------------------------------------------------------------------------- */
|
|
||||||
{
|
|
||||||
displayName: 'Query',
|
|
||||||
name: 'query',
|
|
||||||
type: 'string',
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
operation: [
|
|
||||||
'search',
|
|
||||||
],
|
|
||||||
resource: [
|
|
||||||
'folder',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
default: '',
|
|
||||||
description: `The query text used to search for items. Values may be matched
|
|
||||||
across several fields including filename, metadata, and file content.`,
|
|
||||||
},
|
|
||||||
] as INodeProperties[];
|
|
|
@ -1,16 +1,14 @@
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
|
||||||
BINARY_ENCODING,
|
BINARY_ENCODING,
|
||||||
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
IBinaryKeyData,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeTypeDescription,
|
|
||||||
INodeType,
|
INodeType,
|
||||||
ILoadOptionsFunctions,
|
INodeTypeDescription,
|
||||||
INodePropertyOptions,
|
|
||||||
IBinaryKeyData,
|
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -19,14 +17,14 @@ import {
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
fileOperations,
|
|
||||||
fileFields,
|
fileFields,
|
||||||
|
fileOperations,
|
||||||
} from './FileDescription';
|
} from './FileDescription';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
folderFields,
|
||||||
folderOperations,
|
folderOperations,
|
||||||
folderFields
|
} from './FolderDescription';
|
||||||
} from './FolderDescriptiont';
|
|
||||||
|
|
||||||
export class MicrosoftOneDrive implements INodeType {
|
export class MicrosoftOneDrive implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -113,10 +111,17 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;
|
||||||
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${fileId}`);
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${fileId}`);
|
||||||
|
|
||||||
|
const fileName = responseData.name;
|
||||||
|
|
||||||
if (responseData.file === undefined) {
|
if (responseData.file === undefined) {
|
||||||
throw new Error('The ID you provided does not belong to a file.');
|
throw new Error('The ID you provided does not belong to a file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mimeType: string | undefined;
|
||||||
|
if (responseData.file.mimeType) {
|
||||||
|
mimeType = responseData.file.mimeType;
|
||||||
|
}
|
||||||
|
|
||||||
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${fileId}/content`, {}, {}, undefined, {}, { encoding: null, resolveWithFullResponse: true });
|
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${fileId}/content`, {}, {}, undefined, {}, { encoding: null, resolveWithFullResponse: true });
|
||||||
|
|
||||||
const newItem: INodeExecutionData = {
|
const newItem: INodeExecutionData = {
|
||||||
|
@ -124,8 +129,7 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
binary: {},
|
binary: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
let mimeType: string | undefined;
|
if (mimeType === undefined && responseData.headers['content-type']) {
|
||||||
if (responseData.headers['content-type']) {
|
|
||||||
mimeType = responseData.headers['content-type'];
|
mimeType = responseData.headers['content-type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +144,7 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
|
|
||||||
const data = Buffer.from(responseData.body);
|
const data = Buffer.from(responseData.body);
|
||||||
|
|
||||||
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data as unknown as Buffer, undefined, mimeType);
|
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data as unknown as Buffer, fileName, mimeType);
|
||||||
}
|
}
|
||||||
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
|
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
|
||||||
if (operation === 'get') {
|
if (operation === 'get') {
|
||||||
|
@ -151,17 +155,17 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_search?view=odsp-graph-online
|
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_search?view=odsp-graph-online
|
||||||
if (operation === 'search') {
|
if (operation === 'search') {
|
||||||
const query = this.getNodeParameter('query', i) as string;
|
const query = this.getNodeParameter('query', i) as string;
|
||||||
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='{${query}}')`);
|
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='${query}')`);
|
||||||
responseData = responseData.filter((item: IDataObject) => item.file);
|
responseData = responseData.filter((item: IDataObject) => item.file);
|
||||||
returnData.push(responseData as IDataObject);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
}
|
}
|
||||||
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online#example-upload-a-new-file
|
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online#example-upload-a-new-file
|
||||||
if (operation === 'upload') {
|
if (operation === 'upload') {
|
||||||
const parentId = this.getNodeParameter('parentId', i) as string;
|
const parentId = this.getNodeParameter('parentId', i) as string;
|
||||||
const binaryData = this.getNodeParameter('binaryData', 0) as boolean;
|
const isBinaryData = this.getNodeParameter('binaryData', i) as boolean;
|
||||||
let fileName = this.getNodeParameter('fileName', 0) as string;
|
const fileName = this.getNodeParameter('fileName', i) as string;
|
||||||
|
|
||||||
if (binaryData) {
|
if (isBinaryData) {
|
||||||
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string;
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string;
|
||||||
|
|
||||||
if (items[i].binary === undefined) {
|
if (items[i].binary === undefined) {
|
||||||
|
@ -174,20 +178,16 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
|
|
||||||
const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName];
|
const binaryData = (items[i].binary as IBinaryKeyData)[binaryPropertyName];
|
||||||
|
|
||||||
if (fileName !== '') {
|
|
||||||
fileName = `${fileName}.${binaryData.fileExtension}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = Buffer.from(binaryData.data, BINARY_ENCODING);
|
const body = Buffer.from(binaryData.data, BINARY_ENCODING);
|
||||||
responseData = await microsoftApiRequest.call(this, 'PUT', `/drive/items/${parentId}:/${fileName || binaryData.fileName}:/content`, body , {}, undefined, { 'Content-Type': binaryData.mimeType, 'Content-length': body.length } );
|
responseData = await microsoftApiRequest.call(this, 'PUT', `/drive/items/${parentId}:/${fileName || binaryData.fileName}:/content`, body, {}, undefined, { 'Content-Type': binaryData.mimeType, 'Content-length': body.length }, {} );
|
||||||
returnData.push(responseData as IDataObject);
|
|
||||||
|
|
||||||
|
returnData.push(JSON.parse(responseData) as IDataObject);
|
||||||
} else {
|
} else {
|
||||||
const body = Buffer.from(this.getNodeParameter('fileContent', i) as string, 'utf8');
|
const body = this.getNodeParameter('fileContent', i) as string;
|
||||||
if (fileName === '') {
|
if (fileName === '') {
|
||||||
throw new Error('File name must be defined');
|
throw new Error('File name must be set!');
|
||||||
}
|
}
|
||||||
responseData = await microsoftApiRequest.call(this, 'PUT', `/drive/items/${parentId}:/${fileName}.txt:/content`, body , {}, undefined, { 'Content-Type': 'text/plain' } );
|
responseData = await microsoftApiRequest.call(this, 'PUT', `/drive/items/${parentId}:/${fileName}:/content`, body , {}, undefined, { 'Content-Type': 'text/plain' } );
|
||||||
returnData.push(responseData as IDataObject);
|
returnData.push(responseData as IDataObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,14 +197,14 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
if (operation === 'getChildren') {
|
if (operation === 'getChildren') {
|
||||||
const folderId = this.getNodeParameter('folderId', i) as string;
|
const folderId = this.getNodeParameter('folderId', i) as string;
|
||||||
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/items/${folderId}/children`);
|
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/items/${folderId}/children`);
|
||||||
returnData.push(responseData as IDataObject);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
}
|
}
|
||||||
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_search?view=odsp-graph-online
|
//https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_search?view=odsp-graph-online
|
||||||
if (operation === 'search') {
|
if (operation === 'search') {
|
||||||
const query = this.getNodeParameter('query', i) as string;
|
const query = this.getNodeParameter('query', i) as string;
|
||||||
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='{${query}}')`);
|
responseData = await microsoftApiRequestAllItems.call(this, 'value', 'GET', `/drive/root/search(q='${query}')`);
|
||||||
responseData = responseData.filter((item: IDataObject) => item.folder);
|
responseData = responseData.filter((item: IDataObject) => item.folder);
|
||||||
returnData.push(responseData as IDataObject);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,11 +212,6 @@ export class MicrosoftOneDrive implements INodeType {
|
||||||
// For file downloads the files get attached to the existing items
|
// For file downloads the files get attached to the existing items
|
||||||
return this.prepareOutputData(items);
|
return this.prepareOutputData(items);
|
||||||
} else {
|
} else {
|
||||||
if (Array.isArray(responseData)) {
|
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
||||||
} else if (responseData !== undefined) {
|
|
||||||
returnData.push(responseData as IDataObject);
|
|
||||||
}
|
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 4 KiB After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in a new issue