Use tar.xz for downloading templates

This commit is contained in:
ME1312 2021-06-28 16:34:49 -04:00
parent d3f3f194d0
commit 1427ff04e1
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
10 changed files with 115 additions and 24 deletions

View File

@ -28,7 +28,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUtil</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -43,6 +43,18 @@
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.kamranzafar</groupId>
<artifactId>jtar</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -82,8 +82,9 @@ public class ExternalSubCreator extends SubCreator {
}
if (host.available && !Util.getDespiteException(() -> Util.reflect(SubProxy.class.getDeclaredField("reloading"), host.plugin), false)) {
host.queue(new PacketExUploadTemplates(host.plugin));
host.queue(new PacketExConfigureHost(host.plugin, host), new PacketExUploadTemplates(host.plugin, () -> {
if (enableRT == null || enableRT) host.queue(new PacketExDownloadTemplates(host.plugin, host));
}));
}
}

View File

@ -19,12 +19,7 @@ public abstract class FileScanner {
* @param whitelist File Whitelist
*/
protected void scan(File dir, String... whitelist) throws IOException {
List<String> files;
try {
files = Util.reflect(Util.class.getDeclaredMethod("zipsearch", File.class, File.class), null, dir, dir);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalStateException("Cannot access zipsearch()", e);
}
List<String> files = Util.searchDirectory(dir);
if (files.size() <= 0 || whitelist.length <= 0)
return;

View File

@ -8,10 +8,22 @@ import net.ME1312.SubData.Server.Protocol.PacketStreamOut;
import net.ME1312.SubData.Server.SubDataClient;
import net.ME1312.SubServers.Bungee.Host.External.ExternalHost;
import net.ME1312.SubServers.Bungee.Host.External.ExternalSubCreator;
import net.ME1312.SubServers.Bungee.Host.SubCreator.ServerTemplate;
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
import net.ME1312.SubServers.Bungee.SubProxy;
import org.kamranzafar.jtar.TarEntry;
import org.kamranzafar.jtar.TarOutputStream;
import org.tukaani.xz.LZMA2Options;
import org.tukaani.xz.XZOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import static org.tukaani.xz.LZMA2Options.MODE_FAST;
import static org.tukaani.xz.XZ.CHECK_SHA256;
/**
* External Host Template Download Packet
@ -39,8 +51,27 @@ public class PacketExDownloadTemplates implements PacketIn, PacketStreamOut {
public void send(SubDataClient client, OutputStream stream) throws Throwable {
try {
if (client.getBlockSize() < DataSize.MBB) client.tempBlockSize(DataSize.MBB);
Util.zip(new UniversalFile(plugin.dir, "SubServers:Templates"), stream);
stream.close();
HashMap<String, ServerTemplate> map = Util.getDespiteException(() -> Util.reflect(ExternalSubCreator.class.getDeclaredField("templates"), ((ExternalHost) client.getHandler()).getCreator()), new HashMap<>());
TarOutputStream tar = new TarOutputStream(new XZOutputStream(stream, new LZMA2Options(MODE_FAST), CHECK_SHA256));
File dir = new UniversalFile(plugin.dir, "SubServers:Templates");
byte[] buffer = new byte[4096];
for (String file : Util.searchDirectory(dir)) {
int index = file.indexOf(File.separatorChar);
if (index != -1 && !map.containsKey(file.substring(0, index).toLowerCase())) {
tar.putNextEntry(new TarEntry(new File(dir, file), file.replace(File.separatorChar, '/')));
FileInputStream in = new FileInputStream(dir.getAbsolutePath() + File.separator + file);
int len;
while ((len = in.read(buffer)) != -1) {
tar.write(buffer, 0, len);
}
in.close();
}
}
tar.close();
Util.isException(() -> Util.reflect(ExternalSubCreator.class.getDeclaredField("enableRT"), host.getCreator(), true));
} catch (Exception e) {
@ -58,6 +89,6 @@ public class PacketExDownloadTemplates implements PacketIn, PacketStreamOut {
@Override
public int version() {
return 0x0001;
return 0x0002;
}
}

View File

@ -12,7 +12,9 @@ import net.ME1312.SubServers.Bungee.Host.SubCreator;
import net.ME1312.SubServers.Bungee.SubProxy;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.logging.Logger;
@ -20,13 +22,15 @@ import java.util.logging.Logger;
* External Host Template Upload Packet
*/
public class PacketExUploadTemplates implements PacketObjectIn<Integer>, PacketOut {
private static LinkedList<Runnable> callbacks = new LinkedList<Runnable>();
private SubProxy plugin;
/**
* New PacketExUploadTemplates
*/
public PacketExUploadTemplates(SubProxy plugin) {
public PacketExUploadTemplates(SubProxy plugin, Runnable... callbacks) {
this.plugin = plugin;
PacketExUploadTemplates.callbacks.addAll(Arrays.asList(callbacks));
}
@SuppressWarnings("unchecked")
@ -50,6 +54,12 @@ public class PacketExUploadTemplates implements PacketObjectIn<Integer>, PacketO
e.printStackTrace();
}
}
LinkedList<Runnable> callbacks = PacketExUploadTemplates.callbacks;
PacketExUploadTemplates.callbacks = new LinkedList<Runnable>();
for (Runnable r : callbacks) {
r.run();
}
}
}

View File

@ -18,7 +18,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUtil</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -30,13 +30,13 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiEngine</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiUI</artifactId>
<version>21w26b</version>
<version>21w27b</version>
<scope>runtime</scope>
</dependency>
<dependency>
@ -51,6 +51,18 @@
<version>1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.kamranzafar</groupId>
<artifactId>jtar</artifactId>
<version>2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>

View File

@ -23,12 +23,7 @@ public abstract class FileScanner {
* @param whitelist File Whitelist
*/
protected void scan(File dir, String... whitelist) throws IOException {
List<String> files;
try {
files = Util.reflect(Util.class.getDeclaredMethod("zipsearch", File.class, File.class), null, dir, dir);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalStateException("Cannot access zipsearch()", e);
}
List<String> files = Util.searchDirectory(dir);
if (files.size() <= 0 || whitelist.length <= 0)
return;

View File

@ -9,6 +9,12 @@ import net.ME1312.SubData.Client.SubDataSender;
import net.ME1312.SubServers.Host.ExHost;
import net.ME1312.SubServers.Host.SubAPI;
import org.kamranzafar.jtar.TarEntry;
import org.kamranzafar.jtar.TarInputStream;
import org.tukaani.xz.XZInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
@ -42,7 +48,36 @@ public class PacketExDownloadTemplates implements PacketOut, PacketStreamIn {
try {
dir.mkdirs();
Util.unzip(stream, dir);
TarInputStream tar = new TarInputStream(new XZInputStream(stream));
byte[] buffer = new byte[4096];
TarEntry entry;
while ((entry = tar.getNextEntry()) != null) {
File newFile = new File(dir + File.separator + entry.getName().replace('/', File.separatorChar));
if (newFile.exists()) {
if (newFile.isDirectory()) {
Util.deleteDirectory(newFile);
} else {
newFile.delete();
}
}
if (entry.isDirectory()) {
newFile.mkdirs();
continue;
} else if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = tar.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
}
tar.close();
host.creator.load(true);
host.log.info.println(((first)?"":"New ") + "Remote Template Files Downloaded");
} catch (Exception e) {
@ -54,6 +89,6 @@ public class PacketExDownloadTemplates implements PacketOut, PacketStreamIn {
@Override
public int version() {
return 0x0001;
return 0x0002;
}
}