Search in sources :

Example 1 with MappingMimeTypeService

use of ch.cyberduck.core.MappingMimeTypeService in project cyberduck by iterate-ch.

the class AbstractUploadFilter method prepare.

@Override
public TransferStatus prepare(final Path file, final Local local, final TransferStatus parent, final ProgressListener progress) throws BackgroundException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Prepare %s", file));
    }
    final TransferStatus status = new TransferStatus().hidden(!hidden.accept(file)).withLockId(parent.getLockId());
    // Read remote attributes first
    if (parent.isExists()) {
        if (find.find(file)) {
            status.setExists(true);
            // Read remote attributes
            final PathAttributes attributes = attribute.find(file);
            status.setRemote(attributes);
        } else {
            // Look if there is directory or file that clashes with this upload
            if (file.getType().contains(Path.Type.file)) {
                if (find.find(new Path(file.getAbsolute(), EnumSet.of(Path.Type.directory)))) {
                    throw new AccessDeniedException(String.format("Cannot replace folder %s with file %s", file.getAbsolute(), local.getName()));
                }
            }
            if (file.getType().contains(Path.Type.directory)) {
                if (find.find(new Path(file.getAbsolute(), EnumSet.of(Path.Type.file)))) {
                    throw new AccessDeniedException(String.format("Cannot replace file %s with folder %s", file.getAbsolute(), local.getName()));
                }
            }
        }
    }
    if (file.isFile()) {
        // Set content length from local file
        if (local.isSymbolicLink()) {
            if (!symlinkResolver.resolve(local)) {
                // Will resolve the symbolic link when the file is requested.
                final Local target = local.getSymlinkTarget();
                status.setLength(target.attributes().getSize());
            }
        // No file size increase for symbolic link to be created on the server
        } else {
            // Read file size from filesystem
            status.setLength(local.attributes().getSize());
        }
        if (options.temporary) {
            final Move feature = session.getFeature(Move.class);
            final Path renamed = new Path(file.getParent(), MessageFormat.format(preferences.getProperty("queue.upload.file.temporary.format"), file.getName(), new AlphanumericRandomStringService().random()), file.getType());
            if (feature.isSupported(file, renamed)) {
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Set temporary filename %s", renamed));
                }
                status.temporary(renamed, file);
            }
        }
        status.withMime(new MappingMimeTypeService().getMime(file.getName()));
    }
    if (file.isDirectory()) {
        status.setLength(0L);
    }
    if (options.permissions) {
        final UnixPermission feature = session.getFeature(UnixPermission.class);
        if (feature != null) {
            if (status.isExists()) {
                // Already set when reading attributes of file
                status.setPermission(status.getRemote().getPermission());
            } else {
                status.setPermission(feature.getDefault(local));
            }
        } else {
            // Setting target UNIX permissions in transfer status
            status.setPermission(Permission.EMPTY);
        }
    }
    if (options.acl) {
        final AclPermission feature = session.getFeature(AclPermission.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Getting permission of {0}", "Status"), file.getName()));
                try {
                    status.setAcl(feature.getPermission(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setAcl(feature.getDefault(file, local));
                }
            } else {
                status.setAcl(feature.getDefault(file, local));
            }
        } else {
            // Setting target ACL in transfer status
            status.setAcl(Acl.EMPTY);
        }
    }
    if (options.timestamp) {
        final Timestamp feature = session.getFeature(Timestamp.class);
        if (feature != null) {
            // Read timestamps from local file
            status.setTimestamp(feature.getDefault(local));
        } else {
            if (1L != local.attributes().getModificationDate()) {
                status.setTimestamp(local.attributes().getModificationDate());
            }
        }
    }
    if (options.metadata) {
        final Headers feature = session.getFeature(Headers.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                try {
                    status.setMetadata(feature.getMetadata(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setMetadata(feature.getDefault(local));
                }
            } else {
                status.setMetadata(feature.getDefault(local));
            }
        }
    }
    if (options.encryption) {
        final Encryption feature = session.getFeature(Encryption.class);
        if (feature != null) {
            if (status.isExists()) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                try {
                    status.setEncryption(feature.getEncryption(file));
                } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                    status.setEncryption(feature.getDefault(file));
                }
            } else {
                status.setEncryption(feature.getDefault(file));
            }
        }
    }
    if (options.redundancy) {
        if (file.isFile()) {
            final Redundancy feature = session.getFeature(Redundancy.class);
            if (feature != null) {
                if (status.isExists()) {
                    progress.message(MessageFormat.format(LocaleFactory.localizedString("Reading metadata of {0}", "Status"), file.getName()));
                    try {
                        status.setStorageClass(feature.getClass(file));
                    } catch (NotfoundException | AccessDeniedException | InteroperabilityException e) {
                        status.setStorageClass(feature.getDefault());
                    }
                } else {
                    status.setStorageClass(feature.getDefault());
                }
            }
        }
    }
    if (options.checksum) {
        if (file.isFile()) {
            final ChecksumCompute feature = session.getFeature(Write.class).checksum(file, status);
            if (feature != null) {
                progress.message(MessageFormat.format(LocaleFactory.localizedString("Calculate checksum for {0}", "Status"), file.getName()));
                try {
                    status.setChecksum(feature.compute(local.getInputStream(), status));
                } catch (LocalAccessDeniedException e) {
                    // Ignore failure reading file when in sandbox when we miss a security scoped access bookmark.
                    // Lock for files is obtained only later in Transfer#pre
                    log.warn(e.getMessage());
                }
            }
        }
    }
    return status;
}
Also used : Path(ch.cyberduck.core.Path) Write(ch.cyberduck.core.features.Write) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException) LocalNotfoundException(ch.cyberduck.core.exception.LocalNotfoundException) NotfoundException(ch.cyberduck.core.exception.NotfoundException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) Redundancy(ch.cyberduck.core.features.Redundancy) Headers(ch.cyberduck.core.features.Headers) PathAttributes(ch.cyberduck.core.PathAttributes) Local(ch.cyberduck.core.Local) Encryption(ch.cyberduck.core.features.Encryption) Timestamp(ch.cyberduck.core.features.Timestamp) UnixPermission(ch.cyberduck.core.features.UnixPermission) ChecksumCompute(ch.cyberduck.core.io.ChecksumCompute) AclPermission(ch.cyberduck.core.features.AclPermission) Move(ch.cyberduck.core.features.Move) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) MappingMimeTypeService(ch.cyberduck.core.MappingMimeTypeService) LocalAccessDeniedException(ch.cyberduck.core.exception.LocalAccessDeniedException)

