drop multipe vaults at once

This commit is contained in:
Sebastian Stenzel
2019-09-10 17:08:00 +02:00
parent d6ffb890e3
commit 6a4edbf73d
2 changed files with 30 additions and 25 deletions

View File

@@ -6,6 +6,7 @@
package org.cryptomator.common.settings;
import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import javafx.beans.Observable;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
@@ -23,6 +24,7 @@ import java.util.Base64;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
/**
@@ -36,6 +38,8 @@ public class VaultSettings {
public static final boolean DEFAULT_USES_INDIVIDUAL_MOUNTPATH = false;
public static final boolean DEFAULT_USES_READONLY_MODE = false;
public static final String DEFAULT_MOUNT_FLAGS = "";
private static final Random RNG = new Random();
private final String id;
private final ObjectProperty<Path> path = new SimpleObjectProperty();
@@ -69,20 +73,9 @@ public class VaultSettings {
}
private static String generateId() {
return asBase64String(nineBytesFrom(UUID.randomUUID()));
}
private static String asBase64String(byte[] bytes) {
byte[] base64Bytes = Base64.getUrlEncoder().encode(bytes);
return new String(base64Bytes, StandardCharsets.US_ASCII);
}
private static byte[] nineBytesFrom(UUID uuid) {
ByteBuffer uuidBuffer = ByteBuffer.allocate(9);
uuidBuffer.putLong(uuid.getMostSignificantBits());
uuidBuffer.put((byte) (uuid.getLeastSignificantBits() & 0xFF));
uuidBuffer.flip();
return uuidBuffer.array();
byte[] randomBytes = new byte[9];
RNG.nextBytes(randomBytes);
return BaseEncoding.base64Url().encode(randomBytes);
}
public static String normalizeMountName(String mountName) {

View File

@@ -23,6 +23,10 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.stream.Collectors;
@MainWindowScoped
public class MainWindowController implements FxController {
@@ -89,27 +93,35 @@ public class MainWindowController implements FxController {
if (event.getGestureSource() != root && event.getDragboard().hasFiles()) {
/* allow for both copying and moving, whatever user chooses */
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
File dropped = event.getDragboard().getFiles().get(0);
if (dropped.getName().endsWith(".cryptomator")) {
addVault(dropped);
} else {
Collection<Path> vaultPaths = event.getDragboard().getFiles().stream().map(File::toPath).filter(this::isVaultPath).collect(Collectors.toSet());
if (vaultPaths.isEmpty()) {
wrongFileAlert.build().showWrongFileAlertWindow();
} else {
vaultPaths.forEach(this::addVault);
}
}
event.consume();
});
}
private void addVault(final File dropped) {
if (dropped != null) {
VaultSettings vaultSettings = VaultSettings.withRandomId();
vaultSettings.path().setValue(dropped.toPath().toAbsolutePath().getParent());
Vault newVault = vaultFactory.get(vaultSettings);
vaults.add(newVault);
//TODO: error handling?
private boolean isVaultPath(Path path) {
if (path.getFileName().toString().endsWith(".cryptomator")) {
return true;
} else if (Files.exists(path.resolve("masterkey.cryptomator"))) {
return true;
} else {
return false;
}
}
private void addVault(Path pathToVault) {
VaultSettings vaultSettings = VaultSettings.withRandomId();
vaultSettings.path().setValue(pathToVault);
Vault newVault = vaultFactory.get(vaultSettings);
vaults.add(newVault);
//TODO: error handling?
}
private void loadFont(String resourcePath) {
try {
FontLoader.load(resourcePath);