Skip to content

Commit

Permalink
- add free space display
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Galushko committed Jul 1, 2024
1 parent 9c61d17 commit c7a641c
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/com/halushko/kinocat/core/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ interface Torrent {

interface File {
String SELECT_DESTINATION = "/start_";
String SHOW_FREE_SPACE = "/space";
}
}
1 change: 1 addition & 0 deletions core/src/main/java/com/halushko/kinocat/core/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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"
};
}
}
23 changes: 22 additions & 1 deletion file/src/main/java/com/halushko/kinocat/file/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@ public interface Constants {
String EMPTY_SERVICE_DEFAULT_NAME = "main";
Map<String, String> 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<String, String> 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
)
);
}
32 changes: 32 additions & 0 deletions file/src/main/java/com/halushko/kinocat/file/DevicesProcessor.java
Original file line number Diff line number Diff line change
@@ -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<ValueProcessor> getServiceProcessors() {
return new ArrayList<>() {{
this.add(new ValueProcessor("folder", ""));
}};
}

@Override
public String getUrlTemplate() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions file/src/main/java/com/halushko/kinocat/file/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public static void main(String[] args) {
new UserMessageHandler().run();
new PrintDestinations().run();
new MoveToDestinationFolder().run();
new CheckDiskFreeSpace().run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class PrintDestinations extends CliCommandExecutor {
@Override
protected String getResultString(List<String> 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] = ") ";
Expand All @@ -32,23 +32,19 @@ protected String getResultString(List<String> 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
)
)
.collect(Collectors.joining(""))
);
}

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
Expand Down
5 changes: 5 additions & 0 deletions text/src/main/java/com/halushko/kinocat/text/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,10 @@ public interface Constants {
Queues.File.MOVE_TO_FOLDER,
"<папка> <файл> обрати в яку папку буде завантаження"
);

addValue(Commands.File.SHOW_FREE_SPACE,
Queues.File.SHOW_FREE_SPACE,
"відобразити вільне місце у сховищі"
);
}};
}

0 comments on commit c7a641c

Please sign in to comment.