Example 2 with MappingMimeTypeService

use of ch.cyberduck.core.MappingMimeTypeService in project cyberduck by iterate-ch.

the class TouchWorker method run.

@Override
public Path run(final Session<?> session) throws BackgroundException {
    final Touch feature = session.getFeature(Touch.class);
    if (log.isDebugEnabled()) {
        log.debug(String.format("Run with feature %s", feature));
    }
    final TransferStatus status = new TransferStatus().withTimestamp(System.currentTimeMillis()).hidden(!SearchFilterFactory.HIDDEN_FILTER.accept(file)).exists(false).withLength(0L).withMime(new MappingMimeTypeService().getMime(file.getName())).withLockId(this.getLockId(file));
    final Encryption encryption = session.getFeature(Encryption.class);
    if (encryption != null) {
        status.setEncryption(encryption.getDefault(file));
    }
    final Redundancy redundancy = session.getFeature(Redundancy.class);
    if (redundancy != null) {
        status.setStorageClass(redundancy.getDefault());
    }
    status.setTimestamp(System.currentTimeMillis());
    if (PreferencesFactory.get().getBoolean("touch.permissions.change")) {
        final UnixPermission permission = session.getFeature(UnixPermission.class);
        if (permission != null) {
            status.setPermission(permission.getDefault(EnumSet.of(Path.Type.file)));
        }
        final AclPermission acl = session.getFeature(AclPermission.class);
        if (acl != null) {
            status.setAcl(acl.getDefault(EnumSet.of(Path.Type.file)));
        }
    }
    final Path result = feature.touch(file, status);
    if (result.attributes() == PathAttributes.EMPTY) {
        return result.withAttributes(session.getFeature(AttributesFinder.class).find(result));
    }
    return result;
}
Also used : Path(ch.cyberduck.core.Path) AclPermission(ch.cyberduck.core.features.AclPermission) Redundancy(ch.cyberduck.core.features.Redundancy) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) Encryption(ch.cyberduck.core.features.Encryption) Touch(ch.cyberduck.core.features.Touch) MappingMimeTypeService(ch.cyberduck.core.MappingMimeTypeService) UnixPermission(ch.cyberduck.core.features.UnixPermission)

Aggregations

MappingMimeTypeService (ch.cyberduck.core.MappingMimeTypeService)2 Path (ch.cyberduck.core.Path)2 AclPermission (ch.cyberduck.core.features.AclPermission)2 Encryption (ch.cyberduck.core.features.Encryption)2 Redundancy (ch.cyberduck.core.features.Redundancy)2 UnixPermission (ch.cyberduck.core.features.UnixPermission)2 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)2 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)1 Local (ch.cyberduck.core.Local)1 PathAttributes (ch.cyberduck.core.PathAttributes)1 AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)1 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)1 LocalAccessDeniedException (ch.cyberduck.core.exception.LocalAccessDeniedException)1 LocalNotfoundException (ch.cyberduck.core.exception.LocalNotfoundException)1 NotfoundException (ch.cyberduck.core.exception.NotfoundException)1 Headers (ch.cyberduck.core.features.Headers)1 Move (ch.cyberduck.core.features.Move)1 Timestamp (ch.cyberduck.core.features.Timestamp)1 Touch (ch.cyberduck.core.features.Touch)1 Write (ch.cyberduck.core.features.Write)1