Skip to content

Commit

Permalink
Added check for insufficient space
Browse files Browse the repository at this point in the history
  • Loading branch information
iojw committed Dec 18, 2016
1 parent 9d3bbf4 commit 0b4fbe1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/org/terasology/launcher/game/GameDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.DirectoryUtils;
import org.terasology.launcher.util.DownloadException;
import org.terasology.launcher.util.DownloadUtils;
import org.terasology.launcher.util.FileUtils;
import org.terasology.launcher.util.GuiUtils;
import org.terasology.launcher.util.ProgressListener;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.zip.ZipEntry;

public final class GameDownloader {

Expand Down Expand Up @@ -85,7 +88,17 @@ public File getGameDirectory() {
}

public void download(ProgressListener listener) throws DownloadException {
DownloadUtils.downloadToFile(downloadURL, downloadZipFile, listener);
long contentLength = DownloadUtils.getContentLength(downloadURL);
long availableSpace = downloadZipFile.getParentFile().getUsableSpace();
if (availableSpace >= contentLength) {
if (availableSpace <= contentLength * 2) {
GuiUtils.showWarningMessageDialog(null, BundleUtils.getLabel("message_warning_lowOnSpace"));
}
DownloadUtils.downloadToFile(downloadURL, downloadZipFile, listener);
} else {
logger.error("Insufficient space in " + downloadZipFile.getParent());
GuiUtils.showErrorMessageDialog(null, BundleUtils.getLabel("message_error_insufficientSpace"));
}
}

public boolean extractAfterDownload() {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/terasology/launcher/util/DownloadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -105,6 +106,21 @@ public static void downloadToFile(URL downloadURL, File file, ProgressListener l
}
}

public static long getContentLength(URL downloadURL) throws DownloadException {
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) downloadURL.openConnection();
connection.setRequestMethod("HEAD");
return connection.getContentLengthLong();
} catch (IOException e) {
throw new DownloadException("Could not send HEAD request to HTTP-URL! URL=" + downloadURL, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

private static HttpURLConnection getConnectedDownloadConnection(URL downloadURL) throws DownloadException {
final HttpURLConnection connection;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ message_error_gameDirectory=
message_error_gameDownload_downloadExtract=
message_error_gameDownload_loadVersion=
message_error_gameStart=
message_error_insufficientSpace=
message_error_launcherDirectory=
message_error_launcherInstallationDirectory=
message_error_launcherStart=
Expand All @@ -83,6 +84,7 @@ message_update_installationDirectory=
message_update_latest=
message_update_launcher=
message_update_launcher_title=
message_warning_lowOnSpace=
settings_cancel=
settings_game_buildType_Terasology=
settings_game_buildType_TerasologyStable=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ message_error_gameDirectory=Cannot create or use game installation directory\!
message_error_gameDownload_downloadExtract=The game download and extraction process failed.
message_error_gameDownload_loadVersion=The game downloaded and extracted okay, but required version information was not found.
message_error_gameStart=Could not start the game\!
message_error_insufficientSpace=Insufficient free space in the download directory to download the game\! Aborting...
message_error_launcherDirectory=Cannot create or use launcher directory\!
message_error_launcherInstallationDirectory=Cannot detect or use launcher installation directory\!
message_error_launcherStart=Starting TerasologyLauncher failed\!
Expand All @@ -83,6 +84,7 @@ message_update_installationDirectory=Installation directory\:
message_update_latest=Latest version\:
message_update_launcher=A launcher update is available.\nThe installation directory will be deleted during installation.\nWould you like to update the launcher?
message_update_launcher_title=Launcher update available\!
message_warning_lowOnSpace=The download directory is running low on space\!
settings_cancel=Cancel
settings_game_buildType_Terasology=Development build (normal)
settings_game_buildType_TerasologyStable=Stable release (normal)
Expand Down

0 comments on commit 0b4fbe1

Please sign in to comment.