summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-25 15:25:34 +0800
committerKeuin <[email protected]>2021-01-25 15:25:34 +0800
commit08ac6aaea58b7309ea95a12ca6227a6ca3ce4db5 (patch)
treed305fc23d2cec9935ccb5e5a5390c9d5e54a8726 /src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java
parenta817715ad4f625e355b285d67ee323e8b2006ae4 (diff)
Improve user interaction.
Code refactor.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/ui/BackupManager.java64
1 files changed, 64 insertions, 0 deletions
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);
+ }
+ }
+ };
+ }
+ };
+ }
+}