Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

FS-4355: Dynamically set file url on open method #520

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
{% endif %}

const url = await getPreSignedUrl(file);
this.options.url = url;
file.uploadUrl = url;
done();
},
// https://github.com/dropzone/dropzone/issues/590
Expand Down Expand Up @@ -446,6 +446,25 @@ <h2 class="govuk-error-summary__title" id="error-summary-title">
return response.ok && (await response.json()).url
}

const getFileByXHR = (xhr) => {
return (myDropzone.files || []).find(f => f.xhr === xhr);
}

// Overrides XMLHttpRequest to inject a file-specific upload URL. This custom implementation enables Dropzone to
// upload to multiple URLs by linking an XHR instance to a file's pre-signed URL. The `open` method uses the
// pre-signed URL (set in `accept`) if available, ensuring each file has a unique upload destination. This
// workaround prevents race conditions from Dropzone's global URL configuration by associating each file with its
// XHR instance, avoiding mismatched or duplicate uploads across the event loop.
const OriginalXMLHttpRequest = window.XMLHttpRequest;
class CustomXMLHttpRequest extends OriginalXMLHttpRequest {
open(method, url, async = true, user = null, password = null) {
const file = getFileByXHR(this);
const openUrl = file && file.uploadUrl ? file.uploadUrl : url;
return super.open(method, openUrl, async, user, password);
}
}
window.XMLHttpRequest = CustomXMLHttpRequest;

</script>

{% endmacro %}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=0.1.268
VERSION=0.1.269
Loading