1
0
mirror of https://github.com/aNNiMON/ffmpegbot synced 2024-09-19 22:54:20 +03:00

Add /clear admin command to clear old files

This commit is contained in:
aNNiMON 2023-01-19 23:27:48 +02:00
parent d723c1c2ec
commit 0905595368
4 changed files with 101 additions and 0 deletions

View File

@ -10,5 +10,6 @@ public class AdminCommandBundle implements CommandBundle<For> {
@Override
public void register(@NotNull CommandRegistry commands) {
commands.register(new RunCommand());
commands.register(new ClearCommand());
}
}

View File

@ -0,0 +1,64 @@
package com.annimon.ffmpegbot.commands.admin;
import com.annimon.ffmpegbot.Permissions;
import com.annimon.ffmpegbot.TextUtils;
import com.annimon.ffmpegbot.session.FilePath;
import com.annimon.tgbotsmodule.commands.TextCommand;
import com.annimon.tgbotsmodule.commands.authority.For;
import com.annimon.tgbotsmodule.commands.context.MessageContext;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.EnumSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.annimon.ffmpegbot.commands.admin.ThrowableFunction.safeFunction;
import static com.annimon.ffmpegbot.commands.admin.ThrowablePredicate.safePredicate;
public class ClearCommand implements TextCommand {
@Override
public String command() {
return "/clear";
}
@SuppressWarnings("unchecked")
@Override
public EnumSet<For> authority() {
return Permissions.SUPERUSERS;
}
@Override
public void accept(@NotNull MessageContext ctx) {
try {
final Path inputPath = Paths.get(FilePath.inputDir());
final Path outputPath = Paths.get(FilePath.outputDir());
final var oneDayAgo = Instant.now().minus(1, ChronoUnit.DAYS);
final var oldFiles = Stream.of(inputPath, outputPath)
.flatMap(safeFunction(Files::list))
.filter(safePredicate(p -> Files.getLastModifiedTime(p).toInstant().isBefore(oneDayAgo)))
.collect(Collectors.toSet());
if (oldFiles.isEmpty()) {
ctx.replyToMessage("No files to remove").callAsync(ctx.sender);
} else {
final int count = oldFiles.size();
long size = 0;
for (Path p : oldFiles) {
size += Files.size(p);
Files.delete(p);
}
final String totalSize = TextUtils.readableFileSize(size);
ctx.replyToMessage("Removed %d old files of total size %s".formatted(count, totalSize))
.callAsync(ctx.sender);
}
} catch (IOException e) {
ctx.replyToMessage("Unable to clear directories due to " + e.getMessage()).callAsync(ctx.sender);
}
}
}

View File

@ -0,0 +1,18 @@
package com.annimon.ffmpegbot.commands.admin;
import java.util.function.Function;
public interface ThrowableFunction<T, R> {
R apply(T t) throws Exception;
static <I, O> Function<I, O> safeFunction(ThrowableFunction<I, O> func) {
return value -> {
try {
return func.apply(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
}

View File

@ -0,0 +1,18 @@
package com.annimon.ffmpegbot.commands.admin;
import java.util.function.Predicate;
public interface ThrowablePredicate<T> {
boolean apply(T t) throws Exception;
static <I> Predicate<I> safePredicate(ThrowablePredicate<I> func) {
return value -> {
try {
return func.apply(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
}