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 2d5f4e3e1bb490f9dbf4b693568fcce4e2987e39
Author: Tran Tien Duc <[email protected]>
AuthorDate: Thu Jun 6 17:40:49 2019 +0700

    JAMES-2782 LinshareBlobExport with custom file prefix
---
 .../linshare/LinshareBlobExportMechanism.java      | 21 ++++++++++--------
 .../linshare/LinshareBlobExportMechanismTest.java  | 25 +++++++++++++++++++++-
 2 files changed, 36 insertions(+), 10 deletions(-)

diff --git 
a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
 
b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
index 3af2261..d86dabb 100644
--- 
a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
+++ 
b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareBlobExportMechanism.java
@@ -29,35 +29,43 @@ import org.apache.commons.io.FileUtils;
 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.linshare.client.Document;
 import org.apache.james.linshare.client.LinshareAPI;
 import org.apache.james.linshare.client.ShareRequest;
 
+import com.google.common.io.Files;
+
 public class LinshareBlobExportMechanism implements BlobExportMechanism {
+
     private final LinshareAPI linshareAPI;
     private final BlobStore blobStore;
+    private final File tempDir;
 
     @Inject
     LinshareBlobExportMechanism(LinshareAPI linshareAPI, BlobStore blobStore) {
         this.linshareAPI = linshareAPI;
         this.blobStore = blobStore;
+        this.tempDir = Files.createTempDir();
     }
 
     @Override
     public ShareeStage blobId(BlobId blobId) {
-        return mailAddress -> explanation -> fileExtension -> () ->  {
+        return mailAddress -> explanation -> fileCustomPrefix -> fileExtension 
-> () ->  {
             try {
-                exportBlob(blobId, mailAddress, fileExtension, explanation);
+                exportBlob(blobId, mailAddress, fileCustomPrefix, 
fileExtension, explanation);
             } catch (Exception e) {
                 throw new BlobExportException("Error while exporting blob " + 
blobId.asString() + " to " + mailAddress.asString(), e);
             }
         };
     }
 
-    private void exportBlob(BlobId blobId, MailAddress mailAddress, 
Optional<FileExtension> fileExtension, String explanation) throws IOException {
-        File tempFile = createTempFile(blobId, fileExtension);
+    private void exportBlob(BlobId blobId, MailAddress mailAddress, 
Optional<String> fileCustomPrefix,
+                            Optional<FileExtension> fileExtension, String 
explanation) throws IOException {
+        String fileName = 
ExportedFileNamesGenerator.generateFileName(fileCustomPrefix, blobId, 
fileExtension);
+        File tempFile = new File(tempDir, fileName);
         try {
             FileUtils.copyInputStreamToFile(blobStore.read(blobId), tempFile);
             uploadAndShare(mailAddress, tempFile, explanation);
@@ -74,9 +82,4 @@ public class LinshareBlobExportMechanism implements 
BlobExportMechanism {
             .addRecipient(mailAddress)
             .build());
     }
-
-    private File createTempFile(BlobId blobId, Optional<FileExtension> 
fileExtension) throws IOException {
-        String suffix = 
fileExtension.map(FileExtension::asFileSuffix).orElse("");
-        return File.createTempFile(blobId.asString() + "_", suffix);
-    }
 }
diff --git 
a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
 
b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
index 51150c4..9af73cb 100644
--- 
a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
+++ 
b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareBlobExportMechanismTest.java
@@ -29,6 +29,7 @@ import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.hasSize;
 
 import java.nio.charset.StandardCharsets;
+import java.util.Optional;
 
 import org.apache.james.blob.api.BlobId;
 import org.apache.james.blob.api.HashBlobId;
@@ -74,17 +75,19 @@ class LinshareBlobExportMechanismTest {
     @Test
     void exportShouldShareTheDocumentViaLinshare() throws Exception {
         BlobId blobId = blobStore.save(FILE_CONTENT).block();
+        String filePrefix = "[email protected]";
 
         testee.blobId(blobId)
             .with(new MailAddress(USER_2.getUsername()))
             .explanation(EXPLANATION)
+            .filePrefix(Optional.of(filePrefix))
             .fileExtension(FILE_TEXT_EXTENSION)
             .export();
 
         assertThat(user2API.receivedShares())
             .hasSize(1)
             .allSatisfy(receivedShare -> 
assertThat(receivedShare.getDocument().getName()).endsWith(".txt"))
-            .allSatisfy(receivedShare -> 
assertThat(receivedShare.getDocument().getName()).startsWith(blobId.asString()))
+            .allSatisfy(receivedShare -> 
assertThat(receivedShare.getDocument().getName()).startsWith(filePrefix))
             .allSatisfy(receivedShare -> 
assertThat(receivedShare.getSender().getMail()).isEqualTo(USER_1.getUsername()));
     }
 
@@ -95,6 +98,7 @@ class LinshareBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(new MailAddress(USER_2.getUsername()))
             .explanation(EXPLANATION)
+            .noFileCustomPrefix()
             .fileExtension(FILE_TEXT_EXTENSION)
             .export();
 
@@ -123,6 +127,7 @@ class LinshareBlobExportMechanismTest {
         testee.blobId(blobId)
             .with(new MailAddress(USER_2.getUsername()))
             .explanation(EXPLANATION)
+            .noFileCustomPrefix()
             .fileExtension(FILE_TEXT_EXTENSION)
             .export();
 
@@ -139,8 +144,26 @@ class LinshareBlobExportMechanismTest {
             () -> testee.blobId(blobId)
                 .with(new MailAddress(USER_2.getUsername()))
                 .explanation(EXPLANATION)
+                .noFileCustomPrefix()
                 .fileExtension(FILE_TEXT_EXTENSION)
                 .export())
             .isInstanceOf(BlobExportMechanism.BlobExportException.class);
     }
+
+    @Test
+    void exportWithFilePrefixShouldCreateFileWithCustomPrefix() throws 
Exception {
+        BlobId blobId = blobStore.save(FILE_CONTENT).block();
+        String filePrefix = "[email protected]";
+
+        testee.blobId(blobId)
+            .with(new MailAddress(USER_2.getUsername()))
+            .explanation(EXPLANATION)
+            .filePrefix(Optional.of(filePrefix))
+            .fileExtension(FILE_TEXT_EXTENSION)
+            .export();
+
+        Document sharedDoc = user2API.receivedShares().get(0).getDocument();
+        assertThat(sharedDoc.getName())
+            .startsWith(filePrefix);
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to