Search in sources :

Example 6 with FileStorageException

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) UUID(java.util.UUID) FileStorageException(io.jmix.core.FileStorageException) File(java.io.File)

Example 7 with FileStorageException

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);
    }
}
Also used : DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) FileStorageException(io.jmix.core.FileStorageException)

Example 8 with FileStorageException

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);
    }
}
Also used : CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) ArrayList(java.util.ArrayList) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) IOException(java.io.IOException) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) FileStorageException(io.jmix.core.FileStorageException) BufferedInputStream(java.io.BufferedInputStream) FileRef(io.jmix.core.FileRef) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)

Example 9 with FileStorageException

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SdkException(software.amazon.awssdk.core.exception.SdkException) S3Client(software.amazon.awssdk.services.s3.S3Client) FileStorageException(io.jmix.core.FileStorageException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 10 with FileStorageException

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();
}
Also used : FileStorageException(io.jmix.core.FileStorageException)

Aggregations

FileStorageException (io.jmix.core.FileStorageException)13 IOException (java.io.IOException)8 File (java.io.File)5 InputStream (java.io.InputStream)4 Path (java.nio.file.Path)4 UUID (java.util.UUID)3 SdkException (software.amazon.awssdk.core.exception.SdkException)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 FileRef (io.jmix.core.FileRef)2 BufferedInputStream (java.io.BufferedInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 MailSSLSocketFactory (com.sun.mail.util.MailSSLSocketFactory)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 MessagingException (javax.mail.MessagingException)1 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)1 CompletedMultipartUpload (software.amazon.awssdk.services.s3.model.CompletedMultipartUpload)1