JAL-2520 improvement to use URL leaf name for creating temp file for URL file download
authortcofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 15 May 2017 15:07:09 +0000 (16:07 +0100)
committertcofoegbu <tcnofoegbu@dundee.ac.uk>
Mon, 15 May 2017 15:07:09 +0000 (16:07 +0100)
src/jalview/io/FileLoader.java

index 0152cc4..8b753e6 100755 (executable)
@@ -41,7 +41,8 @@ import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
 import jalview.ws.utils.UrlDownloadClient;
 
-import java.nio.file.Files;
+import java.io.File;
+import java.io.IOException;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -332,10 +333,13 @@ public class FileLoader implements Runnable
             if (downloadStructureFile)
             {
               String structExt = format.getExtensions().split(",")[0];
-              String tempStructurefile = Files.createTempFile(".jalview_",
-                      "." + structExt).toString();
-              UrlDownloadClient.download(file, tempStructurefile);
-              file = tempStructurefile;
+              String urlLeafName = file.substring(file.lastIndexOf(System
+                      .getProperty("file.separator")), file
+                      .lastIndexOf("."));
+              String tempStructureFileStr = createNamedJvTempFile(
+                      urlLeafName, structExt);
+              UrlDownloadClient.download(file, tempStructureFileStr);
+              file = tempStructureFileStr;
               protocol = DataSourceType.FILE;
             }
             al = fa.readFile(file, protocol, format);
@@ -569,6 +573,29 @@ public class FileLoader implements Runnable
 
   }
 
+  /**
+   * This method creates the file -
+   * {tmpdir}/jalview/{current_timestamp}/fileName.exetnsion using the supplied
+   * file name and extension
+   * 
+   * @param fileName
+   *          the name of the temp file to be created
+   * @param extension
+   *          the extension of the temp file to be created
+   * @return
+   */
+  private static String createNamedJvTempFile(String fileName,
+          String extension) throws IOException
+  {
+    String seprator = System.getProperty("file.separator");
+    String jvTempDir = System.getProperty("java.io.tmpdir") + "jalview"
+            + seprator + System.currentTimeMillis();
+    File tempStructFile = new File(jvTempDir + seprator + fileName + "."
+            + extension);
+    tempStructFile.mkdirs();
+    return tempStructFile.toString();
+  }
+
   /*
    * (non-Javadoc)
    *