Hello,
thank you for your response. Using the onBeforeResponse
callback to receive the changes from the pre-create
hook makes sense, thank you!
After managing to set the meta data in the hook and change the response,
I tried using the upload-success
hooks from uppy or the onAfterResponse
functions with medium success to parse the Upload-Metadata
header. It seems they expose the headers also depending on data in local storage (sending HEAD / PATCH requests instead of POST)?
I also saw a solution with the additional HEAD request from the client, but wanted to avoid that - as its one more point of possible failure with stable network conditions.
For now I also got an apparently stable solution working with removeFingerprintOnSuccess: true
and using the pre-finish hook
. Do you have any optimisation suggestions?
it mainly looks like this - I set metadata in the pre-create hook like this - usable for all succeeding hooks:
return context.json({
ChangeFileInfo: {
ID: uploadId, // overwrites tusd fileId - must be unique
MetaData: {
// visible in -> head /files/:id?
...uploadData.MetaData,
// pass fileId + uploadId to client and other hooks
uploadId: uploadId,
fileId: fileId,
},
},
HTTPResponse: {
Header: {
"X-File-Id": fileId, // visible in -> post /files?
},
},
});
Send it to the client in the pre-finish hook:
return context.json({
HTTPResponse: {
Header: {
"X-File-Id": fileId, // visible in -> post /files?
},
},
});
And retrieve it in the client like this + merge it with the client file metadata:
uppy.on<"upload-success">("upload-success", (file, response) => {
// assume X-File-Id header is set in pre-finish hook, which needs to be manually enabled!
const fileId = response.body.xhr.getResponseHeader("X-File-Id");
if (!fileId) {
console.warn("fileId is empty");
return;
}
try {
// merge server metadata with file.meta
uppy.setFileMeta(file.id, { ...file.meta, fileId });
return;
} catch (error) {
console.error(`error handling X-File-Id: ${error}`);
}
});
Thank you!