diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java | 1 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java | 11 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java | 3 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/ui/BackupInfo.java | 34 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java | 64 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/ui/IncrementalBackupInfo.java | 68 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java (renamed from src/main/java/com/keuin/kbackupfabric/KBCommands.java) | 229 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java | 78 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/DateUtil.java | 13 | ||||
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java | 11 |
10 files changed, 419 insertions, 93 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java index b9baf44..a27753f 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java +++ b/src/main/java/com/keuin/kbackupfabric/KBCommandsRegister.java @@ -1,6 +1,7 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; +import com.keuin.kbackupfabric.ui.KBCommands; import com.keuin.kbackupfabric.util.PermissionValidator; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.StringArgumentType; diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index e1d5cbd..8992d17 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -4,6 +4,8 @@ import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.metadata.MetadataHolder; +import com.keuin.kbackupfabric.ui.KBCommands; +import com.keuin.kbackupfabric.util.DateUtil; import com.keuin.kbackupfabric.util.PrintUtil; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.server.ServerStartCallback; @@ -15,8 +17,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; -import java.text.SimpleDateFormat; -import java.util.Date; import static org.apache.commons.io.FileUtils.forceDelete; @@ -39,11 +39,14 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback public void onStartServer(MinecraftServer server) { if (!(server instanceof MinecraftDedicatedServer)) - throw new RuntimeException("KBackup is a server-side-only plugin. Please do not use it in Minecraft client."); + throw new RuntimeException("KBackup is a server-side-only plugin. Please do not use it in client-side."); // Initialize player manager reference PrintUtil.setPlayerManager(server.getPlayerManager()); + // Initialize backup manager server reference + KBCommands.setServer(server); + // Update backup suggestion list BackupNameSuggestionProvider.setBackupSaveDirectory(BackupFilesystemUtil.getBackupSaveDirectory(server).getPath()); @@ -64,7 +67,7 @@ public final class KBPluginEvents implements ModInitializer, ServerStartCallback MetadataHolder.setMetadata(metadata); PrintUtil.info("Restored world from a previous backup:"); PrintUtil.info("Backup Name: " + metadata.getBackupName()); - PrintUtil.info("Create Time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(metadata.getBackupTime()))); + PrintUtil.info("Create Time: " + DateUtil.fromEpochMillis(metadata.getBackupTime())); // Delete metadata file if (!metadataFile.delete()) { diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index ff75d8c..0b15a84 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -18,6 +18,9 @@ import java.util.logging.Logger; import static org.apache.commons.io.FileUtils.forceDelete; +/** + * Managing the base storing all collection objects. + */ public class IncrementalBackupStorageManager { private final Path backupStorageBase; diff --git a/src/main/java/com/keuin/kbackupfabric/ui/BackupInfo.java b/src/main/java/com/keuin/kbackupfabric/ui/BackupInfo.java new file mode 100644 index 0000000..cf537f9 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/ui/BackupInfo.java @@ -0,0 +1,34 @@ +package com.keuin.kbackupfabric.ui; + +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; +import com.keuin.kbackupfabric.util.DateUtil; +import net.minecraft.server.MinecraftServer; + +import java.time.LocalDateTime; + +/** + * Used in UI part. Holds necessary information for displaying a backup. + */ +public interface BackupInfo { + String getName(); + + LocalDateTime getCreationTime(); + + long getSizeBytes(); + + String getType(); + + default String getCanonicalName() { + return getName() + "-" + DateUtil.getString(getCreationTime()); + } + + ConfiguredBackupMethod createConfiguredBackupMethod(MinecraftServer server); + + /** + * This is depreciated. But eliminating all usages needs to refactor the UI code. + * + * @return the backup file name. + */ + @Deprecated + String getBackupFileName(); +}
\ No newline at end of file diff --git a/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java b/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java new file mode 100644 index 0000000..abd7d5b --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java @@ -0,0 +1,64 @@ +package com.keuin.kbackupfabric.ui; + +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; + +/** + * Holding all types of backups for the user interaction logic. + */ +public class BackupManager { + + private final File backupStorageDirectory; + + public BackupManager(File backupStorageDirectory) { + this.backupStorageDirectory = backupStorageDirectory; + } + + public BackupManager(String backupStorageDirectory) { + this.backupStorageDirectory = new File(backupStorageDirectory); + } + + /** + * Get available backups in the disk. + * + * @return all backups. + */ + public Iterable<BackupInfo> getAllBackups() { + return new Iterable<BackupInfo>() { + @NotNull + @Override + public Iterator<BackupInfo> iterator() { + return new Iterator<BackupInfo>() { + private final Iterator<File> fileIterator = Arrays.stream(backupStorageDirectory.listFiles()).filter(file -> { + String name = file.getName().toLowerCase(); + return name.endsWith(".zip") || name.endsWith(".kbi"); + }).iterator(); + + @Override + public boolean hasNext() { + return fileIterator.hasNext(); + } + + @Override + public BackupInfo next() { + try { + File backupFile = fileIterator.next(); + String fileName = backupFile.getName().toLowerCase(); + if (fileName.endsWith(".zip")) + return PrimitiveBackupInfo.fromFile(backupFile); + if (fileName.endsWith(".kbi")) + return IncrementalBackupInfo.fromFile(backupFile); + throw new RuntimeException("Invalid backup file extname"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }; + } + }; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/ui/IncrementalBackupInfo.java b/src/main/java/com/keuin/kbackupfabric/ui/IncrementalBackupInfo.java new file mode 100644 index 0000000..89d7fbf --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/ui/IncrementalBackupInfo.java @@ -0,0 +1,68 @@ +package com.keuin.kbackupfabric.ui; + +import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer; +import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; +import net.minecraft.server.MinecraftServer; + +import java.io.File; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.ZoneId; + +/** + * Used in UI part. + */ +public class IncrementalBackupInfo implements BackupInfo { + private final String name; + private final LocalDateTime creationTime; + private final long sizeBytes; + private final String fileName; + + private IncrementalBackupInfo(String name, LocalDateTime creationTime, long sizeBytes, String fileName) { + this.name = name; + this.creationTime = creationTime; + this.sizeBytes = sizeBytes; + this.fileName = fileName; + } + + public static IncrementalBackupInfo fromFile(File indexFile) throws IOException { + SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(indexFile); + return new IncrementalBackupInfo( + info.getBackupName(), + info.getBackupTime().withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime(), + info.getTotalSizeBytes(), + indexFile.getName() + ); + } + + @Override + public String getName() { + return name; + } + + @Override + public LocalDateTime getCreationTime() { + return creationTime; + } + + @Override + public long getSizeBytes() { + return sizeBytes; + } + + @Override + public String getType() { + return "Incremental"; + } + + @Override + public ConfiguredBackupMethod createConfiguredBackupMethod(MinecraftServer server) { + throw new RuntimeException("not implemented"); + } + + @Override + public String getBackupFileName() { + return fileName; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/KBCommands.java b/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java index fe5ea7f..87f667d 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBCommands.java +++ b/src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java @@ -1,8 +1,6 @@ -package com.keuin.kbackupfabric; +package com.keuin.kbackupfabric.ui; import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; -import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer; -import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup; import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; import com.keuin.kbackupfabric.backup.name.PrimitiveBackupFileNameEncoder; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; @@ -21,14 +19,11 @@ import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; -import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Comparator; import java.util.List; -import java.util.Objects; -import java.util.function.Function; import static com.keuin.kbackupfabric.backup.BackupFilesystemUtil.*; import static com.keuin.kbackupfabric.util.PrintUtil.*; @@ -41,12 +36,35 @@ public final class KBCommands { private static final String DEFAULT_BACKUP_NAME = "noname"; private static boolean notifiedPreviousRestoration = false; + // don't access it directly + private static MinecraftServer server; + private static BackupManager backupManager; + private static final Object managerCreatorLock = new Object(); + //private static final Logger LOGGER = LogManager.getLogger(); - private static final List<String> backupFileNameList = new ArrayList<>(); // index -> backupName + private static final List<BackupInfo> backupList = new ArrayList<>(); // index -> backupName private static Invokable pendingOperation = null; //private static BackupMethod activatedBackupMethod = new PrimitiveBackupMethod(); // The backup method we currently using + public static void setServer(MinecraftServer server) { + KBCommands.server = server; + } + + private static MinecraftServer getServer() { + if (server != null) + return server; + throw new IllegalStateException("server is not initialized."); + } + + private static BackupManager getBackupManager() { + synchronized (managerCreatorLock) { + if (backupManager == null) + backupManager = new BackupManager(getBackupSaveDirectory(getServer())); + return backupManager; + } + } + /** * Print the help menu. * @@ -75,11 +93,24 @@ public final class KBCommands { if (MetadataHolder.hasMetadata() && !notifiedPreviousRestoration) { // Output metadata info notifiedPreviousRestoration = true; - msgStress(context, "Restored from backup " + MetadataHolder.getMetadata().getBackupName()); + msgStress(context, "Restored from backup " + + MetadataHolder.getMetadata().getBackupName() + " (created at " + + DateUtil.fromEpochMillis(MetadataHolder.getMetadata().getBackupTime()) + + ")"); } return statCode; } + private static void updateBackupList() { + synchronized (backupList) { + backupList.clear(); + List<BackupInfo> list = new ArrayList<>(); + getBackupManager().getAllBackups().forEach(list::add); + list.sort(Comparator.comparing(BackupInfo::getCreationTime).reversed()); + backupList.addAll(list); + } + } + /** * List all existing backups. * @@ -91,47 +122,73 @@ public final class KBCommands { // that's enough. // TODO: Show real name and size and etc info for incremental backup // TODO: Show concrete info from metadata for `.zip` backup - MinecraftServer server = context.getSource().getMinecraftServer(); +// MinecraftServer server = context.getSource().getMinecraftServer(); // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory} - File[] files = getBackupSaveDirectory(server).listFiles( - (dir, name) -> dir.isDirectory() && - (name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) - || name.toLowerCase().endsWith(".kbi")) - ); - - Function<File, String> backupInformationProvider = file -> { - Objects.requireNonNull(file); - if (file.getName().toLowerCase().endsWith(".zip")) - return getPrimitiveBackupInformationString(file.getName(), file.length()); - // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory} - else if (file.getName().toLowerCase().endsWith(".kbi")) - return getIncrementalBackupInformationString(file); - return file.getName(); - }; - - synchronized (backupFileNameList) { - backupFileNameList.clear(); - if (files != null) { - if (files.length != 0) { - msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)"); - } else { - msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); - } - int i = 0; - for (File file : files) { - ++i; - String backupFileName = file.getName(); - msgInfo(context, String.format("[%d] %s", i, backupInformationProvider.apply(file))); - backupFileNameList.add(backupFileName); - } - } else { - msgErr(context, "Error: failed to list files in backup folder."); +// File[] files = getBackupSaveDirectory(server).listFiles( +// (dir, name) -> dir.isDirectory() && +// (name.toLowerCase().endsWith(".zip") && name.toLowerCase().startsWith(getBackupFileNamePrefix()) +// || name.toLowerCase().endsWith(".kbi")) +// ); + +// Function<File, String> backupInformationProvider = file -> { +// Objects.requireNonNull(file); +// if (file.getName().toLowerCase().endsWith(".zip")) +// return getPrimitiveBackupInformationString(file.getName(), file.length()); +// // TODO: refactor this to use {@link ObjectCollectionSerializer#fromDirectory} +// else if (file.getName().toLowerCase().endsWith(".kbi")) +// return getIncrementalBackupInformationString(file); +// return file.getName(); +// }; + + updateBackupList(); + synchronized (backupList) { + if (backupList.isEmpty()) + msgInfo(context, "There is no backup available. To make a new backup, use `/kb backup`."); + else + msgInfo(context, "Available backups:"); + for (int i = backupList.size() - 1; i >= 0; --i) { + BackupInfo info = backupList.get(i); + printBackupInfo(context, info, i); } +// if (files != null) { +// if (files.length != 0) { +// msgInfo(context, "Available backups: (file is not checked, manipulation may affect this plugin)"); +// } else { +// msgInfo(context, "There are no available backups. To make a new backup, run /kb backup."); +// } +// int i = 0; +// for (File file : files) { +// ++i; +// String backupFileName = file.getName(); +// msgInfo(context, String.format("[%d] %s", i, backupInformationProvider.apply(file))); +// backupFileNameList.add(backupFileName); +// } +// } else { +// msgErr(context, "Error: failed to list files in backup folder."); +// } } return SUCCESS; } /** + * Print backup information. + * + * @param context the context. + * @param info the info. + * @param i the index, starting from 0. + */ + private static void printBackupInfo(CommandContext<ServerCommandSource> context, BackupInfo info, int i) { + msgInfo(context, String.format( + "[%d] (%s) %s (%s) %s", + i + 1, + info.getType(), + info.getName(), + DateUtil.getPrettyString(info.getCreationTime()), + (info.getSizeBytes() > 0) ? BackupFilesystemUtil.getFriendlyFileSizeString(info.getSizeBytes()) : "" + )); + } + + /** * Backup with context parameter backupName. * * @param context the context. @@ -245,7 +302,6 @@ public final class KBCommands { // Detect backup type - // Update pending task //pendingOperation = AbstractConfirmableOperation.createRestoreOperation(context, backupName); // File backupFile = new File(getBackupSaveDirectory(server), getBackupFileName(backupName)); @@ -365,29 +421,32 @@ public final class KBCommands { // FIXME: This breaks after adding incremental backup try { // List all backups - MinecraftServer server = context.getSource().getMinecraftServer(); - List<File> files = Arrays.asList(Objects.requireNonNull(getBackupSaveDirectory(server).listFiles())); - files.removeIf(f -> !f.getName().startsWith(BackupFilesystemUtil.getBackupFileNamePrefix())); - files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName()))); - File prevBackupFile = files.get(0); - String backupFileName = prevBackupFile.getName(); - int i; - synchronized (backupFileNameList) { - i = backupFileNameList.indexOf(backupFileName); - if (i == -1) { - backupFileNameList.add(backupFileName); - i = backupFileNameList.size(); + updateBackupList(); +// MinecraftServer server = context.getSource().getMinecraftServer(); +// List<File> files = Arrays.asList(Objects.requireNonNull(getBackupSaveDirectory(server).listFiles())); +// files.removeIf(f -> !f.getName().startsWith(BackupFilesystemUtil.getBackupFileNamePrefix())); +// files.sort((x, y) -> (int) (BackupFilesystemUtil.getBackupTimeFromBackupFileName(y.getName()) - BackupFilesystemUtil.getBackupTimeFromBackupFileName(x.getName()))); +// File prevBackupFile = files.get(0); +// String backupFileName = prevBackupFile.getName(); +// int i; +// synchronized (backupList) { +// i = backupList.indexOf(backupFileName); +// if (i == -1) { +// backupList.add(backupFileName); +// i = backupList.size(); +// } else { +// ++i; +// } +// } + synchronized (backupList) { + if (!backupList.isEmpty()) { + BackupInfo info = backupList.get(0); + msgInfo(context, "The most recent backup:"); + printBackupInfo(context, info, 0); } else { - ++i; + msgInfo(context, "There is no backup available."); } } - msgInfo(context, String.format( - "The most recent backup: [%d] %s", - i, - getPrimitiveBackupInformationString(backupFileName, prevBackupFile.length()) - )); - } catch (NullPointerException e) { - msgInfo(context, "There are no backups available."); } catch (SecurityException ignored) { msgErr(context, "Failed to read file."); return FAILED; @@ -395,26 +454,26 @@ public final class KBCommands { return SUCCESS; } - private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { - return String.format( - "(ZIP) %s , size: %s", - PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName), - getFriendlyFileSizeString(backupFileSizeBytes) - ); - } +// private static String getPrimitiveBackupInformationString(String backupFileName, long backupFileSizeBytes) { +// return String.format( +// "(ZIP) %s , size: %s", +// PrimitiveBackupFileNameEncoder.INSTANCE.decode(backupFileName), +// getFriendlyFileSizeString(backupFileSizeBytes) +// ); +// } - private static String getIncrementalBackupInformationString(File backupFile) { - try { - SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(backupFile); - return "(Incremental) " + info.getBackupName() - + ", " + DateUtil.getString(info.getBackupTime()) - + ((info.getTotalSizeBytes() > 0) ? - (" size: " + BackupFilesystemUtil.getFriendlyFileSizeString(info.getTotalSizeBytes())) : ""); - } catch (IOException e) { - e.printStackTrace(); - return "(Incremental) " + backupFile.getName(); - } - } +// private static String getIncrementalBackupInformationString(File backupFile) { +// try { +// SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(backupFile); +// return "(Incremental) " + info.getBackupName() +// + ", " + DateUtil.getString(info.getBackupTime()) +// + ((info.getTotalSizeBytes() > 0) ? +// (" size: " + BackupFilesystemUtil.getFriendlyFileSizeString(info.getTotalSizeBytes())) : ""); +// } catch (IOException e) { +// e.printStackTrace(); +// return "(Incremental) " + backupFile.getName(); +// } +// } // /** // * Select the backup method we use. @@ -444,8 +503,8 @@ public final class KBCommands { if (backupName.matches("[0-9]*")) { // If numeric input int index = Integer.parseInt(backupName) - 1; - synchronized (backupFileNameList) { - return backupFileNameList.get(index); // Replace input number with real backup file name. + synchronized (backupList) { + return backupList.get(index).getBackupFileName(); // Replace input number with real backup file name. } } } catch (NumberFormatException | IndexOutOfBoundsException ignored) { diff --git a/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java b/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java new file mode 100644 index 0000000..8cca8d2 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java @@ -0,0 +1,78 @@ +package com.keuin.kbackupfabric.ui; + +import com.keuin.kbackupfabric.backup.name.BackupFileNameEncoder; +import com.keuin.kbackupfabric.backup.name.PrimitiveBackupFileNameEncoder; +import com.keuin.kbackupfabric.operation.backup.method.ConfiguredBackupMethod; +import com.keuin.kbackupfabric.util.FilesystemUtil; +import net.minecraft.server.MinecraftServer; + +import java.io.File; +import java.time.LocalDateTime; + +/** + * Used in UI part. + */ +public class PrimitiveBackupInfo implements BackupInfo { + private final String name; + private final LocalDateTime creationTime; + private final long sizeBytes; + private final String fileName; + + @Deprecated + private PrimitiveBackupInfo(String name, LocalDateTime creationTime, long sizeBytes) { + this.name = name; + this.creationTime = creationTime; + this.sizeBytes = sizeBytes; + this.fileName = PrimitiveBackupFileNameEncoder.INSTANCE.encode(name, creationTime); + } + + private PrimitiveBackupInfo(String fileName, long sizeBytes) { + this.fileName = fileName; + BackupFileNameEncoder.BackupBasicInformation info = PrimitiveBackupFileNameEncoder.INSTANCE.decode(fileName); + if (info == null) + throw new IllegalArgumentException("Invalid file name."); + this.name = info.customName; + this.creationTime = info.time; + this.sizeBytes = sizeBytes; + } + + public static PrimitiveBackupInfo fromFile(File zipFile) { + // TODO: fix this, use metadata file instead +// fileName = zipFile.getName(); +// BackupFileNameEncoder.BackupBasicInformation info = PrimitiveBackupFileNameEncoder.INSTANCE.decode(fileName); +// if (info == null) +// throw new IllegalArgumentException("Invalid file name."); +// return new PrimitiveBackupInfo(info.customName, info.time, FilesystemUtil.getFileSizeBytes(zipFile)); + return new PrimitiveBackupInfo(zipFile.getName(), FilesystemUtil.getFileSizeBytes(zipFile)); + } + + @Override + public String getName() { + return name; + } + + @Override + public LocalDateTime getCreationTime() { + return creationTime; + } + + @Override + public long getSizeBytes() { + return sizeBytes; + } + + @Override + public String getType() { + return "ZIP"; + } + + @Override + public ConfiguredBackupMethod createConfiguredBackupMethod(MinecraftServer server) { + throw new RuntimeException("not implemented"); + } + + @Override + public String getBackupFileName() { + return null; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java index b101cfd..7111347 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java @@ -1,13 +1,18 @@ package com.keuin.kbackupfabric.util; +import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; public class DateUtil { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss"); + private static final SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private static final DateTimeFormatter prettyFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static String getString(LocalDateTime localDateTime) { return localDateTime.format(formatter); @@ -28,4 +33,12 @@ public class DateUtil { public static LocalDateTime toLocalDateTime(String timeString) { return LocalDateTime.parse(timeString, formatter); } + + public static String fromEpochMillis(long epochMillis) { + return outFormatter.format(new Date(epochMillis)); + } + + public static String getPrettyString(LocalDateTime localDateTime) { + return prettyFormatter.format(localDateTime); + } } diff --git a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java index f245cff..bccd403 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java @@ -15,19 +15,22 @@ public class FilesystemUtil { */ public static long getFileSizeBytes(String parentDirectory, String fileName) { long fileSize = -1; - try{ + try { File backupZipFile = new File(parentDirectory, fileName); fileSize = backupZipFile.length(); - } catch (SecurityException ignored){ + } catch (SecurityException ignored) { } return fileSize; } public static long getFileSizeBytes(String filePath) { + return getFileSizeBytes(new File(filePath)); + } + + public static long getFileSizeBytes(File file) { long fileSize = -1; try { - File backupZipFile = new File(filePath); - fileSize = backupZipFile.length(); + fileSize = file.length(); } catch (SecurityException ignored) { } return fileSize; |