diff --git a/src/main/java/org/sead/uploader/AbstractUploader.java b/src/main/java/org/sead/uploader/AbstractUploader.java index bfc71a5..da96154 100644 --- a/src/main/java/org/sead/uploader/AbstractUploader.java +++ b/src/main/java/org/sead/uploader/AbstractUploader.java @@ -23,13 +23,17 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Paths; import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; - +import java.util.TreeMap; +import java.util.Map.Entry; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.protocol.HttpClientContext; import org.json.JSONObject; @@ -72,6 +76,7 @@ public abstract class AbstractUploader { protected boolean listonly = false; protected Set excluded = new HashSet(); + protected Map excludedDir = new TreeMap(Comparator.reverseOrder()); protected static List requests = new ArrayList(); protected static String server = null; @@ -114,6 +119,16 @@ public static void printStatus(float s) { return; } + /** + * Remove all the {@code .} and {@code ..} in the middle of the path string. + * + * @param path String path of file or directory to be normalized + * @return a String representation of the path normalized + */ + public static String normalizePath(String path) { + return Paths.get(path).toAbsolutePath().normalize().toString(); + } + public void parseArgs(String[] args) { for (String arg : args) { @@ -142,9 +157,17 @@ public void parseArgs(String[] args) { } else if (arg.startsWith("-skip")) { skip = Long.parseLong(arg.substring(arg.indexOf(argSeparator) + 1)); println("Skip file count: " + skip); - } else if (arg.startsWith("-ex")) { + } else if (arg.startsWith("-ex=")) { excluded.add(arg.substring(arg.indexOf(argSeparator) + 1)); println("Excluding pattern: " + arg.substring(arg.indexOf(argSeparator) + 1)); + } else if (arg.startsWith("-exdir")) { + String absolutePathArg = normalizePath(arg.substring(arg.indexOf(argSeparator) + 1)); + excludedDir.put(absolutePathArg, true); + println("Excluding directory pattern: " + absolutePathArg); + } else if (arg.startsWith("-indir")) { + String absolutePathArg = normalizePath(arg.substring(arg.indexOf(argSeparator) + 1)); + excludedDir.put(absolutePathArg, false); + println("Including directory pattern: " + absolutePathArg); } else if (arg.startsWith("-server")) { server = arg.substring(arg.indexOf(argSeparator) + 1); println("Using server: " + server); @@ -299,6 +322,24 @@ protected boolean excluded(String name) { return false; } + protected boolean excludedDir(String path) { + final String absolutePath = normalizePath(path); + for (Entry e : excludedDir.entrySet()) { + String s = e.getKey(); + boolean isExcluded = e.getValue(); + if (absolutePath.startsWith(s)) { + if (isExcluded) { + println("Excluding: " + path); + return true; + } else { + println("Including: " + path); + return false; + } + } + } + return false; + } + protected String uploadCollection(Resource dir, String path, String parentId, String collectionId) { new HashSet(); @@ -329,6 +370,9 @@ protected String uploadCollection(Resource dir, String path, String parentId, St if (excluded(file.getName())) { continue; } + if (!file.isDirectory() && excludedDir(file.getAbsolutePath())) { + continue; + } String existingUri = null; String newUri = null;