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

Upload File To Multiple Endpoints #605

Closed
Cbodfield opened this issue Nov 22, 2021 · 3 comments
Closed

Upload File To Multiple Endpoints #605

Cbodfield opened this issue Nov 22, 2021 · 3 comments

Comments

@Cbodfield
Copy link

I have a use case where I would like to upload the same file two different endpoints - one for file verification/validation and then (after user confirmation) one for processing. However, I didn't see any documentation or issues regarding a way to do this.

I know the internal state for the File is being set to uploaded and preventing the file from requeuing. Is there some undocumented way to allow requeuing a file after a successful upload?

It's not a deal breaker if it's not available. It would just require a little bit of rework on my part but I thought I'd inquire before tackling that.

@gilest
Copy link
Collaborator

gilest commented Nov 29, 2021

Hey @Cbodfield thanks for your issue – it's an interesting one which certainly should be possible.

This is pseudocode, but how about:

<FileUpload
  @name="queueName"
  @onFileAdd={{this.verifyAndUpload}}
>
  <a>Choose files</a>
</FileUpload>
import File from 'ember-file-upload/file';

export default class DemoUpload extends Component {
  @service fileQueue;

  get queue() {
    return this.fileQueue.find('queueName');
  }

  @action
  async verifyAndUpload(verifyFile) {
    try {
      await verifyFile.upload('/verify');
    } catch (e) {
      // Alert user to verification failure
      return;
    }

    // Create a new file with same content as the verified file
    const uploadFile = File.fromBlob(verifyFile.blob);
    // Manually push it to the upload queue
    this.queue.push(uploadFile);
    // Finally upload it
    await uploadFile.upload('/upload');
  }
}

Copying the file to a new instance is a good idea when re-uploading because there is a bit of state in the File object (including the state property as you pointed out) with no clean way to reset it.

We are planning on adding more callbacks (such as when a file is successfully uploaded) in the upcoming major (see #316). At the moment we really have to use onFileAdd.

@Cbodfield
Copy link
Author

@gilest Thanks so much for this! Duplicating the file from the blob is exactly the piece I was missing. I was able to get it working with that change alone. Would you still suggest re-queueing the file? It seems to be working fine without that piece but I want to make sure I'm not stepping outside of the expected behavior of the library's API.

@gilest
Copy link
Collaborator

gilest commented Nov 29, 2021

Would you still suggest re-queueing the file?

I don't think it's mandatory. Main reason would be if you are showing queue data such as progress and number of uploads queued to the user in a template.

If you wanted to separate the UI logic for verify/upload you could actually use a dedicated queue for each of them, too.

It seems to be working fine without that piece but I want to make sure I'm not stepping outside of the expected behaviour of the library's API.

I think everything I've suggested is public API so it should be fine. Not the original author though, just a maintainer 🤷🏻

@gilest gilest closed this as completed Nov 29, 2021
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

No branches or pull requests

2 participants