summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/ui/PrimitiveBackupInfo.java
blob: 8cca8d275911bf8ac96c55ed1ad1eaadcad5dd33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
    }
}