diff --git a/phoenicis-tools/src/main/java/org/phoenicis/tools/http/Downloader.java b/phoenicis-tools/src/main/java/org/phoenicis/tools/http/Downloader.java index f01ed802eb8..9c32430a3b5 100644 --- a/phoenicis-tools/src/main/java/org/phoenicis/tools/http/Downloader.java +++ b/phoenicis-tools/src/main/java/org/phoenicis/tools/http/Downloader.java @@ -29,6 +29,9 @@ 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"; @@ -36,10 +39,20 @@ public class Downloader { 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 onChange) { try { get(new URL(url), new File(localFile), onChange); @@ -48,6 +61,12 @@ public void get(String url, String localFile, Consumer 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 onChange) { try { get(url, new FileOutputStream(localFile), onChange); @@ -56,6 +75,12 @@ public void get(URL url, File localFile, Consumer 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 onChange) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { @@ -66,18 +91,36 @@ public String get(String url, Consumer 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 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 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 onChange) { try { URLConnection connection = url.openConnection(); @@ -87,6 +130,13 @@ private void get(URL url, OutputStream outputStream, Consumer 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 onChange) { float percentage = 0F; @@ -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 onChange) { if (onChange != null) { @@ -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); + } + } }