From 2bad933c85066e403ca7351c0b17fdd2cb49e2e0 Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Fri, 21 Jul 2023 17:09:07 +0200 Subject: [PATCH] First prototype: Moved logic of "getCiphertextPath" Moved logic of "getCiphertextPath" from Vault to VaultState --- .../org/cryptomator/common/vaults/Vault.java | 22 +++---------- .../cryptomator/common/vaults/VaultState.java | 33 ++++++++++++++++++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/cryptomator/common/vaults/Vault.java b/src/main/java/org/cryptomator/common/vaults/Vault.java index 2e1e34a78..1aa1c3273 100644 --- a/src/main/java/org/cryptomator/common/vaults/Vault.java +++ b/src/main/java/org/cryptomator/common/vaults/Vault.java @@ -314,6 +314,10 @@ public class Vault { return vaultSettings.path.get(); } + CryptoFileSystem getCryptoFileSystem() { + return cryptoFileSystem.get(); + } + /** * Gets from the cleartext path its ciphertext counterpart. * @@ -322,23 +326,7 @@ public class Vault { * @throws IllegalStateException if the vault is not unlocked */ public Path getCiphertextPath(Path cleartextPath) throws IOException { - if (!state.getValue().equals(VaultState.Value.UNLOCKED)) { - throw new IllegalStateException("Vault is not unlocked"); - } - var fs = cryptoFileSystem.get(); - var osPathSeparator = cleartextPath.getFileSystem().getSeparator(); - var cryptoFsPathSeparator = fs.getSeparator(); - - if (getMountPoint() instanceof Mountpoint.WithPath mp) { - var absoluteCryptoFsPath = cryptoFsPathSeparator + mp.path().relativize(cleartextPath).toString(); - if (!cryptoFsPathSeparator.equals(osPathSeparator)) { - absoluteCryptoFsPath = absoluteCryptoFsPath.replace(osPathSeparator, cryptoFsPathSeparator); - } - var cryptoPath = fs.getPath(absoluteCryptoFsPath); - return fs.getCiphertextPath(cryptoPath); - } else { - throw new UnsupportedOperationException("URI mount points not supported."); - } + return state.get().getCiphertextPath(this, cleartextPath); } public VaultConfigCache getVaultConfigCache() { diff --git a/src/main/java/org/cryptomator/common/vaults/VaultState.java b/src/main/java/org/cryptomator/common/vaults/VaultState.java index ff09c8b82..824b5350b 100644 --- a/src/main/java/org/cryptomator/common/vaults/VaultState.java +++ b/src/main/java/org/cryptomator/common/vaults/VaultState.java @@ -1,6 +1,7 @@ package org.cryptomator.common.vaults; import com.google.common.base.Preconditions; +import org.cryptomator.integrations.mount.Mountpoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,6 +9,8 @@ import javax.inject.Inject; import javafx.application.Platform; import javafx.beans.value.ObservableObjectValue; import javafx.beans.value.ObservableValueBase; +import java.io.IOException; +import java.nio.file.Path; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.Condition; @@ -43,12 +46,40 @@ public class VaultState extends ObservableValueBase implements /** * Vault is unlocked */ - UNLOCKED, + UNLOCKED { + /** + * Gets from the cleartext path its ciphertext counterpart. + * + * @return Local os path to the ciphertext resource + * @throws IOException if an I/O error occurs + * @throws IllegalStateException if the vault is not unlocked + */ + Path getCiphertextPath(Vault vault, Path cleartextPath) throws IOException { + var fs = vault.getCryptoFileSystem(); + var osPathSeparator = cleartextPath.getFileSystem().getSeparator(); + var cryptoFsPathSeparator = fs.getSeparator(); + + if (vault.getMountPoint() instanceof Mountpoint.WithPath mp) { + var absoluteCryptoFsPath = cryptoFsPathSeparator + mp.path().relativize(cleartextPath).toString(); + if (!cryptoFsPathSeparator.equals(osPathSeparator)) { + absoluteCryptoFsPath = absoluteCryptoFsPath.replace(osPathSeparator, cryptoFsPathSeparator); + } + var cryptoPath = fs.getPath(absoluteCryptoFsPath); + return fs.getCiphertextPath(cryptoPath); + } else { + throw new UnsupportedOperationException("URI mount points not supported."); + } + } + }, /** * Unknown state due to preceding unrecoverable exceptions. */ ERROR; + + Path getCiphertextPath(Vault vault, Path cleartextPath) throws IOException { + throw new IllegalStateException("Vault is not unlocked"); + } } private final AtomicReference value;