Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/main/java/org/sead/uploader/AbstractUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,6 +76,7 @@ public abstract class AbstractUploader {
protected boolean listonly = false;

protected Set<String> excluded = new HashSet<String>();
protected Map<String, Boolean> excludedDir = new TreeMap<String, Boolean>(Comparator.reverseOrder());
protected static List<String> requests = new ArrayList<String>();

protected static String server = null;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR doesn't handle the include side yet?

println("Including directory pattern: " + absolutePathArg);
} else if (arg.startsWith("-server")) {
server = arg.substring(arg.indexOf(argSeparator) + 1);
println("Using server: " + server);
Expand Down Expand Up @@ -299,6 +322,24 @@ protected boolean excluded(String name) {
return false;
}

protected boolean excludedDir(String path) {
final String absolutePath = normalizePath(path);
for (Entry<String, Boolean> 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<String>();
Expand Down Expand Up @@ -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;

Expand Down