From 203ceabdf13dc3bb5bc24b97b804ab4c823e72c6 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 12 Mar 2022 18:10:01 +0100 Subject: [PATCH] =?UTF-8?q?V=C3=A9rification=20du=20nombre=20de=20fichiers?= =?UTF-8?q?=20pr=C3=A9sents=20pour=20un=20utilisateur=20avant=20de=20lui?= =?UTF-8?q?=20permettre=20d'ajouter=20un=20fichier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protocol/rules/reader/SavefileRule.java | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) 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 f9afdad..069e27f 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -56,42 +56,47 @@ public class SavefileRule extends ProtocolReader { super.read(reader); System.out.printf("Sauvegarde du fichier : %s %d\n", filename, size); - try { - FileReceiver fileReceiver = new FileReceiver(storagePath); + if (getContext().canAddFile()) { + try { + FileReceiver fileReceiver = new FileReceiver(storagePath); - // Ajout login devant le nom du fichier - this.filename = getContext().getLogin() + "_" + 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(); + // 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(); + 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(); + // 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); + String fileHash = SHA.hashFile(storagePath, this.filename); - // On met les données de la requête actuelle - getContext().putDataString("fileName", filename); - getContext().putDataInt("size", size); - getContext().putDataString("iv", iv); - getContext().putDataString("fileNameSalt", fileNameSalt); + // On met les données de la requête actuelle + getContext().putDataString("fileName", filename); + 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(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND); + } catch (IOException | SHA.ShaException e) { + this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + e.printStackTrace(); + } + } else { this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - e.printStackTrace(); } } } /** * 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