Skip to content

Commit

Permalink
fix(Microsoft OneDrive Node): Try to download file using downloadUrl (#…
Browse files Browse the repository at this point in the history
…13200)

Co-authored-by: Michael Kret <[email protected]>
  • Loading branch information
ShireenMissi and michael-radency authored Feb 12, 2025
1 parent 56426e9 commit 67cd05c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingMessage } from 'http';
import type {
IDataObject,
IExecuteFunctions,
Expand Down Expand Up @@ -105,6 +106,7 @@ export class MicrosoftOneDrive implements INodeType {
responseData = await microsoftApiRequest.call(this, 'GET', `/drive/items/${fileId}`);

const fileName = responseData.name;
const downloadUrl = responseData['@microsoft.graph.downloadUrl'];

if (responseData.file === undefined) {
throw new NodeApiError(this.getNode(), responseData as JsonObject, {
Expand All @@ -117,16 +119,28 @@ export class MicrosoftOneDrive implements INodeType {
mimeType = responseData.file.mimeType;
}

responseData = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${fileId}/content`,
{},
{},
undefined,
{},
{ encoding: null, resolveWithFullResponse: true },
);
try {
responseData = await microsoftApiRequest.call(
this,
'GET',
`/drive/items/${fileId}/content`,
{},
{},
undefined,
{},
{ encoding: null, resolveWithFullResponse: true },
);
} catch (error) {
if (downloadUrl) {
responseData = await this.helpers.httpRequest({
method: 'GET',
url: downloadUrl,
returnFullResponse: true,
encoding: 'arraybuffer',
json: false,
});
}
}

const newItem: INodeExecutionData = {
json: items[i].json,
Expand All @@ -146,10 +160,15 @@ export class MicrosoftOneDrive implements INodeType {

items[i] = newItem;

const data = Buffer.from(responseData.body as Buffer);
let data;
if (responseData?.body instanceof IncomingMessage) {
data = responseData.body;
} else {
data = Buffer.from(responseData.body as Buffer);
}

items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(
data as unknown as Buffer,
data,
fileName as string,
mimeType,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { IncomingMessage } from 'http';
import type { MockProxy } from 'jest-mock-extended';
import { mock } from 'jest-mock-extended';
import { type IHttpRequestMethods, type IExecuteFunctions, ApplicationError } from 'n8n-workflow';

import * as genericFunctions from '../../GenericFunctions';
import { MicrosoftOneDrive } from '../../MicrosoftOneDrive.node';

jest.mock('../../GenericFunctions', () => ({
...jest.requireActual('../../GenericFunctions'),
microsoftApiRequest: jest.fn(async function (_: IHttpRequestMethods, resource: string) {
if (resource === '/drive/items/fileID') {
return {
name: 'MyFile',
'@microsoft.graph.downloadUrl': 'https://test.com/file',
file: {
mimeType: 'image/png',
},
};
}
if (resource === '/drive/items/fileID/content') {
throw new ApplicationError('Error');
}
}),
}));

describe('Test MicrosoftOneDrive, file > download', () => {
let mockExecuteFunctions: MockProxy<IExecuteFunctions>;
let microsoftOneDrive: MicrosoftOneDrive;
const httpRequest = jest.fn(async () => ({ body: mock<IncomingMessage>() }));
const prepareBinaryData = jest.fn(async () => ({ data: 'testBinary' }));

beforeEach(() => {
mockExecuteFunctions = mock<IExecuteFunctions>();
microsoftOneDrive = new MicrosoftOneDrive();

mockExecuteFunctions.helpers = {
httpRequest,
prepareBinaryData,
returnJsonArray: jest.fn((data) => [data]),
constructExecutionMetaData: jest.fn((data) => data),
} as any;
});

afterEach(() => {
jest.clearAllMocks();
});

it('should call helpers.httpRequest when request to /drive/items/{fileId}/content fails', async () => {
const items = [{ json: { data: 'test' } }];
mockExecuteFunctions.getInputData.mockReturnValue(items);
mockExecuteFunctions.getNodeParameter.mockImplementation((key: string) => {
if (key === 'resource') return 'file';
if (key === 'operation') return 'download';
if (key === 'fileId') return 'fileID';
if (key === 'binaryPropertyName') return 'data';
});

const result = await microsoftOneDrive.execute.call(mockExecuteFunctions);

expect(genericFunctions.microsoftApiRequest).toHaveBeenCalledTimes(2);
expect(httpRequest).toHaveBeenCalledTimes(1);
expect(httpRequest).toHaveBeenCalledWith({
encoding: 'arraybuffer',
json: false,
method: 'GET',
returnFullResponse: true,
url: 'https://test.com/file',
});
expect(prepareBinaryData).toHaveBeenCalledTimes(1);
expect(result).toEqual([
[{ binary: { data: { data: 'testBinary' } }, json: { data: 'test' } }],
]);
});
});

0 comments on commit 67cd05c

Please sign in to comment.