Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added isUpdateAvailable to Downloader #979

Merged
merged 2 commits into from
Aug 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,30 @@
import java.net.URLConnection;
import java.util.function.Consumer;

/**
* file download utilities (mainly to be used from JS)
*/
@Safe
public class Downloader {
private static final String EXCEPTION_ITEM_DOWNLOAD_FAILED = "Download of %s has failed";
private static final int BLOCK_SIZE = 1024;

private final FileSizeUtilities fileSizeUtilities;

/**
* constructor
* @param fileSizeUtilities
*/
public Downloader(FileSizeUtilities fileSizeUtilities) {
this.fileSizeUtilities = fileSizeUtilities;
}

/**
* downloads url to localFile, shows progress via onChange
* @param url download URL
* @param localFile destination of the download
* @param onChange consumer to show the download progress (e.g. a progress bar)
*/
public void get(String url, String localFile, Consumer<ProgressEntity> onChange) {
try {
get(new URL(url), new File(localFile), onChange);
Expand All @@ -48,6 +61,12 @@ public void get(String url, String localFile, Consumer<ProgressEntity> onChange)
}
}

/**
* downloads url to localFile, shows progress via onChange
* @param url download URL
* @param localFile destination of the download
* @param onChange consumer to show the download progress (e.g. a progress bar)
*/
public void get(URL url, File localFile, Consumer<ProgressEntity> onChange) {
try {
get(url, new FileOutputStream(localFile), onChange);
Expand All @@ -56,6 +75,12 @@ public void get(URL url, File localFile, Consumer<ProgressEntity> onChange) {
}
}

/**
* downloads url and returns downloaded content, shows progress via onChange
* @param url download URL
* @param onChange consumer to show the download progress (e.g. a progress bar)
* @return downloaded content
*/
public String get(String url, Consumer<ProgressEntity> onChange) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
Expand All @@ -66,18 +91,36 @@ public String get(String url, Consumer<ProgressEntity> onChange) {
return outputStream.toString();
}

/**
* downloads url and returns downloaded content, shows progress via onChange
* @param url download URL
* @param onChange consumer to show the download progress (e.g. a progress bar)
* @return downloaded content
*/
public String get(URL url, Consumer<ProgressEntity> onChange) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
get(url, outputStream, onChange);
return outputStream.toString();
}

/**
* downloads url and returns downloaded content, shows progress via onChange
* @param url download URL
* @param onChange consumer to show the download progress (e.g. a progress bar)
* @return downloaded content
*/
public byte[] getBytes(URL url, Consumer<ProgressEntity> onChange) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
get(url, outputStream, onChange);
return outputStream.toByteArray();
}

/**
* downloads url to outputStream, shows progress via onChange
* @param url download URL
* @param outputStream file is downloaded to this stream
* @param onChange consumer to show the download progress (e.g. a progress bar)
*/
private void get(URL url, OutputStream outputStream, Consumer<ProgressEntity> onChange) {
try {
URLConnection connection = url.openConnection();
Expand All @@ -87,6 +130,13 @@ private void get(URL url, OutputStream outputStream, Consumer<ProgressEntity> on
}
}

/**
* downloads url to outputStream, shows progress via onChange
* @param url download URL
* @param connection URLConnection which is used for the download
* @param outputStream file is downloaded to this stream
* @param onChange consumer to show the download progress (e.g. a progress bar)
*/
private void saveConnectionToStream(URL url, URLConnection connection, OutputStream outputStream,
Consumer<ProgressEntity> onChange) {
float percentage = 0F;
Expand Down Expand Up @@ -125,6 +175,13 @@ private void saveConnectionToStream(URL url, URLConnection connection, OutputStr
changeState(ProgressState.SUCCESS, percentage, "", onChange);
}

/**
* updates the progress indicator
* @param state current state (e.g. if download finished)
* @param percentage progress percentage
* @param progressText e.g. downloaded x of y bytes
* @param onChange consumer to show the download progress (e.g. a progress bar)
*/
private void changeState(ProgressState state, float percentage, String progressText,
Consumer<ProgressEntity> onChange) {
if (onChange != null) {
Expand All @@ -134,4 +191,46 @@ private void changeState(ProgressState state, float percentage, String progressT
}
}

/**
* checks if the downloadable file has been updated more recently than localFile
* This can be used to avoid re-downloading existing resources if no new version is available.
* Note: The last modified date must be set correctly by the server.
* @param localFile local file (should have been downloaded from url)
* @param url download URL
* @return update for localFile is available
*/
public boolean isUpdateAvailable(String localFile, String url) {
try {
return isUpdateAvailable(new File(localFile), new URL(url));
} catch (MalformedURLException e) {
throw new DownloadException(String.format(EXCEPTION_ITEM_DOWNLOAD_FAILED, url), e);
}
}

/**
* checks if the downloadable file has been updated more recently than localFile
* This can be used to avoid re-downloading existing resources if no new version is available.
* Note: The last modified date must be set correctly by the server.
* @param localFile local file (should have been downloaded from url)
* @param url download URL
* @return update for localFile is available
*/
public boolean isUpdateAvailable(File localFile, URL url) {
if (!localFile.exists()) {
return true;
}
try {
URLConnection connection = url.openConnection();
connection.connect();
long fileLastModified = localFile.lastModified();
long urlLastModified = connection.getLastModified();
if (fileLastModified == 0 || urlLastModified == 0) {
// we know nothing
return true;
}
return localFile.lastModified() <= connection.getLastModified();
} catch (IOException e) {
throw new DownloadException(String.format(EXCEPTION_ITEM_DOWNLOAD_FAILED, url), e);
}
}
}