Skip to content

Commit

Permalink
1.6.4-SNAPSHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Dec 2, 2021
1 parent 71fcf8b commit 0334f85
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.mengcraft</groupId>
<artifactId>simpleorm</artifactId>
<version>1.6.3-SNAPSHOT</version>
<version>1.6.4-SNAPSHOT</version>

<name>SimpleORM</name>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mengcraft/simpleorm/IResourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.InputStream;
import java.util.List;

public interface IResourceManager {

Expand All @@ -27,4 +28,6 @@ public interface IResourceManager {
void saveConfig();

void saveConfig(@NotNull String filename, @NotNull FileConfiguration config);

void sync(List<String> filenames);
}
8 changes: 8 additions & 0 deletions src/main/java/com/mengcraft/simpleorm/lib/Utils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.mengcraft.simpleorm.lib;

import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.mengcraft.simpleorm.async.Handler;
import lombok.SneakyThrows;
import org.yaml.snakeyaml.Yaml;

import javax.persistence.Table;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -128,4 +131,9 @@ public static <T> CompletableFuture<T> future() {
public static boolean isNullOrClosed(Handler ref) {
return ref == null || !ref.isOpen();
}

@SneakyThrows
public static String md5(File f) {
return Hashing.md5().hashBytes(Files.toByteArray(f)).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -16,10 +17,13 @@
import org.jetbrains.annotations.Nullable;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;

Expand Down Expand Up @@ -141,4 +145,41 @@ public void saveConfig(@NotNull String filename, @NotNull FileConfiguration conf

saveResource(filename, new ByteArrayInputStream(config.saveToString().getBytes(StandardCharsets.UTF_8)));
}

@Override
public void sync(List<String> filenames) {
for (String filename : filenames) {
sync(filename);
}
}

@SneakyThrows
private void sync(String filename) {
File f = new File(owner.getDataFolder(), filename);
GridFSDBFile one = fs.findOne(filename);
if (one == null) {
if (!f.exists()) {
owner.saveResource(filename, false);
}
saveResource(filename, new FileInputStream(f));
owner.getLogger().info(String.format("MongoResourceManager.sync() Upload %s to server", filename));
} else {// remote exists
if (f.exists()) {// local exists
String md5 = one.getMD5();
if (!md5.equals(Utils.md5(f))) {// not matches
long t = one.getUploadDate().getTime();
if (t >= f.lastModified()) {
one.writeTo(f);
owner.getLogger().info(String.format("MongoResourceManager.sync() Overwrite %s from server", filename));
} else {
saveResource(filename, new FileInputStream(f));
owner.getLogger().info(String.format("MongoResourceManager.sync() Upload %s to file server", filename));
}
}
} else {// merge to local
one.writeTo(f);
owner.getLogger().info(String.format("MongoResourceManager.sync() Overwrite %s from server", filename));
}
}
}
}

0 comments on commit 0334f85

Please sign in to comment.