Ticket #20310: 20310.2.diff
| File 20310.2.diff, 8.5 KB (added by , 5 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/io/importexport/ImageImporter.java
8 8 import java.util.ArrayList; 9 9 import java.util.Arrays; 10 10 import java.util.Collections; 11 import java.util.EnumSet; 11 12 import java.util.HashSet; 12 13 import java.util.List; 13 14 import java.util.Set; 15 import java.util.regex.Matcher; 16 import java.util.regex.Pattern; 14 17 import java.util.stream.Collectors; 15 18 16 19 import javax.imageio.ImageIO; … … 19 22 import org.openstreetmap.josm.gui.layer.GpxLayer; 20 23 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; 21 24 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 25 import org.openstreetmap.josm.io.CachedFile; 22 26 import org.openstreetmap.josm.io.IllegalDataException; 23 27 24 28 /** … … 26 30 * @since 17548 27 31 */ 28 32 public class ImageImporter extends FileImporter { 33 34 /** Check if the filename starts with a borked path ({@link java.io.File#File} drops consecutive {@code /} characters). */ 35 private static final Pattern URL_START_BAD = Pattern.compile("^(https?:/)([^/].*)$"); 36 /** Check for the beginning of a "good" url */ 37 private static final Pattern URL_START_GOOD = Pattern.compile("^https?://.*$"); 38 29 39 private GpxLayer gpx; 30 40 31 41 /** … … 90 100 try { 91 101 List<File> files = new ArrayList<>(); 92 102 Set<String> visitedDirs = new HashSet<>(); 93 addRecursiveFiles( files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true));103 addRecursiveFiles(this.options, files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true)); 94 104 95 105 if (progressMonitor.isCanceled()) 96 106 return; … … 106 116 107 117 static void addRecursiveFiles(List<File> files, Set<String> visitedDirs, List<File> sel, ProgressMonitor progressMonitor) 108 118 throws IOException { 119 addRecursiveFiles(EnumSet.noneOf(Options.class), files, visitedDirs, sel, progressMonitor); 120 } 109 121 122 static void addRecursiveFiles(Set<Options> options, List<File> files, Set<String> visitedDirs, List<File> sel, 123 ProgressMonitor progressMonitor) throws IOException { 124 110 125 if (progressMonitor.isCanceled()) 111 126 return; 112 127 … … 117 132 if (visitedDirs.add(f.getCanonicalPath())) { // Do not loop over symlinks 118 133 File[] dirFiles = f.listFiles(); // Can be null for some strange directories (like lost+found) 119 134 if (dirFiles != null) { 120 addRecursiveFiles(files, visitedDirs, Arrays.asList(dirFiles), progressMonitor.createSubTaskMonitor(1, true)); 135 addRecursiveFiles(options, files, visitedDirs, Arrays.asList(dirFiles), 136 progressMonitor.createSubTaskMonitor(1, true)); 121 137 } 122 138 } else { 123 139 progressMonitor.worked(1); 124 140 } 125 141 } else { 126 if (FILE_FILTER.accept(f)) { 142 /* Check if the path is a web path, and if so, ensure that it is "correct" */ 143 final String path = f.getPath(); 144 Matcher matcherBad = URL_START_BAD.matcher(path); 145 final String realPath; 146 if (matcherBad.matches()) { 147 realPath = matcherBad.replaceFirst(matcherBad.group(1) + "/" + matcherBad.group(2)); 148 } else { 149 realPath = path; 150 } 151 if (URL_START_GOOD.matcher(realPath).matches() && FILE_FILTER.accept(f) 152 && options.contains(Options.ALLOW_WEB_RESOURCES)) { 153 try (CachedFile cachedFile = new CachedFile(realPath)) { 154 files.add(cachedFile.getFile()); 155 } 156 } else if (FILE_FILTER.accept(f)) { 127 157 files.add(f); 128 158 } 129 159 progressMonitor.worked(1);
