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

@tus/server: add lastPath arg to getFileIdFromRequest #626

Merged
merged 2 commits into from
Jun 12, 2024

Conversation

Murderlon
Copy link
Member

As soon as people want nested directories they have to implement namingFunction,generateUrl, and getFileIdFromRequest.

Almost always, the last part of the path (after last /) is the ID. It's therefor a little awkward to let users repeat our internal code in getFileIdFromRequest again.

const path = '/files'
const server = new Server({
  path,
  datastore: new FileStore({directory: './test/output'}),
  namingFunction(req) {
    const id = crypto.randomBytes(16).toString('hex')
    const folder = getFolderForUser(req) // your custom logic
    return `users/${folder}/${id}`
  },
  generateUrl(req, {proto, host, path, id}) {
    id = Buffer.from(id, 'utf-8').toString('base64url')
    return `${proto}://${host}${path}/${id}`
  },
  getFileIdFromRequest(req) {
    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')
  },
})

Which can be simplified to this for most cases now:

const server = new Server({
  // ...
  getFileIdFromRequest(req, lastPath) {
    return Buffer.from(lastPath, 'base64url').toString('utf-8')
  },
})

@Murderlon Murderlon self-assigned this Jun 11, 2024
Copy link

changeset-bot bot commented Jun 11, 2024

🦋 Changeset detected

Latest commit: 0fa15d6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tus/server Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Murderlon Murderlon merged commit ea2bf07 into main Jun 12, 2024
4 checks passed
@Murderlon Murderlon deleted the getFileIdFromRequest-lastPath branch June 12, 2024 12:01
Murderlon added a commit to netdown/tus-node-server that referenced this pull request Jul 15, 2024
* main:
  Add issue templates
  [ci] release (tus#625)
  Add .npmrc
  @tus/server: add `lastPath` arg to `getFileIdFromRequest` (tus#626)
  Add basic storage info to Upload model (tus#624)
  Release
  v0.0.0
  @tus/server: allow onUploadFinish hook to override response data (tus#615)
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.

1 participant