114 lines
4.3 KiB
Java
114 lines
4.3 KiB
Java
package lightcontainer.protocol.rules.reader;
|
|
|
|
import lightcontainer.domains.client.Context;
|
|
import lightcontainer.interfaces.ProtocolRepository;
|
|
import lightcontainer.protocol.ProtocolReader;
|
|
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
|
import lightcontainer.protocol.rules.writer.SaveFileOkRule;
|
|
import lightcontainer.protocol.rules.writer.SendfileRule;
|
|
import lightcontainer.utils.*;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
/**
|
|
* Règle permettant de sauvegarder un fichier sur le SBE.
|
|
* Celui-ci va chiffre le contenu du fichier à l'aide de AES.
|
|
*/
|
|
public class SavefileRule extends ProtocolReader {
|
|
// Constants
|
|
private static final String PATTERN = "^SAVEFILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
|
|
|
|
private static final String NAME = "SAVEFILE";
|
|
|
|
private static final int FILE_NAME = 0; // Index file name.
|
|
private static final int FILE_SIZE = 1; // Index file size.
|
|
|
|
private ProtocolRepository protocolRep;
|
|
private String storagePath;
|
|
|
|
// Constructor
|
|
public SavefileRule(ProtocolRepository protocolRep, String storagePath) {
|
|
super(NAME, PATTERN);
|
|
this.protocolRep = protocolRep;
|
|
this.storagePath = storagePath;
|
|
}
|
|
|
|
public class Result extends ProtocolResult {
|
|
// Variables
|
|
private String filename;
|
|
private int size;
|
|
|
|
// Construct
|
|
public Result(Context context, String filename, int size) {
|
|
super(context);
|
|
this.filename = filename;
|
|
this.size = size;
|
|
}
|
|
|
|
@Override
|
|
public void read(InputStream reader) {
|
|
super.read(reader);
|
|
System.out.printf("Sauvegarde du fichier : %s %d\n", filename, size);
|
|
|
|
if (getContext().canAddFile()) {
|
|
try {
|
|
FileReceiver fileReceiver = new FileReceiver(storagePath);
|
|
getContext().putDataString("fileName", this.filename);
|
|
|
|
// Ajout login devant le nom du fichier
|
|
this.filename = getContext().getLogin() + "_" + this.filename;
|
|
|
|
// Hashage du nom du fichier
|
|
ShaHasher hasher = new ShaHasher(this.filename);
|
|
this.filename = hasher.nextHashing();
|
|
String fileNameSalt = hasher.getSalt();
|
|
|
|
String key = getContext().getAesKey();
|
|
String iv = AES_GCM.generateIV();
|
|
|
|
// retrieve file and new size
|
|
int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv);
|
|
if (encryptedFileSize < 0) throw new IOException();
|
|
|
|
String fileHash = SHA.hashFile(storagePath, this.filename);
|
|
|
|
// On met les données de la requête actuelle
|
|
getContext().putDataInt("size", size);
|
|
getContext().putDataString("iv", iv);
|
|
getContext().putDataString("fileNameSalt", fileNameSalt);
|
|
|
|
this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND);
|
|
} catch (IOException | SHA.ShaException e) {
|
|
this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
|
|
}
|
|
} else {
|
|
this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cette méthode est appelée lors de l'exécution de la règle
|
|
*
|
|
* @param data Paramètres pour créer la commande.
|
|
*/
|
|
@Override
|
|
protected SavefileRule.Result onExecuted(Context context, String... data) {
|
|
SavefileRule.Result result = new SavefileRule.Result(context, data[FILE_NAME], Integer.parseInt(data[FILE_SIZE]));
|
|
|
|
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
protected ProtocolReader.ProtocolResult onError(Context context) {
|
|
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
|
|
// Commande renvoyée en cas d'erreur
|
|
result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
|
|
return result;
|
|
}
|
|
}
|