Ticket #20310: 20310.patch

File 20310.patch, 2.4 KB (added by taylor.smock, 5 years ago)

Allow JpgImporter to import remote images

  • src/org/openstreetmap/josm/gui/io/importexport/JpgImporter.java

     
    1010import java.util.HashSet;
    1111import java.util.List;
    1212import java.util.Set;
     13import java.util.regex.Matcher;
     14import java.util.regex.Pattern;
    1315
    1416import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1517import org.openstreetmap.josm.gui.layer.GpxLayer;
    1618import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer;
    1719import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     20import org.openstreetmap.josm.io.CachedFile;
    1821import org.openstreetmap.josm.io.IllegalDataException;
    1922
    2023/**
     
    2225 *
    2326 */
    2427public class JpgImporter extends FileImporter {
     28    /** Check if the filename starts with a borked path ({@link java.io.File#File} drops consecutive {@code /} characters). */
     29    private static final Pattern URL_START_BAD = Pattern.compile("^(https?:/)([^/].*)$");
     30    /** Check for the beginning of a "good" url */
     31    private static final Pattern URL_START_GOOD = Pattern.compile("^https?://.*$");
     32
    2533    private GpxLayer gpx;
    2634
    2735    /**
     
    106114                        progressMonitor.worked(1);
    107115                    }
    108116                } else {
    109                     if (FILE_FILTER.accept(f)) {
     117                    /* Check if the path is a web path, and if so, ensure that it is "correct" */
     118                    final String path = f.getPath();
     119                    Matcher matcherBad = URL_START_BAD.matcher(path);
     120                    final String realPath;
     121                    if (matcherBad.matches()) {
     122                        realPath = matcherBad.replaceFirst(matcherBad.group(1) + "/" + matcherBad.group(2));
     123                    } else {
     124                        realPath = path;
     125                    }
     126                    if (URL_START_GOOD.matcher(realPath).matches() && FILE_FILTER.accept(f)) {
     127                        try (CachedFile cachedFile = new CachedFile(realPath)) {
     128                            files.add(cachedFile.getFile());
     129                        }
     130                    } else if (FILE_FILTER.accept(f)) {
    110131                        files.add(f);
    111132                    }
    112133                    progressMonitor.worked(1);