Browse files

Makes directory listing a little lazier

  • Loading branch information...
1 parent bebcbf4 commit 7216f3fcc7ad66ce110559576a8605bb7d51fe30 @jamessanders committed Dec 22, 2012
View
2 VERSION
@@ -1 +1 @@
-0.1.1
+0.1.5
View
0 configure 100644 → 100755
File mode changed.
View
32 java/src/main/java/com/jsync/S3Resource.java
@@ -29,6 +29,7 @@
S3ObjectSummary s3ObjectSummary;
ObjectMetadata s3MetaData;
boolean useReducedRedundancy;
+ int offset;
public S3Resource(S3ObjectSummary s3ObjectSummary,
String bucket,
@@ -37,7 +38,8 @@ public S3Resource(S3ObjectSummary s3ObjectSummary,
Boolean isDirectory,
S3ResourceGenerator s3ResourceGenerator,
AmazonS3Client client,
- boolean useReducedRedundacy) {
+ boolean useReducedRedundacy,
+ int offset) {
try {
this.path = new URI(path);
} catch (URISyntaxException e) {
@@ -51,6 +53,7 @@ public S3Resource(S3ObjectSummary s3ObjectSummary,
this.s3ResourceGenerator = s3ResourceGenerator;
this.s3MetaData = null;
this.useReducedRedundancy = useReducedRedundacy;
+ this.offset = offset;
}
public AmazonS3Client getClient() { return client; }
@@ -67,10 +70,27 @@ public String toString () {
return path.toString();
}
+ public List<S3Resource> getChildren() {
+ if (this.children != null) {
+ return this.children;
+ }
+ try {
+ this.children = this.s3ResourceGenerator.getChildren(this.path.getPath(),this.offset);
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
+ }
+ return this.children;
+ }
+
public IResource[] list() {
- IResource[] ir = new IResource[children.size()];
- this.children.toArray(ir);
- return ir;
+ List<S3Resource> children = this.getChildren();
+ if (children != null) {
+ IResource[] ir = new IResource[children.size()];
+ children.toArray(ir);
+ return ir;
+ } else {
+ return new IResource[0];
+ }
}
public Boolean isDirectory() {
@@ -109,11 +129,11 @@ public IResource join(String name) {
return new S3Resource(null,
bucket,
builder.build().toString(),
- new ArrayList<S3Resource>(),
+ null,
false,
s3ResourceGenerator,
client,
- useReducedRedundancy);
+ useReducedRedundancy, this.offset);
} catch (URISyntaxException e) {
e.printStackTrace();
}
View
73 java/src/main/java/com/jsync/S3ResourceGenerator.java
@@ -64,33 +64,28 @@ public AmazonS3Client getClient() {
}
}
}
- /*
- for (S3ObjectSummary summary : this.objects) {
- System.out.println(summary.getKey());
- }
- System.exit(1);
- */
+
return this.objects;
}
- public S3Resource getResource(String path) throws URISyntaxException {
- return this.getResource(path, 0);
- }
+ public List<S3Resource> getChildren(String path, int point) throws URISyntaxException {
+
+ //System.out.println("GETTING CHILDREN: " + path);
- public S3Resource getResource(String path, int point) throws URISyntaxException {
URI uri;
uri = new URI(path);
+ S3ObjectSummary theFile = null;
+
List<S3Resource> files = new ArrayList<S3Resource>();
List<S3Resource> subdirs = new ArrayList<S3Resource>();
List<String> visitedSubdirs = new ArrayList<String>();
- S3ObjectSummary theFile = null;
-
int c = point;
for (S3ObjectSummary object : getObjectList().subList(point, getObjectList().size())) {
String key = "/" + object.getKey();
+ // System.out.println(" -> " + key);
String repl = key.replaceFirst("^" + Pattern.quote(uri.getPath()) + "/", "");
Boolean matches = key.matches("^" + Pattern.quote(uri.getPath()) + "/.*$");
@@ -118,7 +113,7 @@ else if (!repl.contains("/") && !key.equals(uri.getPath()) && matches) {
builder.setScheme("s3");
builder.setHost(bucket);
builder.setPath(key);
- files.add(new S3Resource(object, bucket, builder.build().toString(), null, false, this, client, useReducedRedundancy));
+ files.add(new S3Resource(object, bucket, builder.build().toString(), null, false, this, client, useReducedRedundancy, c));
} else if ((files.size() != 0 || subdirs.size() != 0) || theFile != null){
break;
}
@@ -131,12 +126,56 @@ else if (!repl.contains("/") && !key.equals(uri.getPath()) && matches) {
files.add(dir);
}
- //System.out.println(files);
+ return files;
+ }
+
+
+ public S3Resource getResource(String path) throws URISyntaxException {
+ return this.getResource(path, 0);
+ }
+
+ public S3Resource getResource(String path, int point) throws URISyntaxException {
+ //System.out.println("GETTING RESOURCE: " + path);
+ URI uri;
+ uri = new URI(path);
+
+ int numberOfChildren = 0;
+
+ S3ObjectSummary theFile = null;
+
+ int c = point;
+ int off = point;
+
+ for (S3ObjectSummary object : getObjectList().subList(point, getObjectList().size())) {
+ String key = "/" + object.getKey();
+ String repl = key.replaceFirst("^" + Pattern.quote(uri.getPath()) + "/", "");
+
+ Boolean matches = key.matches("^" + Pattern.quote(uri.getPath()) + "/.*$");
+
+ if (key.equals(uri.getPath())) {
+ theFile = object;
+ off = c;
+ }
+
+ // is subdir
+ if (repl.contains("/") && !key.equals(uri.getPath()) && matches) {
+ numberOfChildren += 1;
+ }
+
+ // is file
+ else if (!repl.contains("/") && !key.equals(uri.getPath()) && matches) {
+ numberOfChildren += 1;
+ } else if (theFile != null){
+ break;
+ }
+
+ c += 1;
+ }
- if (files.size() > 0) {
- return new S3Resource(theFile, bucket, path, files, true, this, client, useReducedRedundancy);
+ if (numberOfChildren > 0) {
+ return new S3Resource(theFile, bucket, path, null, true, this, client, useReducedRedundancy, off);
} else {
- return new S3Resource(theFile, bucket, path, files, false, this, client, useReducedRedundancy);
+ return new S3Resource(theFile, bucket, path, null, false, this, client, useReducedRedundancy, off);
}
}
}

0 comments on commit 7216f3f

Please sign in to comment.