|
| 1 | +package org.cryptomator.cloudaccess.localfs; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.nio.channels.Channels; |
| 6 | +import java.nio.channels.FileChannel; |
| 7 | +import java.nio.file.CopyOption; |
| 8 | +import java.nio.file.FileVisitOption; |
| 9 | +import java.nio.file.FileVisitResult; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.LinkOption; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.SimpleFileVisitor; |
| 14 | +import java.nio.file.StandardCopyOption; |
| 15 | +import java.nio.file.StandardOpenOption; |
| 16 | +import java.nio.file.attribute.BasicFileAttributes; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.EnumSet; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.CompletionStage; |
| 23 | + |
| 24 | +import com.google.common.io.ByteStreams; |
| 25 | +import com.google.common.io.MoreFiles; |
| 26 | +import com.google.common.io.RecursiveDeleteOption; |
| 27 | +import org.cryptomator.cloudaccess.api.CloudItemList; |
| 28 | +import org.cryptomator.cloudaccess.api.CloudItemMetadata; |
| 29 | +import org.cryptomator.cloudaccess.api.CloudItemType; |
| 30 | +import org.cryptomator.cloudaccess.api.CloudProvider; |
| 31 | +import org.cryptomator.cloudaccess.api.ProgressListener; |
| 32 | + |
| 33 | +public class LocalFsCloudProvider implements CloudProvider { |
| 34 | + |
| 35 | + private static final Path ABS_ROOT = Path.of("/"); |
| 36 | + |
| 37 | + private final Path root; |
| 38 | + |
| 39 | + public LocalFsCloudProvider(Path root) { |
| 40 | + this.root = root; |
| 41 | + } |
| 42 | + |
| 43 | + private Path resolve(Path cloudPath) { |
| 44 | + Path relPath = ABS_ROOT.relativize(cloudPath); |
| 45 | + return root.resolve(relPath); |
| 46 | + } |
| 47 | + |
| 48 | + private CloudItemMetadata createMetadata(Path fullPath, BasicFileAttributes attr) { |
| 49 | + var relPath = root.relativize(fullPath); |
| 50 | + var type = attr.isDirectory() ? CloudItemType.FOLDER : attr.isRegularFile() ? CloudItemType.FILE : CloudItemType.UNKNOWN; |
| 51 | + var modifiedDate = Optional.of(attr.lastModifiedTime().toInstant()); |
| 52 | + var size = Optional.of(attr.size()); |
| 53 | + return new CloudItemMetadata(relPath.getFileName().toString(), ABS_ROOT.resolve(relPath), type, modifiedDate, size); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public CompletionStage<CloudItemMetadata> itemMetadata(Path node) { |
| 58 | + Path path = resolve(node); |
| 59 | + try { |
| 60 | + var attr = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
| 61 | + var metadata = createMetadata(path, attr); |
| 62 | + return CompletableFuture.completedFuture(metadata); |
| 63 | + } catch (IOException e) { |
| 64 | + return CompletableFuture.failedFuture(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public CompletionStage<CloudItemList> list(Path folder, Optional<String> pageToken) { |
| 70 | + Path folderPath = resolve(folder); |
| 71 | + try { |
| 72 | + List<CloudItemMetadata> items = new ArrayList<>(); |
| 73 | + Files.walkFileTree(folderPath, EnumSet.noneOf(FileVisitOption.class), 1, new SimpleFileVisitor<>() { |
| 74 | + |
| 75 | + @Override |
| 76 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 77 | + items.add(createMetadata(file, attrs)); |
| 78 | + return FileVisitResult.CONTINUE; |
| 79 | + } |
| 80 | + }); |
| 81 | + return CompletableFuture.completedFuture(new CloudItemList(items, Optional.empty())); |
| 82 | + } catch (IOException e) { |
| 83 | + return CompletableFuture.failedFuture(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public CompletionStage<InputStream> read(Path file, long offset, long count, ProgressListener progressListener) { |
| 89 | + Path filePath = resolve(file); |
| 90 | + try { |
| 91 | + var ch = Files.newByteChannel(filePath, StandardOpenOption.READ); |
| 92 | + ch.position(offset); |
| 93 | + return CompletableFuture.completedFuture(ByteStreams.limit(Channels.newInputStream(ch), count)); |
| 94 | + } catch (IOException e) { |
| 95 | + return CompletableFuture.failedFuture(e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public CompletionStage<CloudItemMetadata> write(Path file, boolean replace, InputStream data, ProgressListener progressListener) { |
| 101 | + Path filePath = resolve(file); |
| 102 | + var options = replace |
| 103 | + ? EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING) |
| 104 | + : EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); |
| 105 | + |
| 106 | + try (var ch = FileChannel.open(filePath, options)) { |
| 107 | + var size = ch.transferFrom(Channels.newChannel(data), 0, Long.MAX_VALUE); |
| 108 | + var modifiedDate = Files.getLastModifiedTime(filePath).toInstant(); |
| 109 | + var metadata = new CloudItemMetadata(file.getFileName().toString(), file, CloudItemType.FILE, Optional.of(modifiedDate), Optional.of(size)); |
| 110 | + return CompletableFuture.completedFuture(metadata); |
| 111 | + } catch (IOException e) { |
| 112 | + return CompletableFuture.failedFuture(e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public CompletionStage<Path> createFolder(Path folder) { |
| 118 | + Path folderPath = resolve(folder); |
| 119 | + try { |
| 120 | + Files.createDirectory(folderPath); |
| 121 | + return CompletableFuture.completedFuture(folder); |
| 122 | + } catch (IOException e) { |
| 123 | + return CompletableFuture.failedFuture(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public CompletionStage<Void> delete(Path node) { |
| 129 | + Path path = resolve(node); |
| 130 | + try { |
| 131 | + MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE); |
| 132 | + return CompletableFuture.completedFuture(null); |
| 133 | + } catch (IOException e) { |
| 134 | + return CompletableFuture.failedFuture(e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public CompletionStage<Path> move(Path source, Path target, boolean replace) { |
| 140 | + Path src = resolve(source); |
| 141 | + Path dst = resolve(target); |
| 142 | + try { |
| 143 | + var options = replace ? EnumSet.of(StandardCopyOption.REPLACE_EXISTING) : EnumSet.noneOf(StandardCopyOption.class); |
| 144 | + Files.move(src, dst, options.toArray(CopyOption[]::new)); |
| 145 | + return CompletableFuture.completedFuture(target); |
| 146 | + } catch (IOException e) { |
| 147 | + return CompletableFuture.failedFuture(e); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments