Command FilelistRule

This commit is contained in:
Maximilien LEDOUX 2022-03-12 18:25:21 +01:00
parent 203ceabdf1
commit 07f87d40e5
4 changed files with 37 additions and 2 deletions

View File

@ -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<String> getStringifiedFilesOf() {
return repository.load().getStringifiedFilesOf(login);
}
public boolean canAddFile() {
return repository.canAddFile(login);
}

View File

@ -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<String> 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;
}
}

View File

@ -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<String> getStringifiedFilesOf(String login) {
User user = users.get(login);
if (user != null) {
return user.getFilesWithSize();
}
return null;
}
/**
* Use this method when a user signs up.

View File

@ -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<String> getFilesWithSize() {
Iterator<File> fileIterator = fileIterator();
List<String> 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;
}
/**