summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/name
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-20 19:50:42 +0800
committerkeuin <[email protected]>2021-01-20 19:50:42 +0800
commitac7e70883c18602e7fd4b525b9e6fb9ea9620a6b (patch)
tree63b2d65d2fe489ef0cc1fe6e9a87b65c0b69de84 /src/main/java/com/keuin/kbackupfabric/backup/name
parent7a5297de3467b1069fdf5e4a1b2aaf510ca35663 (diff)
refactor
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup/name')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java64
-rw-r--r--src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java37
-rw-r--r--src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java33
3 files changed, 134 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java
new file mode 100644
index 0000000..8ad91f7
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/backup/name/BackupFileNameEncoder.java
@@ -0,0 +1,64 @@
+package com.keuin.kbackupfabric.backup.name;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * Encode and decode backup file name for a specific backup type.
+ */
+public interface BackupFileNameEncoder {
+
+ /**
+ * Construct full backup file name from custom name and creation time.
+ * @param customName the custom name. If the custom name contains invalid chars, an exception will be thrown.
+ * @param time the creation time.
+ * @return the file name.
+ */
+ String encode(String customName, LocalDateTime time);
+
+ /**
+ * Extract custom and backup time from backup file name.
+ *
+ * @param fileName the backup file name.
+ * @return the information. If the given file name is invalid, return null.
+ */
+ BackupBasicInformation decode(String fileName);
+
+ default boolean isValidFileName(String fileName) {
+ return decode(fileName) != null;
+ }
+
+ /**
+ * Check if the given string is a valid custom backup name.
+ *
+ * @param customName the custom backup name.
+ * @return if the name is valid.
+ */
+ default boolean isValidCustomName(String customName) {
+ final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':'};
+ for (char c : ILLEGAL_CHARACTERS) {
+ if (customName.contains(String.valueOf(c))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ class BackupBasicInformation {
+
+ public final String customName;
+ public final LocalDateTime time;
+
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm.ss");
+
+ protected BackupBasicInformation(String customName, LocalDateTime time) {
+ this.customName = customName;
+ this.time = time;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%s, %s", customName, time.format(formatter));
+ }
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java
new file mode 100644
index 0000000..8910d7f
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/backup/name/IncrementalBackupFileNameEncoder.java
@@ -0,0 +1,37 @@
+package com.keuin.kbackupfabric.backup.name;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class IncrementalBackupFileNameEncoder implements BackupFileNameEncoder {
+ private static final String backupFileNamePrefix = "incremental";
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
+
+ // TODO: make this private and use singleton pattern
+ public IncrementalBackupFileNameEncoder() {
+ }
+
+ @Override
+ public String encode(String customName, LocalDateTime time) {
+ if (!isValidCustomName(customName))
+ throw new IllegalArgumentException("Invalid custom name");
+ String timeString = time.format(formatter);
+ return backupFileNamePrefix + "-" + timeString + "_" + customName + ".kbi";
+ }
+
+ @Override
+ public BackupFileNameEncoder.BackupBasicInformation decode(String fileName) {
+ Pattern pattern = Pattern.compile(
+ "^" + backupFileNamePrefix + "-" + "([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})_(.+)\\.kbi" + "$"
+ );
+ Matcher matcher = pattern.matcher(fileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(1);
+ String customName = matcher.group(2);
+ return new BackupFileNameEncoder.BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter));
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java
new file mode 100644
index 0000000..c8e73f9
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/backup/name/PrimitiveBackupFileNameEncoder.java
@@ -0,0 +1,33 @@
+package com.keuin.kbackupfabric.backup.name;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class PrimitiveBackupFileNameEncoder implements BackupFileNameEncoder {
+ private static final String backupFileNamePrefix = "kbackup";
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
+
+ @Override
+ public String encode(String customName, LocalDateTime time) {
+ if (!isValidCustomName(customName))
+ throw new IllegalArgumentException("Invalid custom name");
+ String timeString = time.format(formatter);
+ return backupFileNamePrefix + "-" + timeString + "_" + customName + ".zip";
+ }
+
+ @Override
+ public BackupBasicInformation decode(String fileName) {
+ Pattern pattern = Pattern.compile(
+ "^" + backupFileNamePrefix + "-" + "([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})_(.+)\\.zip" + "$"
+ );
+ Matcher matcher = pattern.matcher(fileName);
+ if (matcher.find()) {
+ String timeString = matcher.group(1);
+ String customName = matcher.group(2);
+ return new BackupBasicInformation(customName, LocalDateTime.parse(timeString, formatter));
+ }
+ return null;
+ }
+}