use of io.jmix.core.FileStorageException in project jmix by jmix-framework.
the class TemporaryStorageImpl method saveFile.
@Override
public UUID saveFile(byte[] data) {
checkNotNullArgument(data, "No file content");
UUID uuid = UuidProvider.createUuid();
File dir = new File(tempDir);
if (!dir.exists() && !dir.mkdirs()) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, "Cannot create temp directory: " + dir.getAbsolutePath());
}
File file = new File(dir, uuid.toString());
try {
if (file.exists()) {
throw new FileStorageException(FileStorageException.Type.FILE_ALREADY_EXISTS, file.getAbsolutePath());
}
try (FileOutputStream os = new FileOutputStream(file)) {
os.write(data);
}
tempFiles.put(uuid, file);
} catch (RuntimeException | IOException e) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, file.getAbsolutePath());
}
return uuid;
}
use of io.jmix.core.FileStorageException in project jmix by jmix-framework.
the class AwsFileStorage method removeFile.
@Override
public void removeFile(FileRef reference) {
try {
S3Client s3Client = s3ClientReference.get();
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(reference.getPath()).build();
s3Client.deleteObject(deleteObjectRequest);
} catch (SdkException e) {
log.error("Error removing file from S3 storage", e);
String message = String.format("Could not delete file %s.", reference.getFileName());
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
}
use of io.jmix.core.FileStorageException in project jmix by jmix-framework.
the class AwsFileStorage method saveStream.
@Override
public FileRef saveStream(String fileName, InputStream inputStream) {
String fileKey = createFileKey(fileName);
int s3ChunkSizeBytes = this.chunkSize * 1024;
try (BufferedInputStream bos = new BufferedInputStream(inputStream, s3ChunkSizeBytes)) {
S3Client s3Client = s3ClientReference.get();
int totalSizeBytes = bos.available();
if (totalSizeBytes == 0) {
s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(fileKey).build(), RequestBody.empty());
return new FileRef(getStorageName(), fileKey, fileName);
}
CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder().bucket(bucket).key(fileKey).build();
CreateMultipartUploadResponse response = s3Client.createMultipartUpload(createMultipartUploadRequest);
List<CompletedPart> completedParts = new ArrayList<>();
for (int partNumber = 1, readBytes = 0; readBytes != totalSizeBytes; partNumber++) {
byte[] chunkBytes = new byte[Math.min(totalSizeBytes - readBytes, s3ChunkSizeBytes)];
readBytes += bos.read(chunkBytes);
UploadPartRequest uploadPartRequest = UploadPartRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).partNumber(partNumber).build();
String eTag = s3Client.uploadPart(uploadPartRequest, RequestBody.fromBytes(chunkBytes)).eTag();
CompletedPart part = CompletedPart.builder().partNumber(partNumber).eTag(eTag).build();
completedParts.add(part);
}
CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder().parts(completedParts).build();
CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder().bucket(bucket).key(fileKey).uploadId(response.uploadId()).multipartUpload(completedMultipartUpload).build();
s3Client.completeMultipartUpload(completeMultipartUploadRequest);
return new FileRef(getStorageName(), fileKey, fileName);
} catch (IOException | SdkException e) {
log.error("Error saving file to S3 storage", e);
String message = String.format("Could not save file %s.", fileName);
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
}
use of io.jmix.core.FileStorageException in project jmix by jmix-framework.
the class AwsFileStorage method openStream.
@Override
public InputStream openStream(FileRef reference) {
InputStream is;
try {
S3Client s3Client = s3ClientReference.get();
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucket).key(reference.getPath()).build();
is = s3Client.getObject(getObjectRequest, ResponseTransformer.toInputStream());
} catch (SdkException e) {
log.error("Error loading file from S3 storage", e);
String message = String.format("Could not load file %s.", reference.getFileName());
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, message);
}
return is;
}
use of io.jmix.core.FileStorageException in project jmix by jmix-framework.
the class FileStorageExceptionHandler method doHandle.
@Override
protected void doHandle(String className, String message, @Nullable Throwable throwable, UiContext context) {
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(FileStorageExceptionHandler.class, "fileStorageException.fileNotFound", fileName);
} else if (storageException.getType().equals(FileStorageException.Type.STORAGE_INACCESSIBLE)) {
msg = messages.getMessage(FileStorageExceptionHandler.class, "fileStorageException.fileStorageInaccessible");
}
}
if (msg == null) {
msg = messages.getMessage(FileStorageExceptionHandler.class, "fileStorageException.message");
if (throwable != null) {
log.error("Unable to handle FileStorageException", throwable);
}
}
context.getNotifications().create(Notifications.NotificationType.ERROR).withCaption(msg).show();
}
Aggregations