Skip to content

Commit e28d7de

Browse files
committed
Test: Use copied implementation of FileUtil.makeLegalName()
1 parent 170faf3 commit e28d7de

1 file changed

Lines changed: 59 additions & 10 deletions

File tree

src/org/labkey/test/TestFileUtils.java

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package org.labkey.test;
1717

18-
import org.apache.commons.compress.archivers.ArchiveException;
1918
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
2019
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
2120
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
22-
import org.apache.commons.io.FileSystem;
2321
import org.apache.commons.io.FileUtils;
2422
import org.apache.commons.io.IOUtils;
2523
import org.apache.commons.lang3.StringUtils;
@@ -70,7 +68,6 @@
7068
import java.util.List;
7169
import java.util.Set;
7270
import java.util.TreeSet;
73-
import java.util.regex.Pattern;
7471
import java.util.stream.Collectors;
7572
import java.util.zip.GZIPInputStream;
7673
import java.util.zip.ZipEntry;
@@ -628,7 +625,7 @@ public static List<File> unzipToDirectory(File sourceZip, File unzipDir) throws
628625
* The output file is created in the output folder, having the same name
629626
* as the input file, minus the '.tar' extension.
630627
*/
631-
private static List<File> unTar(final File inputFile, final File outputDir) throws IOException, ArchiveException
628+
private static List<File> unTar(final File inputFile, final File outputDir) throws IOException
632629
{
633630
final List<File> untaredFiles = new ArrayList<>();
634631
try (InputStream is = new FileInputStream(inputFile);
@@ -678,7 +675,7 @@ private static File unGzip(final File inputFile, final File outputDir) throws IO
678675
return outputFile;
679676
}
680677

681-
public static List<File> extractTarGz(File archive, File destDir) throws IOException, ArchiveException
678+
public static List<File> extractTarGz(File archive, File destDir) throws IOException
682679
{
683680
destDir.mkdirs();
684681
return unTar(unGzip(archive, destDir), destDir);
@@ -717,15 +714,67 @@ public static byte[] decrypt(byte[] encrypted, char[] passPhrase) throws IOExcep
717714
return Streams.readAll(ld.getInputStream());
718715
}
719716

720-
private static final Pattern badChars = Pattern.compile("[\\\\:/\\[\\]?*|]");
717+
// NOTE: These constants are copied from FileUtil.java and should be kept in sync.
718+
private static final char[] ILLEGAL_CHARS = {'/','\\',':','?','<','>','*','|','"','^', '\n', '\r', '\''};
719+
public static final String ILLEGAL_CHARS_STRING = new String(ILLEGAL_CHARS);
721720

722721
/**
723722
* Determining expected file names for downloaded files that are named according to some
724-
* value that might include characters that are not legal for files
725-
* @see FileSystem#toLegalFileName(String, char)
723+
* value that might include characters that are not legal for files.
724+
* NOTE: This implementation is expected to exactly match FileUtil.makeLegalName(String name) defined on the server.
726725
*/
727-
public static String makeLegalFileName(String candidate)
726+
public static String makeLegalFileName(String name)
728727
{
729-
return badChars.matcher(candidate).replaceAll("_");
728+
if (name == null)
729+
{
730+
return "__null__";
731+
}
732+
733+
if (name.isEmpty())
734+
{
735+
return "__empty__";
736+
}
737+
738+
//limit to 255 chars (FAT and OS X)
739+
//replace illegal chars
740+
char[] ret = new char[Math.min(255, name.length())];
741+
for(int idx = 0; idx < ret.length; ++idx)
742+
{
743+
char ch = name.charAt(idx);
744+
// Reject characters that are illegal anywhere
745+
if (StringUtils.contains(ILLEGAL_CHARS_STRING, ch) ||
746+
// Or characters that are illegal starts to a file name
747+
(idx == 0 && (ch == '-' || ch == '$')))
748+
{
749+
ch = '_';
750+
}
751+
else if (ch == '-' &&
752+
idx > 0 &&
753+
name.charAt(idx - 1) == ' ')
754+
{
755+
int i = idx + 1;
756+
// Skip through as many consecutive '-' as there might be
757+
while (i < name.length() && name.charAt(i) == '-')
758+
{
759+
i++;
760+
}
761+
// If the next character after the '-' isn't a space, transform the leading '-' in the sequence
762+
if (i < name.length() && name.charAt(i) != ' ')
763+
{
764+
ch = '_';
765+
}
766+
}
767+
768+
ret[idx] = ch;
769+
}
770+
771+
//can't end with space (windows)
772+
//can't end with period (windows)
773+
int lastIndex = ret.length - 1;
774+
char ch = ret[lastIndex];
775+
if (ch == ' ' || ch == '.')
776+
ret[lastIndex] = '_';
777+
778+
return new String(ret);
730779
}
731780
}

0 commit comments

Comments
 (0)