moved download logic to DownloadService
parent
24e09eeec9
commit
fd59c434dc
|
@ -1,6 +1,13 @@
|
|||
package amidst.mojangapi.file;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import amidst.documentation.Immutable;
|
||||
import amidst.logging.AmidstLogger;
|
||||
|
@ -11,28 +18,53 @@ public class DownloadService {
|
|||
private final FilenameService filenameService = new FilenameService();
|
||||
|
||||
public boolean hasServer(VersionListEntryJson version) {
|
||||
return URIUtils.exists(filenameService.getRemoteServerJar(version.getId()));
|
||||
return exists(filenameService.getRemoteServerJar(version.getId()));
|
||||
}
|
||||
|
||||
public boolean hasClient(VersionListEntryJson version) {
|
||||
return URIUtils.exists(filenameService.getRemoteClientJar(version.getId()));
|
||||
return exists(filenameService.getRemoteClientJar(version.getId()));
|
||||
}
|
||||
|
||||
private static boolean exists(String location) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) URIUtils.newURL(location).openConnection();
|
||||
connection.setRequestMethod("HEAD");
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void downloadServer(String prefix, VersionListEntryJson version) throws IOException {
|
||||
URIUtils.download(
|
||||
download(
|
||||
filenameService.getRemoteServerJar(version.getId()),
|
||||
filenameService.getServerJar(prefix, version.getId()));
|
||||
}
|
||||
|
||||
public void downloadClient(String prefix, VersionListEntryJson version) throws IOException {
|
||||
URIUtils.download(
|
||||
download(
|
||||
filenameService.getRemoteClientJar(version.getId()),
|
||||
filenameService.getClientJar(prefix, version.getId()));
|
||||
URIUtils.download(
|
||||
download(
|
||||
filenameService.getRemoteClientJson(version.getId()),
|
||||
filenameService.getClientJson(prefix, version.getId()));
|
||||
}
|
||||
|
||||
private void download(String from, String to) throws IOException {
|
||||
download(URIUtils.newURL(from), Paths.get(to));
|
||||
}
|
||||
|
||||
private void download(URL from, Path to) throws IOException {
|
||||
to.getParent().toFile().mkdirs();
|
||||
if (to.toFile().exists()) {
|
||||
return;
|
||||
}
|
||||
Path part = Paths.get(to.toString() + ".part");
|
||||
InputStream in = URIUtils.newInputStream(from);
|
||||
Files.copy(in, part, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.move(part, to, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
public boolean tryDownloadServer(String prefix, VersionListEntryJson version) {
|
||||
try {
|
||||
downloadServer(prefix, version);
|
||||
|
|
|
@ -6,16 +6,10 @@ import java.io.File;
|
|||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import amidst.documentation.Immutable;
|
||||
|
||||
|
@ -47,40 +41,7 @@ public enum URIUtils {
|
|||
return new BufferedReader(new FileReader(file));
|
||||
}
|
||||
|
||||
public static boolean exists(String location) {
|
||||
try {
|
||||
return exists(newURL(location));
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean exists(URL url) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("HEAD");
|
||||
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void download(String from, String to) throws IOException {
|
||||
download(newURL(from), Paths.get(to));
|
||||
}
|
||||
|
||||
private static void download(URL from, Path to) throws IOException {
|
||||
to.getParent().toFile().mkdirs();
|
||||
if (to.toFile().exists()) {
|
||||
return;
|
||||
}
|
||||
Path part = Paths.get(to.toString() + ".part");
|
||||
InputStream in = newInputStream(from);
|
||||
Files.copy(in, part, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.move(part, to, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
private static BufferedInputStream newInputStream(URL url) throws IOException {
|
||||
public static BufferedInputStream newInputStream(URL url) throws IOException {
|
||||
return new BufferedInputStream(url.openStream());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue