summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-13 12:51:15 +0800
committerkeuin <[email protected]>2021-01-13 12:51:15 +0800
commit81497ebee2cdb0dac3bcdc26c5d77d06bdb98b5d (patch)
tree9e7aad680a438b845126b266374155c8fb306179 /src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
parentf4d926c1eb91749a4b0f89c7a35538821ff7e21b (diff)
Clean old incremental backup codes.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
new file mode 100644
index 0000000..83967b7
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/name/BackupFileNameEncoder.java
@@ -0,0 +1,49 @@
+package com.keuin.kbackupfabric.util.backup.name;
+
+import java.time.LocalDateTime;
+
+/**
+ * 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);
+
+ /**
+ * 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;
+
+ BackupBasicInformation(String customName, LocalDateTime time) {
+ this.customName = customName;
+ this.time = time;
+ }
+ }
+}