Skip to content

fix!: Add setter helpers to DownloadEvent and new method for inline#21438

Merged
mshabarov merged 8 commits intomainfrom
refactor-download-event
May 20, 2025
Merged

fix!: Add setter helpers to DownloadEvent and new method for inline#21438
mshabarov merged 8 commits intomainfrom
refactor-download-event

Conversation

@mshabarov
Copy link
Copy Markdown
Contributor

@mshabarov mshabarov commented May 16, 2025

Refactors the DownloadEvent to be a normal class.
Removed the getters for file name, type and size and add setters instead that are basically helper methods for setting various attributes to a response.
Adds inline method so that you can call DownloadHandler.forFile(new File("path/to/file")).inline() and the download will be inlined onto page.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 16, 2025

Test Results

1 230 files  + 1  1 230 suites  +1   1h 13m 13s ⏱️ + 1m 27s
8 478 tests +15  8 422 ✅ +15  56 💤 ±0  0 ❌ ±0 
8 875 runs  +32  8 810 ✅ +31  65 💤 +1  0 ❌ ±0 

Results for commit 07c91da. ± Comparison against base commit a1a4943.

♻️ This comment has been updated with latest results.

return fileName == null ? "" : fileName;
public void setFileName(String fileName) {
response.setHeader("Content-Disposition",
"attachment;filename=" + fileName);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about adding overloaded methods to support inline vs attachment as well as encoding but skipped since it's just a convenient helper for people.

I would still suggest to change the filename to use "" around it so that spaces in the filename are correctly supported. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Disposition for more details

It's up to you if you also wanna support filename* by default since Vaadin is a globally used framework

Copy link
Copy Markdown
Contributor Author

@mshabarov mshabarov May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good comment! I've added the "" wrapping. filename* would make sense to do, but I'd prefer it in a separate PR (would need an internal deiscussion and a bit of insvestigation how to implement this).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might wanna create a test for "" - it's easy to miss (speaking from personal experience where I forgot about it and user complaint about bad file names)

mshabarov added 2 commits May 19, 2025 12:48
# Conflicts:
#	flow-server/src/main/java/com/vaadin/flow/server/streams/FileDownloadHandler.java
@mshabarov mshabarov requested a review from caalador May 19, 2025 09:52
} catch (IOException ioe) {
// Set status before output is closed (see #8740)
downloadEvent.getResponse().setStatus(
response.setStatus(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be downloadEvent.setStatus(int) but can be in other PR just wouldn't change this here in that case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add this helper separately.


@Override
public void handleDownloadRequest(DownloadEvent downloadEvent) {
String resourceName = getUrlPostfix();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resourceName should be taken from the DownloadResponse as it is dynamically determined when handler.appy(downloadEvent) is called.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +73 to +78
String resourceName = getUrlPostfix();
downloadEvent.setContentType(
getContentType(resourceName, downloadEvent.getResponse()));
if (!isInline()) {
downloadEvent.setFileName(resourceName);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be set after the hasError check

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

mshabarov added 3 commits May 19, 2025 15:40
# Conflicts:
#	flow-server/src/main/java/com/vaadin/flow/server/streams/DownloadHandler.java
#	flow-server/src/test/java/com/vaadin/flow/server/streams/AbstractDownloadHandlerTest.java
Comment on lines +143 to +144
response.setHeader("Content-Disposition",
"attachment; filename=\"" + fileName + "\"");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

if (fileName.isEmpty()) {
  response.setHeader("Content-Disposition", "attachment");
} else {
  response.setHeader("Content-Disposition",
                "attachment; filename=\"" + fileName + "\"");
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mshabarov mshabarov changed the title fix: Add setter helpers to DownloadEvent fix!: Add setter helpers to DownloadEvent and new method for inline May 20, 2025
@sonarqubecloud
Copy link
Copy Markdown

@mshabarov mshabarov merged commit 3d2a29f into main May 20, 2025
26 of 31 checks passed
@mshabarov mshabarov deleted the refactor-download-event branch May 20, 2025 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants