HttpDownloader

This commit is contained in:
Gravit 2018-09-19 21:37:12 +07:00
parent 85559c0ea3
commit a7dcfc4183
5 changed files with 73 additions and 31 deletions

View file

@ -0,0 +1,32 @@
package ru.gravit.launchserver.command.basic;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.HttpDownloader;
import ru.gravit.utils.helper.LogHelper;
import java.net.URL;
public class TestCommand extends Command {
public TestCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return "Test command. Only developer!";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args,1);
LogHelper.debug("start downloading");
HttpDownloader.downloadFile(new URL(args[0]),"test.html");
LogHelper.debug("end downloading");
}
}

View file

@ -12,6 +12,7 @@
import java.util.concurrent.ConcurrentHashMap;
import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launchserver.command.basic.*;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launchserver.LaunchServer;
@ -22,18 +23,6 @@
import ru.gravit.launchserver.command.auth.UUIDToUsernameCommand;
import ru.gravit.launchserver.command.auth.UnbanCommand;
import ru.gravit.launchserver.command.auth.UsernameToUUIDCommand;
import ru.gravit.launchserver.command.basic.BuildCommand;
import ru.gravit.launchserver.command.basic.ClearCommand;
import ru.gravit.launchserver.command.basic.DebugCommand;
import ru.gravit.launchserver.command.basic.GCCommand;
import ru.gravit.launchserver.command.basic.HelpCommand;
import ru.gravit.launchserver.command.basic.LogConnectionsCommand;
import ru.gravit.launchserver.command.basic.ProguardCleanCommand;
import ru.gravit.launchserver.command.basic.RebindCommand;
import ru.gravit.launchserver.command.basic.RegenProguardDictCommand;
import ru.gravit.launchserver.command.basic.RemoveMappingsProguardCommand;
import ru.gravit.launchserver.command.basic.StopCommand;
import ru.gravit.launchserver.command.basic.VersionCommand;
import ru.gravit.launchserver.command.hash.DownloadAssetCommand;
import ru.gravit.launchserver.command.hash.DownloadClientCommand;
import ru.gravit.launchserver.command.hash.IndexAssetCommand;
@ -112,6 +101,7 @@ protected CommandHandler(LaunchServer server) {
registerCommand("logConnections", new LogConnectionsCommand(server));
registerCommand("loadModule", new LoadModuleCommand(server));
registerCommand("modules", new ModulesCommand(server));
registerCommand("test",new TestCommand(server));
// Register sync commands
registerCommand("indexAsset", new IndexAssetCommand(server));

View file

@ -1,13 +1,11 @@
package ru.gravit.launchserver.command.hash;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import ru.gravit.utils.HttpDownloader;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.launcher.profiles.ClientProfile.Version;
@ -17,20 +15,6 @@
public final class DownloadAssetCommand extends Command {
private static final String ASSET_URL_MASK = "http://launcher.sashok724.net/download/assets/%s.zip";
public static void unpack(URL url, Path dir) throws IOException {
try (ZipInputStream input = IOHelper.newZipInput(url)) {
for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) {
if (entry.isDirectory())
continue; // Skip directories
// Unpack entry
String name = entry.getName();
LogHelper.subInfo("Downloading file: '%s'", name);
IOHelper.transfer(input, dir.resolve(IOHelper.toPath(name)));
}
}
}
public DownloadAssetCommand(LaunchServer server) {
super(server);
}
@ -58,7 +42,7 @@ public void invoke(String... args) throws Exception {
// Download required asset
LogHelper.subInfo("Downloading asset, it may take some time");
unpack(new URL(String.format(ASSET_URL_MASK, IOHelper.urlEncode(version.name))), assetDir);
HttpDownloader.downloadZip(new URL(String.format(ASSET_URL_MASK, IOHelper.urlEncode(version.name))), assetDir);
// Finished
server.syncUpdatesDir(Collections.singleton(dirName));

View file

@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.Collections;
import ru.gravit.utils.HttpDownloader;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.launcher.profiles.ClientProfile;
@ -49,7 +50,7 @@ public void invoke(String... args) throws IOException, CommandException {
// Download required client
LogHelper.subInfo("Downloading client, it may take some time");
DownloadAssetCommand.unpack(new URL(String.format(CLIENT_URL_MASK,
HttpDownloader.downloadZip(new URL(String.format(CLIENT_URL_MASK,
IOHelper.urlEncode(version.name))), clientDir);
// Create profile file

View file

@ -0,0 +1,35 @@
package ru.gravit.utils;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class HttpDownloader {
public static void downloadFile(URL url, String file) throws IOException {
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
public static void downloadZip(URL url, Path dir) throws IOException {
try (ZipInputStream input = IOHelper.newZipInput(url)) {
for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) {
if (entry.isDirectory())
continue; // Skip directories
// Unpack entry
String name = entry.getName();
LogHelper.subInfo("Downloading file: '%s'", name);
IOHelper.transfer(input, dir.resolve(IOHelper.toPath(name)));
}
}
}
}