Sauvegarde et lecture du fichier de synchronisation -> AppData vers Json terminé
This commit is contained in:
parent
a185a3ecb7
commit
57984456f1
16
app/src/main/java/lightcontainer/storage/Adapter.java
Normal file
16
app/src/main/java/lightcontainer/storage/Adapter.java
Normal file
@ -0,0 +1,16 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
public interface Adapter {
|
||||
|
||||
/**
|
||||
* @param appData The appData to stringify
|
||||
* @return The string representation of AppData
|
||||
*/
|
||||
String toString(AppData appData);
|
||||
|
||||
/**
|
||||
* @param appDataString The appData string to parse
|
||||
* @return AppData
|
||||
*/
|
||||
AppData fromString(String appDataString);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -81,6 +82,10 @@ public class AppData {
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<User> usersIterator() {
|
||||
return users.values().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fileName The name of the file
|
||||
* @param user The user
|
||||
@ -107,7 +112,8 @@ public class AppData {
|
||||
if (!this.users.containsKey(user.getName())) {
|
||||
return false;
|
||||
} else {
|
||||
return this.users.get(user.getName()).addFile(file);
|
||||
this.users.get(user.getName()).addFile(file);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
70
app/src/main/java/lightcontainer/storage/JsonAdapter.java
Normal file
70
app/src/main/java/lightcontainer/storage/JsonAdapter.java
Normal file
@ -0,0 +1,70 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class JsonAdapter implements Adapter {
|
||||
|
||||
@Override
|
||||
public String toString(AppData appData) {
|
||||
return addData(appData);
|
||||
}
|
||||
|
||||
private String addData(AppData appData) {
|
||||
AppConfig appConfig = appData.getAppConfig();
|
||||
JsonObject config = new JsonObject();
|
||||
config.addProperty("unicast_port", appConfig.getUnicastPort());
|
||||
config.addProperty("multicast_ip", appConfig.getMulticastIp());
|
||||
config.addProperty("multicast_port", appConfig.getMulticastPort());
|
||||
config.addProperty("network_interface", appConfig.getNetworkInterface());
|
||||
config.addProperty("tls", appConfig.isTls());
|
||||
JsonArray users = new JsonArray();
|
||||
Iterator<User> userIterator = appData.usersIterator();
|
||||
addUsers(users, userIterator);
|
||||
config.addProperty("users", users.toString());
|
||||
return config.toString();
|
||||
}
|
||||
|
||||
private void addUsers(JsonArray users, Iterator<User> userIterator) {
|
||||
while (userIterator.hasNext()) {
|
||||
User current = userIterator.next();
|
||||
JsonObject user = new JsonObject();
|
||||
user.addProperty("name", current.getName());
|
||||
user.addProperty("password", current.getPassword());
|
||||
user.addProperty("aes_key", current.getAesKey());
|
||||
JsonArray files = new JsonArray();
|
||||
Iterator<File> fileIterator = current.fileIterator();
|
||||
addFiles(fileIterator);
|
||||
user.addProperty("files", files.toString());
|
||||
users.add(user);
|
||||
}
|
||||
}
|
||||
|
||||
private void addFiles(Iterator<File> fileIterator) {
|
||||
while (fileIterator.hasNext()) {
|
||||
File currentFile = fileIterator.next();
|
||||
JsonObject file = new JsonObject();
|
||||
file.addProperty("name", currentFile.getName());
|
||||
file.addProperty("size", currentFile.getSize());
|
||||
file.addProperty("iv", currentFile.getIv());
|
||||
JsonArray storage = new JsonArray();
|
||||
Iterator<String> storageIterator = currentFile.getStorageIterator();
|
||||
addStorage(storage, storageIterator);
|
||||
file.addProperty("storage", storage.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void addStorage(JsonArray storage, Iterator<String> storageIterator) {
|
||||
while (storageIterator.hasNext()) {
|
||||
String storageString = storageIterator.next();
|
||||
storage.add(storageString);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppData fromString(String appDataString) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
public class JsonReader implements FileReader {
|
||||
|
||||
@Override
|
||||
public AppData readFile(String fileName) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
public class JsonRepository implements Repository {
|
||||
|
||||
|
||||
@Override
|
||||
public void save(FileWriter fileWriter) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppData load(FileReader fileReader) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
public class JsonWriter implements FileWriter {
|
||||
|
||||
@Override
|
||||
public void writeToFile(String fileName, AppData appData) {
|
||||
|
||||
}
|
||||
}
|
@ -1,8 +1,14 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
public interface Repository {
|
||||
public class Repository {
|
||||
|
||||
void save(FileWriter fileWriter);
|
||||
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
|
||||
|
||||
AppData load(FileReader fileReader);
|
||||
void save(String filePath, Adapter adapter) {
|
||||
|
||||
}
|
||||
|
||||
AppData load(String filePath, Adapter adapter) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package lightcontainer.storage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -36,8 +38,8 @@ public class User {
|
||||
return aesKey;
|
||||
}
|
||||
|
||||
public Set<Map.Entry<String, File>> getFilesIterator() {
|
||||
return files.entrySet();
|
||||
public Iterator<File> fileIterator() {
|
||||
return files.values().iterator();
|
||||
}
|
||||
|
||||
public File getFile(String fileName) {
|
||||
@ -48,13 +50,8 @@ public class User {
|
||||
* @param file The file to add.
|
||||
* @return False if a file with the same name already exists. Otherwise, adds the file and returns true.
|
||||
*/
|
||||
public boolean addFile(File file) {
|
||||
if (this.files.containsKey(file.getName())) {
|
||||
return false;
|
||||
} else {
|
||||
this.files.put(file.getName(), file);
|
||||
return true;
|
||||
}
|
||||
public void addFile(File file) {
|
||||
this.files.put(file.getName(), file);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user