| 1 | package com.mojang.realmsclient.client.worldupload;
|
| 2 |
|
| 3 | import java.io.File;
|
| 4 | import java.io.FileInputStream;
|
| 5 | import java.io.FileOutputStream;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.InputStream;
|
| 8 | import java.nio.file.Path;
|
| 9 | import java.util.function.BooleanSupplier;
|
| 10 | import java.util.zip.GZIPOutputStream;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
| 14 | import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class RealmsUploadWorldPacker {
|
| 18 | private static final long SIZE_LIMIT = 5368709120L;
|
| 19 | private static final String WORLD_FOLDER_NAME = "world";
|
| 20 | private final BooleanSupplier isCanceled;
|
| 21 | private final Path directoryToPack;
|
| 22 |
|
| 23 | public static File pack(final Path directoryToPack, final BooleanSupplier isCanceled) throws IOException {
|
| 24 | return new RealmsUploadWorldPacker(directoryToPack, isCanceled).tarGzipArchive();
|
| 25 | }
|
| 26 |
|
| 27 | private RealmsUploadWorldPacker(final Path directoryToPack, final BooleanSupplier isCanceled) {
|
| 28 | this.isCanceled = isCanceled;
|
| 29 | this.directoryToPack = directoryToPack;
|
| 30 | }
|
| 31 |
|
| 32 | private File tarGzipArchive() throws IOException {
|
| 33 | TarArchiveOutputStream tar = null;
|
| 34 |
|
| 35 | try {
|
| 36 | File file = File.createTempFile("realms-upload-file", ".tar.gz");
|
| 37 | tar = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
|
| 38 | tar.setLongFileMode(3);
|
| 39 | this.addFileToTarGz(tar, this.directoryToPack, "world", true);
|
| 40 | if (this.isCanceled.getAsBoolean()) {
|
| 41 | throw new RealmsUploadCanceledException();
|
| 42 | }
|
| 43 |
|
| 44 | tar.finish();
|
| 45 | this.verifyBelowSizeLimit(file.length());
|
| 46 | return file;
|
| 47 | } finally {
|
| 48 | if (tar != null) {
|
| 49 | tar.close();
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | private void addFileToTarGz(final TarArchiveOutputStream out, final Path path, final String base, final boolean root) throws IOException {
|
| 55 | if (this.isCanceled.getAsBoolean()) {
|
| 56 | throw new RealmsUploadCanceledException();
|
| 57 | }
|
| 58 |
|
| 59 | this.verifyBelowSizeLimit(out.getBytesWritten());
|
| 60 | File file = path.toFile();
|
| 61 | String entryName = root ? base : base + file.getName();
|
| 62 | TarArchiveEntry entry = new TarArchiveEntry(file, entryName);
|
| 63 | out.putArchiveEntry(entry);
|
| 64 | if (file.isFile()) {
|
| 65 | try (InputStream is = new FileInputStream(file)) {
|
| 66 | is.transferTo(out);
|
| 67 | }
|
| 68 |
|
| 69 | out.closeArchiveEntry();
|
| 70 | } else {
|
| 71 | out.closeArchiveEntry();
|
| 72 | File[] children = file.listFiles();
|
| 73 | if (children != null) {
|
| 74 | for (File child : children) {
|
| 75 | this.addFileToTarGz(out, child.toPath(), entryName + "/", false);
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private void verifyBelowSizeLimit(final long sizeInByte) {
|
| 82 | if (sizeInByte > 5368709120L) {
|
| 83 | throw new RealmsUploadTooLargeException(5368709120L);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|