-
Notifications
You must be signed in to change notification settings - Fork 201
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
@tus/server: allow customising upload url and upload id extraction #515
@tus/server: allow customising upload url and upload id extraction #515
Conversation
a433f73
to
14fe44b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution. I think it makes sense to offer this feature but I'm not sure about the approach.
- Conceptually, I'd like to keep "models" for simple interfaces/structs, I don't think we need this as a class there. (from that perspective
StreamSplitter
andMetadata
also don't really belong there but that's another conversation). - This puts a bigger implementation burden on the implementer as the entire
generateUrl
has to be recreated, including the internal options. - This makes makes it hard to introduce future changes without breaking custom implementations.
I would go for two simple functions (no class needed I think)
getFileIdFromRequest
as it is currentlygenerateUrl
ideally gets no internal options, instead it receiveshost
andproto
which are done by us internally before passing it in. There should be no need to passrelativeLocation
because if that's what you want to do in your custom logic, you can just do that. Same forpath
, this is already known for yourself top level as you pass it into the server. I wonder if this is possible with composition somehow so that we do have those options in scope but we don't expose it to the implementer.
What do you think?
Thanks for this!
We can refactor this in another PR
Not necessarily, because if we go with a class such as the above, we can always extend it and get the default behavior + any additional behavior. However, I'm not against having them as functions, since they can be equally re-used, but they will need to be exported independently and not embedded in the BaseHandler if we want to allow to be re-used
Ok, this looks like a good compromise! |
@Murderlon i've reworked the implementation and now the interface looks as following: new Server({
...
generateUrl: (req, { proto, host, baseUrl, path, id}) => {
return 'whatever url composition you want'
},
getFileIdFromRequest: (req) => {
return myCustomExtraction()
}
}) we can also call the default functions in case we want to just add encoding for example: import {generateUrl, getFileIdFromRequest} from '@tus/server'
new Server({
...
generateUrl: (req, { proto, host, baseUrl, path, id}) => {
id = Buffer.from(id, 'utf-8').toString('base64url')
return generateUrl(id, { proto, host, baseUrl, path })
},
getFileIdFromRequest: (req) => {
const id = getFileIdFromRequest(req.url as string)
if (!id) return false
return Buffer.from(id, 'base64url').toString('utf-8')
}
}) |
3fe711d
to
2ed9f27
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for applying the feedback! I think we're close to being ready. Some final things:
- We need docs for this
- Would be nice to have a simple test in
BaseHandler.test.ts
- I'm inclined not to export the default functions for this. Doesn't add that much value on top of already handling
proto
andhost
and we increase the API surface and loose flexibility to change the internal signature. - If we don't export, we can move the
extractHostAndProto
intoBaseHandler
and inline thegenerateUrl
logic at line 56. Similarly,getFileIdFromRequest
can just be inline logic at line 69.
What do you think?
2ed9f27
to
76856b0
Compare
All ready! |
bb3643f
to
76c9e62
Compare
76c9e62
to
3018929
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect! ✨
Co-authored-by: Merlijn Vos <[email protected]>
@Murderlon ready to merge 🎉 |
Allow full customization of upload url and upload-id extraction from the URL.
Keeping the default behavior intact.
This PR will allow providing an
UploadIdGenerator
interface with 2 methods to implement:generateUrl
getFileIdFromRequest
This way we can provide users with the possibility of customizing the URL to their liking.
Closes #457