Skip to content

Add a resumable upload to a resource, sent in chunks over tus - #2010

Draft
reiern70 wants to merge 2 commits into
masterfrom
resumable-upload
Draft

reiern70 wants to merge 2 commits into
masterfrom
resumable-upload

Conversation

@reiern70

@reiern70 reiern70 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

FileUploadToResourceField sends all its files in one multipart request. After a dropped connection, a proxy timeout or a reload of the page, a large file has to be sent again from its first byte. It cannot be paused, and a proxy may refuse a request that large.

This adds ResumableUploadToResourceField to wicket-extensions. It is a FileUploadToResourceField whose files each go in chunks, over the open tus protocol 1.0, to a ResumableUploadResourceReference. Each upload can be paused, resumed and canceled. After a broken connection it goes on from the byte the server got to. Once the user chooses the same file again after a reload of the page, or a restart of the browser, it goes on where it stopped.

// Application#init(), next to or instead of FileUploadResourceReference
mountResource("/resumable-uploads",
	new ResumableUploadResourceReference(new FolderUploadsFileManager(folder)));

// a page
ResumableUploadToResourceField field = new ResumableUploadToResourceField("files")
{
	@Override
	protected void onUploadSuccess(AjaxRequestTarget target, List<UploadInfo> infos)
	{
		// as for FileUploadToResourceField
	}
};
dropZone.add(new FileDropZoneBehavior(field));
list.add(new FileListBehavior(field));

How it works

  • It reuses the upload to a resource as far as it goes. Once a file is whole, the reference hands it to the application's IUploadsFileManager. The field reports it to onUploadSuccess() as an UploadInfo, through the callback of its parent. onUploadFailure(), onUploadCanceled(), the limits (maxSize, fileMaxSize, fileCountMax), startUpload()/cancelUpload(), FileDropZoneBehavior and FileListBehavior all behave as before. pauseUpload() and resumeUpload() are new.
  • The script is plain JavaScript. Wicket.ResumableUploadToResourceField builds on the prototype of Wicket.FileUploadToResourceField, so choosing, dropping and listing files are the parent's code. The list shows each upload with a <progress>, its state, and buttons to pause or resume it and to cancel it. A configurable number of files go at the same time while the others wait. A failed request is retried with backoff, and an upload waits for online when the browser went offline. The URLs of unfinished uploads are kept in localStorage by the name, size and date of their file, never their content, so setRemembering(false) turns that off.
  • The resource implements tus core plus creation, termination and expiration, with X-HTTP-Method-Override for proxies that refuse PATCH. Bytes go into a file of their own per upload. A chunk that breaks off keeps what arrived, and the browser asks with HEAD and goes on from there. A refusal carries the resource key of its error in Wicket-Upload-Error, which the field words. The answer that completes an upload carries the stored name in Wicket-Upload-File-Name.
  • Every request carries the field's id and limits, signed with the application's crypt, as the query of the multipart resource does, so the browser cannot raise them. An upload belongs to the HTTP session that created it, and the other sessions are told there is no such upload. A request without Tus-Resumable is refused. The reference limits the size of a file and of a chunk, the uploads of a session (count and bytes) and of all sessions, and keeps a minimum of free disk space. Uploads the browser has not touched for 30 minutes (by default) are deleted, and so are those of a session that ends, and all of them when the application stops.
  • In wicket-core:
    • FileUploadToResourceField.getUploadCallbackUrl() (protected final) lets a subclass rendering its own script report to the same callback.
    • module-info now exports org.apache.wicket.markup.html.form.upload.resource. The package holds public API but was only opened, so an application on the module path could not use the upload to a resource.
    • Javadoc now covers that package, and three @params with raw generics there are fixed.
  • Applications that do not use the new field see no change. The upload examples get a "Resumable upload to a resource" page.

