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

companion: set upload filename from metadata during uploads #1587

Merged
merged 4 commits into from
May 27, 2019

Conversation

ifedapoolarewaju
Copy link
Contributor

fixes #1409

@@ -329,10 +331,7 @@ class Uploader {
* start the tus upload
*/
uploadTus () {
const fname = path.basename(this.options.path)
const ftype = this.options.metadata.type
const metadata = Object.assign({ filename: fname, filetype: ftype }, this.options.metadata || {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah ok, so we did already do it here!

this.path = `${this.options.pathPrefix}/${Uploader.FILE_NAME_PREFIX}-${this.token}`
this.metadata = Object.assign({}, this.options.metadata || {})
this.metadata.filename = this.metadata.name || path.basename(this.path)
this.metadata.filetype = this.metadata.type
this.streamsEnded = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding this filename and filetype conversion to uploadRemote on @uppy/tus client instead? Then it’s consistent — tusd expects metadata this way, it doesn’t convert name to filename for you, and so will Companion. We have that conversion for direct uploads, and we’ll do the same in the same place for remote uploads.

https://github.com/transloadit/uppy/blob/master/packages/%40uppy/tus/src/index.js#L180-L184

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we have to keep it in companion until 2.0 :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s right, forgot about that, good catch! But what about adding it on the client anyway, then we can add a 2.0 todo to remove on Companion and forget about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding this filename and filetype conversion to uploadRemote on @uppy/tus client instead? Then it’s consistent

@arturi but then we'd have to change it for XHR-Uploads too. And as you can see from the code, the way companion sets filetype for multipart is quite different and prone to change depending on the request library used. So I don't think it matters if we change it from the client. So long as companion knows what to expect, it can change it accordingly

@arturi
Copy link
Contributor

arturi commented May 23, 2019

The filepath property overrides filename and may contain a relative path. This is typically used when uploading multiple files from a directory.
https://github.com/form-data/form-data#alternative-submission-methods

Do we want this too? We get the path when a folder with files is dropped in uppy.

#1509 the property is relativePath and it will be null if there was no folder.

@arturi
Copy link
Contributor

arturi commented May 23, 2019

We discussed in Slack that meta like filetype and filename should be set in the actual upload handler, specific to each protocol — tus/multipart/s3. This is because tusd uses filetype, but request uses contentType, for example.

@ifedapoolarewaju
Copy link
Contributor Author

Do we want this too? We get the path when a folder with files is dropped in uppy.

@arturi I'll rather try to avoid this now, as I am not sure of the security implications

@arturi
Copy link
Contributor

arturi commented May 24, 2019

Do we want this too? We get the path when a folder with files is dropped in uppy.
I'll rather try to avoid this now, as I am not sure of the security implications

Discussed with Ife. Because the filepath overrides the filename, we’ve decided against using it for now, since we are sending relativePath anyway, that can already be used on the server side to mimic the user’s file structure.

contentType: this.options.metadata.type
}
}
}
Copy link
Contributor

@arturi arturi May 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment/link here too, as to why filename and contentType? Stating that it’s conventional and linking to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives and https://github.com/request/request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be redundant because Content-Disposition is pretty common and that's what the browser also uses, and the library used is request so linking to request is unnecessary because this exact example is in the readme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metadata,
metadata: Object.assign(
{
// file name and type as specified by tus protocol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but is it defined by the protocol, or just the tusd implementation? I didn’t find it in the protocol, so maybe linking here: https://github.com/tus/tusd/blob/5b376141903c1fd64480c06dde3dfe61d191e53d/unrouted_handler.go#L614-L646?

Copy link
Contributor Author

@ifedapoolarewaju ifedapoolarewaju May 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since filetype is not documented, I have added a comment to the URL you shared

Copy link
Contributor

@arturi arturi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested again locally with latest changes, works 👌

@arturi
Copy link
Contributor

arturi commented May 27, 2019

I think it’s good to go! Nice work!

@goto-bus-stop
Copy link
Contributor

Changes look solid!

In xhr-upload client uploads, we use the original file name and type instead of the metadata file name and type. So there will be a difference between client and remote uploads still with this PR. I think using metadata is probably better though.

Maybe we can still do this and then change the name/type for xhr-upload client uploads in a minor release? It would be a breaking change but on an extreme technicality so it may be fine. The only problem is that changing the type may be harder for client uploads. You can create a new Blob but I don't know if that will affect memory usage/performance.

@goto-bus-stop
Copy link
Contributor

To recap from the call: We'll merge this first, then change the xhr-upload client uploads to send metadata name and type instead, by putting file.meta.name in formData.append and by slicing the file data Blob + using the contentType option.

@ifedapoolarewaju ifedapoolarewaju merged commit 3c08486 into master May 27, 2019
@arturi arturi deleted the companion-filename branch June 6, 2019 16:28
HeavenFox pushed a commit to docsend/uppy that referenced this pull request Jun 27, 2023
companion: set upload  filename from metadata during uploads
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.

Uploading From Dropbox - incorrect file name & extension after uploading
3 participants