use of ch.cyberduck.core.onedrive.GraphExceptionMappingService in project cyberduck by iterate-ch.
the class GraphCopyFeature method copy.
@Override
public Path copy(final Path source, final Path target, final TransferStatus status, final ConnectionCallback callback, final StreamListener listener) throws BackgroundException {
final CopyOperation copyOperation = new CopyOperation();
if (!StringUtils.equals(source.getName(), target.getName())) {
copyOperation.rename(target.getName());
}
if (status.isExists()) {
new GraphDeleteFeature(session, fileid).delete(Collections.singletonMap(target, status), callback, new Delete.DisabledCallback());
}
final DriveItem targetItem = session.getItem(target.getParent());
copyOperation.copy(targetItem);
final DriveItem item = session.getItem(source);
try {
Files.copy(item, copyOperation).await(statusObject -> logger.info(String.format("Copy Progress Operation %s progress %f status %s", statusObject.getOperation(), statusObject.getPercentage(), statusObject.getStatus())));
listener.sent(status.getLength());
target.attributes().setFileId(null);
final PathAttributes attr = attributes.find(target);
fileid.cache(target, attr.getFileId());
return target.withAttributes(attr);
} catch (OneDriveAPIException e) {
throw new GraphExceptionMappingService(fileid).map("Cannot copy {0}", e, source);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Cannot copy {0}", e, source);
}
}
use of ch.cyberduck.core.onedrive.GraphExceptionMappingService in project cyberduck by iterate-ch.
the class GraphDirectoryFeature method mkdir.
@Override
public Path mkdir(final Path directory, final TransferStatus status) throws BackgroundException {
final DriveItem folder = session.getItem(directory.getParent());
try {
final DriveItem.Metadata metadata = Files.createFolder(folder, directory.getName());
final PathAttributes attr = attributes.toAttributes(metadata);
fileid.cache(directory, attr.getFileId());
return directory.withAttributes(attr);
} catch (OneDriveAPIException e) {
throw new GraphExceptionMappingService(fileid).map("Cannot create folder {0}", e, directory);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Cannot create folder {0}", e, directory);
}
}
use of ch.cyberduck.core.onedrive.GraphExceptionMappingService in project cyberduck by iterate-ch.
the class GraphMoveFeature method move.
@Override
public Path move(final Path file, final Path renamed, final TransferStatus status, final Delete.Callback callback, final ConnectionCallback connectionCallback) throws BackgroundException {
if (status.isExists()) {
delete.delete(Collections.singletonMap(renamed, status), connectionCallback, callback);
}
final PatchOperation patchOperation = new PatchOperation();
if (!StringUtils.equals(file.getName(), renamed.getName())) {
patchOperation.rename(renamed.getName());
}
if (!file.getParent().equals(renamed.getParent())) {
final DriveItem moveTarget = session.getItem(renamed.getParent());
patchOperation.move(moveTarget);
}
// Keep current timestamp set
final FileSystemInfo info = new FileSystemInfo();
info.setLastModifiedDateTime(Instant.ofEpochMilli(file.attributes().getModificationDate()).atOffset(ZoneOffset.UTC));
patchOperation.facet("fileSystemInfo", info);
final DriveItem item = session.getItem(file);
try {
Files.patch(item, patchOperation);
final PathAttributes attributes = new GraphAttributesFinderFeature(session, fileid).toAttributes(item.getMetadata());
fileid.cache(file, null);
fileid.cache(renamed, attributes.getFileId());
return renamed.withAttributes(attributes);
} catch (OneDriveAPIException e) {
throw new GraphExceptionMappingService(fileid).map("Cannot rename {0}", e, file);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Cannot rename {0}", e, file);
}
}
use of ch.cyberduck.core.onedrive.GraphExceptionMappingService in project cyberduck by iterate-ch.
the class GraphQuotaFeature method get.
@Override
public Space get() throws BackgroundException {
final Path home = new DefaultHomeFinderService(session).find();
if (!session.isAccessible(home)) {
// not accessible (important for Sharepoint)
return unknown;
}
final Drive.Metadata metadata;
try {
// retrieve OneDriveItem from home
final DriveItem item = session.getItem(home, true);
// returns drive, which can then query metadata.
metadata = item.getDrive().getMetadata();
} catch (OneDriveAPIException e) {
throw new GraphExceptionMappingService(fileid).map("Failure to read attributes of {0}", e, home);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Failure to read attributes of {0}", e, home);
}
final org.nuxeo.onedrive.client.types.Quota quota = metadata.getQuota();
if (quota != null) {
Long used = quota.getUsed();
if (used != null) {
Long remaining = quota.getRemaining();
if (remaining != null && (used != 0 || remaining != 0)) {
return new Space(used, remaining);
}
Long total = quota.getTotal();
if (total != null && (used != 0 || total != 0)) {
return new Space(used, total - used);
}
}
}
return unknown;
}
use of ch.cyberduck.core.onedrive.GraphExceptionMappingService in project cyberduck by iterate-ch.
the class GraphReadFeature method read.
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
try {
if (file.getType().contains(Path.Type.placeholder)) {
final DescriptiveUrl link = new OneDriveUrlProvider().toUrl(file).find(DescriptiveUrl.Type.http);
if (DescriptiveUrl.EMPTY.equals(link)) {
log.warn(String.format("Missing web link for file %s", file));
return new NullInputStream(file.attributes().getSize());
}
// Write web link file
return IOUtils.toInputStream(UrlFileWriterFactory.get().write(link), Charset.defaultCharset());
} else {
final DriveItem target = session.getItem(file);
if (status.isAppend()) {
final HttpRange range = HttpRange.withStatus(status);
final String header;
if (TransferStatus.UNKNOWN_LENGTH == range.getEnd()) {
header = String.format("%d-", range.getStart());
} else {
header = String.format("%d-%d", range.getStart(), range.getEnd());
}
if (log.isDebugEnabled()) {
log.debug(String.format("Add range header %s for file %s", header, file));
}
return Files.download(target, header);
}
return Files.download(target);
}
} catch (OneDriveAPIException e) {
switch(e.getResponseCode()) {
case HttpStatus.SC_NOT_FOUND:
fileid.cache(file, null);
}
throw new GraphExceptionMappingService(fileid).map("Download {0} failed", e, file);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
}
}
Aggregations