Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to change Raw Body Data before sending. #276

Merged
merged 3 commits into from
Dec 20, 2019

Conversation

aayusharyan
Copy link
Collaborator

@aayusharyan aayusharyan commented Dec 20, 2019

Add functionality to change Raw Body Data before sending.
This is particularly helpful when using Upload with Google Storage and many other platforms where we have to upload multipart related form data. (i.e. Different parts in the same request).
Example:
Google Cloud Storage expects data body to be in this format.
[BOUNDARY] is the Content Boundary which can be set using Custom Header function.
[FILE_DATA] is actually the chunk body in this case. (which is a blob).

--[BOUNDARY]
Content-Type: application/json; charset=UTF-8

{
  "name": "myObject",
  "metadata": {
  "a":"b"
  }
}

--[BOUNDARY]
Content-Type: image/jpeg

[FILE_DATA]
--[BOUNDARY]--

If this is feasible and merged, then I'll change the Documentation...

@aayusharyan
Copy link
Collaborator Author

This PR will fix. #170

@AidasK
Copy link
Member

AidasK commented Dec 20, 2019

I think I can merge this, though it would be great that you could include GCS usage example with this option too and add this param in the docs

@aayusharyan
Copy link
Collaborator Author

aayusharyan commented Dec 20, 2019

@AidasK
Hey,
Thanks for the quick response.
There is specification in the body for GCS, (check here). So, the code can be used to customize like this. 👇

var preprocessChunk = function(flowChunk, data) {
  var e = data.get('file');
  var chunk_data = flowChunk.bytes;
  var chunkNumber = flowChunk.offset;

  var objectNameInfo = {
    'name': 'file'+chunkNumber+'_.jpg',
    'metadata': {
        'chunkNumber': chunkNumber
    }
  };
  var jsonse = JSON.stringify(objectNameInfo);

  var fileMetaBlob = new Blob([jsonse], {type: 'application/json'});

  var blob = new Blob([
    "--foo_bar_baz\n",
    "Content-Type: "+fileMetaBlob.type+"; charset=UTF-8\n",
    "\n",
    fileMetaBlob,
    "\n",
    "--foo_bar_baz\n",
    "Content-Type: "+chunk_data.type+"\n",
    "\n",
    chunk_data,
    "\n",
    "--foo_bar_baz--",
  ], {type: "text/plain"});
  
  return blob;
}

However, I am not sure what to include in the documentation. I mean, there is no example for other functionalities of the library. So, maybe keep it uniform and include explanation only without example... 🤷‍♂

@AidasK
Copy link
Member

AidasK commented Dec 20, 2019

Add param and explanation to the docs and link to this issue #276

@aayusharyan
Copy link
Collaborator Author

@AidasK
Please check now.

@AidasK AidasK merged commit 6aca5b2 into flowjs:master Dec 20, 2019
@AidasK
Copy link
Member

AidasK commented Dec 20, 2019

Thanks for your time, merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants