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 static org.jackhuang.hmcl.util.Logging.LOG;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; 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. * Creates a task that downloads the given version to local repository.
*/ */
public static FileDownloadTask downloadFromRemote(RemoteVersion version) throws IOException { public static FileDownloadTask downloadFromRemote(RemoteVersion version) throws IOException {
Path stage = Files.createTempFile("hmcl-update-", ""); Path downloaded = Files.createTempFile("hmcl-update-", null);
return new FileDownloadTask(new URL(version.getUrl()), stage.toFile(), version.getIntegrityCheck()) { return new FileDownloadTask(new URL(version.getUrl()), downloaded.toFile(), version.getIntegrityCheck()) {
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
Path jar = stage; super.execute();
try { try {
super.execute(); switch (version.getType()) {
if (version.getType() == RemoteVersion.Type.PACK) { case JAR:
Path unpacked = Files.createTempFile("hmcl-update-", ".jar"); writeToStorage(downloaded, false);
try (GZIPInputStream stream = new GZIPInputStream(Files.newInputStream(jar)); break;
JarOutputStream out = new JarOutputStream(Files.newOutputStream(unpacked))) {
Pack200.newUnpacker().unpack(stream, out); case PACK:
} Path unpacked = Files.createTempFile("hmcl-update-unpack-", null);
jar = unpacked; try {
try (InputStream in = new GZIPInputStream(Files.newInputStream(downloaded));
JarOutputStream out = new JarOutputStream(Files.newOutputStream(unpacked))) {
Pack200.newUnpacker().unpack(in, out);
}
writeToStorage(unpacked, false);
} finally {
Files.deleteIfExists(unpacked);
}
break;
default:
throw new IllegalArgumentException("Unknown type: " + version.getType());
} }
IntegrityChecker.requireVerifiedJar(jar);
Files.createDirectories(localStorage.getParent());
Files.copy(jar, localStorage, StandardCopyOption.REPLACE_EXISTING);
} finally { } finally {
Files.deleteIfExists(jar); Files.deleteIfExists(downloaded);
} }
} }
}; };
@@ -108,9 +129,7 @@ final class LocalRepository {
} }
LOG.info("Downloading " + current.get()); LOG.info("Downloading " + current.get());
try { try {
IntegrityChecker.requireVerifiedJar(current.get().getLocation()); writeToStorage(current.get().getLocation(), true);
Files.createDirectories(localStorage.getParent());
ExecutableHeaderHelper.copyWithoutHeader(current.get().getLocation(), localStorage);
} catch (IOException e) { } catch (IOException e) {
LOG.log(Level.WARNING, "Failed to download " + current.get(), 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 jarHash = Optional.ofNullable(response.get("jarsha1")).map(JsonElement::getAsString).orElse(null);
String packUrl = Optional.ofNullable(response.get("pack")).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); 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)); 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)); return new RemoteVersion(version, jarUrl, Type.JAR, new IntegrityCheck("SHA-1", jarHash));
else } else {
throw new IOException("Missing both jar and pack download URL"); throw new IOException("No download url is available");
}
} catch (JsonParseException e) { } catch (JsonParseException e) {
throw new IOException("Malformed response", e); throw new IOException("Malformed response", e);
} }