use of com.haulmont.cuba.web.toolkit.ui.CubaUpload in project cuba by cuba-platform.
the class WebFileUploadField method initOldUploadButton.
protected void initOldUploadButton() {
uploadButton = createOldComponent();
final CubaUpload impl = (CubaUpload) uploadButton;
impl.setButtonCaption(messages.getMainMessage("upload.submit"));
impl.setDescription(null);
impl.setReceiver((fileName1, MIMEType) -> {
FileOutputStream outputStream;
try {
tempFileId = fileUploading.createEmptyFile();
File tmpFile = fileUploading.getFile(tempFileId);
// noinspection ConstantConditions
outputStream = new FileOutputStream(tmpFile);
} catch (Exception e) {
throw new RuntimeException("Unable to receive file", e);
}
return outputStream;
});
// Set single click upload functional
impl.setImmediate(true);
impl.addStartedListener(event -> {
if (event.getContentLength() > getActualFileSizeLimit()) {
impl.interruptUpload();
String warningMsg = messages.formatMainMessage("upload.fileTooBig.message", event.getFilename(), getFileSizeLimitString());
getFrame().showNotification(warningMsg, NotificationType.WARNING);
} else if (hasInvalidExtensionOld(event.getFilename())) {
impl.interruptUpload();
String warningMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", event.getFilename());
getFrame().showNotification(warningMsg, NotificationType.WARNING);
} else {
fireFileUploadStart(event.getFilename(), event.getContentLength());
}
});
impl.addFinishedListener(event -> fireFileUploadFinish(event.getFilename(), event.getLength()));
impl.addSucceededListener(event -> {
fileName = event.getFilename();
fileId = tempFileId;
saveFile(getFileDescriptor());
component.setFileNameButtonCaption(fileName);
fireFileUploadSucceed(event.getFilename(), event.getLength());
});
impl.addFailedListener(event -> {
try {
fileUploading.deleteFile(tempFileId);
tempFileId = null;
} catch (Exception e) {
if (e instanceof FileStorageException) {
FileStorageException fse = (FileStorageException) e;
if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND)
log.warn(String.format("Could not remove temp file %s after broken uploading", tempFileId));
}
log.warn(String.format("Error while delete temp file %s", tempFileId));
}
fireFileUploadError(event.getFilename(), event.getLength(), event.getReason());
});
}
Aggregations