You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2024/03/06 02:29:16 UTC

(james-project) 02/05: [ENHANCEMENT] AESBlobStoreDAO: carry over the size

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c0ecbcbacac4563b9086a5c00b38d718e80504a4
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Mar 5 09:23:19 2024 +0100

    [ENHANCEMENT] AESBlobStoreDAO: carry over the size
    
    Not doing so causes the S3BlobStoreDAO to read the file
    in order to retrieve the size.
---
 .../org/apache/james/blob/aes/AESBlobStoreDAO.java | 32 ++++++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java b/server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java
index 662e2ff08f..ca1cbeef1d 100644
--- a/server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java
+++ b/server/blob/blob-aes/src/main/java/org/apache/james/blob/aes/AESBlobStoreDAO.java
@@ -30,6 +30,7 @@ import java.util.Collection;
 import java.util.Optional;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.BlobStoreDAO;
 import org.apache.james.blob.api.BucketName;
@@ -72,13 +73,13 @@ public class AESBlobStoreDAO implements BlobStoreDAO {
         this.streamingAead = PBKDF2StreamingAeadFactory.newAesGcmHkdfStreaming(cryptoConfig);
     }
 
-    public FileBackedOutputStream encrypt(InputStream input) throws IOException {
+    private Pair<FileBackedOutputStream, Long> encrypt(InputStream input) throws IOException {
         FileBackedOutputStream encryptedContent = new FileBackedOutputStream(FILE_THRESHOLD_ENCRYPT);
-        try {
-            OutputStream outputStream = streamingAead.newEncryptingStream(encryptedContent, PBKDF2StreamingAeadFactory.EMPTY_ASSOCIATED_DATA);
+        try (CountingOutputStream countingOutputStream = new CountingOutputStream(encryptedContent)) {
+            OutputStream outputStream = streamingAead.newEncryptingStream(countingOutputStream, PBKDF2StreamingAeadFactory.EMPTY_ASSOCIATED_DATA);
             input.transferTo(outputStream);
             outputStream.close();
-            return encryptedContent;
+            return Pair.of(encryptedContent, countingOutputStream.getCount());
         } catch (Exception e) {
             encryptedContent.reset();
             throw new RuntimeException("Unable to build payload for object storage, failed to encrypt", e);
@@ -175,12 +176,31 @@ public class AESBlobStoreDAO implements BlobStoreDAO {
 
         return Mono.using(
                 () -> encrypt(inputStream),
-                fileBackedOutputStream -> Mono.from(underlying.save(bucketName, blobId, fileBackedOutputStream.asByteSource())),
-                Throwing.consumer(FileBackedOutputStream::reset))
+                pair -> Mono.from(underlying.save(bucketName, blobId, byteSourceWithSize(pair.getLeft().asByteSource(), pair.getRight()))),
+                Throwing.consumer(pair -> pair.getLeft().reset()))
             .subscribeOn(Schedulers.boundedElastic())
             .onErrorMap(e -> new ObjectStoreIOException("Exception occurred while saving bytearray", e));
     }
 
+    private ByteSource byteSourceWithSize(ByteSource byteSource, long size) {
+        return new ByteSource() {
+            @Override
+            public InputStream openStream() throws IOException {
+                return byteSource.openStream();
+            }
+
+            @Override
+            public com.google.common.base.Optional<Long> sizeIfKnown() {
+                return com.google.common.base.Optional.of(size);
+            }
+
+            @Override
+            public long size() {
+                return size;
+            }
+        };
+    }
+
     @Override
     public Publisher<Void> save(BucketName bucketName, BlobId blobId, ByteSource content) {
         Preconditions.checkNotNull(bucketName);


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org