First prototype: Moved logic of "getCiphertextPath"

Moved logic of "getCiphertextPath" from Vault to VaultState
This commit is contained in:
JaniruTEC 2023-07-21 17:09:07 +02:00
parent ca3a11de90
commit 2bad933c85
No known key found for this signature in database
GPG Key ID: F696B974E62833E3
2 changed files with 37 additions and 18 deletions

View File

@ -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() {

View File

@ -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<VaultState.Value> 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> value;