Search in sources :

Example 1 with GraphExceptionMappingService

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);
    }
}
Also used : Delete(ch.cyberduck.core.features.Delete) CopyOperation(org.nuxeo.onedrive.client.CopyOperation) DriveItem(org.nuxeo.onedrive.client.types.DriveItem) OneDriveAPIException(org.nuxeo.onedrive.client.OneDriveAPIException) PathAttributes(ch.cyberduck.core.PathAttributes) GraphExceptionMappingService(ch.cyberduck.core.onedrive.GraphExceptionMappingService) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService)

Example 2 with GraphExceptionMappingService

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);
    }
}
Also used : DriveItem(org.nuxeo.onedrive.client.types.DriveItem) OneDriveAPIException(org.nuxeo.onedrive.client.OneDriveAPIException) PathAttributes(ch.cyberduck.core.PathAttributes) GraphExceptionMappingService(ch.cyberduck.core.onedrive.GraphExceptionMappingService) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService)

Example 3 with GraphExceptionMappingService

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);
    }
}
Also used : DriveItem(org.nuxeo.onedrive.client.types.DriveItem) OneDriveAPIException(org.nuxeo.onedrive.client.OneDriveAPIException) FileSystemInfo(org.nuxeo.onedrive.client.types.FileSystemInfo) PathAttributes(ch.cyberduck.core.PathAttributes) PatchOperation(org.nuxeo.onedrive.client.PatchOperation) GraphExceptionMappingService(ch.cyberduck.core.onedrive.GraphExceptionMappingService) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService)

Example 4 with GraphExceptionMappingService

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;
}
Also used : Path(ch.cyberduck.core.Path) OneDriveAPIException(org.nuxeo.onedrive.client.OneDriveAPIException) DefaultHomeFinderService(ch.cyberduck.core.shared.DefaultHomeFinderService) IOException(java.io.IOException) DriveItem(org.nuxeo.onedrive.client.types.DriveItem) Drive(org.nuxeo.onedrive.client.types.Drive) GraphExceptionMappingService(ch.cyberduck.core.onedrive.GraphExceptionMappingService) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService)

Example 5 with GraphExceptionMappingService

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);
    }
}
Also used : DriveItem(org.nuxeo.onedrive.client.types.DriveItem) DescriptiveUrl(ch.cyberduck.core.DescriptiveUrl) OneDriveAPIException(org.nuxeo.onedrive.client.OneDriveAPIException) GraphExceptionMappingService(ch.cyberduck.core.onedrive.GraphExceptionMappingService) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) OneDriveUrlProvider(ch.cyberduck.core.onedrive.OneDriveUrlProvider) NullInputStream(org.apache.commons.io.input.NullInputStream) HttpRange(ch.cyberduck.core.http.HttpRange)

Aggregations

DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)9 GraphExceptionMappingService (ch.cyberduck.core.onedrive.GraphExceptionMappingService)9 IOException (java.io.IOException)9 OneDriveAPIException (org.nuxeo.onedrive.client.OneDriveAPIException)9 DriveItem (org.nuxeo.onedrive.client.types.DriveItem)9 PathAttributes (ch.cyberduck.core.PathAttributes)4 Path (ch.cyberduck.core.Path)2 PatchOperation (org.nuxeo.onedrive.client.PatchOperation)2 FileSystemInfo (org.nuxeo.onedrive.client.types.FileSystemInfo)2 DescriptiveUrl (ch.cyberduck.core.DescriptiveUrl)1 NotfoundException (ch.cyberduck.core.exception.NotfoundException)1 UnsupportedException (ch.cyberduck.core.exception.UnsupportedException)1 Delete (ch.cyberduck.core.features.Delete)1 HttpRange (ch.cyberduck.core.http.HttpRange)1 MemorySegementingOutputStream (ch.cyberduck.core.io.MemorySegementingOutputStream)1 VoidStatusOutputStream (ch.cyberduck.core.io.VoidStatusOutputStream)1 OneDriveUrlProvider (ch.cyberduck.core.onedrive.OneDriveUrlProvider)1 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)1 DefaultHomeFinderService (ch.cyberduck.core.shared.DefaultHomeFinderService)1 NullInputStream (org.apache.commons.io.input.NullInputStream)1