Testing

  • ResumableUploadToResourceFieldTest, 19 tests with WicketTester: the protocol (create, chunks, HEAD, DELETE, method override, offsets that do not follow, chunks that break off, an empty file), the signed grant, the limits, sessions, expiry, cleanup when the application stops, the hand-over to the file manager and the field, and a reload that completes an upload under the new field.

  • resumable-upload-test.js, 14 QUnit tests with a fake XHR, run against both the jQuery and the vanilla Ajax engine: chunks and the report, pause and resume, a retry after a broken connection, waiting while offline, resuming an upload remembered from an earlier page or starting over when the server lost it, refusals and lost uploads, cancel with DELETE, limits checked before anything is sent, parallel uploads, an empty file, and a new rendering taking over the uploads.

  • mvn clean install -Pjs-test is green: 3549 Java tests, and 264 and 263 JavaScript tests (jQuery and vanilla engines), none failing.

  • The example in headless Chrome against Jetty, with the network throttled through the DevTools protocol:

    • two files paused one by one, then all at once, and resumed;
    • a reload in the middle of a 24 MB file, which resumed with HEAD from where it stopped after the same file was chosen again;
    • the network taken offline mid-upload, where the upload waited "Offline" and went on once back online.

    The SHA-256 of every stored file matched the original.

Backported to wicket-11.x in #2011: no API break there.

Open questions

  • Is wicket-extensions the right home, or should it sit next to FileUploadToResourceField in wicket-core?
  • Should remembering unfinished uploads in localStorage be on by default?
  • After a reload the page does not list the interrupted uploads until the user chooses the files again. Worth adding?

FileUploadToResourceField sends all its files in one multipart request.
After a dropped connection, a proxy timeout or a reload of the page, a
large file has to be sent again from its first byte. It cannot be paused,
and a proxy may refuse a request that large.

wicket-extensions gets ResumableUploadToResourceField, a subclass of
FileUploadToResourceField. Each of its files goes in chunks, over the
open tus protocol 1.0, to a ResumableUploadResourceReference that the
application mounts once, as it mounts FileUploadResourceReference. Each
upload can be paused, resumed and canceled. After a broken connection it
goes on from the byte the server got to, once the browser is online
again. The browser remembers unfinished uploads in localStorage, so the
same file chosen after a reload or a browser restart goes on where it
stopped. Once a file is whole the reference hands it to the application's
IUploadsFileManager, and the field reports it to onUploadSuccess() as an
UploadInfo, exactly as for the multipart resource. The limits, the
failure and cancel hooks, FileDropZoneBehavior and FileListBehavior all
work as before. The list shows each file's progress, with buttons to
pause, resume and cancel it.

The script is plain JavaScript and builds on Wicket.FileUploadToResourceField.
Neither side adds a dependency. Every request carries the field's id and
limits, signed with the application's crypt, as the query of the
multipart resource does. An upload belongs to the HTTP session that
created it. The reference limits the size of a file and of a chunk, the
uploads of a session and of all sessions, and the free space on disk. It
deletes uploads the browser has not touched for a while, those of a
session that ends, and all of them when the application stops. Wicket's
filter answers OPTIONS itself, so the resource does not announce its
tus version there; tus clients do not need it.

Three small changes in wicket-core make the subclass possible.
FileUploadToResourceField gets getUploadCallbackUrl(), so a subclass
rendering its own script reports to the same callback. module-info now
exports org.apache.wicket.markup.html.form.upload.resource: the package
holds public API but was only opened, so an application on the module
path could not use the upload to a resource. Javadoc now covers that
package, and its raw generics are fixed.

Applications that do not use the new field see no change. The upload
examples get a "Resumable upload to a resource" page.
@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.52468% with 112 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.26%. Comparing base (23b0c41) to head (5935a7f).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2010      +/-   ##
============================================
+ Coverage     63.06%   63.26%   +0.19%     
- Complexity    11691    11866     +175     
============================================
  Files          1255     1261       +6     
  Lines         48921    49467     +546     
  Branches       6922     6979      +57     
============================================
+ Hits          30854    31297     +443     
- Misses        15386    15463      +77     
- Partials       2681     2707      +26     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

A React application or a mobile app has no ResumableUploadToResourceField
to sign its requests, so the resource refused all of them.
ResumableUploadResourceReference.newQuery() now creates the signed query
for an id the application chooses and limits it sets. The names of the
headers that carry the stored file name and the error key are now public
constants of the reference. A client like that can complete an upload
and hand the stored name back to the application, which finds the file
in the file manager under its id.
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

Successfully merging this pull request may close these issues.

2 participants