diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
10 files changed, 94 insertions, 78 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java new file mode 100644 index 0000000..7f74725 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java @@ -0,0 +1,33 @@ +package com.keuin.kbackupfabric.util; + +import java.io.File; + +public class FilesystemUtil { + + /** + * Get file sizes in bytes. + * @param parentDirectory path to specific file. + * @param fileName file name. + * @return bytes. If failed, return -1. + */ + public static long getFileSizeBytes(String parentDirectory, String fileName) { + long fileSize = -1; + try{ + File backupZipFile = new File(parentDirectory, fileName); + fileSize = backupZipFile.length(); + } catch (SecurityException ignored){ + } + return fileSize; + } + + public static long getFileSizeBytes(String filePath) { + long fileSize = -1; + try{ + File backupZipFile = new File(filePath); + fileSize = backupZipFile.length(); + } catch (SecurityException ignored){ + } + return fileSize; + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/ServerRestartUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ServerRestartUtil.java deleted file mode 100644 index 1642e5c..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/ServerRestartUtil.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.keuin.kbackupfabric.util; - -public class ServerRestartUtil { - - public static void forkAndRestart() { -// Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { -// @Override -// public void run() { -// // Here we restart the minecraft server -// StringBuilder cmd = new StringBuilder(); -// cmd.append(System.getProperty("java.home")).append(File.separator).append("bin").append(File.separator).append("java "); -// for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) { -// cmd.append(jvmArg + " "); -// } -// cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" "); -// cmd.append(MinecraftServer.class.getName()).append(" "); -// for (String arg : args) { -// cmd.append(arg).append(" "); -// } -// Runtime.getRuntime().exec(cmd.toString()); -// System.exit(0); -// } -// })); - } - - private static void startRestartThread() { -// (new Thread(() -> { -// -// -// // kill threads -// Set<Thread> threads = Thread.getAllStackTraces().keySet(); -// Thread currentThread = Thread.currentThread(); -// for (Thread t : threads) { -// if(t != currentThread && t.isAlive()) { -// t.setUncaughtExceptionHandler((t1, e) -> { -// // set empty handler -// }); -// //t.interrupt(); -// try { -// t.setDaemon(true); -// } catch (Exception ignored) { -// } -// -// t.stop(); -// } -// } -// -// try { -// Thread.sleep(5000); -// } catch (InterruptedException ignored) { -// } -// -// // restart Minecraft server -// String[] args = new String[]{}; -// MinecraftServer.main(args); -// })).start(); - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java index 0ca8da3..c670cf1 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/ZipUtil.java @@ -147,9 +147,9 @@ public final class ZipUtil { * @param unzipFilePath 解压后的文件保存的路径 * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含 */ - public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws ZipUtilException, IOException { + public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws IOException { if (zipFilePath.isEmpty() || unzipFilePath.isEmpty()) { - throw new ZipUtilException("Parameter for unzip() contains null."); + throw new IllegalArgumentException("Parameter for unzip() contains null."); } File zipFile = new File(zipFilePath); // 如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径 diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java index 95a32ae..d02ce77 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/BackupType.java @@ -4,18 +4,30 @@ package com.keuin.kbackupfabric.util.backup; * Representing the backup type. * Should only be used in BackupFileNameBuilder and BackupFileNameFormatter */ +@Deprecated public enum BackupType { - PRIMITIVE_ZIP_BACKUP("Primitive Zip Backup"), - OBJECT_TREE_BACKUP("Object Tree Backup"); + PRIMITIVE_ZIP_BACKUP("Primitive Zip Backup", "zip"), + OBJECT_TREE_BACKUP("Object Tree Backup", "incremental"); - private final String name; - BackupType(String name) { + private final String friendlyName; // e.g. Primitive Zip Backup + private final String name; // e.g. zip + + BackupType(String friendlyName, String name) { + this.friendlyName = friendlyName; this.name = name; } + /** + * Get name used in command. + * @return name (such as "zip", "incremental"). + */ + public String getName() { + return name; + } + @Override public String toString() { - return name; + return friendlyName; } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java index 16c598b..f57302c 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/builder/BackupFileNameBuilder.java @@ -1,6 +1,5 @@ package com.keuin.kbackupfabric.util.backup.builder; -import com.sun.istack.internal.NotNull; import java.time.LocalDateTime; @@ -20,6 +19,6 @@ public interface BackupFileNameBuilder { * @param backupName the custom name of this backup. Note that this should be a valid file name in current file system. * @return the backup file name string. */ - String build(@NotNull LocalDateTime time, @NotNull String backupName); + String build(LocalDateTime time, String backupName); } diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java index eae3639..a437629 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/BackupFileNameFormatter.java @@ -1,7 +1,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; import java.time.LocalDateTime; import java.util.regex.Matcher; @@ -9,7 +8,7 @@ import java.util.regex.Pattern; public interface BackupFileNameFormatter { - BackupFileName format(@NotNull String fileName); + BackupFileName format(String fileName); class BackupFileName { public final LocalDateTime time; diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java index e5503a9..08805b2 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/ObjectTreeBackupFileNameFormatter.java @@ -2,8 +2,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; -import com.sun.istack.internal.Nullable; import org.spongepowered.asm.mixin.Overwrite; import java.time.LocalDateTime; @@ -19,13 +17,12 @@ public class ObjectTreeBackupFileNameFormatter implements BackupFileNameFormatte } @Override - public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) { + public BackupFileNameFormatter.BackupFileName format(String fileName) { LocalDateTime time = getTime(fileName); String name = getBackupName(fileName); return new BackupFileNameFormatter.BackupFileName(time,name); } - @Nullable private LocalDateTime getTime(String fileName) { Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); if (matcher.find()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java index 40450eb..2d50d17 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/formatter/PrimitiveZipBackupFileNameFormatter.java @@ -2,8 +2,6 @@ package com.keuin.kbackupfabric.util.backup.formatter; import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; -import com.sun.istack.internal.NotNull; -import com.sun.istack.internal.Nullable; import java.time.LocalDateTime; import java.util.regex.Matcher; @@ -18,13 +16,12 @@ public class PrimitiveZipBackupFileNameFormatter implements BackupFileNameFormat } @Override - public BackupFileNameFormatter.BackupFileName format(@NotNull String fileName) { + public BackupFileNameFormatter.BackupFileName format(String fileName) { LocalDateTime time = getTime(fileName); String name = getBackupName(fileName); return new BackupFileNameFormatter.BackupFileName(time,name); } - @Nullable private LocalDateTime getTime(String fileName) { Matcher matcher = Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}").matcher(fileName); if (matcher.find()) { diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java new file mode 100644 index 0000000..320d9bf --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java @@ -0,0 +1,36 @@ +package com.keuin.kbackupfabric.util.backup.suggestion; + +import com.keuin.kbackupfabric.util.backup.BackupType; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.ServerCommandSource; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.concurrent.CompletableFuture; + +public class BackupMethodSuggestionProvider { + + private static final List<String> suggestions = Arrays.asList( + BackupType.OBJECT_TREE_BACKUP.getName(), + BackupType.PRIMITIVE_ZIP_BACKUP.getName() + ); // All backup methods + + public static SuggestionProvider<ServerCommandSource> getProvider() { + return (context, builder) -> getCompletableFuture(builder); + } + + private static CompletableFuture<Suggestions> getCompletableFuture(SuggestionsBuilder builder) { + String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); + for (String string : suggestions) { // Iterate through the supplied list + if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { + builder.suggest(string); // Add every single entry to suggestions list. + } + } + return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java index 4639e99..f6f4056 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/BackupNameSuggestionProvider.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java @@ -1,5 +1,6 @@ -package com.keuin.kbackupfabric.util.backup; +package com.keuin.kbackupfabric.util.backup.suggestion; +import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; |