use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class WebFileUploadField method getFileContent.
@Override
public InputStream getFileContent() {
if (contentProvider != null) {
return contentProvider.get();
}
FileDescriptor fileDescriptor = getValue();
switch(mode) {
case MANUAL:
if (fileId == null) {
return new FileDataProvider(fileDescriptor).provide();
}
File file = fileUploading.getFile(fileId);
if (file != null) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
log.error("Unable to get content of {}", file, e);
}
return null;
}
FileStorageService fileStorageService = beanLocator.get(FileStorageService.NAME);
try {
if (fileStorageService.fileExists(fileDescriptor)) {
return new FileDataProvider(fileDescriptor).provide();
}
} catch (FileStorageException e) {
log.error("Unable to get content of {}", fileDescriptor, e);
return null;
}
break;
case IMMEDIATE:
if (fileDescriptor != null) {
return new FileDataProvider(fileDescriptor).provide();
}
}
return null;
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class WebFileUploadField method initUploadButton.
protected void initUploadButton(CubaFileUpload impl) {
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.singleDropZonePrompt"));
impl.setDescription(null);
impl.setFileSizeLimit(getActualFileSizeLimit());
impl.setReceiver(this::receiveUpload);
impl.addStartedListener(event -> fireFileUploadStart(event.getFileName(), event.getContentLength()));
impl.addFinishedListener(event -> fireFileUploadFinish(event.getFileName(), event.getContentLength()));
impl.addSucceededListener(event -> {
fileName = event.getFileName();
fileId = tempFileId;
saveFile(getFileDescriptor());
component.setFileNameButtonCaption(fileName);
fireFileUploadSucceed(event.getFileName(), event.getContentLength());
});
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.getContentLength(), event.getReason());
});
impl.addFileSizeLimitExceededListener(e -> {
Notifications notifications = getScreenContext(this).getNotifications();
notifications.create(NotificationType.WARNING).withCaption(messages.formatMainMessage("upload.fileTooBig.message", 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();
});
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class WebFileDescriptorResource method createResource.
@Override
protected void createResource() {
resource = new StreamResource(() -> {
try {
return AppBeans.get(FileLoader.class).openStream(fileDescriptor);
} catch (FileStorageException e) {
throw new RuntimeException(FILE_STORAGE_EXCEPTION_MESSAGE, e);
}
}, getResourceName());
StreamResource streamResource = (StreamResource) this.resource;
streamResource.setCacheTime(cacheTime);
streamResource.setBufferSize(bufferSize);
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class FileDownloadController method downloadFromMiddlewareAndWriteResponse.
protected void downloadFromMiddlewareAndWriteResponse(FileDescriptor fd, HttpServletResponse response) throws IOException {
ServletOutputStream os = response.getOutputStream();
try (InputStream is = fileLoader.openStream(fd)) {
IOUtils.copy(is, os);
os.flush();
} catch (FileStorageException e) {
log.error("Unable to load file from middleware", e);
error(response);
}
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class AmazonS3FileStorage method openStream.
@Override
public InputStream openStream(FileDescriptor fileDescr) throws FileStorageException {
URL amazonUrl = getAmazonUrl(fileDescr);
// for a simple GET, we have no body so supply the precomputed 'empty' hash
Map<String, String> headers = new HashMap<>();
headers.put("x-amz-content-sha256", AWS4SignerBase.EMPTY_BODY_SHA256);
String authorization = createAuthorizationHeader(amazonUrl, "GET", headers);
headers.put("Authorization", authorization);
HttpUtils.HttpResponse httpResponse = HttpUtils.invokeHttpRequest(amazonUrl, "GET", headers, null);
if (httpResponse.isStatusOk()) {
return httpResponse.getInputStream();
} else if (httpResponse.isStatusNotFound()) {
throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, "File not found" + getFileName(fileDescr));
} else {
String message = String.format("Could not get file %s. %s", getFileName(fileDescr), getInputStreamContent(httpResponse));
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
}
Aggregations