From: <hs...@us...> - 2011-04-11 19:01:11
|
Revision: 782 http://treebase.svn.sourceforge.net/treebase/?rev=782&view=rev Author: hshyket Date: 2011-04-11 19:01:05 +0000 (Mon, 11 Apr 2011) Log Message: ----------- Prefixing a number to the timestamp to avoid the issue of duplicate file uploads Modified Paths: -------------- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/ProcessUserController.java trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/UploadFileController.java Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/ProcessUserController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/ProcessUserController.java 2011-04-08 17:53:18 UTC (rev 781) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/ProcessUserController.java 2011-04-11 19:01:05 UTC (rev 782) @@ -4,7 +4,9 @@ import java.io.FileFilter; import java.util.Collection; import java.util.List; +import java.util.HashMap; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -94,15 +96,39 @@ Submission submission = mSubmissionService.createSubmission(user, study); long unixTime = System.currentTimeMillis() / 1000L; - List<File> files = DryadUtil.getDataFiles(dataPath); - for(int i=0; i<files.size(); i++ ) { + List<File> files = DryadUtil.getDataFiles(dataPath); + HashMap<String, Integer> filenamesHash = new HashMap<String, Integer>(); + for(int i=0; i<files.size(); i++ ) { + + int filecount = 1; + + File originalFile = new File(files.get(i).getAbsolutePath()); + + /* This keeps a hashmap of the files so that going through it knows the count of each file + * Each file then has a prefix of the count and unix timestamp of the upload + */ + + if (filenamesHash.containsKey(originalFile.getName())) { + filecount = filenamesHash.get(originalFile.getName()) + 1; + filenamesHash.put(originalFile.getName(), filecount); + } + else { + filenamesHash.put(originalFile.getName(), filecount); + } + String copyDir = request.getSession().getServletContext() .getRealPath(TreebaseUtil.FILESEP + "NexusFileUpload") + TreebaseUtil.FILESEP + request.getRemoteUser(); - File originalFile = new File(files.get(i).getAbsolutePath()); - File copyFile = new File(copyDir + TreebaseUtil.FILESEP + unixTime + "_" + files.get(i).getName()); + File copyFile = new File(copyDir + + TreebaseUtil.FILESEP + + filenamesHash.get(originalFile.getName()) + + "_" + + unixTime + + "_" + + files.get(i).getName()); + FileUtils.copyFile(originalFile, copyFile); files.remove(i); Modified: trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/UploadFileController.java =================================================================== --- trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/UploadFileController.java 2011-04-08 17:53:18 UTC (rev 781) +++ trunk/treebase-web/src/main/java/org/cipres/treebase/web/controllers/UploadFileController.java 2011-04-11 19:01:05 UTC (rev 782) @@ -3,6 +3,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -169,14 +170,29 @@ String firstFile = null; long unixTime = System.currentTimeMillis() / 1000L; - + HashMap<String, Integer> filenamesHash = new HashMap<String, Integer>(); + for (FileBean file : getFiles(request)) { if (LOGGER.isDebugEnabled()) { LOGGER .debug("Uploading file to =>" + uploadDir + TreebaseUtil.FILESEP + file.getName()); //$NON-NLS-1$ } - file.setName(unixTime + "_" + file.getName()); + /* This keeps a hashmap of the files so that going through it knows the count of each file + * Each file then has a prefix of the count and unix timestamp of the upload + */ + + int filecount = 1; + + if (filenamesHash.containsKey(file.getName())) { + filecount = filenamesHash.get(file.getName()) + 1; + filenamesHash.put(file.getName(), filecount); + } + else { + filenamesHash.put(file.getName(), filecount); + } + + file.setName(filecount + "_" + unixTime + "_" + file.getName()); File uploadedFile = new File(uploadDir + TreebaseUtil.FILESEP + file.getName()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |