msgraph-sdk-javascript icon indicating copy to clipboard operation
msgraph-sdk-javascript copied to clipboard

LargeFileUploadTask is ending with PassThrough stream instead of saying JSON response with id

Open hilarudeens opened this issue 6 years ago • 11 comments

Bug Report

I'm using msgraph-sdk-javascript in NodeJs and try attach a document in Outlook email. The issue is, LargeFileUploadTask.uploadSlice function returns PassThrough readable stream on end of file upload instead of JSON response with id.

Prerequisites

  • [ Yes] Can you reproduce the problem?
  • [Yes ] Are you running the latest version? I'm using 2.1.1 as of 19th Nov 2020
  • [Yes ] Are you reporting to the correct repository?
  • [ ] Did you perform a cursory search?

Steps to Reproduce

  1. Create an Outlook draft message by usingPostman.

  2. Create a simple HttpTrigger Azure Function and Add following code in Azure Function handler.

const saveAttachment = async (oid, tenantId, messageId, file) => {
    // getClient is just creating client with GraphToken generate authProvider.
    // authProvider is doing client-credential flow to generate token.
    const client = await getClient(tenantId);

    const uploadSessionPayload = {
        AttachmentItem: {
          attachmentType: 'file',
          name: file.filename,
          size: file.size
        }
    };

    const uploadSession = await LargeFileUploadTask.createUploadSession(
        client, 
        `/users/${oid}/messages/${messageId}/attachments/createUploadSession`,
        uploadSessionPayload,
    );
    const fileObject = {
        content: file.data,
        name: file.filename,
        size: file.size
    };

    const { url: uploadUrl } = uploadSession; 
    const uploadUrlTokens = uploadUrl.split('?');
    const uploadUrlAuthToken = uploadUrlTokens[1].replace('authtoken=', '');
    const uploadClient = Client.init({
        authProvider: (done) => done(null, uploadUrlAuthToken),
        debugLogging: true
    });

    const uploadTask = new LargeFileUploadTask(uploadClient, fileObject, uploadSession, {
        rangeSize: 5 * 1024 * 1024,
    });

    const uploadResponse = await uploadTask.upload();
    return uploadResponse;
};
  1. Execute AzureFunction in Visual Studio Code.

  2. Call HttpTrigger from Postman

Expected behavior: Large file should be uploaded in sequential PUT call. In end of all chunks upload, LargeFileUploadTask.uploadSlice function should give response with 'id'

ShouldEndUpFileJsonResponseWithIdProperty

Actual behavior: LargeFileUploadTask.uploadSlice function is giving NodeJs ReadableStream.

Additional Context

LargeFileUploadResponse1 LargeFileUploadResponseEndOfUpload

Code Line:

https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/41306143d8978f5256b20354adf7486451a11dc6/src/tasks/LargeFileUploadTask.ts#L254

AB#7549

hilarudeens avatar Nov 19 '20 21:11 hilarudeens

@hilarudeens Thank you for reporting this issue!

I will take a look at this issue.

nikithauc avatar Nov 19 '20 21:11 nikithauc

@hilarudeens The LargeFileUploadTask is designed for uploading to OneDrive. I see that you are trying to upload an attachment to Outlook. The responses by Drive and Outlook differ from each another. Please refer to this https://docs.microsoft.com/en-us/graph/outlook-large-attachments?tabs=javascript. In the document you will see that the final response from outlook is expected to be an empty body with the Location (of the attachment you uploaded) header. Please let me know if that helps.

Thank you @ificator for your help with this issue!

nikithauc avatar Nov 25 '20 22:11 nikithauc

TODO - The current LargeFileUpload task implementation in JS sdk needs an update. We should be allowing uploads to Outlook and the design should not be tightly coupled with the response format of one service like the current implementation .

nikithauc avatar Dec 04 '20 07:12 nikithauc

@nikithauc Thanks for responding.

I'm expecting a way to get id. I'm okay to read Location header, however the current SDK implementation is giving passThrough stream. I'm unable to read Location header from passThrough.

Do I need to wait for SDK upgrade? or is there any workaround?

hilarudeens avatar Dec 04 '20 13:12 hilarudeens

@hilarudeens There will be an design change for this issue.

A suggested workaround would be following this https://docs.microsoft.com/en-us/graph/outlook-large-attachments?tabs=javascript documention. On creation of the upload session as shown in the documentation, you can use a pattern similar to upload and change the condition to end the upload when the location or success condition is met.

I apologize for the inconvenience. Please let me know if you have any questions.

nikithauc avatar Dec 07 '20 19:12 nikithauc

https://github.com/microsoftgraph/msgraph-sdk-serviceissues/issues/39.

nikithauc avatar Dec 11 '20 08:12 nikithauc

this issue of the sdk returning a passThrough stream can also be reproduced by making a call to the copy endpoint as specified here (specified in the 'initial action request' section).

clawyq avatar Feb 04 '21 19:02 clawyq

@clawyq Thanks for bringing this to our attention! This issue will be addressed in 3.0.0.

nikithauc avatar Feb 07 '21 08:02 nikithauc

@clawyq Can you please check if the following works for you -

let res = await client.api('/me/drive/items/{folder-item-id}/copy').responseType(ResponseType.RAW).
	.version('beta')
	.post(driveItem);

using responseType(ResponseType.RAW) should fetch the entire response with the headers.

nikithauc avatar Feb 07 '21 08:02 nikithauc

@nikithauc I was able to retrieve the monitor URL through the steps detailed here. It would be very helpful if there was documentation for this flow with the API.

Also, does the graph client support querying for the progress of the operation? Currently, after obtaining the monitor URL, I will then use axios to get the status of the operation - was wondering if the SDK itself natively provides the ability to query for the status of the operation. Thanks!

clawyq avatar Feb 07 '21 09:02 clawyq

@clawyq can you try the following ?

let res = await client.api(<monitor url>)
	.version(<v1.0/beta>)
	.get();

nikithauc avatar Feb 09 '21 07:02 nikithauc