mirror of
https://github.com/BlueMap-Minecraft/BlueMap.git
synced 2025-01-28 19:21:22 +01:00
Improve minecraft-client.jar download to use a temporary file before verification
This commit is contained in:
parent
7200e94262
commit
51f12741c9
@ -134,16 +134,22 @@ public static MinecraftVersion load(@Nullable String id, Path dataRoot, boolean
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void download(VersionManifest.Version version, Path file) throws IOException {
|
private static void download(VersionManifest.Version version, Path file) throws IOException {
|
||||||
boolean downloadCompletedAndVerified = false;
|
|
||||||
VersionManifest.Download download = version.fetchDetail().getDownloads().getClient();
|
VersionManifest.Download download = version.fetchDetail().getDownloads().getClient();
|
||||||
Logger.global.logInfo("Downloading '" + download.getUrl() + "' to '" + file + "'...");
|
Logger.global.logInfo("Downloading '" + download.getUrl() + "' to '" + file + "'...");
|
||||||
|
|
||||||
FileHelper.createDirectories(file.toAbsolutePath().normalize().getParent());
|
FileHelper.createDirectories(file.toAbsolutePath().normalize().getParent());
|
||||||
|
Path unverifiedFile = file.getParent().resolve(file.getFileName().toString() + ".unverified");
|
||||||
|
|
||||||
|
try {
|
||||||
try (
|
try (
|
||||||
DigestInputStream in = new DigestInputStream(new URI(download.getUrl()).toURL().openStream(), MessageDigest.getInstance("SHA-1"));
|
DigestInputStream in = new DigestInputStream(
|
||||||
OutputStream out = Files.newOutputStream(file, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW, StandardOpenOption.TRUNCATE_EXISTING)
|
new URI(download.getUrl()).toURL().openStream(),
|
||||||
|
MessageDigest.getInstance("SHA-1")
|
||||||
|
);
|
||||||
|
OutputStream out = Files.newOutputStream(unverifiedFile)
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
// download
|
||||||
in.transferTo(out);
|
in.transferTo(out);
|
||||||
|
|
||||||
// verify sha-1
|
// verify sha-1
|
||||||
@ -154,12 +160,15 @@ private static void download(VersionManifest.Version version, Path file) throws
|
|||||||
throw new IOException("SHA-1 of the downloaded file does not match!");
|
throw new IOException("SHA-1 of the downloaded file does not match!");
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadCompletedAndVerified = true;
|
}
|
||||||
|
|
||||||
|
// rename once verified
|
||||||
|
FileHelper.atomicMove(unverifiedFile, file);
|
||||||
|
|
||||||
} catch (NoSuchAlgorithmException | IOException | URISyntaxException ex) {
|
} catch (NoSuchAlgorithmException | IOException | URISyntaxException ex) {
|
||||||
Logger.global.logWarning("Failed to download '" + download.getUrl() + "': " + ex);
|
Logger.global.logWarning("Failed to download '" + download.getUrl() + "': " + ex);
|
||||||
} finally {
|
} finally {
|
||||||
if (!downloadCompletedAndVerified)
|
Files.deleteIfExists(unverifiedFile);
|
||||||
Files.deleteIfExists(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user