diff --git a/core/src/main/java/com/halushko/kinocat/core/Commands.java b/core/src/main/java/com/halushko/kinocat/core/Commands.java index 5b45990..1682136 100644 --- a/core/src/main/java/com/halushko/kinocat/core/Commands.java +++ b/core/src/main/java/com/halushko/kinocat/core/Commands.java @@ -23,5 +23,6 @@ interface Torrent { interface File { String SELECT_DESTINATION = "/start_"; + String SHOW_FREE_SPACE = "/space"; } } diff --git a/core/src/main/java/com/halushko/kinocat/core/Queues.java b/core/src/main/java/com/halushko/kinocat/core/Queues.java index d455bcf..eced973 100644 --- a/core/src/main/java/com/halushko/kinocat/core/Queues.java +++ b/core/src/main/java/com/halushko/kinocat/core/Queues.java @@ -31,5 +31,6 @@ interface Text { interface File { String CHOOSE_THE_DESTINATION = "FILE_CHOOSE_THE_DESTINATION"; String MOVE_TO_FOLDER = "FILE_MOVE_TO_FOLDER"; + String SHOW_FREE_SPACE = "FILE_SHOW_FREE_SPACE"; } } diff --git a/file/src/main/java/com/halushko/kinocat/file/CheckDiskFreeSpace.java b/file/src/main/java/com/halushko/kinocat/file/CheckDiskFreeSpace.java new file mode 100644 index 0000000..9e76ba3 --- /dev/null +++ b/file/src/main/java/com/halushko/kinocat/file/CheckDiskFreeSpace.java @@ -0,0 +1,50 @@ +package com.halushko.kinocat.file; + +import com.halushko.kinocat.core.Queues; +import com.halushko.kinocat.core.handlers.input.CliCommandExecutor; +import com.halushko.kinocat.core.rabbit.SmartJson; +import lombok.extern.slf4j.Slf4j; +import lombok.val; + +import java.util.List; + +@Slf4j +public class CheckDiskFreeSpace extends CliCommandExecutor { + @Override + protected String getResultString(List lines, SmartJson rabbitMessage) { + log.debug(String.format("[CheckDiskFreeSpace] [%s]", String.join(", ", lines))); + StringBuilder sb = new StringBuilder("Вільного місця у сховищі:"); + for (val device : Constants.FOLDERS.entrySet()) { + val key = device.getKey(); + if (Constants.DEVICES.containsKey(key)) { + val value = Constants.DEVICES.get(key); + for (String line : lines) { + if (line.matches(value + ".*")) { + sb.append("\n") + .append(key) + .append(": ") + .append( + line.replaceAll("^\\S+\\s+\\S+\\s+\\S+\\s+", "") + .replaceAll("\\S+\\s+\\S+\\s*$", "") + ); + } + } + } + } + + return sb.toString(); + } + + @Override + protected String getQueue() { + return Queues.File.CHOOSE_THE_DESTINATION; + } + + @Override + protected String[] getScript(SmartJson rabbitMessage) { + return new String[]{ + "/usr/bin/df", + "-h" + }; + } +} diff --git a/file/src/main/java/com/halushko/kinocat/file/Constants.java b/file/src/main/java/com/halushko/kinocat/file/Constants.java index a57a491..47f7b25 100644 --- a/file/src/main/java/com/halushko/kinocat/file/Constants.java +++ b/file/src/main/java/com/halushko/kinocat/file/Constants.java @@ -9,8 +9,29 @@ public interface Constants { String EMPTY_SERVICE_DEFAULT_NAME = "main"; Map FOLDERS = new FoldersProcessor(System.getenv("TORRENT_IP")) .values.keySet().stream() - .collect(Collectors.toMap(key -> key, key -> String.format(PATH_TO_DESTINATION_FOLDER + "%s", !key.isEmpty() ? "_" + key : ""))); + .collect( + Collectors.toMap( + key -> key, + key -> String.format(PATH_TO_DESTINATION_FOLDER + "%s", !key.isEmpty() ? "_" + key : "") + ) + ); String NAME_LINE = "^\\s+Name:\\s+"; String SIZE_LINE = "^\\s+Total Size:\\s+"; + Map DEVICES = new DevicesProcessor(System.getenv("TORRENT_IP")) + .values.entrySet().stream() + .collect( + Collectors.toMap( + Map.Entry::getKey, + value -> value.getValue().get("folder") + ) + ) + .entrySet().stream() + .filter(x -> !x.getValue().isEmpty()) + .collect( + Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue + ) + ); } diff --git a/file/src/main/java/com/halushko/kinocat/file/DevicesProcessor.java b/file/src/main/java/com/halushko/kinocat/file/DevicesProcessor.java new file mode 100644 index 0000000..223457d --- /dev/null +++ b/file/src/main/java/com/halushko/kinocat/file/DevicesProcessor.java @@ -0,0 +1,32 @@ +package com.halushko.kinocat.file; + +import com.halushko.kinocat.core.JsonConstants; +import com.halushko.kinocat.core.prcessors.ServicesInfoProcessor; +import com.halushko.kinocat.core.prcessors.ValueProcessor; + +import java.util.ArrayList; +import java.util.List; + +public class DevicesProcessor extends ServicesInfoProcessor { + + public DevicesProcessor(String json) { + super(json); + } + + @Override + public ValueProcessor getNameProcessor() { + return new ValueProcessor(JsonConstants.WebKeys.KEY_NAME, Constants.EMPTY_SERVICE_DEFAULT_NAME); + } + + @Override + public List getServiceProcessors() { + return new ArrayList<>() {{ + this.add(new ValueProcessor("folder", "")); + }}; + } + + @Override + public String getUrlTemplate() { + return ""; + } +} diff --git a/file/src/main/java/com/halushko/kinocat/file/FoldersProcessor.java b/file/src/main/java/com/halushko/kinocat/file/FoldersProcessor.java index a0dc56a..a82133f 100644 --- a/file/src/main/java/com/halushko/kinocat/file/FoldersProcessor.java +++ b/file/src/main/java/com/halushko/kinocat/file/FoldersProcessor.java @@ -15,7 +15,7 @@ public FoldersProcessor(String json) { @Override public ValueProcessor getNameProcessor() { - return new ValueProcessor(JsonConstants.WebKeys.KEY_NAME, ""); + return new ValueProcessor(JsonConstants.WebKeys.KEY_NAME, Constants.EMPTY_SERVICE_DEFAULT_NAME); } @Override diff --git a/file/src/main/java/com/halushko/kinocat/file/Main.java b/file/src/main/java/com/halushko/kinocat/file/Main.java index 2126a0c..3f8ea4f 100644 --- a/file/src/main/java/com/halushko/kinocat/file/Main.java +++ b/file/src/main/java/com/halushko/kinocat/file/Main.java @@ -7,5 +7,6 @@ public static void main(String[] args) { new UserMessageHandler().run(); new PrintDestinations().run(); new MoveToDestinationFolder().run(); + new CheckDiskFreeSpace().run(); } } \ No newline at end of file diff --git a/file/src/main/java/com/halushko/kinocat/file/PrintDestinations.java b/file/src/main/java/com/halushko/kinocat/file/PrintDestinations.java index eb58710..d45672c 100644 --- a/file/src/main/java/com/halushko/kinocat/file/PrintDestinations.java +++ b/file/src/main/java/com/halushko/kinocat/file/PrintDestinations.java @@ -14,7 +14,7 @@ public class PrintDestinations extends CliCommandExecutor { @Override protected String getResultString(List lines, SmartJson rabbitMessage) { - log.debug(String.format("[getResultString] [%s]", String.join(", ", lines))); + log.debug(String.format("[PrintDestinations] [%s]", String.join(", ", lines))); String[] result = new String[4]; result[0] = "("; result[2] = ") "; @@ -32,9 +32,9 @@ protected String getResultString(List lines, SmartJson rabbitMessage) { Constants.FOLDERS.keySet().stream() .map(folder -> String.format("\n%s: %s%s_%s", - getServiceLable(folder), + folder, Commands.File.SELECT_DESTINATION, - getServiceLable(folder), + folder, fileName ) ) @@ -42,13 +42,9 @@ protected String getResultString(List lines, SmartJson rabbitMessage) { ); } - private static String getServiceLable(String folder) { - return folder.isEmpty() ? Constants.EMPTY_SERVICE_DEFAULT_NAME : folder; - } - @Override protected String getQueue() { - return Queues.File.CHOOSE_THE_DESTINATION; + return Queues.File.SHOW_FREE_SPACE; } @Override diff --git a/text/src/main/java/com/halushko/kinocat/text/Constants.java b/text/src/main/java/com/halushko/kinocat/text/Constants.java index 232b9ce..dc1b4f1 100644 --- a/text/src/main/java/com/halushko/kinocat/text/Constants.java +++ b/text/src/main/java/com/halushko/kinocat/text/Constants.java @@ -86,5 +86,10 @@ public interface Constants { Queues.File.MOVE_TO_FOLDER, "<папка> <файл> обрати в яку папку буде завантаження" ); + + addValue(Commands.File.SHOW_FREE_SPACE, + Queues.File.SHOW_FREE_SPACE, + "відобразити вільне місце у сховищі" + ); }}; }