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

Support nested directories with namingFunction & clarify docs #549

Merged
merged 6 commits into from
Jan 17, 2024

Conversation

Murderlon
Copy link
Member

@Murderlon Murderlon commented Jan 8, 2024

Resolves #457 (comment)

  • Support nested directories in @tus/file-store (@tus/s3-store: add object prefixing #457 (comment))
  • Remove req.baseUrl, which was ts-ignore'd, from generateUrl. This is always undefined and only exists in Express.js. From a type perspective it's a breaking change, but since we always passed undefined and removing the type means it's still undefined so it's practically speaking the same.
  • Clarify docs on generateUrl, getFileIdFromRequest, and namingFunction.

I thought generateUrl supersedes namingFunction, as instead of only generating an ID we now do the entire URL. Except generateUrl only changes the returned Location header, while we used to have the principle of upload ID mapping to the file name in storage. So if you return an entirely different ID in generateUrl, the default of namingFunction is the name written to storage. If you define namingFunction, then that becomes the id argument in generateUrl.

@Murderlon Murderlon self-assigned this Jan 8, 2024
Copy link
Member

@Acconut Acconut left a comment

Choose a reason for hiding this comment

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

Great improvement! After these changes, the two options don't seem odd to me anymore. namingFunction customizes the upload ID and file name, while generateURL and getFileIdFromRequest (which should maybe have been called getUploadIdFromRequest?) embed or extract the upload ID from the URL.

packages/server/README.md Show resolved Hide resolved
packages/server/README.md Show resolved Hide resolved
packages/server/README.md Outdated Show resolved Hide resolved
@Murderlon Murderlon changed the title @tus/server: remove useless req.baseUrl & clarify docs Support nested directories with namingFunction & clarify docs Jan 16, 2024
@Murderlon
Copy link
Member Author

Murderlon commented Jan 16, 2024

To simplify the code for custom implementations, we could also pass a default id to namingFunction and the default extracted id to getFileIdFromRequest. Then it becomes a lot cleaner:

const path = '/files'
const server = new Server({
  path,
  datastore: new FileStore({directory: './test/output'}),
-  namingFunction(req) {
+ namingFunction(req, defaultId) {
-    const id = crypto.randomBytes(16).toString('hex')
    const folder = getFolderForUser(req) // your custom logic
-    return `users/${folder}/${id}`
+    return `users/${folder}/${defaultId}`
  },
  generateUrl(req, {proto, host, path, id}) {
    id = Buffer.from(id, 'utf-8').toString('base64url')
    return `${proto}://${host}${path}/${id}`
  },
-  getFileIdFromRequest(req) {
+ getFileIdFromRequest(req, lastPath) {
-    const reExtractFileID = /([^/]+)\/?$/
-    const match = reExtractFileID.exec(req.url as string)
-
-   if (!match || path.includes(match[1])) {
-      return
-   }
-
-    return Buffer.from(match[1], 'base64url').toString('utf-8')
+    return Buffer.from(lastPath, 'base64url').toString('utf-8')
  },
})

@fenos
Copy link
Collaborator

fenos commented Jan 17, 2024

@Murderlon not sure i like the idea of passing a default id in the namingFunction - it feels like wasted computation for users who really don't need the defaultId

However, I do like the lastPath param on the getFileIdFromRequest it makes things easier.
I'm wondering if passing the full uri instead of lastpath could also unlock more flexibilities?

that way an implementor might also decide to not base64 the id and simply do:

getFileIdFromRequest(req, uri) {
   return uri.split('/').slice(3).join('/')
}

@Murderlon
Copy link
Member Author

Fair enough, I was mostly thinking out loud. We can keep things as is for now. uri probably isn't different from req.url either.

Copy link
Collaborator

@fenos fenos left a comment

Choose a reason for hiding this comment

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

Looks great!

@Murderlon Murderlon merged commit 367fdec into main Jan 17, 2024
2 checks passed
@Murderlon Murderlon deleted the clarrify-namingFunction branch January 17, 2024 14:33
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.

@tus/s3-store: add object prefixing
3 participants