Skip to content

Commit

Permalink
feat: add file name to progress and file rejected listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin committed Nov 29, 2023
1 parent bf88f53 commit 0ac82cc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ private void createSimpleUpload() {
eventsOutput.add("-succeeded");
});
upload.addAllFinishedListener(event -> eventsOutput.add("-finished"));
upload.addFileRejectedListener(event -> eventsOutput.add("-rejected"));
upload.addFileRejectedListener(event -> {
eventsOutput.add("-rejected");
output.add("REJECTED:" + event.getFileName());
});
upload.addProgressListener(
event -> output.add("PROGRESS:" + event.getFileName()));

NativeButton clearFileListBtn = new NativeButton("Clear file list",
e -> upload.clearFileList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void testUploadAnyFile() throws Exception {

String content = uploadOutput.getText();

String expectedContent = tempFile.getName() + getTempFileContents();

Assert.assertEquals("Upload content does not match expected",
expectedContent, content);
Assert.assertTrue("Upload content does not contain file details",
content.contains(tempFile.getName() + getTempFileContents()));
Assert.assertTrue("Progress update event was not fired properly",
content.contains("PROGRESS:" + tempFile.getName()));
}

@Test
Expand Down Expand Up @@ -114,9 +114,12 @@ public void uploadInvalidFile_fileIsRejected() throws Exception {

WebElement eventsOutput = getDriver()
.findElement(By.id("test-events-output"));

Assert.assertEquals("Invalid file was not rejected", "-rejected",
eventsOutput.getText());

WebElement uploadOutput = getDriver().findElement(By.id("test-output"));
Assert.assertTrue("Rejected file name was incorrect", uploadOutput
.getText().contains("REJECTED:" + invalidFile.getName()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/
public class FileRejectedEvent extends ComponentEvent<Upload> {

private String errorMessage;
private final String fileName;

private final String errorMessage;

/**
* Creates a new event using the given source and indicator whether the
Expand All @@ -35,10 +37,14 @@ public class FileRejectedEvent extends ComponentEvent<Upload> {
* the source component
* @param errorMessage
* the error message
* @param fileName
* the rejected file name
*/
public FileRejectedEvent(Upload source, String errorMessage) {
public FileRejectedEvent(Upload source, String errorMessage,
String fileName) {
super(source, true);
this.errorMessage = errorMessage;
this.fileName = fileName;
}

/**
Expand All @@ -49,4 +55,13 @@ public FileRejectedEvent(Upload source, String errorMessage) {
public String getErrorMessage() {
return errorMessage;
}

/**
* Get the file name.
*
* @return file name
*/
public String getFileName() {
return fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ProgressUpdateEvent extends ComponentEvent<Upload> {
*/
private final long contentLength;

/**
* Name of file currently being uploaded
*/
private final String fileName;

/**
* Event constructor method to construct a new progress event.
*
Expand All @@ -43,12 +48,15 @@ public class ProgressUpdateEvent extends ComponentEvent<Upload> {
* bytes transferred
* @param contentLength
* total size of file currently being uploaded, -1 if unknown
* @param fileName
* name of file currently being uploaded
*/
public ProgressUpdateEvent(Upload source, long readBytes,
long contentLength) {
long contentLength, String fileName) {
super(source, false);
this.readBytes = readBytes;
this.contentLength = contentLength;
this.fileName = fileName;
}

/**
Expand Down Expand Up @@ -77,4 +85,13 @@ public long getReadBytes() {
public long getContentLength() {
return contentLength;
}

/**
* Get file name.
*
* @return file name
*/
public String getFileName() {
return fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.EventData;
import com.vaadin.flow.component.HasSize;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.Tag;
Expand All @@ -40,7 +37,6 @@
import com.vaadin.flow.component.shared.SlotUtils;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.dom.DomEventListener;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.internal.JsonSerializer;
import com.vaadin.flow.server.NoInputStreamException;
Expand Down Expand Up @@ -108,11 +104,14 @@ public UploadIcon() {
*/
public Upload() {
final String eventDetailError = "event.detail.error";
final String eventDetailFileName = "event.detail.file.name";
getElement().addEventListener("file-reject", event -> {
String detailError = event.getEventData()
.getString(eventDetailError);
fireEvent(new FileRejectedEvent(this, detailError));
}).addEventData(eventDetailError);
String detailFileName = event.getEventData()
.getString(eventDetailFileName);
fireEvent(new FileRejectedEvent(this, detailError, detailFileName));
}).addEventData(eventDetailError).addEventData(eventDetailFileName);

// If client aborts upload mark upload as interrupted on server also
getElement().addEventListener("upload-abort",
Expand Down Expand Up @@ -486,9 +485,13 @@ private void fireAllFinish() {
* bytes received so far
* @param contentLength
* actual size of the file being uploaded, if known
* @param contentLength
* name of the file being uploaded
*/
protected void fireUpdateProgress(long totalBytes, long contentLength) {
fireEvent(new ProgressUpdateEvent(this, totalBytes, contentLength));
protected void fireUpdateProgress(long totalBytes, long contentLength,
String fileName) {
fireEvent(new ProgressUpdateEvent(this, totalBytes, contentLength,
fileName));
}

/**
Expand Down Expand Up @@ -729,7 +732,7 @@ public boolean listenProgress() {
@Override
public void onProgress(StreamVariable.StreamingProgressEvent event) {
upload.fireUpdateProgress(event.getBytesReceived(),
event.getContentLength());
event.getContentLength(), event.getFileName());
}

@Override
Expand Down

0 comments on commit 0ac82cc

Please sign in to comment.