summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation
diff options
context:
space:
mode:
authorKeuin <[email protected]>2020-08-02 18:31:50 +0800
committerkeuin <[email protected]>2020-08-02 18:31:50 +0800
commitd9c2af035ac2edb68bc8b6a1f94b7c772c3509c8 (patch)
tree4a27dd9a313090ae75d35e4d27d5bdd40b1eb218 /src/main/java/com/keuin/kbackupfabric/operation
parentf09d420df609194a17e9d9ec63d710df8135aa8e (diff)
Refactor & object tree impl. stage 1
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java23
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java6
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java4
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java22
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java42
5 files changed, 81 insertions, 16 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
index 53b05b8..4c08d14 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java
@@ -3,9 +3,12 @@ package com.keuin.kbackupfabric.operation;
import com.keuin.kbackupfabric.exception.ZipUtilException;
import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation;
+import com.keuin.kbackupfabric.operation.backup.BackupMethod;
import com.keuin.kbackupfabric.util.PrintUtil;
-import com.keuin.kbackupfabric.util.ZipUtil;
+import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder;
+import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter;
import com.mojang.brigadier.context.CommandContext;
+import com.sun.istack.internal.NotNull;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.world.World;
@@ -15,7 +18,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*;
+import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*;
import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo;
public class BackupOperation extends InvokableAsyncBlockingOperation {
@@ -23,15 +26,15 @@ public class BackupOperation extends InvokableAsyncBlockingOperation {
private final CommandContext<ServerCommandSource> context;
private final String backupName;
private final Map<World, Boolean> oldWorldsSavingDisabled = new HashMap<>();
- private final BackupMetadata backupMetadata;
+ private final BackupMethod backupMethod;
private long startTime;
- public BackupOperation(CommandContext<ServerCommandSource> context, String backupName, BackupMetadata backupMetadata) {
+ public BackupOperation(@NotNull CommandContext<ServerCommandSource> context, @NotNull String backupName, @NotNull BackupMethod backupMethod, @NotNull BackupFileNameBuilder backupFileNameBuilder, @NotNull BackupFileNameFormatter backupFileNameFormatter) {
super("BackupWorker");
this.context = context;
this.backupName = backupName;
- this.backupMetadata = backupMetadata;
+ this.backupMethod = backupMethod;
}
@Override
@@ -52,11 +55,8 @@ public class BackupOperation extends InvokableAsyncBlockingOperation {
// Make zip
String levelPath = getLevelPath(server);
String backupFileName = getBackupFileName(backupName);
- PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectoryFile.toString()));
- msgInfo(context, "Compressing worlds ...");
- PrintUtil.info("Compressing level ...");
- ZipUtil.makeBackupZip(levelPath, backupSaveDirectoryFile.toString(), backupFileName, backupMetadata);
- File backupZipFile = new File(backupSaveDirectoryFile, backupFileName);
+
+ backupMethod.backup(backupName,levelPath,backupSaveDirectory);
// Restore old autosave switch stat
server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true));
@@ -64,6 +64,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation {
// Print finish message: time elapsed and file size
long timeElapsedMillis = System.currentTimeMillis() - startTime;
String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0);
+ File backupZipFile = new File(backupSaveDirectory, backupFileName);
try {
msgText += String.format(" File size: %s.", humanFileSize(backupZipFile.length()));
} catch (SecurityException ignored) {
@@ -72,7 +73,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation {
} catch (SecurityException e) {
msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory));
- } catch (IOException | ZipUtilException e) {
+ } catch (IOException e) {
msgInfo(context, "Failed to make zip: " + e.getMessage());
}
}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java
index ee1a57e..30fdfc0 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java
@@ -1,7 +1,7 @@
package com.keuin.kbackupfabric.operation;
import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation;
-import com.keuin.kbackupfabric.util.BackupNameSuggestionProvider;
+import com.keuin.kbackupfabric.util.backup.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.util.PrintUtil;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.MinecraftServer;
@@ -10,8 +10,8 @@ import net.minecraft.server.command.ServerCommandSource;
import java.io.File;
import java.io.IOException;
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupFileName;
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupSaveDirectory;
+import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName;
+import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory;
import static com.keuin.kbackupfabric.util.PrintUtil.msgErr;
import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo;
import static org.apache.commons.io.FileUtils.forceDelete;
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
index 6754393..ef6ab2b 100644
--- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
+++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java
@@ -11,8 +11,8 @@ import net.minecraft.server.command.ServerCommandSource;
import java.io.File;
import java.io.IOException;
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupFileName;
-import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupSaveDirectory;
+import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName;
+import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory;
import static org.apache.commons.io.FileUtils.forceDelete;
public class RestoreOperation extends InvokableBlockingOperation {
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java
new file mode 100644
index 0000000..97c609d
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java
@@ -0,0 +1,22 @@
+package com.keuin.kbackupfabric.operation.backup;
+
+import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder;
+import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter;
+import com.sun.istack.internal.NotNull;
+
+import java.io.IOException;
+
+public interface BackupMethod {
+
+ /**
+ * Perform a backup with given method. The backup will be saved as the given name.
+ * Note: real file name is depended by the backup type.
+ * @param backupName the backup name.
+ */
+ void backup(@NotNull String backupName, @NotNull String levelPath, @NotNull String backupSaveDirectory) throws IOException;
+
+ BackupFileNameBuilder getBackupFileNameBuilder();
+
+ BackupFileNameFormatter getBackupFileNameFormatter();
+
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java
new file mode 100644
index 0000000..3c2a608
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java
@@ -0,0 +1,42 @@
+package com.keuin.kbackupfabric.operation.backup;
+
+import com.keuin.kbackupfabric.exception.ZipUtilException;
+import com.keuin.kbackupfabric.metadata.BackupMetadata;
+import com.keuin.kbackupfabric.util.PrintUtil;
+import com.keuin.kbackupfabric.util.ZipUtil;
+import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder;
+import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter;
+
+import java.io.File;
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+
+public class PrimitiveBackupMehtod implements BackupMethod {
+ @Override
+ public boolean backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException {
+ try {
+ String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName);
+ BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), backupName);
+
+ PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory));
+ PrintUtil.info("Compressing level ...");
+ ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata);
+
+ } catch (ZipUtilException exception) {
+ PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted.");
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public BackupFileNameBuilder getBackupFileNameBuilder() {
+ return BackupFileNameBuilder.primitiveZipBackup();
+ }
+
+ @Override
+ public BackupFileNameFormatter getBackupFileNameFormatter() {
+ return BFNF;
+ }
+}