mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +03:00
[FEATURE][EXPERIMENTAL] Скачивание zip архивом
This commit is contained in:
parent
46d3d11e15
commit
b98aba374a
4 changed files with 62 additions and 3 deletions
|
@ -263,6 +263,11 @@ public static class ExeConf {
|
|||
public String txtFileVersion;
|
||||
public String txtProductVersion;
|
||||
}
|
||||
public static class NettyUpdatesBind
|
||||
{
|
||||
public String url;
|
||||
public boolean zip;
|
||||
}
|
||||
|
||||
public class LauncherConf {
|
||||
public String guardType;
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class ListDownloader {
|
||||
@FunctionalInterface
|
||||
|
@ -55,10 +57,21 @@ public void download(String base, List<DownloadTask> applies, Path dstDirFile, D
|
|||
get.reset();
|
||||
get.setURI(u);
|
||||
}
|
||||
httpclient.execute(get, new FileDownloadResponseHandler(dstDirFile.resolve(apply.apply), apply, callback, totalCallback));
|
||||
httpclient.execute(get, new FileDownloadResponseHandler(dstDirFile.resolve(apply.apply), apply, callback, totalCallback, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void downloadZip(String base, Path dstDirFile, DownloadCallback callback, DownloadTotalCallback totalCallback) throws IOException, URISyntaxException {
|
||||
try (CloseableHttpClient httpclient = HttpClients.custom()
|
||||
.setRedirectStrategy(new LaxRedirectStrategy())
|
||||
.build()) {
|
||||
HttpGet get;
|
||||
URI u = new URL(base).toURI();
|
||||
LogHelper.debug("Download ZIP URL: %s", u.toString());
|
||||
get = new HttpGet(u);
|
||||
httpclient.execute(get, new FileDownloadResponseHandler(dstDirFile, callback, totalCallback, true));
|
||||
}
|
||||
}
|
||||
|
||||
public void downloadOne(String url, Path target) throws IOException, URISyntaxException {
|
||||
try (CloseableHttpClient httpclient = HttpClients.custom()
|
||||
|
@ -78,24 +91,48 @@ static class FileDownloadResponseHandler implements ResponseHandler<Path> {
|
|||
private final DownloadTask task;
|
||||
private final DownloadCallback callback;
|
||||
private final DownloadTotalCallback totalCallback;
|
||||
private final boolean zip;
|
||||
|
||||
public FileDownloadResponseHandler(Path target) {
|
||||
this.target = target;
|
||||
this.task = null;
|
||||
this.zip = false;
|
||||
callback = null;
|
||||
totalCallback = null;
|
||||
}
|
||||
|
||||
public FileDownloadResponseHandler(Path target, DownloadTask task, DownloadCallback callback, DownloadTotalCallback totalCallback) {
|
||||
public FileDownloadResponseHandler(Path target, DownloadTask task, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) {
|
||||
this.target = target;
|
||||
this.task = task;
|
||||
this.callback = callback;
|
||||
this.totalCallback = totalCallback;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
public FileDownloadResponseHandler(Path target, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) {
|
||||
this.target = target;
|
||||
this.task = null;
|
||||
this.callback = callback;
|
||||
this.totalCallback = totalCallback;
|
||||
this.zip = zip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path handleResponse(HttpResponse response) throws IOException {
|
||||
InputStream source = response.getEntity().getContent();
|
||||
if(zip)
|
||||
{
|
||||
try(ZipInputStream input = IOHelper.newZipInput(source))
|
||||
{
|
||||
ZipEntry entry = input.getNextEntry();
|
||||
long size = entry.getSize();
|
||||
String filename = entry.getName();
|
||||
Path target = this.target.resolve(filename);
|
||||
LogHelper.dev("Resolved filename %s to %s", filename, target.toAbsolutePath().toString());
|
||||
transfer(source, target, filename, size, callback, totalCallback);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (callback != null && task != null) {
|
||||
callback.stateChanged(task.apply, 0, task.size);
|
||||
transfer(source, this.target, task.apply, task.size, callback, totalCallback);
|
||||
|
|
|
@ -9,6 +9,8 @@ public class UpdateRequestEvent extends RequestEvent {
|
|||
public HashedDir hdir;
|
||||
@LauncherNetworkAPI
|
||||
public String url;
|
||||
@LauncherNetworkAPI
|
||||
public boolean zip;
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
|
@ -17,10 +19,18 @@ public String getType() {
|
|||
|
||||
public UpdateRequestEvent(HashedDir hdir) {
|
||||
this.hdir = hdir;
|
||||
this.zip = false;
|
||||
}
|
||||
|
||||
public UpdateRequestEvent(HashedDir hdir, String url) {
|
||||
this.hdir = hdir;
|
||||
this.url = url;
|
||||
this.zip = false;
|
||||
}
|
||||
|
||||
public UpdateRequestEvent(HashedDir hdir, String url, boolean zip) {
|
||||
this.hdir = hdir;
|
||||
this.url = url;
|
||||
this.zip = zip;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -191,7 +191,14 @@ public UpdateRequestEvent requestDo(StandartClientWebSocketService service) thro
|
|||
startTime = Instant.now();
|
||||
updateState("UnknownFile", 0L, 100);
|
||||
ListDownloader listDownloader = new ListDownloader();
|
||||
listDownloader.download(e.url, adds, dir, this::updateState, (add) -> totalDownloaded += add);
|
||||
if(e.zip)
|
||||
{
|
||||
listDownloader.downloadZip(e.url, dir, this::updateState, (add) -> totalDownloaded += add);
|
||||
}
|
||||
else
|
||||
{
|
||||
listDownloader.download(e.url, adds, dir, this::updateState, (add) -> totalDownloaded += add);
|
||||
}
|
||||
deleteExtraDir(dir, diff.extra, diff.extra.flag);
|
||||
LogHelper.debug("Update success");
|
||||
return e;
|
||||
|
|
Loading…
Reference in a new issue