|
15 | 15 | */ |
16 | 16 | package org.labkey.test; |
17 | 17 |
|
18 | | -import org.apache.commons.compress.archivers.ArchiveException; |
19 | 18 | import org.apache.commons.compress.archivers.ArchiveStreamFactory; |
20 | 19 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
21 | 20 | import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
22 | | -import org.apache.commons.io.FileSystem; |
23 | 21 | import org.apache.commons.io.FileUtils; |
24 | 22 | import org.apache.commons.io.IOUtils; |
25 | 23 | import org.apache.commons.lang3.StringUtils; |
|
70 | 68 | import java.util.List; |
71 | 69 | import java.util.Set; |
72 | 70 | import java.util.TreeSet; |
73 | | -import java.util.regex.Pattern; |
74 | 71 | import java.util.stream.Collectors; |
75 | 72 | import java.util.zip.GZIPInputStream; |
76 | 73 | import java.util.zip.ZipEntry; |
@@ -628,7 +625,7 @@ public static List<File> unzipToDirectory(File sourceZip, File unzipDir) throws |
628 | 625 | * The output file is created in the output folder, having the same name |
629 | 626 | * as the input file, minus the '.tar' extension. |
630 | 627 | */ |
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 |
632 | 629 | { |
633 | 630 | final List<File> untaredFiles = new ArrayList<>(); |
634 | 631 | try (InputStream is = new FileInputStream(inputFile); |
@@ -678,7 +675,7 @@ private static File unGzip(final File inputFile, final File outputDir) throws IO |
678 | 675 | return outputFile; |
679 | 676 | } |
680 | 677 |
|
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 |
682 | 679 | { |
683 | 680 | destDir.mkdirs(); |
684 | 681 | return unTar(unGzip(archive, destDir), destDir); |
@@ -717,15 +714,67 @@ public static byte[] decrypt(byte[] encrypted, char[] passPhrase) throws IOExcep |
717 | 714 | return Streams.readAll(ld.getInputStream()); |
718 | 715 | } |
719 | 716 |
|
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); |
721 | 720 |
|
722 | 721 | /** |
723 | 722 | * 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. |
726 | 725 | */ |
727 | | - public static String makeLegalFileName(String candidate) |
| 726 | + public static String makeLegalFileName(String name) |
728 | 727 | { |
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); |
730 | 779 | } |
731 | 780 | } |
0 commit comments