Skip to content

Add support for relative path for file attachments#203

Closed
monkbroc wants to merge 2 commits intoform-data:masterfrom
particle-iot:relative-path
Closed

Add support for relative path for file attachments#203
monkbroc wants to merge 2 commits intoform-data:masterfrom
particle-iot:relative-path

Conversation

@monkbroc
Copy link
Copy Markdown

@monkbroc monkbroc commented May 11, 2016

I need to be able to provide a relative path in addition to the filename for an attachment.

The change adds an optional option relativePath to append() to include a path before the file basename in the form multi-part header.

I added a test for the new behavior and a line in the Readme.

This is similar to #181.

@review-ninja
Copy link
Copy Markdown

ReviewNinja

@alexindigo
Copy link
Copy Markdown
Member

Thank you for the PR.
Do you mind to explain a bit more what problem you're trying to solve?
Thanks.

@monkbroc
Copy link
Copy Markdown
Author

Sure. This is for the Particle API for internet-connected microcontrollers.

One API endpoint supports cloud compiling of firmware source code. It takes a multi-part upload with source code files as file uploads. The compile server puts the files in their relative paths, compiles them and returns a binary.

For example, if you send main.cpp, lib/gps.cpp and lib/gps.h the API expect these headers:

Content-Disposition: form-data; name="file"; filename="main.cpp"

Content-Disposition: form-data; name="file1"; filename="lib/gps.cpp"

Content-Disposition: form-data; name="file2"; filename="lib/gps.h"

Without the lib part the file hierarchy is flattened and the compilation will fail.

@alexindigo
Copy link
Copy Markdown
Member

I see. Thanks for the details.

What if we do it in more standard compliant way? (https://developer.mozilla.org/en-US/docs/Web/API/FormData/append)

form.append('field1', data, 'lib/gps.cpp');

and

form.append('field1', data, {
  more: 'things',
  filename: 'lib/gps.cpp'
});

@monkbroc
Copy link
Copy Markdown
Author

Thanks for the quick response and pointing to the standard.

The issue with your proposed approach is that it would be a breaking API change for form-data. Current callers expect an absolute path to be changed to a basename. (BTW I don't think this is captured in a test. I will add this to the PR)

Another implementation would be to add a flag includePath that would disable the basename call and pass the filename parameter directly.

@monkbroc
Copy link
Copy Markdown
Author

I changed the implementation. Do you feel this is more standards-compliant?

form.append('field1', data, {
  more: 'things',
  filename: 'lib/gps.cpp'
  includePath: true
});

@coveralls
Copy link
Copy Markdown

coveralls commented May 31, 2016

Coverage Status

Coverage increased (+0.02%) to 96.703% when pulling e622bd8 on spark:relative-path into ddb3762 on form-data:master.

@monkbroc
Copy link
Copy Markdown
Author

Is there a chance to merge this in? I can rebase if it helps.

@alexindigo
Copy link
Copy Markdown
Member

Hey sorry, I've been far from this part of the internet for too long.

I like your solution that it doesn't break compatibility.
But other solution has it's simplicity factor (although it's a breaking change).

What do you feel like, if we add filepath option, would it work for you?

@monkbroc
Copy link
Copy Markdown
Author

Welcome back to this corner of the Internet :)

Given that #355 was merged, I don't think this PR is necessary anymore. I'll close it in favor of #355.

@alexindigo
Copy link
Copy Markdown
Member

👍

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.

4 participants