fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ptuom...@apache.org
Subject [fineract] branch develop updated: Prepare FileSystemContentRepository for code re-use with S3ContentRepository (FINERACT-1201)
Date Wed, 14 Oct 2020 07:34:12 GMT
This is an automated email from the ASF dual-hosted git repository.

ptuomola pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new 2af0970  Prepare FileSystemContentRepository for code re-use with S3ContentRepository
(FINERACT-1201)
2af0970 is described below

commit 2af09708f1b0be012d8a5aadf995aa77365b1055
Author: Michael Vorburger <mike@vorburger.ch>
AuthorDate: Wed Oct 14 01:02:42 2020 +0200

    Prepare FileSystemContentRepository for code re-use with S3ContentRepository (FINERACT-1201)
---
 .../FileSystemContentRepository.java               | 66 +++++++---------------
 1 file changed, 21 insertions(+), 45 deletions(-)

diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/contentrepository/FileSystemContentRepository.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/contentrepository/FileSystemContentRepository.java
index a4dab8e..5722e6c 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/contentrepository/FileSystemContentRepository.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/contentrepository/FileSystemContentRepository.java
@@ -18,12 +18,13 @@
  */
 package org.apache.fineract.infrastructure.documentmanagement.contentrepository;
 
+import com.google.common.io.Files;
+import java.io.ByteArrayInputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.Base64;
+import org.apache.commons.io.FileUtils;
 import org.apache.fineract.infrastructure.core.domain.Base64EncodedImage;
 import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
 import org.apache.fineract.infrastructure.documentmanagement.command.DocumentCommand;
@@ -44,13 +45,10 @@ public class FileSystemContentRepository implements ContentRepository
{
     @Override
     public String saveFile(final InputStream uploadedInputStream, final DocumentCommand documentCommand)
{
         final String fileName = documentCommand.getFileName();
-        final String uploadDocumentLocation = generateFileParentDirectory(documentCommand.getParentEntityType(),
-                documentCommand.getParentEntityId());
-
         ContentRepositoryUtils.validateFileSizeWithinPermissibleRange(documentCommand.getSize(),
fileName);
-        makeDirectories(uploadDocumentLocation);
 
-        final String fileLocation = uploadDocumentLocation + File.separator + fileName;
+        final String fileLocation = generateFileParentDirectory(documentCommand.getParentEntityType(),
documentCommand.getParentEntityId())
+                + File.separator + fileName;
 
         writeFileToFileSystem(fileName, uploadedInputStream, fileLocation);
         return fileLocation;
@@ -58,38 +56,25 @@ public class FileSystemContentRepository implements ContentRepository
{
 
     @Override
     public String saveImage(final InputStream uploadedInputStream, final Long resourceId,
final String imageName, final Long fileSize) {
-        final String uploadImageLocation = generateClientImageParentDirectory(resourceId);
-
         ContentRepositoryUtils.validateFileSizeWithinPermissibleRange(fileSize, imageName);
-        makeDirectories(uploadImageLocation);
-
-        final String fileLocation = uploadImageLocation + File.separator + imageName;
-
+        final String fileLocation = generateClientImageParentDirectory(resourceId) + File.separator
+ imageName;
         writeFileToFileSystem(imageName, uploadedInputStream, fileLocation);
         return fileLocation;
     }
 
     @Override
     public String saveImage(final Base64EncodedImage base64EncodedImage, final Long resourceId,
final String imageName) {
-        final String uploadImageLocation = generateClientImageParentDirectory(resourceId);
-
-        makeDirectories(uploadImageLocation);
-
-        final String fileLocation = uploadImageLocation + File.separator + imageName + base64EncodedImage.getFileExtension();
-        final String base64EncodedImageString = base64EncodedImage.getBase64EncodedString();
+        final String fileLocation = generateClientImageParentDirectory(resourceId) + File.separator
+ imageName
+                + base64EncodedImage.getFileExtension();
+        String base64EncodedImageString = base64EncodedImage.getBase64EncodedString();
         try {
-            final OutputStream out = new FileOutputStream(new File(fileLocation));
-            final byte[] imgBytes = Base64.getMimeDecoder().decode(base64EncodedImageString);
-            out.write(imgBytes);
-            out.flush();
-            out.close();
-        } catch (final IOException ioe) {
-            throw new ContentManagementException(imageName, ioe.getMessage(), ioe);
+            final InputStream toUploadInputStream = new ByteArrayInputStream(Base64.getMimeDecoder().decode(base64EncodedImageString));
+            writeFileToFileSystem(imageName, toUploadInputStream, fileLocation);
+            return fileLocation;
         } catch (IllegalArgumentException iae) {
             LOG.error("IllegalArgumentException due to invalid Base64 encoding: {}", base64EncodedImageString,
iae);
             throw iae;
         }
-        return fileLocation;
     }
 
     @Override
@@ -115,11 +100,6 @@ public class FileSystemContentRepository implements ContentRepository
{
     }
 
     @Override
-    public StorageType getStorageType() {
-        return StorageType.FILE_SYSTEM;
-    }
-
-    @Override
     public FileData fetchFile(final DocumentData documentData) {
         final File file = new File(documentData.fileLocation());
         return new FileData(file, documentData.fileName(), documentData.contentType());
@@ -132,6 +112,11 @@ public class FileSystemContentRepository implements ContentRepository
{
         return imageData;
     }
 
+    @Override
+    public StorageType getStorageType() {
+        return StorageType.FILE_SYSTEM;
+    }
+
     /**
      * Generate the directory path for storing the new document
      */
@@ -153,23 +138,14 @@ public class FileSystemContentRepository implements ContentRepository
{
     /**
      * Recursively create the directory if it does not exist.
      */
-    private void makeDirectories(final String uploadDocumentLocation) {
-        if (!new File(uploadDocumentLocation).isDirectory()) {
-            new File(uploadDocumentLocation).mkdirs();
-        }
+    private void makeDirectories(final String uploadDocumentLocation) throws IOException
{
+        Files.createParentDirs(new File(uploadDocumentLocation));
     }
 
     private void writeFileToFileSystem(final String fileName, final InputStream uploadedInputStream,
final String fileLocation) {
         try {
-            final OutputStream out = new FileOutputStream(new File(fileLocation));
-            int read = 0;
-            final byte[] bytes = new byte[1024];
-
-            while ((read = uploadedInputStream.read(bytes)) != -1) {
-                out.write(bytes, 0, read);
-            }
-            out.flush();
-            out.close();
+            makeDirectories(fileLocation);
+            FileUtils.copyInputStreamToFile(uploadedInputStream, new File(fileLocation));
         } catch (final IOException ioException) {
             LOG.warn("writeFileToFileSystem() IOException (logged because cause is not propagated
in ContentManagementException)",
                     ioException);


Mime
View raw message