Correction bugs de suppression de fichiers sur le FFE après upload et récupération, optimisation

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-15 21:10:34 +01:00
parent a4b4929bf6
commit f87c81aba4
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
7 changed files with 37 additions and 21 deletions

View File

@ -43,6 +43,14 @@ public class RetrieveOkRule extends ProtocolReader {
this.hashedFileContent = hashedFileContent; this.hashedFileContent = hashedFileContent;
} }
public String getFilename() {
return filename;
}
public int getFilesize() {
return filesize;
}
/** /**
* Bitch has bettern than my money * Bitch has bettern than my money
* @param reader Buffer rempli du fichier * @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]); 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 // 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 // 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.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT);

View File

@ -6,6 +6,8 @@ import lightcontainer.utils.FileSender;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class GetFileOkRule extends ProtocolWriter { public class GetFileOkRule extends ProtocolWriter {
// Constants // Constants
@ -47,12 +49,16 @@ public class GetFileOkRule extends ProtocolWriter {
System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize")); System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize"));
FileSender fileSender = new FileSender(storagePath); FileSender fileSender = new FileSender(storagePath);
fileSender.sendFile( fileSender.sendFile(
getContext().getHashedFileName(this.filename), getContext().getDataString("hashedFileName"),
writer, writer,
getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system)
getContext().getAesKey(), getContext().getAesKey(),
getContext().getDataString("fileIV") getContext().getDataString("fileIV")
); );
try {
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, getContext().getDataString("hashedFileName"))));
} catch (IOException ignore) { }
} }
} }

View File

@ -34,8 +34,7 @@ public class SaveFileOkRule extends ProtocolWriter {
String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); String hashedFileName = context.getHashedFileName(context.getDataString("fileName"));
try { try {
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName)));
} catch (IOException e) {} } catch (IOException ignore) {}
return result; return result;
} }
} }

View File

@ -8,18 +8,17 @@ public class FileReceiver {
public FileReceiver(String path) { this.path = path; } public FileReceiver(String path) { this.path = path; }
public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) {
BufferedOutputStream bosFile = null;
try { try {
bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName))); File file = new File(String.format("%s/%s", path, fileName));
if (file.createNewFile()) {
AES_GCM.encryptStream(input, bosFile, fileSize, key, iv); try (BufferedOutputStream bufferedStream = new BufferedOutputStream(new FileOutputStream(file))) {
// AES close l'outputstream mais on le try with ressources au cas ou ;)
File f = new File(String.format("%s/%s", path, fileName)); AES_GCM.encryptStream(input, bufferedStream, fileSize, key, iv);
return (int)f.length(); // TODO change the size to LONG }
}
return (int)file.length(); // TODO change the size to LONG
} catch(IOException | AES_GCM.AesGcmException ex) { } catch(IOException | AES_GCM.AesGcmException ex) {
ex.printStackTrace(); ex.printStackTrace();
if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} }
return -1; return -1;
} }
} }

View File

@ -27,13 +27,12 @@ public class FileSender {
public boolean sendFile(String filename, OutputStream out) throws IOException { public boolean sendFile(String filename, OutputStream out) throws IOException {
BufferedInputStream bisFile; BufferedInputStream bisFile;
int bytesReaded = 0; int bytesReaded = 0;
try { try {
File f = new File(String.format("%s/%s", path, filename)); File file = new File(String.format("%s/%s", path, filename));
long fileSize = f.length(); long fileSize = file.length();
if(f.exists()) { if(file.exists()) {
byte[] buffer = new byte[DEFAULT_BUFFER]; byte[] buffer = new byte[DEFAULT_BUFFER];
bisFile = new BufferedInputStream(new FileInputStream(f)); bisFile = new BufferedInputStream(new FileInputStream(file));
long currentOffset = 0; long currentOffset = 0;
while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) { while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) {
out.write(buffer, 0, bytesReaded); out.flush(); out.write(buffer, 0, bytesReaded); out.flush();
@ -43,7 +42,7 @@ public class FileSender {
return true; return true;
} else } else
return false; return false;
} catch(IOException ex) { } catch(IOException ex) {// todo change
throw ex; throw ex;
} }
} }

View File

@ -87,7 +87,11 @@ public class SHA {
public static String hashFile(String rootPath, String fileName) throws ShaException { public static String hashFile(String rootPath, String fileName) throws ShaException {
try { try {
File file = new File(String.format("%s/%s", rootPath, fileName)); 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) { } catch (Exception e) {
throw new ShaException(e); throw new ShaException(e);
} }

View File

@ -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"]}]}]} {"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"]}]}]}