From 07f87d40e5727d6b13d7ed6110206344b31c1cac Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 12 Mar 2022 18:25:21 +0100 Subject: [PATCH] Command FilelistRule --- .../lightcontainer/domains/client/Context.java | 5 +++++ .../protocol/rules/reader/FilelistRule.java | 10 +++++++++- .../main/java/lightcontainer/storage/AppData.java | 9 +++++++++ .../main/java/lightcontainer/storage/User.java | 15 ++++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index a87ef6a..089474e 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -9,6 +9,7 @@ import lightcontainer.utils.ShaHasher; import java.security.NoSuchAlgorithmException; import java.util.LinkedList; +import java.util.List; import java.util.Set; /** @@ -171,6 +172,10 @@ public class Context { return this.repository.addFileFor(new File(fileName, fileNameSalt, size, iv, Set.of(domain)), getLogin()); } + public List getStringifiedFilesOf() { + return repository.load().getStringifiedFilesOf(login); + } + public boolean canAddFile() { return repository.canAddFile(login); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java index 920b6a9..a1c1da9 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java @@ -5,6 +5,8 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.FilesRule; +import java.util.List; + /** * Règle permettant de récupérer la liste des fichiers d'un utilisateur */ @@ -30,12 +32,18 @@ public class FilelistRule extends ProtocolReader { /** * 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 FilelistRule.Result onExecuted(Context context, String... data) { FilelistRule.Result result = new Result(context); - result.setResultCommand(this.protocolRep.executeWriter(context, FilesRule.NAME, "endbenja.txt!500"), ResultCmdReceiver.CLIENT); + List files = context.getStringifiedFilesOf(); + if (files != null) { + result.setResultCommand(this.protocolRep.executeWriter(context, FilesRule.NAME, files.toArray(new String[0])), ResultCmdReceiver.CLIENT); + } else { + result.setResultCommand(this.protocolRep.executeWriter(context, FilesRule.NAME), ResultCmdReceiver.CLIENT); + } return result; } } diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java index 474c53b..1a712b6 100644 --- a/app/src/main/java/lightcontainer/storage/AppData.java +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -2,6 +2,7 @@ package lightcontainer.storage; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; /** @@ -67,6 +68,14 @@ public class AppData { return this.users.get(userName); } + public List getStringifiedFilesOf(String login) { + User user = users.get(login); + if (user != null) { + return user.getFilesWithSize(); + } + return null; + } + /** * Use this method when a user signs up. diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 4b1c1d0..0db77b0 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -1,6 +1,8 @@ package lightcontainer.storage; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Map; /** @@ -46,6 +48,17 @@ public class User { return files.values().iterator(); } + public List getFilesWithSize() { + Iterator fileIterator = fileIterator(); + List result = new ArrayList<>(); + while (fileIterator.hasNext()) { + File file = fileIterator.next(); + String stringify = " " + file.getName() + "!" + file.getSize(); + result.add(stringify); + } + return result; + } + public File getFile(String fileName) { return this.files.get(fileName); } @@ -63,7 +76,7 @@ public class User { } public boolean canAddFile() { - return this.files.size() < 50; + return this.files.size() <= 50; } /**