[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 {
private transient LaunchServer server = null;
public boolean zipDownload;
public String projectName;
public String[] mirrors;

View file

@ -36,10 +36,17 @@ public interface DownloadTotalCallback {
public static class DownloadTask {
public String apply;
public long size;
public boolean isZip;
public DownloadTask(String apply, long size) {
this.apply = apply;
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.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;
}
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.task = task;
this.callback = callback;
this.totalCallback = totalCallback;
this.zip = zip;
this.zip = task != null ? task.isZip : false;
}
public FileDownloadResponseHandler(Path target, DownloadCallback callback, DownloadTotalCallback totalCallback, boolean zip) {
@ -137,16 +144,6 @@ public FileDownloadResponseHandler(Path target, DownloadCallback callback, Downl
@Override
public Path handleResponse(HttpResponse response) throws IOException {
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) {
try (ZipInputStream input = IOHelper.newZipInput(source)) {
ZipEntry entry = input.getNextEntry();
@ -171,13 +168,14 @@ public Path handleResponse(HttpResponse response) throws IOException {
}
if (callback != null && task != null) {
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
IOHelper.transfer(source, this.target);
IOHelper.transfer(source, IOHelper.newOutput(target));
return this.target;
}
}
public static void transfer(InputStream input, Path file, String filename, long size, DownloadCallback callback, DownloadTotalCallback totalCallback) throws IOException {
try (OutputStream fileOutput = IOHelper.newOutput(file)) {
long downloaded = 0L;

View file

@ -211,6 +211,10 @@ public UpdateRequestEvent requestDo(StandartClientWebSocketService service) thro
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;
});
totalSize = diff.mismatch.size();
@ -230,6 +234,10 @@ public UpdateRequestEvent requestDo(StandartClientWebSocketService service) thro
return e;
}
private boolean isSmaller(long zipSize, long size) {
return zipSize != -1 && zipSize < size;
}
// Instance
@LauncherNetworkAPI
private final String dirName;
@ -306,4 +314,11 @@ private void updateState(String filePath, long fileDownloaded, long fileSize) {
stateCallback.call(new State(filePath, fileDownloaded, fileSize,
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
private final Map<String, HashedEntry> map = new HashMap<>(32);
@LauncherNetworkAPI
public long zipSize = -1;
@LauncherAPI
public HashedDir() {
}
@ -350,7 +353,7 @@ public void walk(CharSequence separator, WalkCallback callback) throws IOExcepti
}
public enum WalkAction {
STOP, CONTINUE
STOP, SKIP_DIR, CONTINUE
}
@FunctionalInterface
@ -375,7 +378,9 @@ private WalkAction walk(String append, CharSequence separator, WalkCallback call
else newAppend = append + separator + entry.getKey();
WalkAction a = callback.walked(newAppend, entry.getKey(), e);
if (a == WalkAction.STOP) return a;
if (a == WalkAction.CONTINUE)
a = ((HashedDir) e).walk(newAppend, separator, callback, false);
else a = WalkAction.CONTINUE; // skip
if (a == WalkAction.STOP) return a;
}
}