fix(S3): folder / file handling
This commit is contained in:
parent
e4ad22b00b
commit
6503727a40
@ -17,6 +17,11 @@ class RootS3Folder extends S3Folder {
|
|||||||
return cloud;
|
return cloud;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getKey() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public S3Folder withCloud(Cloud cloud) {
|
public S3Folder withCloud(Cloud cloud) {
|
||||||
return new RootS3Folder((S3Cloud) cloud);
|
return new RootS3Folder((S3Cloud) cloud);
|
||||||
|
@ -9,7 +9,7 @@ import java.util.Date;
|
|||||||
|
|
||||||
class S3CloudNodeFactory {
|
class S3CloudNodeFactory {
|
||||||
|
|
||||||
private static final String SUFFIX = "/";
|
private static final String DELIMITER = "/";
|
||||||
|
|
||||||
public static S3File file(S3Folder parent, S3ObjectSummary file) {
|
public static S3File file(S3Folder parent, S3ObjectSummary file) {
|
||||||
String name = getNameFromKey(file.getKey());
|
String name = getNameFromKey(file.getKey());
|
||||||
@ -47,15 +47,19 @@ class S3CloudNodeFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String getNodePath(S3Folder parent, String name) {
|
private static String getNodePath(S3Folder parent, String name) {
|
||||||
return parent.getPath() + SUFFIX + name;
|
return parent.getKey() + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getNameFromKey(String key) {
|
public static String getNameFromKey(String key) {
|
||||||
return key.substring(key.lastIndexOf(SUFFIX) + 1);
|
String name = key;
|
||||||
|
if (key.endsWith(DELIMITER)) {
|
||||||
|
name = key.substring(0, key.length() -1);
|
||||||
|
}
|
||||||
|
return name.contains(DELIMITER) ? name.substring(name.lastIndexOf(DELIMITER) + 1) : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static S3Node from(S3Folder parent, S3ObjectSummary objectSummary) {
|
public static S3Node from(S3Folder parent, S3ObjectSummary objectSummary) {
|
||||||
if (objectSummary.getKey().endsWith(SUFFIX)) {
|
if (objectSummary.getKey().endsWith(DELIMITER)) {
|
||||||
return folder(parent, objectSummary);
|
return folder(parent, objectSummary);
|
||||||
} else {
|
} else {
|
||||||
return file(parent, objectSummary);
|
return file(parent, objectSummary);
|
||||||
|
@ -37,6 +37,11 @@ class S3File implements CloudFile, S3Node {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getKey() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public S3Folder getParent() {
|
public S3Folder getParent() {
|
||||||
return parent;
|
return parent;
|
||||||
|
@ -5,6 +5,8 @@ import org.cryptomator.domain.CloudFolder;
|
|||||||
|
|
||||||
class S3Folder implements CloudFolder, S3Node {
|
class S3Folder implements CloudFolder, S3Node {
|
||||||
|
|
||||||
|
private static final String DELIMITER = "/";
|
||||||
|
|
||||||
private final S3Folder parent;
|
private final S3Folder parent;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String path;
|
private final String path;
|
||||||
@ -30,6 +32,11 @@ class S3Folder implements CloudFolder, S3Node {
|
|||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getKey() {
|
||||||
|
return path + DELIMITER;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public S3Folder getParent() {
|
public S3Folder getParent() {
|
||||||
return parent;
|
return parent;
|
||||||
|
@ -8,6 +8,7 @@ import com.amazonaws.services.s3.model.CopyObjectResult;
|
|||||||
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
|
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
|
||||||
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;
|
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion;
|
||||||
import com.amazonaws.services.s3.model.GetObjectRequest;
|
import com.amazonaws.services.s3.model.GetObjectRequest;
|
||||||
|
import com.amazonaws.services.s3.model.ListObjectsV2Request;
|
||||||
import com.amazonaws.services.s3.model.ListObjectsV2Result;
|
import com.amazonaws.services.s3.model.ListObjectsV2Result;
|
||||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
import com.amazonaws.services.s3.model.Owner;
|
import com.amazonaws.services.s3.model.Owner;
|
||||||
@ -49,7 +50,7 @@ import static org.cryptomator.util.file.LruFileCacheUtil.storeToLruCache;
|
|||||||
|
|
||||||
class S3Impl {
|
class S3Impl {
|
||||||
|
|
||||||
private static final String SUFFIX = "/";
|
private static final String DELIMITER = "/";
|
||||||
|
|
||||||
private final S3ClientFactory clientFactory = new S3ClientFactory();
|
private final S3ClientFactory clientFactory = new S3ClientFactory();
|
||||||
private final S3Cloud cloud;
|
private final S3Cloud cloud;
|
||||||
@ -79,10 +80,10 @@ class S3Impl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public S3Folder resolve(String path) {
|
public S3Folder resolve(String path) {
|
||||||
if (path.startsWith(SUFFIX)) {
|
if (path.startsWith(DELIMITER)) {
|
||||||
path = path.substring(1);
|
path = path.substring(1);
|
||||||
}
|
}
|
||||||
String[] names = path.split(SUFFIX);
|
String[] names = path.split(DELIMITER);
|
||||||
S3Folder folder = root;
|
S3Folder folder = root;
|
||||||
for (String name : names) {
|
for (String name : names) {
|
||||||
if (!name.isEmpty()) {
|
if (!name.isEmpty()) {
|
||||||
@ -97,17 +98,17 @@ class S3Impl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public S3File file(S3Folder parent, String name, Optional<Long> size) throws BackendException, IOException {
|
public S3File file(S3Folder parent, String name, Optional<Long> size) throws BackendException, IOException {
|
||||||
return S3CloudNodeFactory.file(parent, name, size, parent.getPath() + name);
|
return S3CloudNodeFactory.file(parent, name, size, parent.getKey() + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public S3Folder folder(S3Folder parent, String name) {
|
public S3Folder folder(S3Folder parent, String name) {
|
||||||
return S3CloudNodeFactory.folder(parent, name, parent.getPath() + name + SUFFIX);
|
return S3CloudNodeFactory.folder(parent, name, parent.getKey() + name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists(S3Node node) {
|
public boolean exists(S3Node node) {
|
||||||
String path = node.getPath();
|
String key = node.getKey();
|
||||||
|
|
||||||
ListObjectsV2Result result = client().listObjectsV2(cloud.s3Bucket(), path);
|
ListObjectsV2Result result = client().listObjectsV2(cloud.s3Bucket(), key);
|
||||||
|
|
||||||
if (result.getObjectSummaries().size() > 0) {
|
if (result.getObjectSummaries().size() > 0) {
|
||||||
return true;
|
return true;
|
||||||
@ -119,10 +120,21 @@ class S3Impl {
|
|||||||
public List<S3Node> list(S3Folder folder) throws IOException, BackendException {
|
public List<S3Node> list(S3Folder folder) throws IOException, BackendException {
|
||||||
List<S3Node> result = new ArrayList<>();
|
List<S3Node> result = new ArrayList<>();
|
||||||
|
|
||||||
ListObjectsV2Result listObjects = client().listObjectsV2(cloud.s3Bucket(), folder.getPath());
|
ListObjectsV2Request request = new ListObjectsV2Request()
|
||||||
|
.withBucketName(cloud.s3Bucket())
|
||||||
|
.withPrefix(folder.getKey())
|
||||||
|
.withDelimiter(DELIMITER);
|
||||||
|
|
||||||
|
ListObjectsV2Result listObjects = client().listObjectsV2(request);
|
||||||
|
for(String prefix : listObjects.getCommonPrefixes()) {
|
||||||
|
result.add(S3CloudNodeFactory.folder(folder, S3CloudNodeFactory.getNameFromKey(prefix)));
|
||||||
|
}
|
||||||
|
|
||||||
for (S3ObjectSummary objectSummary : listObjects.getObjectSummaries()) {
|
for (S3ObjectSummary objectSummary : listObjects.getObjectSummaries()) {
|
||||||
|
if (!objectSummary.getKey().equals(listObjects.getPrefix())) {
|
||||||
result.add(S3CloudNodeFactory.from(folder, objectSummary));
|
result.add(S3CloudNodeFactory.from(folder, objectSummary));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,9 +228,9 @@ class S3Impl {
|
|||||||
ListObjectsV2Result listObjects;
|
ListObjectsV2Result listObjects;
|
||||||
|
|
||||||
if (sharedPreferencesHandler.useLruCache() && createLruCache(sharedPreferencesHandler.lruCacheSize())) {
|
if (sharedPreferencesHandler.useLruCache() && createLruCache(sharedPreferencesHandler.lruCacheSize())) {
|
||||||
listObjects = client().listObjectsV2(cloud.s3Bucket(), file.getPath());
|
listObjects = client().listObjectsV2(cloud.s3Bucket(), file.getKey());
|
||||||
if (listObjects.getObjectSummaries().size() != 1) {
|
if (listObjects.getObjectSummaries().size() != 1) {
|
||||||
throw new NoSuchCloudFileException(file.getPath());
|
throw new NoSuchCloudFileException(file.getKey());
|
||||||
}
|
}
|
||||||
S3ObjectSummary summary = listObjects.getObjectSummaries().get(0);
|
S3ObjectSummary summary = listObjects.getObjectSummaries().get(0);
|
||||||
cacheKey = Optional.of(summary.getKey() + summary.getETag());
|
cacheKey = Optional.of(summary.getKey() + summary.getETag());
|
||||||
|
@ -7,4 +7,6 @@ interface S3Node extends CloudNode {
|
|||||||
@Override
|
@Override
|
||||||
S3Folder getParent();
|
S3Folder getParent();
|
||||||
|
|
||||||
|
String getKey();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user