use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class AmazonS3FileStorage method saveStream.
@Override
public long saveStream(FileDescriptor fileDescr, InputStream inputStream) throws FileStorageException {
Preconditions.checkNotNullArgument(fileDescr.getSize());
int chunkSize = amazonS3Config.getChunkSize();
long fileSize = fileDescr.getSize();
URL amazonUrl = getAmazonUrl(fileDescr);
// set the markers indicating we're going to send the upload as a series
// of chunks:
// -- 'x-amz-content-sha256' is the fixed marker indicating chunked
// upload
// -- 'content-length' becomes the total size in bytes of the upload
// (including chunk headers),
// -- 'x-amz-decoded-content-length' is used to transmit the actual
// length of the data payload, less chunk headers
Map<String, String> headers = new HashMap<>();
headers.put("x-amz-storage-class", "REDUCED_REDUNDANCY");
headers.put("x-amz-content-sha256", AWS4SignerForChunkedUpload.STREAMING_BODY_SHA256);
headers.put("content-encoding", "aws-chunked");
headers.put("x-amz-decoded-content-length", "" + fileSize);
AWS4SignerForChunkedUpload signer = new AWS4SignerForChunkedUpload(amazonUrl, "PUT", "s3", amazonS3Config.getRegionName());
// how big is the overall request stream going to be once we add the signature
// 'headers' to each chunk?
long totalLength = AWS4SignerForChunkedUpload.calculateChunkedContentLength(fileSize, chunkSize);
headers.put("content-length", "" + totalLength);
String authorization = signer.computeSignature(headers, // no query parameters
null, AWS4SignerForChunkedUpload.STREAMING_BODY_SHA256, amazonS3Config.getAccessKey(), amazonS3Config.getSecretAccessKey());
// place the computed signature into a formatted 'Authorization' header
// and call S3
headers.put("Authorization", authorization);
try {
// first set up the connection
HttpURLConnection connection = HttpUtils.createHttpConnection(amazonUrl, "PUT", headers);
// get the request stream and start writing the user data as chunks, as outlined
// above;
int bytesRead;
byte[] buffer = new byte[chunkSize];
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
// subclasses of InputStream
while ((bytesRead = IOUtils.read(inputStream, buffer, 0, chunkSize)) > 0) {
// process into a chunk
byte[] chunk = signer.constructSignedChunk(bytesRead, buffer);
// send the chunk
outputStream.write(chunk);
outputStream.flush();
}
// last step is to send a signed zero-length chunk to complete the upload
byte[] finalChunk = signer.constructSignedChunk(0, buffer);
outputStream.write(finalChunk);
outputStream.flush();
outputStream.close();
// make the call to Amazon S3
HttpUtils.HttpResponse httpResponse = HttpUtils.executeHttpRequest(connection);
if (!httpResponse.isStatusOk()) {
String message = String.format("Could not save file %s. %s", getFileName(fileDescr), getInputStreamContent(httpResponse));
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
} catch (IOException e) {
throw new RuntimeException("Error when sending chunked upload request", e);
}
return fileDescr.getSize();
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class WebFileUploadField method initUploadButton.
protected void initUploadButton() {
uploadButton = createComponent();
CubaFileUpload impl = (CubaFileUpload) uploadButton;
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((fileName1, 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.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 -> {
String warningMsg = messages.formatMainMessage("upload.fileTooBig.message", e.getFileName(), getFileSizeLimitString());
getFrame().showNotification(warningMsg, NotificationType.WARNING);
});
impl.addFileExtensionNotAllowedListener(e -> {
String warningMsg = messages.formatMainMessage("upload.fileIncorrectExtension.message", e.getFileName());
getFrame().showNotification(warningMsg, NotificationType.WARNING);
});
}
use of com.haulmont.cuba.core.global.FileStorageException in project documentation by cuba-platform.
the class FileLoaderScreen method onButtonInClick.
public void onButtonInClick() {
byte[] bytes = textAreaIn.getRawValue().getBytes();
fileDescriptor = metadata.create(FileDescriptor.class);
fileDescriptor.setName("Input.txt");
fileDescriptor.setExtension("txt");
fileDescriptor.setSize((long) bytes.length);
fileDescriptor.setCreateDate(new Date());
try {
fileLoader.saveStream(fileDescriptor, () -> new ByteArrayInputStream(bytes));
} catch (FileStorageException e) {
throw new RuntimeException(e);
}
dataManager.commit(fileDescriptor);
}
use of com.haulmont.cuba.core.global.FileStorageException in project documentation by cuba-platform.
the class FileLoaderScreen method onButtonOutClick.
public void onButtonOutClick() {
try {
InputStream inputStream = fileLoader.openStream(fileDescriptor);
textAreaOut.setValue(IOUtils.toString(inputStream));
} catch (FileStorageException | IOException e) {
throw new RuntimeException(e);
}
}
use of com.haulmont.cuba.core.global.FileStorageException in project cuba by cuba-platform.
the class FileStorage method findOrphanFiles.
@Override
public String findOrphanFiles() {
FileStorageAPI fileStorageAPI = AppBeans.get(FileStorageAPI.class);
if (!(fileStorageAPI instanceof com.haulmont.cuba.core.app.filestorage.FileStorage)) {
return "<not supported>";
}
File[] roots = getStorageRoots();
if (roots.length == 0)
return "No storage directories defined";
StringBuilder sb = new StringBuilder();
File storageFolder = roots[0];
if (!storageFolder.exists())
return ExceptionUtils.getStackTrace(new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, storageFolder.getAbsolutePath()));
Collection<File> systemFiles = FileUtils.listFiles(storageFolder, null, true);
Collection<File> filesInRootFolder = FileUtils.listFiles(storageFolder, null, false);
// remove files of root storage folder (e.g. storage.log) from files collection
systemFiles.removeAll(filesInRootFolder);
List<FileDescriptor> fileDescriptors;
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<FileDescriptor> query = em.createQuery("select fd from sys$FileDescriptor fd", FileDescriptor.class);
fileDescriptors = query.getResultList();
tx.commit();
} catch (Exception e) {
return ExceptionUtils.getStackTrace(e);
} finally {
tx.end();
}
Set<String> descriptorsFileNames = new HashSet<>();
for (FileDescriptor fileDescriptor : fileDescriptors) {
descriptorsFileNames.add(com.haulmont.cuba.core.app.filestorage.FileStorage.getFileName(fileDescriptor));
}
for (File file : systemFiles) {
if (!descriptorsFileNames.contains(file.getName()))
// Encode file path if it contains non-ASCII characters
if (!file.getPath().matches("\\p{ASCII}+")) {
String encodedFilePath = URLEncodeUtils.encodeUtf8(file.getPath());
sb.append(encodedFilePath).append("\n");
} else {
sb.append(file.getPath()).append("\n");
}
}
return sb.toString();
}
Aggregations