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;
}
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;
}
Aggregations