use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateWriteFeature method write.
@Override
public HttpResponseOutputStream<FileMetadata> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final DelayedHttpEntityCallable<FileMetadata> command = new DelayedHttpEntityCallable<FileMetadata>() {
@Override
public FileMetadata call(final AbstractHttpEntity entity) throws BackgroundException {
// Initiate a resumable upload
String location;
try {
location = start(file, status);
} catch (InteroperabilityException e) {
if (null == status.getLockId()) {
throw e;
}
location = start(file, status.withLockId(null));
}
final StoregateApiClient client = session.getClient();
try {
// Upload the file
final HttpPut put = new HttpPut(location);
put.setEntity(entity);
final String header;
if (status.getLength() == 0) {
// Touch
header = "*/0";
} else {
final HttpRange range = HttpRange.byLength(0, status.getLength());
header = String.format("%d-%d/%d", range.getStart(), range.getEnd(), status.getLength());
}
put.addHeader(HttpHeaders.CONTENT_RANGE, String.format("bytes %s", header));
final HttpResponse putResponse = client.getClient().execute(put);
try {
switch(putResponse.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_CREATED:
final FileMetadata result = new JSON().getContext(FileMetadata.class).readValue(new InputStreamReader(putResponse.getEntity().getContent(), StandardCharsets.UTF_8), FileMetadata.class);
fileid.cache(file, result.getId());
return result;
default:
throw new StoregateExceptionMappingService(fileid).map(new ApiException(putResponse.getStatusLine().getStatusCode(), putResponse.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(putResponse.getEntity())));
}
} catch (BackgroundException e) {
// Cancel upload on error reply
cancel(file, location);
throw e;
} finally {
EntityUtils.consume(putResponse.getEntity());
}
} catch (IOException e) {
// Cancel upload on I/O failure
cancel(file, location);
throw new HttpExceptionMappingService().map("Upload {0} failed", e, file);
}
}
@Override
public long getContentLength() {
return status.getLength();
}
};
return this.write(file, status, command);
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateListService method list.
@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
if (directory.isRoot()) {
final AttributedList<Path> list = new AttributedList<>();
for (RootFolder root : session.roots()) {
switch(root.getRootFolderType()) {
// My Files
case 0:
case // Common
1:
final PathAttributes attr = new PathAttributes().withFileId(root.getId());
attr.setModificationDate(root.getModified().getMillis());
attr.setCreationDate(root.getCreated().getMillis());
list.add(new Path(PathNormalizer.normalize(root.getName()), EnumSet.of(Path.Type.directory, Path.Type.volume), attr));
break;
}
}
listener.chunk(directory, list);
return list;
} else {
try {
final AttributedList<Path> children = new AttributedList<>();
final StoregateAttributesFinderFeature attributes = new StoregateAttributesFinderFeature(session, fileid);
int pageIndex = 0;
FileContents files;
do {
files = new FilesApi(this.session.getClient()).filesGet(URIEncoder.encode(fileid.getPrefixedPath(directory)), pageIndex, chunksize, "Name asc", // All
0, true, false, false);
for (File f : files.getFiles()) {
final PathAttributes attrs = attributes.toAttributes(f);
final EnumSet<Path.Type> type = (f.getFlags() & 1) == 1 ? EnumSet.of(Path.Type.directory) : EnumSet.of(Path.Type.file);
final Path p = new Path(directory, f.getName(), type, attrs);
children.add(p);
listener.chunk(directory, children);
}
pageIndex++;
} while (children.size() < files.getTotalRowCount());
return children;
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map("Listing directory {0} failed", e, directory);
}
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateMoveFeature method move.
@Override
public Path move(final Path file, final Path renamed, final TransferStatus status, final Delete.Callback delete, final ConnectionCallback callback) throws BackgroundException {
try {
final StoregateApiClient client = session.getClient();
final MoveFileRequest move = new MoveFileRequest().name(renamed.getName()).parentID(fileid.getFileId(renamed.getParent(), new DisabledListProgressListener())).mode(// Overwrite
1);
final HttpEntityEnclosingRequestBase request;
request = new HttpPost(String.format("%s/v4/files/%s/move", client.getBasePath(), fileid.getFileId(file, new DisabledListProgressListener())));
if (status.getLockId() != null) {
request.addHeader("X-Lock-Id", status.getLockId().toString());
}
request.setEntity(new StringEntity(new JSON().getContext(move.getClass()).writeValueAsString(move), ContentType.create("application/json", StandardCharsets.UTF_8.name())));
request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE);
final HttpResponse response = client.getClient().execute(request);
try {
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_NO_CONTENT:
final PathAttributes attr = new PathAttributes(file.attributes());
fileid.cache(file, null);
fileid.cache(renamed, file.attributes().getFileId());
return renamed.withAttributes(attr);
default:
throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
}
} finally {
EntityUtils.consume(response.getEntity());
}
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateQuotaFeature method get.
@Override
public Space get() throws BackgroundException {
try {
final AccountSettingsApi account = new AccountSettingsApi(session.getClient());
final AccountStorage quota = account.accountSettingsGetAccountStorage();
return new Space(quota.getUsed(), quota.getAvailable());
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map("Failure to read attributes of {0}", e, new DefaultHomeFinderService(session).find());
}
}
use of ch.cyberduck.core.storegate.io.swagger.client.ApiException in project cyberduck by iterate-ch.
the class StoregateCopyFeature method copy.
@Override
public Path copy(final Path source, final Path target, final TransferStatus status, final ConnectionCallback callback, final StreamListener listener) throws BackgroundException {
try {
final CopyFileRequest copy = new CopyFileRequest().name(target.getName()).parentID(fileid.getFileId(target.getParent(), new DisabledListProgressListener())).mode(// Overwrite
1);
final File file = new FilesApi(session.getClient()).filesCopy(fileid.getFileId(source, new DisabledListProgressListener()), copy);
listener.sent(status.getLength());
fileid.cache(target, file.getId());
return target.withAttributes(new StoregateAttributesFinderFeature(session, fileid).toAttributes(file));
} catch (ApiException e) {
throw new StoregateExceptionMappingService(fileid).map("Cannot copy {0}", e, source);
}
}
Aggregations