Changeset 17558 in josm


Ignore:
Timestamp:
2021-03-10T20:33:44+01:00 (3 years ago)
Author:
Don-vip
Message:

fix #20310 - ImageImporter improvements (patch by Bjoeni)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/ImageImporter.java

    r17553 r17558  
    99import java.util.Arrays;
    1010import java.util.Collections;
     11import java.util.EnumSet;
    1112import java.util.HashSet;
    1213import java.util.List;
    1314import java.util.Set;
     15import java.util.regex.Matcher;
     16import java.util.regex.Pattern;
    1417import java.util.stream.Collectors;
    1518
     
    2023import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
    2124import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     25import org.openstreetmap.josm.io.CachedFile;
    2226import org.openstreetmap.josm.io.IllegalDataException;
    2327
     
    2731 */
    2832public 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
    2939    private GpxLayer gpx;
    3040
     
    91101            List<File> files = new ArrayList<>();
    92102            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));
    94104
    95105            if (progressMonitor.isCanceled())
     
    107117    static void addRecursiveFiles(List<File> files, Set<String> visitedDirs, List<File> sel, ProgressMonitor progressMonitor)
    108118            throws IOException {
     119        addRecursiveFiles(EnumSet.noneOf(Options.class), files, visitedDirs, sel, progressMonitor);
     120    }
    109121
     122    static void addRecursiveFiles(Set<Options> options, List<File> files, Set<String> visitedDirs, List<File> sel,
     123            ProgressMonitor progressMonitor) throws IOException {
    110124        if (progressMonitor.isCanceled())
    111125            return;
     
    118132                        File[] dirFiles = f.listFiles(); // Can be null for some strange directories (like lost+found)
    119133                        if (dirFiles != null) {
    120                             addRecursiveFiles(files, visitedDirs, Arrays.asList(dirFiles), progressMonitor.createSubTaskMonitor(1, true));
     134                            addRecursiveFiles(options, files, visitedDirs, Arrays.asList(dirFiles),
     135                                    progressMonitor.createSubTaskMonitor(1, true));
    121136                        }
    122137                    } else {
     
    124139                    }
    125140                } else {
    126                     if (FILE_FILTER.accept(f)) {
     141                    /* Check if the path is a web path, and if so, ensure that it is "correct" */
     142                    final String path = f.getPath();
     143                    Matcher matcherBad = URL_START_BAD.matcher(path);
     144                    final String realPath;
     145                    if (matcherBad.matches()) {
     146                        realPath = matcherBad.replaceFirst(matcherBad.group(1) + "/" + matcherBad.group(2));
     147                    } else {
     148                        realPath = path;
     149                    }
     150                    if (URL_START_GOOD.matcher(realPath).matches() && FILE_FILTER.accept(f)
     151                            && options.contains(Options.ALLOW_WEB_RESOURCES)) {
     152                        try (CachedFile cachedFile = new CachedFile(realPath)) {
     153                            files.add(cachedFile.getFile());
     154                        }
     155                    } else if (FILE_FILTER.accept(f)) {
    127156                        files.add(f);
    128157                    }
Note: See TracChangeset for help on using the changeset viewer.