Simplify pack hosting with JDK HTTP server and reduce resource handling state
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package online.astradev.packs;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -30,20 +31,25 @@ public final class PackBuilder {
|
||||
|
||||
public Pack build(Path root, String resourceVersion) throws IOException {
|
||||
Path contents = config.contents(root);
|
||||
if (!Files.isDirectory(contents) || Files.isSymbolicLink(contents)) {
|
||||
throw new IOException("Resource directory is missing or is a symbolic link: " + contents);
|
||||
if (!Files.isDirectory(contents)) {
|
||||
throw new IOException("Resource directory is missing: " + contents);
|
||||
}
|
||||
for (Path parent = contents; parent != null; parent = parent.getParent()) {
|
||||
if (Files.isSymbolicLink(parent)) {
|
||||
throw new IOException("Symbolic links are not supported: " + parent);
|
||||
}
|
||||
}
|
||||
Path output = config.storage(root).resolve("output");
|
||||
Files.createDirectories(output);
|
||||
Path temporary = Files.createTempFile(output, "build-", ".tmp");
|
||||
try {
|
||||
int files = writeArchive(contents, temporary, resourceVersion);
|
||||
String sha1 = hash(temporary, "SHA-1");
|
||||
String sha1 = sha1(temporary);
|
||||
Path destination = output.resolve(sha1 + ".zip");
|
||||
boolean created = !Files.exists(destination);
|
||||
if (created) {
|
||||
Files.move(temporary, destination, StandardCopyOption.ATOMIC_MOVE);
|
||||
} else if (!hash(destination, "SHA-1").equals(sha1)) {
|
||||
} else if (!sha1(destination).equals(sha1)) {
|
||||
throw new IOException("Published ZIP was modified: " + destination);
|
||||
}
|
||||
Pack pack = new Pack(destination, sha1, files, Files.size(destination));
|
||||
@@ -68,7 +74,6 @@ public final class PackBuilder {
|
||||
private int writeArchive(Path contents, Path target, String resourceVersion) throws IOException {
|
||||
int files = 0;
|
||||
long totalBytes = 0;
|
||||
byte[] buffer = new byte[8192];
|
||||
try (var paths = Files.walk(contents);
|
||||
var zip = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(target)))) {
|
||||
for (Path file : paths.sorted().toList()) {
|
||||
@@ -78,11 +83,13 @@ public final class PackBuilder {
|
||||
if (!Files.isRegularFile(file)) {
|
||||
continue;
|
||||
}
|
||||
String name = contents.relativize(file).toString().replace('\\', '/');
|
||||
String name = contents.relativize(file).toString().replace(File.separatorChar, '/');
|
||||
if (!name.startsWith("assets/") && !name.equals("pack.png")) {
|
||||
continue;
|
||||
}
|
||||
PackFiles.safe(contents, name);
|
||||
if (name.contains("\\") || name.contains(":")) {
|
||||
throw new IOException("Invalid resource name: " + name);
|
||||
}
|
||||
if (++files > config.build.maxFiles) {
|
||||
throw new IOException("Too many resources");
|
||||
}
|
||||
@@ -90,21 +97,9 @@ public final class PackBuilder {
|
||||
ZipEntry entry = new ZipEntry(name);
|
||||
entry.setTime(0);
|
||||
zip.putNextEntry(entry);
|
||||
long fileBytes = 0;
|
||||
try (var input = Files.newInputStream(file)) {
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
fileBytes += read;
|
||||
totalBytes += read;
|
||||
if (fileBytes > config.build.maxFileMiB * 1024L * 1024L) {
|
||||
throw new IOException("Resource exceeds maxFileMiB: " + file);
|
||||
}
|
||||
if (totalBytes > config.build.maxTotalMiB * 1024L * 1024L) {
|
||||
throw new IOException("Resources exceed maxTotalMiB");
|
||||
}
|
||||
zip.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
long limit = Math.min(config.build.maxFileMiB * 1024L * 1024L,
|
||||
config.build.maxTotalMiB * 1024L * 1024L - totalBytes);
|
||||
totalBytes += copyResource(file, zip, limit);
|
||||
zip.closeEntry();
|
||||
}
|
||||
ZipEntry metadata = new ZipEntry("pack.mcmeta");
|
||||
@@ -116,6 +111,22 @@ public final class PackBuilder {
|
||||
return files + 1;
|
||||
}
|
||||
|
||||
private static long copyResource(Path file, OutputStream output, long limit) throws IOException {
|
||||
byte[] buffer = new byte[8192];
|
||||
long size = 0;
|
||||
try (var input = Files.newInputStream(file)) {
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
size += read;
|
||||
if (size > limit) {
|
||||
throw new IOException("Resource exceeds configured size limit: " + file);
|
||||
}
|
||||
output.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
private byte[] metadata(String resourceVersion) {
|
||||
var version = JsonParser.parseString(resourceVersion);
|
||||
JsonObject pack = new JsonObject();
|
||||
@@ -164,7 +175,7 @@ public final class PackBuilder {
|
||||
JsonObject info = readManifest(output.resolve("current.json"));
|
||||
String sha1 = info.get("sha1").getAsString();
|
||||
Path path = output.resolve(sha1 + ".zip");
|
||||
if (!hash(path, "SHA-1").equals(sha1)) {
|
||||
if (!sha1(path).equals(sha1)) {
|
||||
throw new IOException("Stored resource pack hash mismatch");
|
||||
}
|
||||
return new Pack(path, sha1, info.get("files").getAsInt(), Files.size(path));
|
||||
@@ -184,9 +195,9 @@ public final class PackBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
public static String hash(Path file, String algorithm) throws IOException {
|
||||
private static String sha1(Path file) throws IOException {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance(algorithm);
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
||||
try (var input = new DigestInputStream(Files.newInputStream(file), digest)) {
|
||||
input.transferTo(OutputStream.nullOutputStream());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user