use of com.haulmont.cuba.core.global.FileStorageException in project documentation by cuba-platform.
the class EmployeeEdit method init.
@Override
public void init(Map<String, Object> params) {
uploadField.addFileUploadSucceedListener(event -> {
FileDescriptor fd = uploadField.getFileDescriptor();
try {
fileUploadingAPI.putFileIntoStorage(uploadField.getFileId(), fd);
} catch (FileStorageException e) {
throw new RuntimeException("Error saving file to FileStorage", e);
}
getItem().setImageFile(dataSupplier.commit(fd));
displayImage();
});
uploadField.addFileUploadErrorListener(event -> showNotification("File upload error", NotificationType.HUMANIZED));
employeeDs.addItemPropertyChangeListener(event -> {
if ("imageFile".equals(event.getProperty()))
updateImageButtons(event.getValue() != null);
});
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class FileDownloadController method download.
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
UserSession userSession = getSession(request, response);
if (userSession == null)
return;
AppContext.setSecurityContext(new SecurityContext(userSession));
try {
File file = null;
FileDescriptor fd = null;
if (request.getParameter("p") != null)
file = getFile(request, response);
else
fd = getFileDescriptor(request, response);
if (fd == null && file == null)
return;
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setIntHeader("Expires", -1);
response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE);
InputStream is = null;
ServletOutputStream os = null;
try {
is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
} catch (FileStorageException e) {
log.error("Unable to download file", e);
response.sendError(e.getType().getHttpStatus());
} catch (Exception ex) {
log.error("Unable to download file", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
} finally {
AppContext.setSecurityContext(null);
}
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class FileLoaderImpl method checkIfFileDescriptorExists.
protected void checkIfFileDescriptorExists(FileDescriptor fd) throws FileStorageException {
try (Transaction tx = persistence.getTransaction()) {
FileDescriptor existingFile = persistence.getEntityManager().find(FileDescriptor.class, fd.getId());
if (existingFile == null || entityStates.isDeleted(existingFile)) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fd.getName());
}
tx.commit();
}
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class FileStorageExceptionHandler method doHandle.
@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) {
String msg = null;
if (throwable != null) {
FileStorageException storageException = (FileStorageException) throwable;
String fileName = storageException.getFileName();
if (storageException.getType().equals(FileStorageException.Type.FILE_NOT_FOUND))
msg = messages.formatMessage(getClass(), "fileNotFound.message", fileName);
else if (storageException.getType().equals(FileStorageException.Type.STORAGE_INACCESSIBLE))
msg = messages.getMessage(getClass(), "fileStorageInaccessible.message");
}
if (msg == null) {
msg = messages.getMessage(getClass(), "fileStorageException.message");
}
windowManager.showNotification(msg, Frame.NotificationType.ERROR);
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class WebFileMultiUploadField method initComponent.
protected void initComponent(CubaFileUpload impl) {
impl.setMultiSelect(true);
Messages messages = beanLocator.get(Messages.NAME);
impl.setProgressWindowCaption(messages.getMainMessage("upload.uploadingProgressTitle"));
impl.setUnableToUploadFileMessage(messages.getMainMessage("upload.unableToUploadFile"));
impl.setCancelButtonCaption(messages.getMainMessage("upload.cancel"));
impl.setCaption(messages.getMainMessage("upload.submit"));
impl.setDropZonePrompt(messages.getMainMessage("upload.dropZonePrompt"));
impl.setDescription(null);
Configuration configuration = beanLocator.get(Configuration.NAME);
int maxUploadSizeMb = configuration.getConfig(ClientConfig.class).getMaxUploadSizeMb();
int maxSizeBytes = maxUploadSizeMb * BYTES_IN_MEGABYTE;
impl.setFileSizeLimit(maxSizeBytes);
impl.setReceiver((fileName, MIMEType) -> {
FileOutputStream outputStream;
try {
FileUploadingAPI.FileInfo fileInfo = fileUploading.createFile();
tempFileId = fileInfo.getId();
File tmpFile = fileInfo.getFile();
outputStream = new FileOutputStream(tmpFile);
} catch (Exception e) {
throw new RuntimeException("Unable to receive file", e);
}
return outputStream;
});
impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
impl.addQueueUploadFinishedListener(event -> fireQueueUploadComplete());
impl.addSucceededListener(event -> {
files.put(tempFileId, event.getFileName());
fireFileUploadFinish(event.getFileName(), event.getContentLength());
});
impl.addFailedListener(event -> {
try {
// close and remove temp file
fileUploading.deleteFile(tempFileId);
tempFileId = null;
} catch (Exception e) {
if (e instanceof FileStorageException) {
FileStorageException fse = (FileStorageException) e;
if (fse.getType() != FileStorageException.Type.FILE_NOT_FOUND) {
LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Could not remove temp file {} after broken uploading", tempFileId);
}
}
LoggerFactory.getLogger(WebFileMultiUploadField.class).warn("Error while delete temp file {}", tempFileId);
}
fireFileUploadError(event.getFileName(), event.getContentLength(), event.getReason());
});
impl.addFileSizeLimitExceededListener(e -> {
Notifications notifications = getScreenContext(this).getNotifications();
notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("multiupload.filesizeLimitExceed", e.getFileName(), getFileSizeLimitString())).show();
});
impl.addFileExtensionNotAllowedListener(e -> {
Notifications notifications = getScreenContext(this).getNotifications();
notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName())).show();
});
}
Aggregations