Created
April 3, 2014 19:05
-
-
Save gabrielruiu/9960772 to your computer and use it in GitHub Desktop.
Upload component (from Vaadin 7.1.13) that remembers the files it has uploaded and asks, in case a duplicate file is uploaded, if we wish to overwrite it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.vaadin.ui.Upload; | |
import org.apache.commons.io.IOUtils; | |
import org.vaadin.dialogs.ConfirmDialog; | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Upload component that remembers the files it has uploaded and asks, in case a duplicate file is uploaded, if | |
* we wish to overwrite it. | |
* | |
* Uses Apache Commons IO 2.4 (http://mvnrepository.com/artifact/commons-io/commons-io/2.4) | |
* and the ConfirmDialog addon from Vaadin Directory (https://vaadin.com/directory#addon/confirmdialog) | |
* | |
* @author Ruiu Gabriel Mihai ([email protected]) | |
*/ | |
public class RestrictingUpload extends Upload implements Upload.SucceededListener, Upload.Receiver { | |
private List<String> uploadedFilenames; | |
private ByteArrayOutputStream latestUploadedOutputStream; | |
public RestrictingUpload() { | |
setCaption("Upload"); | |
setButtonCaption("Upload file"); | |
addSucceededListener(this); | |
setReceiver(this); | |
uploadedFilenames = new ArrayList<String>(); | |
} | |
@Override | |
public OutputStream receiveUpload(String filename, String mimeType) { | |
latestUploadedOutputStream = new ByteArrayOutputStream(); | |
return latestUploadedOutputStream; | |
} | |
@Override | |
public void uploadSucceeded(SucceededEvent event) { | |
if (fileExistsInSystem(event.getFilename())) { | |
confirmOverwrite(event.getFilename()); | |
} else { | |
uploadedFilenames.add(event.getFilename()); | |
} | |
} | |
private void confirmOverwrite(final String filename) { | |
ConfirmDialog confirmDialog = new ConfirmDialog(); | |
String message = String.format("The file %s already exists in the system. Overwrite?", filename); | |
confirmDialog.show(getUI(), "Overwrite?", message, "Overwrite", "Cancel", new ConfirmDialog.Listener() { | |
@Override | |
public void onClose(ConfirmDialog dialog) { | |
if (dialog.isConfirmed()) { | |
copyFileToSystem(filename); | |
} | |
} | |
}); | |
} | |
private void copyFileToSystem(String filename) { | |
try { | |
IOUtils.write(latestUploadedOutputStream.toByteArray(), new FileOutputStream(filename)); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e2) { | |
e2.printStackTrace(); | |
} | |
} | |
private boolean fileExistsInSystem(String filename) { | |
return uploadedFilenames.contains(filename); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment