[FEATURE] Rewrite обновления zip архивами часть 1.

This commit is contained in:
Zaxar163 2019-07-19 17:43:36 +03:00
parent 85c1985435
commit 8ebe6983f3
4 changed files with 37 additions and 19 deletions

View file

@ -97,7 +97,7 @@ public void reload() throws Exception {
public static final class Config { public static final class Config {
private transient LaunchServer server = null; private transient LaunchServer server = null;
public boolean zipDownload;
public String projectName; public String projectName;
public String[] mirrors; public String[] mirrors;

View file

@ -36,10 +36,17 @@ public interface DownloadTotalCallback {
public static class DownloadTask { public static class DownloadTask {
public String apply; public String apply;
public long size; public long size;
public boolean isZip;
public DownloadTask(String apply, long size) { public DownloadTask(String apply, long size) {
this.apply = apply; this.apply = apply;
this.size = size; this.size = size;
isZip = false;
}
public DownloadTask(String apply, long size, boolean isZip) {
this.apply = apply;
this.size = size;
this.isZip = isZip;
} }
} }
@ -59,7 +66,7 @@ public void download(String base, List<DownloadTask> applies, Path dstDirFile, D
get.reset(); get.reset();
get.setURI(u); get.setURI(u);
} }
httpclient.execute(get, new FileDownloadResponseHandler(targetPath, apply, callback, totalCallback, false)); httpclient.execute(get, new FileDownloadResponseHandler(targetPath, apply, callback, totalCallback));
} }
} }
} }
@ -118,12 +125,12 @@ public FileDownloadResponseHandler(Path target) {
totalCallback = null; totalCallback = null;
} }
public FileDownloadResponseHandler(Path target, DownloadTask task, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) { public FileDownloadResponseHandler(Path target, DownloadTask task, DownloadCallback callback, DownloadTotalCallback totalCallback) {
this.target = target; this.target = target;
this.task = task; this.task = task;
this.callback = callback; this.callback = callback;
this.totalCallback = totalCallback; this.totalCallback = totalCallback;
this.zip = zip; this.zip = task != null ? task.isZip : false;
} }
public FileDownloadResponseHandler(Path target, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) { public FileDownloadResponseHandler(Path target, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) {
@ -137,16 +144,6 @@ public FileDownloadResponseHandler(Path target, DownloadCallback callback, Downl
@Override @Override
public Path handleResponse(HttpResponse response) throws IOException { public Path handleResponse(HttpResponse response) throws IOException {
InputStream source = response.getEntity().getContent(); InputStream source = response.getEntity().getContent();
int returnCode = response.getStatusLine().getStatusCode();
if(returnCode != 200)
{
throw new IllegalStateException(String.format("Request download file %s return code %d", target.toString(), returnCode));
}
long contentLength = response.getEntity().getContentLength();
if (task != null && contentLength != task.size)
{
LogHelper.warning("Missing content length: expected %d | found %d", task.size, contentLength);
}
if (zip) { if (zip) {
try (ZipInputStream input = IOHelper.newZipInput(source)) { try (ZipInputStream input = IOHelper.newZipInput(source)) {
ZipEntry entry = input.getNextEntry(); ZipEntry entry = input.getNextEntry();
@ -171,13 +168,14 @@ public Path handleResponse(HttpResponse response) throws IOException {
} }
if (callback != null && task != null) { if (callback != null && task != null) {
callback.stateChanged(task.apply, 0, task.size); callback.stateChanged(task.apply, 0, task.size);
transfer(source, this.target, task.apply, task.size, callback, totalCallback); transfer(source, target, task.apply, task.size < 0 ? source.available() : task.size, callback, totalCallback);
} else } else
IOHelper.transfer(source, this.target); IOHelper.transfer(source, IOHelper.newOutput(target));
return this.target; return this.target;
} }
} }
public static void transfer(InputStream input, Path file, String filename, long size, DownloadCallback callback, DownloadTotalCallback totalCallback) throws IOException { public static void transfer(InputStream input, Path file, String filename, long size, DownloadCallback callback, DownloadTotalCallback totalCallback) throws IOException {
try (OutputStream fileOutput = IOHelper.newOutput(file)) { try (OutputStream fileOutput = IOHelper.newOutput(file)) {
long downloaded = 0L; long downloaded = 0L;

View file

@ -211,6 +211,10 @@ public UpdateRequestEvent requestDo(StandartClientWebSocketService service) thro
LogHelper.error(ex); LogHelper.error(ex);
} }
} }
if (isSmaller(((HashedDir)getPathed(name, e.hdir)).zipSize, entry.size())) {
adds.add(new ListDownloader.DownloadTask(path + ".zip", -1, true));
return HashedDir.WalkAction.SKIP_DIR;
}
return HashedDir.WalkAction.CONTINUE; return HashedDir.WalkAction.CONTINUE;
}); });
totalSize = diff.mismatch.size(); totalSize = diff.mismatch.size();
@ -230,6 +234,10 @@ public UpdateRequestEvent requestDo(StandartClientWebSocketService service) thro
return e; return e;
} }
private boolean isSmaller(long zipSize, long size) {
return zipSize != -1 && zipSize < size;
}
// Instance // Instance
@LauncherNetworkAPI @LauncherNetworkAPI
private final String dirName; private final String dirName;
@ -306,4 +314,11 @@ private void updateState(String filePath, long fileDownloaded, long fileSize) {
stateCallback.call(new State(filePath, fileDownloaded, fileSize, stateCallback.call(new State(filePath, fileDownloaded, fileSize,
totalDownloaded, totalSize, Duration.between(startTime, Instant.now()))); totalDownloaded, totalSize, Duration.between(startTime, Instant.now())));
} }
private static HashedEntry getPathed(String path, HashedDir in) {
String[] parts = path.split(IOHelper.CROSS_SEPARATOR);
HashedEntry current = in;
for (String part : parts) current = ((HashedDir) current).getEntry(part);
return current;
}
} }

View file

@ -111,6 +111,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
@LauncherNetworkAPI @LauncherNetworkAPI
private final Map<String, HashedEntry> map = new HashMap<>(32); private final Map<String, HashedEntry> map = new HashMap<>(32);
@LauncherNetworkAPI
public long zipSize = -1;
@LauncherAPI @LauncherAPI
public HashedDir() { public HashedDir() {
} }
@ -350,7 +353,7 @@ public void walk(CharSequence separator, WalkCallback callback) throws IOExcepti
} }
public enum WalkAction { public enum WalkAction {
STOP, CONTINUE STOP, SKIP_DIR, CONTINUE
} }
@FunctionalInterface @FunctionalInterface
@ -375,7 +378,9 @@ private WalkAction walk(String append, CharSequence separator, WalkCallback call
else newAppend = append + separator + entry.getKey(); else newAppend = append + separator + entry.getKey();
WalkAction a = callback.walked(newAppend, entry.getKey(), e); WalkAction a = callback.walked(newAppend, entry.getKey(), e);
if (a == WalkAction.STOP) return a; if (a == WalkAction.STOP) return a;
if (a == WalkAction.CONTINUE)
a = ((HashedDir) e).walk(newAppend, separator, callback, false); a = ((HashedDir) e).walk(newAppend, separator, callback, false);
else a = WalkAction.CONTINUE; // skip
if (a == WalkAction.STOP) return a; if (a == WalkAction.STOP) return a;
} }
} }