details:   https://code.openbravo.com/erp/devel/pi/rev/87ee63f96d1f
changeset: 33868:87ee63f96d1f
user:      Carlos Aristu <carlos.aristu <at> openbravo.com>
date:      Wed Apr 25 15:50:10 2018 +0200
summary:   fixes bug 38450: Remove temp file's directory used to download the 
attachment

  Now the AttachImplementationManager deletes also the temp file's directory 
used to download the attachment. This change only affects to those attach 
implementations which return the downloaded attachment inside a temporary file.

  With this change now that attach implementations are able generate those 
temporary files with any name by creating them inside folders with random 
names. This is intended to avoid concurrency problems in case two users 
download the same attachment at the same time.

diffstat:

 
modules/org.openbravo.client.application/src/org/openbravo/client/application/attachment/AttachImplementationManager.java
 |  20 +++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)

diffs (51 lines):

diff -r 36350507a8ac -r 87ee63f96d1f 
modules/org.openbravo.client.application/src/org/openbravo/client/application/attachment/AttachImplementationManager.java
--- 
a/modules/org.openbravo.client.application/src/org/openbravo/client/application/attachment/AttachImplementationManager.java
 Mon Apr 23 17:20:41 2018 +0200
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/attachment/AttachImplementationManager.java
 Wed Apr 25 15:50:10 2018 +0200
@@ -25,6 +25,8 @@
 import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -81,6 +83,7 @@
 public class AttachImplementationManager {
 
   private static final Logger log = 
LoggerFactory.getLogger(AttachImplementationManager.class);
+  private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
 
   public static final String REFERENCE_LIST = "17";
   public static final String REFERENCE_SELECTOR_REFERENCE = 
"95E2A8B50A254B2AAE6774B8C2F28120";
@@ -262,7 +265,7 @@
 
       boolean isTempFile = handler.isTempFile();
       if (isTempFile) {
-        file.delete();
+        deleteTempFile(file);
       }
 
     } catch (IOException e) {
@@ -272,6 +275,21 @@
     }
   }
 
+  private void deleteTempFile(File file) {
+    Path parent = file.toPath().getParent();
+    Path tmpDir = Paths.get(TEMP_DIR);
+    file.delete();
+    if (parent.equals(tmpDir)) {
+      return;
+    }
+    try {
+      // Delete also temporary file's parent directory if it is empty
+      Files.delete(parent);
+    } catch (IOException ioex) {
+      log.error("Could not delete directory {}", parent, ioex);
+    }
+  }
+
   /**
    * Method to download all the files related to the record, in a single .zip 
file. This method
    * calls needed handler class

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to