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 226e2f5..3d7044d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -43,6 +43,14 @@ public class RetrieveOkRule extends ProtocolReader { this.hashedFileContent = hashedFileContent; } + public String getFilename() { + return filename; + } + + public int getFilesize() { + return filesize; + } + /** * Bitch has bettern than my money * @param reader Buffer rempli du fichier @@ -70,7 +78,8 @@ 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", Integer.parseInt(data[FILE_SIZE])); // TODO to long ?! + context.putDataInt("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); 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 87dc71d..8b5078a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -6,6 +6,8 @@ import lightcontainer.utils.FileSender; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; public class GetFileOkRule extends ProtocolWriter { // Constants @@ -47,12 +49,16 @@ public class GetFileOkRule extends ProtocolWriter { System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize")); FileSender fileSender = new FileSender(storagePath); fileSender.sendFile( - getContext().getHashedFileName(this.filename), + getContext().getDataString("hashedFileName"), writer, getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) getContext().getAesKey(), getContext().getDataString("fileIV") ); + + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, getContext().getDataString("hashedFileName")))); + } catch (IOException ignore) { } } } 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 fac890f..13473f5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -34,8 +34,7 @@ public class SaveFileOkRule extends ProtocolWriter { String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); try { Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); - } catch (IOException e) {} - + } catch (IOException ignore) {} return result; } } diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index 826bb1f..f852587 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -8,18 +8,17 @@ public class FileReceiver { public FileReceiver(String path) { this.path = path; } public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { - BufferedOutputStream bosFile = null; - try { - bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName))); - - AES_GCM.encryptStream(input, bosFile, fileSize, key, iv); - - File f = new File(String.format("%s/%s", path, fileName)); - return (int)f.length(); // TODO change the size to LONG + File file = new File(String.format("%s/%s", path, fileName)); + if (file.createNewFile()) { + try (BufferedOutputStream bufferedStream = new BufferedOutputStream(new FileOutputStream(file))) { + // AES close l'outputstream mais on le try with ressources au cas ou ;) + AES_GCM.encryptStream(input, bufferedStream, fileSize, key, iv); + } + } + return (int)file.length(); // TODO change the size to LONG } catch(IOException | AES_GCM.AesGcmException ex) { ex.printStackTrace(); - if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } return -1; } } diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index 008618a..227855d 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -27,13 +27,12 @@ public class FileSender { public boolean sendFile(String filename, OutputStream out) throws IOException { BufferedInputStream bisFile; int bytesReaded = 0; - try { - File f = new File(String.format("%s/%s", path, filename)); - long fileSize = f.length(); - if(f.exists()) { + File file = new File(String.format("%s/%s", path, filename)); + long fileSize = file.length(); + if(file.exists()) { byte[] buffer = new byte[DEFAULT_BUFFER]; - bisFile = new BufferedInputStream(new FileInputStream(f)); + bisFile = new BufferedInputStream(new FileInputStream(file)); long currentOffset = 0; while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) { out.write(buffer, 0, bytesReaded); out.flush(); @@ -43,7 +42,7 @@ public class FileSender { return true; } else return false; - } catch(IOException ex) { + } catch(IOException ex) {// todo change throw ex; } } diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java index 0f76efa..25cd806 100644 --- a/app/src/main/java/lightcontainer/utils/SHA.java +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -87,7 +87,11 @@ public class SHA { public static String hashFile(String rootPath, String fileName) throws ShaException { try { File file = new File(String.format("%s/%s", rootPath, fileName)); - return hashStream(new FileInputStream(file), file.length()); + String hash; + try (InputStream inStream = new FileInputStream(file)) { + hash = hashStream(inStream, file.length()); + } + return hash; } catch (Exception e) { throw new ShaException(e); } diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 1b71617..e26d31b 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1 @@ -{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"README.md","fileNameSalt":"QnjgVrrW2KADUYUdeD/KcQ==","size":17,"iv":"E5JtY/JrH1B447F/my8Hkg==","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":"D:\\ffe","users":[{"name":"aaaaa","password":"$2a$10$HAupXer5aVEDgIAtFQchyOSB.SyCq7irV7EZNcMTE3R9k.1UJGOLu","aes_key":"FvsflaQGR+OoV11chJTCxd24owRPvyz1T42JXIU9fwE=","files":[{"name":"map_91.dat","fileNameSalt":"vtUOlQQ6Vt4bbpNexyBvog==","size":693,"iv":"ylk0R79Fq2/eDccVi8GTfA==","storage":["orglightcont01"]},{"name":"OutPutStream.mp3","fileNameSalt":"3qw+LauZUc13xh6fwnMHnQ==","size":53037,"iv":"40sIX0VtASM9man5XkM64Q==","storage":["orglightcont01"]},{"name":"wait.mp4","fileNameSalt":"1wnuehjUPI895TgCy2e/EQ==","size":1210962,"iv":"fCM6WG1PxOu9ieEjPuQiFg==","storage":["orglightcont01"]},{"name":"loader.gif","fileNameSalt":"05nNafgD/SNPg+y/MzI2DA==","size":89594,"iv":"GLoY4/PybIz72Sbz9jFgRQ==","storage":["orglightcont01"]},{"name":"extensions.mp4","fileNameSalt":"BYam5baM4WUPdaQZDu2eyw==","size":1501471,"iv":"ugCXHr7yVsY8rnB1znEfew==","storage":["orglightcont01"]},{"name":"007.mp4","fileNameSalt":"12jEfd3/dT9akqzdWn1vsg==","size":1742707,"iv":"CyKf2OHPABn//ez5L2o3Jg==","storage":["orglightcont01"]}]}]} \ No newline at end of file