james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] 04/06: JAMES-2782 LocalFileExport with custom file prefix
Date Mon, 10 Jun 2019 04:15:57 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 7852f95f0db7cb711fbbff78fe01bfc924bbd701
Author: Tran Tien Duc <dtran@linagora.com>
AuthorDate: Thu Jun 6 17:32:38 2019 +0700

    JAMES-2782 LocalFileExport with custom file prefix
---
 .../export/file/LocalFileBlobExportMechanism.java  | 25 ++++++-----------
 .../file/LocalFileBlobExportMechanismTest.java     | 31 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 17 deletions(-)

diff --git a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
index 32e899b..15add95 100644
--- a/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
+++ b/server/blob/blob-export-file/src/main/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanism.java
@@ -28,10 +28,10 @@ import java.util.Optional;
 import javax.inject.Inject;
 
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.BlobStore;
 import org.apache.james.blob.export.api.BlobExportMechanism;
+import org.apache.james.blob.export.api.ExportedFileNamesGenerator;
 import org.apache.james.blob.export.api.FileExtension;
 import org.apache.james.core.MailAddress;
 import org.apache.james.core.builder.MimeMessageBuilder;
@@ -44,9 +44,6 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 public class LocalFileBlobExportMechanism implements BlobExportMechanism {
-    private static final int STRING_LENGTH = 32;
-    private static final boolean WITH_LETTERS = true;
-    private static final boolean WITH_NUMBERS = true;
     private static final String SUBJECT = "Some content had had just been exported";
     static final String CORRESPONDING_FILE_HEADER = "corresponding-file";
 
@@ -107,18 +104,21 @@ public class LocalFileBlobExportMechanism implements BlobExportMechanism
{
 
     @Override
     public ShareeStage blobId(BlobId blobId) {
-        return mailAddress -> explanation -> fileExtension -> () ->  {
-            String fileUrl = copyBlobToFile(blobId, fileExtension);
+        return mailAddress -> explanation -> fileCustomPrefix -> fileExtension ->
() ->  {
+            String fileUrl = copyBlobToFile(blobId, fileCustomPrefix, fileExtension);
             sendMail(mailAddress, fileUrl, explanation);
         };
     }
 
-    private String copyBlobToFile(BlobId blobId, Optional<FileExtension> fileExtension)
{
+    private String copyBlobToFile(BlobId blobId,
+                                  Optional<String> fileCustomPrefix,
+                                  Optional<FileExtension> fileExtension) {
         try {
             File exportingDirectory = fileSystem.getFile(configuration.exportDirectory);
             FileUtils.forceMkdir(exportingDirectory);
 
-            String fileURL = generateFileUrl(fileExtension);
+            String fileName = ExportedFileNamesGenerator.generateFileName(fileCustomPrefix,
blobId, fileExtension);
+            String fileURL = configuration.exportDirectory + "/" + fileName;
             File file = fileSystem.getFile(fileURL);
             FileUtils.copyToFile(blobStore.read(blobId), file);
 
@@ -128,15 +128,6 @@ public class LocalFileBlobExportMechanism implements BlobExportMechanism
{
         }
     }
 
-    private String generateFileUrl(Optional<FileExtension> fileExtension) {
-        String fileName = RandomStringUtils.random(STRING_LENGTH, WITH_LETTERS, !WITH_NUMBERS);
-        String filePathWithoutExtension = configuration.exportDirectory + "/" + fileName;
-
-        return fileExtension
-            .map(extension -> extension.appendExtension(filePathWithoutExtension))
-            .orElse(filePathWithoutExtension);
-    }
-
     private void sendMail(MailAddress mailAddress, String fileUrl, String explanation) {
         try {
             MimeMessageBuilder mimeMessage = MimeMessageBuilder.mimeMessageBuilder()
diff --git a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
index 2b91681..522f9d8 100644
--- a/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
+++ b/server/blob/blob-export-file/src/test/java/org/apache/james/blob/export/file/LocalFileBlobExportMechanismTest.java
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
 import java.io.FileInputStream;
 import java.net.InetAddress;
 import java.nio.charset.StandardCharsets;
+import java.util.Optional;
 
 import javax.mail.Message;
 import javax.mail.internet.InternetAddress;
@@ -84,6 +85,7 @@ class LocalFileBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(MailAddressFixture.RECIPIENT1)
             .explanation(explanation)
+            .noFileCustomPrefix()
             .noFileExtension()
             .export();
 
@@ -115,6 +117,7 @@ class LocalFileBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(MailAddressFixture.RECIPIENT1)
             .explanation("The content of a deleted message vault had been shared with you.")
+            .noFileCustomPrefix()
             .noFileExtension()
             .export();
 
@@ -139,6 +142,7 @@ class LocalFileBlobExportMechanismTest {
             testee.blobId(blobId)
                 .with(MailAddressFixture.RECIPIENT1)
                 .explanation("The content of a deleted message vault had been shared with
you.")
+                .noFileCustomPrefix()
                 .noFileExtension()
                 .export())
             .isInstanceOf(ObjectStoreException.class);
@@ -151,6 +155,7 @@ class LocalFileBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(MailAddressFixture.RECIPIENT1)
             .explanation("The content of a deleted message vault had been shared with you.")
+            .noFileCustomPrefix()
             .noFileExtension()
             .export();
 
@@ -174,6 +179,7 @@ class LocalFileBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(MailAddressFixture.RECIPIENT1)
             .explanation("The content of a deleted message vault had been shared with you.")
+            .noFileCustomPrefix()
             .fileExtension(FileExtension.ZIP)
             .export();
 
@@ -191,6 +197,31 @@ class LocalFileBlobExportMechanismTest {
             });
     }
 
+    @Test
+    void exportingBlobShouldCreateAFileWithPrefixWhenDeclaringPrefix() {
+        BlobId blobId = blobStore.save(BLOB_CONTENT).block();
+        String filePrefix = "deleted-message-of-bob@james.org";
+
+        testee.blobId(blobId)
+            .with(MailAddressFixture.RECIPIENT1)
+            .explanation("The content of a deleted message vault had been shared with you.")
+            .filePrefix(Optional.of(filePrefix))
+            .fileExtension(FileExtension.ZIP)
+            .export();
+
+        assertThat(mailetContext.getSentMails())
+            .element(0)
+            .satisfies(sentMail -> {
+                try {
+                    String fileUrl = sentMail.getMsg().getHeader(LocalFileBlobExportMechanism.CORRESPONDING_FILE_HEADER)[0];
+                    assertThat(FilenameUtils.getName(fileUrl))
+                        .startsWith(filePrefix);
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            });
+    }
+
     @Nested
     class ConfigurationTest {
         @Test


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


Mime
View raw message