Extract writeToStorage()

This commit is contained in:
yushijinhun
2018-08-04 23:10:14 +08:00
parent b1ce3c31f5
commit 86030e64ca
2 changed files with 42 additions and 22 deletions

View File

@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.upgrade;
import static org.jackhuang.hmcl.util.Logging.LOG;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -62,30 +63,50 @@ final class LocalRepository {
}
}
private static void writeToStorage(Path source, boolean checkHeaders) throws IOException {
IntegrityChecker.requireVerifiedJar(source);
Files.createDirectories(localStorage.getParent());
if (checkHeaders) {
ExecutableHeaderHelper.copyWithoutHeader(source, localStorage);
} else {
Files.copy(source, localStorage, StandardCopyOption.REPLACE_EXISTING);
}
}
/**
* Creates a task that downloads the given version to local repository.
*/
public static FileDownloadTask downloadFromRemote(RemoteVersion version) throws IOException {
Path stage = Files.createTempFile("hmcl-update-", "");
return new FileDownloadTask(new URL(version.getUrl()), stage.toFile(), version.getIntegrityCheck()) {
Path downloaded = Files.createTempFile("hmcl-update-", null);
return new FileDownloadTask(new URL(version.getUrl()), downloaded.toFile(), version.getIntegrityCheck()) {
@Override
public void execute() throws Exception {
Path jar = stage;
try {
super.execute();
if (version.getType() == RemoteVersion.Type.PACK) {
Path unpacked = Files.createTempFile("hmcl-update-", ".jar");
try (GZIPInputStream stream = new GZIPInputStream(Files.newInputStream(jar));
try {
switch (version.getType()) {
case JAR:
writeToStorage(downloaded, false);
break;
case PACK:
Path unpacked = Files.createTempFile("hmcl-update-unpack-", null);
try {
try (InputStream in = new GZIPInputStream(Files.newInputStream(downloaded));
JarOutputStream out = new JarOutputStream(Files.newOutputStream(unpacked))) {
Pack200.newUnpacker().unpack(stream, out);
Pack200.newUnpacker().unpack(in, out);
}
jar = unpacked;
}
IntegrityChecker.requireVerifiedJar(jar);
Files.createDirectories(localStorage.getParent());
Files.copy(jar, localStorage, StandardCopyOption.REPLACE_EXISTING);
writeToStorage(unpacked, false);
} finally {
Files.deleteIfExists(jar);
Files.deleteIfExists(unpacked);
}
break;
default:
throw new IllegalArgumentException("Unknown type: " + version.getType());
}
} finally {
Files.deleteIfExists(downloaded);
}
}
};
@@ -108,9 +129,7 @@ final class LocalRepository {
}
LOG.info("Downloading " + current.get());
try {
IntegrityChecker.requireVerifiedJar(current.get().getLocation());
Files.createDirectories(localStorage.getParent());
ExecutableHeaderHelper.copyWithoutHeader(current.get().getLocation(), localStorage);
writeToStorage(current.get().getLocation(), true);
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to download " + current.get(), e);
}

View File

@@ -38,12 +38,13 @@ public class RemoteVersion {
String jarHash = Optional.ofNullable(response.get("jarsha1")).map(JsonElement::getAsString).orElse(null);
String packUrl = Optional.ofNullable(response.get("pack")).map(JsonElement::getAsString).orElse(null);
String packHash = Optional.ofNullable(response.get("packsha1")).map(JsonElement::getAsString).orElse(null);
if (packUrl != null && packHash != null)
if (packUrl != null && packHash != null) {
return new RemoteVersion(version, packUrl, Type.PACK, new IntegrityCheck("SHA-1", packHash));
else if (jarUrl != null && jarHash != null)
} else if (jarUrl != null && jarHash != null) {
return new RemoteVersion(version, jarUrl, Type.JAR, new IntegrityCheck("SHA-1", jarHash));
else
throw new IOException("Missing both jar and pack download URL");
} else {
throw new IOException("No download url is available");
}
} catch (JsonParseException e) {
throw new IOException("Malformed response", e);
}