Skip to content

Commit

Permalink
Merge pull request #10613 from IQSS/9401-fix-truncated-collection-nam…
Browse files Browse the repository at this point in the history
…e-guestbook-csv-file

adding fix to file name for download guestbook responses
  • Loading branch information
sekmiller authored Jul 8, 2024
2 parents 5d32ed5 + 88877a4 commit a466c97
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand;

import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;
import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -101,8 +102,9 @@ public String init() {
private String getFileName(){
// The fix below replaces any spaces in the name of the dataverse with underscores;
// without it, the filename was chopped off (by the browser??), and the user
// was getting the file name "Foo", instead of "Foo and Bar in Social Sciences.csv". -- L.A.
return dataverse.getName().replace(' ', '_') + "_" + guestbook.getId() + "_GuestbookReponses.csv";
// was getting the file name "Foo", instead of "Foo and Bar in Social Sciences.csv". -- L.A.
// Also removing some chars that have been reported to cause issues with certain browsers
return FileUtil.sanitizeFileName(dataverse.getName() + "_" + guestbook.getId() + "_GuestbookResponses.csv");
}

public void streamResponsesByDataverseAndGuestbook(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseGuestbookRootCommand;
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.FileUtil;
import edu.harvard.iq.dataverse.util.JsfHelper;
import static edu.harvard.iq.dataverse.util.JsfHelper.JH;
import java.util.LinkedList;
Expand Down Expand Up @@ -220,7 +221,8 @@ private String getFileName(){
// The fix below replaces any spaces in the name of the dataverse with underscores;
// without it, the filename was chopped off (by the browser??), and the user
// was getting the file name "Foo", instead of "Foo and Bar in Social Sciences.csv". -- L.A.
return dataverse.getName().replace(' ', '_') + "_GuestbookReponses.csv";
// Also removing some chars that have been reported to cause issues with certain browsers
return FileUtil.sanitizeFileName(dataverse.getName() + "_GuestbookResponses.csv");
}

public void deleteGuestbook() {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1816,5 +1816,13 @@ public static String getStorageDriver(DataFile dataFile) {
String storageIdentifier = dataFile.getStorageIdentifier();
return storageIdentifier.substring(0, storageIdentifier.indexOf(DataAccess.SEPARATOR));
}


/**
* Replace spaces with "_" and remove invalid chars
* @param fileNameIn - Name before sanitization NOTE: not full path since this method removes '/' and '\'
* @return filename without spaces or invalid chars
*/
public static String sanitizeFileName(String fileNameIn) {
return fileNameIn == null ? null : fileNameIn.replace(' ', '_').replaceAll("[\\\\/:*?\"<>|,;]", "");
}
}
7 changes: 7 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/util/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,11 @@ public void testDetermineFileTypeROCrate() {
assertEquals("Code", FileUtil.getIndexableFacetFileType(dockerDataFile));
}

@Test
public void testSanitizeFileName() {
assertEquals(null, FileUtil.sanitizeFileName(null));
assertEquals("with_space", FileUtil.sanitizeFileName("with space"));
assertEquals("withcomma", FileUtil.sanitizeFileName("with,comma"));
assertEquals("with.txt", FileUtil.sanitizeFileName("with,\\?:;,.txt"));
}
}

0 comments on commit a466c97

Please sign in to comment.