flowjs/flow.js

how do i check the file before upload?

Opened this issue · 7 comments

Now I have a requirement to call the background interface to detect before uploading the file. If the file already exists, cancel the upload and prompt, no upload file. Which method or configuration property do I use to perform this judgment? Thank you

you can use the events "fileAdded" and "filesAdded"

const flow = new Flow()
flow.on("filesAdded", function(flowFiles) {
// ToDo implement duplication check
});

you can check the files there, remove all duplicated files from the flow instance via flowFile.cancel() and then after that start the upload with flow.resume() or flow.upload()

Is it possible to perform an async operation in fileAdded?

No, it's not. You have to reject a file and add it later instead

Highly related to the discussion initiated at #319

  • filesAdded as a hook to interact with the process (not async) like a filter
    vs
  • filesAdded as an event/notification providing information to a (possibly async) listener acting on a side-channel

The last comment from madsnow was look for an async filter withing the general processing of filesAdded. I can't figure how this could be done correctly and don't think this should be supported (at least for now).

I plan to add a preFilterFiles hook (filesAdded staying as a simple notification event), in a future PR.
This hook would expect a Promise to be returned and block further processing (the call to addFiles, (async too)) until the hook's promise returns.

I don't know if it's the best JS-development pattern, but it'd resolve that problem (and the one caused by fileAdded) using a reproducible pattern and avoid adding state variables as Flow or FlowFile properties.

@drzraf sounds awesome 👍

@madsnow : You could have a look at #329 and see if

await asyncAddFile(yourfile, null, async (flowFile, event) => {
 // whatever you want before initialization
});

would fit (where the 3rd parameter is an async equivalent of initFileFn).

Note:

  • flowFile isn't yet fully bootstrapped at that moment (but FlowFile _bootstrap() isn't rocket-science either)
  • event may indicates whether its a retried attempt.