Example usage for java.nio.file.attribute BasicFileAttributes isDirectory

List of usage examples for java.nio.file.attribute BasicFileAttributes isDirectory

Introduction

In this page you can find the example usage for java.nio.file.attribute BasicFileAttributes isDirectory.

Prototype

boolean isDirectory();

Source Link

Document

Tells whether the file is a directory.

Usage

From source file:ubicrypt.core.provider.LocalRepository.java

@Override
public Observable<Boolean> save(final FileProvenience fp) {
    try {/*from w  ww. j a  va 2  s  .  co  m*/
        check(fp);
        final UbiFile rfile = fp.getFile();
        final Optional<LocalFile> lfile = localConfig.getLocalFiles().stream().filter(lf -> lf.equals(rfile))
                .findFirst();
        //if path not set, take the getName()
        final Path path = rfile.getPath() != null ? rfile.getPath() : Paths.get(rfile.getName());
        if (!lfile.isPresent()) {
            if (!rfile.isDeleted() && !rfile.isRemoved()) {
                log.info("copy to:{} from repo:{}, file:{} ", basePath.resolve(path), fp.getOrigin(),
                        rfile.getPath());
                final LocalFile lc = new LocalFile();
                lc.copyFrom(rfile);
                localConfig.getLocalFiles().add(lc);
                if (Files.exists(basePath.resolve(path))) {
                    final BasicFileAttributes attrs = SupplierExp.silent(
                            () -> Files.readAttributes(basePath.resolve(path), BasicFileAttributes.class));
                    if (attrs.isDirectory()) {
                        log.info("can't import file, a folder already exists with the same name:{}", path);
                        return Observable.just(false);
                    }
                    if (attrs.size() == rfile.getSize()) {
                        log.info("identical file already present locally:{}", path);
                        localConfig.getLocalFiles().add(LocalFile.copy(rfile));
                        return Observable.just(true).doOnCompleted(() -> fileEvents.onNext(
                                new FileEvent(rfile, FileEvent.Type.created, FileEvent.Location.local)));
                    }
                    log.info("conflicting file already present locally:{}", path);
                    conflictEvents.onNext(rfile);
                    return Observable.just(false);
                }
                AtomicReference<Path> tempFile = new AtomicReference<>();
                return fp.getOrigin().get(rfile).flatMap(new StoreTempFile()).doOnNext(tempFile::set)
                        .map(new CopyFile(rfile.getSize(), basePath.resolve(path), true,
                                fp.getFile().getLastModified()))
                        .doOnCompleted(() -> {
                            if (tempFile.get() != null) {
                                try {
                                    Files.delete(tempFile.get());
                                } catch (IOException e) {
                                }
                            }
                        }).doOnCompleted(() -> localConfig.getLocalFiles().add(LocalFile.copy(rfile)))
                        .doOnCompleted(() -> fileEvents.onNext(
                                new FileEvent(rfile, FileEvent.Type.created, FileEvent.Location.local)));
            }
            //if present
        } else {
            if (rfile.getVclock().compare(lfile.get().getVclock()) == VClock.Comparison.newer) {
                lfile.get().copyFrom(rfile);
                if (!rfile.isDeleted() && !rfile.isRemoved()) {
                    log.info("update file:{} locally from repo:{}", rfile.getPath(), fp.getOrigin());
                    AtomicReference<Path> tempFile = new AtomicReference<>();
                    return fp.getOrigin().get(fp.getFile()).flatMap(new StoreTempFile())
                            .map(new CopyFile(rfile.getSize(), basePath.resolve(path), false,
                                    fp.getFile().getLastModified()))
                            .doOnCompleted(() -> {
                                if (tempFile.get() != null) {
                                    try {
                                        Files.delete(tempFile.get());
                                    } catch (IOException e) {
                                    }
                                }
                            }).doOnCompleted(() -> fileEvents.onNext(new FileEvent(fp.getFile(),
                                    FileEvent.Type.updated, FileEvent.Location.local)));
                }
                //removed or deleted
                fileEvents.onNext(new FileEvent(fp.getFile(),
                        rfile.isDeleted() ? FileEvent.Type.deleted : FileEvent.Type.removed,
                        FileEvent.Location.local));

            }
        }
        return Observable.just(false);
    } catch (Exception e) {
        return Observable.error(e);
    }
}