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

Event listener that fires before file upload process starts #5184

Closed
2 tasks done
jerryseigle opened this issue May 20, 2024 · 4 comments
Closed
2 tasks done

Event listener that fires before file upload process starts #5184

jerryseigle opened this issue May 20, 2024 · 4 comments
Labels

Comments

@jerryseigle
Copy link

Initial checklist

  • I understand this is a feature request and questions should be posted in the Community Forum
  • I searched issues and couldn’t find anything (or linked relevant results below)

Problem

We often need to upload multiple files, each requiring different configurations. For instance, when using a service like Supabase storage with Tus, we need to include an authorization token that is unique for each file. This means the authorization token is only valid for the specific file that requested it.

Solution

To address this, we propose creating an event listener that fires before each upload attempt. For example, if there are three files, the event would trigger just before each file starts uploading, allowing us to adjust the configuration based on the file details. This ensures that each file can be uploaded with its unique settings.

Alternatives

na

@Murderlon
Copy link
Member

Murderlon commented May 21, 2024

Hi, what about doing something like this with what we have already?

import Uppy from "@uppy/core";
import Tus from "@uppy/tus";

let token = null;

async function getAuthToken() {
  const res = await fetch("/auth/token");
  const json = await res.json();
  return json.token;
}

new Uppy().use(Tus, {
  endpoint: "<your-endpoint>",
  // Called again for every retry too.
  async onBeforeRequest(req) {
    if (!token) {
      token = await getAuthToken();
    }
    req.setHeader("Authorization", `Bearer ${token}`);
  },
  async onAfterResponse(req, res) {
    if (res.getStatus() === 401) {
      token = await getAuthToken();
    }
  },
});

@jerryseigle
Copy link
Author

@Murderlon is exactly what was needed in my case. If I try to upload 3 images I can see that it generates 3 signedUploadTokens as what I needed. I wonder do it apply the correct tokens to the corresponding file. I asked because I notice that it seem to run my the function to get the signUploadToken all at the same time

@Murderlon
Copy link
Member

I'm not sure what you mean. Are you still running into problems?

@jerryseigle
Copy link
Author

Everything is working as expected now. Thank very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants