From 06c16a289e6ed217695623011fa56f635853d876 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 19 Mar 2022 14:51:16 +0100 Subject: [PATCH] La taille du fichier est maintenant en long pour supporter les gros fichiers --- .../domains/client/Context.java | 18 +++++++++++++++++- .../domains/client/RequestBundle.java | 19 +++++++++++++++++++ .../protocol/rules/reader/EraseErrorRule.java | 1 - .../protocol/rules/reader/GetFileRule.java | 3 +-- .../protocol/rules/reader/RetrieveOkRule.java | 10 +++++----- .../protocol/rules/reader/SavefileRule.java | 8 ++++---- .../protocol/rules/reader/SignupRule.java | 1 - .../protocol/rules/writer/GetFileOkRule.java | 8 ++++---- .../protocol/rules/writer/SaveFileOkRule.java | 2 +- .../protocol/rules/writer/SendfileRule.java | 4 ++-- .../java/lightcontainer/storage/File.java | 6 +++--- .../lightcontainer/storage/ReadOnlyFile.java | 2 +- .../lightcontainer/utils/FileReceiver.java | 2 +- .../java/lightcontainer/utils/FileSender.java | 10 +++++----- app/src/main/resources/appdata.json | 17 ++++++++++++++++- 15 files changed, 79 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index dc143a1..8e63323 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -126,6 +126,14 @@ public class Context { requestBundle.putInt(key, value); } + /** + * @param key La clé permettant de retrouver la valeur + * @param value La valeur associée à la clé + */ + public void putDataLong(String key, long value) { + requestBundle.putLong(key, value); + } + /** * Permet de récupérer des données pour la requête courante * @@ -146,6 +154,14 @@ public class Context { return requestBundle.getInt(key); } + /** + * @param key La clé permettant de retrouver la valeur + * @return La valeur associée à la clé + */ + public long getDataLong(String key) { + return requestBundle.getLong(key); + } + /** * Permet d'ajouter un fichier à l'utilisateur * @@ -156,7 +172,7 @@ public class Context { * @param domain Domain dans lequel est stocké le fichier * @return TRUE si le fichier a pu être enregistré. */ - public boolean addFile(String fileName, String fileNameSalt, int size, String iv, String domain) { + public boolean addFile(String fileName, String fileNameSalt, long size, String iv, String domain) { return this.repository.addFileFor(new File(fileName, fileNameSalt, size, iv, Set.of(domain)), getLogin()); } diff --git a/app/src/main/java/lightcontainer/domains/client/RequestBundle.java b/app/src/main/java/lightcontainer/domains/client/RequestBundle.java index 6d9fd09..99e0615 100644 --- a/app/src/main/java/lightcontainer/domains/client/RequestBundle.java +++ b/app/src/main/java/lightcontainer/domains/client/RequestBundle.java @@ -9,6 +9,8 @@ public class RequestBundle { private final Map intData = new HashMap<>(); + private final Map longData = new HashMap<>(); + /** * Permet d'ajouter des String * @@ -48,4 +50,21 @@ public class RequestBundle { public int getInt(String key) { return intData.get(key); } + + /** + * @param key La clé permettant de retrouver la valeur + * @param value La valeur associée à la clé + */ + public void putLong(String key, long value) { + longData.put(key, value); + } + + /** + * + * @param key La clé permettant de retrouver la valeur + * @return La valeur associée à la clé ou null + */ + public long getLong(String key) { + return longData.get(key); + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java index cb25c5e..f9a4be3 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java @@ -3,7 +3,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.StandardizedDefinitions; import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; import static lightcontainer.protocol.StandardizedDefinitions.SBE_ERASE_RESULT_ERROR; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 12d5a45..1fe4b87 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -3,7 +3,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.StandardizedDefinitions; import lightcontainer.protocol.rules.writer.GetFileErrorRule; import lightcontainer.protocol.rules.writer.RetrieveFileRule; import lightcontainer.storage.ReadOnlyFile; @@ -62,7 +61,7 @@ public class GetFileRule extends ProtocolReader { // Save into bundle context.putDataString("fileName", file.getName()); - context.putDataInt("fileSize", file.getSize()); + context.putDataLong("fileSize", file.getSize()); context.putDataString("fileIV", file.getIv()); // Create cmd for storebacked diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java index 24d1fc9..a2ac0cb 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -37,10 +37,10 @@ public class RetrieveOkRule extends ProtocolReader { public class Result extends ProtocolResult { // Variables private final String filename; - private final int filesize; + private final long filesize; private String hashedFileContent; - public Result(Context context, String filename, int filesize, String hashedFileContent) { + public Result(Context context, String filename, long filesize, String hashedFileContent) { super(context); this.filename = filename; this.filesize = filesize; @@ -51,7 +51,7 @@ public class RetrieveOkRule extends ProtocolReader { return filename; } - public int getFilesize() { + public long getFilesize() { return filesize; } @@ -81,11 +81,11 @@ public class RetrieveOkRule extends ProtocolReader { RetrieveOkRule.Result result = new RetrieveOkRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]); // save encrypted file size into bundle - context.putDataInt("encryptedFileSize", result.getFilesize()); // TODO to long ?! + context.putDataLong("encryptedFileSize", result.getFilesize()); // TODO to long ?! context.putDataString("hashedFileName", result.getFilename()); // Set result command - result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT); + result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataLong("fileSize"))), ResultCmdReceiver.CLIENT); return result; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index b573454..8112cad 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -43,10 +43,10 @@ public class SavefileRule extends ProtocolReader { public class Result extends ProtocolResult { // Variables private String filename; - private final int size; + private final long size; // Construct - public Result(Context context, String filename, int size) { + public Result(Context context, String filename, long size) { super(context); this.filename = filename; this.size = size; @@ -75,7 +75,7 @@ public class SavefileRule extends ProtocolReader { String iv = AES_GCM.generateIV(); // retrieve file and new size - int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv); + long encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv); if (encryptedFileSize < 0) throw new IOException(); System.out.println(encryptedFileSize); @@ -83,7 +83,7 @@ public class SavefileRule extends ProtocolReader { String fileHash = SHA.hashFile(storagePath, this.filename); // On met les données de la requête actuelle - getContext().putDataInt("size", size); + getContext().putDataLong("size", size); getContext().putDataString("iv", iv); getContext().putDataString("fileNameSalt", fileNameSalt); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java index ebf7468..ce08172 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java @@ -3,7 +3,6 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; -import lightcontainer.protocol.StandardizedDefinitions; import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignOkRule; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java index 2f7e66c..a0327b5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -33,10 +33,10 @@ public class GetFileOkRule extends ProtocolWriter { public class Result extends ProtocolWriter.ProtocolResult { // Variables private final String filename; - private final int filesize; + private final long filesize; // Constructors - public Result(Context context, String filename, int filesize) { + public Result(Context context, String filename, long filesize) { super(context); this.filename = filename; this.filesize = filesize; @@ -51,12 +51,12 @@ public class GetFileOkRule extends ProtocolWriter { public void write(OutputStream writer) throws IOException { System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize); - System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize") + " normal: " + getContext().getDataInt("fileSize")); + System.out.println("Encrypted size for parsing: " + getContext().getDataLong("encryptedFileSize") + " normal: " + getContext().getDataLong("fileSize")); FileSender fileSender = new FileSender(storagePath); fileSender.sendFile( getContext().getDataString("hashedFileName"), writer, - getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) + getContext().getDataLong("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) getContext().getAesKey(), getContext().getDataString("fileIV") ); diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java index 6078af1..7debda1 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -32,7 +32,7 @@ public class SaveFileOkRule extends ProtocolWriter { System.out.println("===> Save en json " + context.getDomain() + " - " + context.getLogin()); // Sauvegarder dans JSON - context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); + context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataLong("size"), context.getDataString("iv"), context.getDomain()); // Suppression du fichier temporaire dans le stockage du FFE String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index aa44928..4dd9b18 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -33,10 +33,10 @@ public class SendfileRule extends ProtocolWriter { public class Result extends ProtocolWriter.ProtocolResult { private final String hashedFileName; - private final int fileSize; + private final long fileSize; private final String hashedFileContent; - public Result(Context context, String hashedFileName, int fileSize, String hashedFileContent) { + public Result(Context context, String hashedFileName, long fileSize, String hashedFileContent) { super(context); this.hashedFileName = hashedFileName; this.fileSize = fileSize; diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java index 4c3ab3d..18ee90d 100644 --- a/app/src/main/java/lightcontainer/storage/File.java +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -10,12 +10,12 @@ public class File implements ReadOnlyFile { // Variables private final String name; private final String fileNameSalt; - private final int size; + private final long size; private final String iv; private final Set storage; // Constructor - public File(String name, String fileNameSalt, int size, String iv, Set storage) { + public File(String name, String fileNameSalt, long size, String iv, Set storage) { this.name = name; this.fileNameSalt = fileNameSalt; this.size = size; @@ -34,7 +34,7 @@ public class File implements ReadOnlyFile { } @Override - public int getSize() { + public long getSize() { return size; } diff --git a/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java index ae7bf29..f5f77b7 100644 --- a/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java +++ b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java @@ -8,7 +8,7 @@ public interface ReadOnlyFile { String getFileNameSalt(); - int getSize(); + long getSize(); String getIv(); diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index 634e5ec..3a6e4d1 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -9,7 +9,7 @@ public class FileReceiver { this.path = path; } - public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { + public int receiveFile(InputStream input, String fileName, long fileSize, String key, String iv) { try { File file = new File(String.format("%s/%s", path, fileName)); if (file.createNewFile()) { diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index fe97aa3..5781961 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -10,7 +10,7 @@ public class FileSender { this.path = path; } - public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) throws IOException { + public boolean sendFile(String filename, OutputStream out, long fileSize, String aesKey, String iv) throws IOException { BufferedInputStream bisFile; System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv); try { @@ -28,7 +28,7 @@ public class FileSender { public boolean sendFile(String filename, OutputStream out) throws IOException { BufferedInputStream bisFile; - int bytesReaded = 0; + int bytesRead = 0; try { File file = new File(String.format("%s/%s", path, filename)); long fileSize = file.length(); @@ -36,10 +36,10 @@ public class FileSender { byte[] buffer = new byte[DEFAULT_BUFFER]; bisFile = new BufferedInputStream(new FileInputStream(file)); long currentOffset = 0; - while ((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) { - out.write(buffer, 0, bytesReaded); + while ((currentOffset < fileSize) && (bytesRead = bisFile.read(buffer)) > 0) { + out.write(buffer, 0, bytesRead); out.flush(); - currentOffset += bytesReaded; + currentOffset += bytesRead; } bisFile.close(); return true; diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 5cd8dca..51a1b6b 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,16 @@ -{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"wlp1s0","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"benjamin","password":"$2a$10$I4vHt83CTYuQCP7xvZ04Ne7Vb0cswBiVZhV0n23k9FCxoH0ny9fZG","aes_key":"mAP6izUBUhBxIkakH2yB/TplhRz1OQV5Fp6HQmhywns=","files":[{"name":"README.md","fileNameSalt":"atK0eGo/J8IBOTCpIZpWow==","size":17,"iv":"fXPzefxl0cO4Hf02Qwi+BA==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"ca.crt","fileNameSalt":"qz6FoPGyJJ8Hy+1LIouvZA==","size":4207,"iv":"8v2H+cOzztD++NXHXw5Ofw==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "224.66.66.1", + "multicast_port": 15502, + "network_interface": "lo", + "tls": true, + "storagePath": "C:\\Users\\ledou\\Documents\\ffe", + "users": [ + { + "name": "aaaaa", + "password": "$2a$10$6KbB.cBqsWyRExIf2ugkhebsJOhUG9goXFEfOYWXJI4dOQNT1uXhu", + "aes_key": "2gYT1TeILBswvCbxAth24ZNLtFhaA1zvdJmtfh0unNE=", + "files": [] + } + ] +} \ No newline at end of file