fix: #699
This commit is contained in:
@@ -26,6 +26,8 @@ import org.jackhuang.hmcl.task.FileDownloadTask;
|
|||||||
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
|
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
|
||||||
import org.jackhuang.hmcl.task.Task;
|
import org.jackhuang.hmcl.task.Task;
|
||||||
import org.jackhuang.hmcl.util.Logging;
|
import org.jackhuang.hmcl.util.Logging;
|
||||||
|
import org.jackhuang.hmcl.util.StringUtils;
|
||||||
|
import org.jackhuang.hmcl.util.io.CompressingUtils;
|
||||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||||
import org.jackhuang.hmcl.util.io.IOUtils;
|
import org.jackhuang.hmcl.util.io.IOUtils;
|
||||||
import org.jackhuang.hmcl.util.io.NetworkUtils;
|
import org.jackhuang.hmcl.util.io.NetworkUtils;
|
||||||
@@ -34,13 +36,11 @@ import org.tukaani.xz.XZInputStream;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.FileSystem;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.jar.JarEntry;
|
import java.util.jar.*;
|
||||||
import java.util.jar.JarInputStream;
|
|
||||||
import java.util.jar.JarOutputStream;
|
|
||||||
import java.util.jar.Pack200;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
import static org.jackhuang.hmcl.util.DigestUtils.digest;
|
||||||
@@ -142,6 +142,13 @@ public class LibraryDownloadTask extends Task<Void> {
|
|||||||
library.getDownload().getSha1() != null ? new IntegrityCheck("SHA-1", library.getDownload().getSha1()) : null)
|
library.getDownload().getSha1() != null ? new IntegrityCheck("SHA-1", library.getDownload().getSha1()) : null)
|
||||||
.setCacheRepository(cacheRepository)
|
.setCacheRepository(cacheRepository)
|
||||||
.setCaching(true);
|
.setCaching(true);
|
||||||
|
task.addIntegrityCheckHandler((file, dest) -> {
|
||||||
|
String ext = FileUtils.getExtension(dest).toLowerCase();
|
||||||
|
if (ext.equals("jar")) {
|
||||||
|
JarFile jarFile = new JarFile(file.toFile());
|
||||||
|
jarFile.getManifest();
|
||||||
|
}
|
||||||
|
});
|
||||||
xz = false;
|
xz = false;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ public class FileDownloadTask extends Task<Void> {
|
|||||||
private CacheRepository repository = CacheRepository.getInstance();
|
private CacheRepository repository = CacheRepository.getInstance();
|
||||||
private RandomAccessFile rFile;
|
private RandomAccessFile rFile;
|
||||||
private InputStream stream;
|
private InputStream stream;
|
||||||
|
private final ArrayList<IntegrityCheckHandler> integrityCheckHandlers = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param url the URL of remote file.
|
* @param url the URL of remote file.
|
||||||
@@ -196,6 +197,10 @@ public class FileDownloadTask extends Task<Void> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addIntegrityCheckHandler(IntegrityCheckHandler handler) {
|
||||||
|
integrityCheckHandlers.add(Objects.requireNonNull(handler));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() throws Exception {
|
public void execute() throws Exception {
|
||||||
boolean checkETag;
|
boolean checkETag;
|
||||||
@@ -296,23 +301,27 @@ public class FileDownloadTask extends Task<Void> {
|
|||||||
|
|
||||||
closeFiles();
|
closeFiles();
|
||||||
|
|
||||||
|
if (downloaded != contentLength)
|
||||||
|
throw new IOException("Unexpected file size: " + downloaded + ", expected: " + contentLength);
|
||||||
|
|
||||||
// Restore temp file to original name.
|
// Restore temp file to original name.
|
||||||
if (isCancelled()) {
|
if (isCancelled()) {
|
||||||
temp.toFile().delete();
|
temp.toFile().delete();
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
Files.deleteIfExists(file.toPath());
|
|
||||||
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile()))
|
|
||||||
throw new IOException("Unable to make parent directory " + file);
|
|
||||||
try {
|
|
||||||
FileUtils.moveFile(temp.toFile(), file);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IOException("Unable to move temp file from " + temp + " to " + file, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (downloaded != contentLength)
|
for (IntegrityCheckHandler handler : integrityCheckHandlers) {
|
||||||
throw new IOException("Unexpected file size: " + downloaded + ", expected: " + contentLength);
|
handler.checkIntegrity(temp, file.toPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
Files.deleteIfExists(file.toPath());
|
||||||
|
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile()))
|
||||||
|
throw new IOException("Unable to make parent directory " + file);
|
||||||
|
try {
|
||||||
|
FileUtils.moveFile(temp.toFile(), file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException("Unable to move temp file from " + temp + " to " + file, e);
|
||||||
|
}
|
||||||
|
|
||||||
// Integrity check
|
// Integrity check
|
||||||
if (integrityCheck != null) {
|
if (integrityCheck != null) {
|
||||||
@@ -375,7 +384,7 @@ public class FileDownloadTask extends Task<Void> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Download speed in byte/sec.
|
* Download speed in byte/sec.
|
||||||
* @return
|
* @return download speed
|
||||||
*/
|
*/
|
||||||
public int getSpeed() {
|
public int getSpeed() {
|
||||||
return speed;
|
return speed;
|
||||||
@@ -387,4 +396,13 @@ public class FileDownloadTask extends Task<Void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IntegrityCheckHandler {
|
||||||
|
/**
|
||||||
|
* Check whether the file is corrupted or not.
|
||||||
|
* @param filePath the file locates in (maybe in temp directory)
|
||||||
|
* @param destinationPath for real file name
|
||||||
|
* @throws IOException if the file is corrupted
|
||||||
|
*/
|
||||||
|
void checkIntegrity(Path filePath, Path destinationPath) throws IOException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user