Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Adds download manager, prepares for large file downloads

AuthorDaveJarvis <email>
Date2022-12-17 17:36:42 GMT-0800
Commit8d794de1d2a881c4622f43a5934d45bc205d582c
Parente767901
Delta97 lines added, 1 line removed, 96-line increase
src/main/resources/com/keenwrite/messages.properties
Main.status.image.request.init=Initializing HTTP request
-Main.status.image.request.fetch=Requesting content type from ''{0}''
+Main.status.image.request.fetch=Downloaded image ''{0}''
Main.status.image.request.success=Determined content type ''{0}''
Main.status.image.request.error.media=No media type for ''{0}''
src/main/java/com/keenwrite/io/downloads/events/DownloadEvent.java
+/* Copyright 2022 White Magic Software, Ltd. -- All rights reserved. */
+package com.keenwrite.io.downloads.events;
+
+import com.keenwrite.events.AppEvent;
+
+import java.net.URL;
+import java.time.Instant;
+
+/**
+ * The parent class to all download-related status events.
+ */
+public class DownloadEvent implements AppEvent {
+
+ private final Instant mInstant = Instant.now();
+ private final URL mUrl;
+
+ /**
+ * Constructs a new event that tracks the status of downloading a file.
+ *
+ * @param url The {@link URL} that has triggered a download event.
+ */
+ public DownloadEvent( final URL url ) {
+ mUrl = url;
+ }
+
+ /**
+ * Returns the download link as an instance of {@link URL}.
+ *
+ * @return The {@link URL} being downloaded.
+ */
+ public URL getUrl() {
+ return mUrl;
+ }
+
+ /**
+ * Returns the moment in time that this event was published.
+ *
+ * @return The published date and time.
+ */
+ public Instant when() {
+ return mInstant;
+ }
+}
src/main/java/com/keenwrite/io/downloads/events/DownloadFailedEvent.java
+/* Copyright 2022 White Magic Software, Ltd. -- All rights reserved. */
+package com.keenwrite.io.downloads.events;
+
+import java.net.URL;
+
+public class DownloadFailedEvent extends DownloadEvent {
+
+ private final int mResponseCode;
+
+ /**
+ * Constructs a new event that indicates downloading a file was not
+ * successful.
+ *
+ * @param url The {@link URL} that has triggered a download event.
+ * @param responseCode The HTTP response code associated with the failure.
+ */
+ public DownloadFailedEvent( final URL url, final int responseCode ) {
+ super( url );
+
+ mResponseCode = responseCode;
+ }
+
+ public static void fire( final URL url, final int responseCode ) {
+ new DownloadFailedEvent( url, responseCode ).publish();
+ }
+
+ /**
+ * Returns the HTTP response code for a failed download.
+ *
+ * @return An HTTP response code.
+ */
+ public int getResponseCode() {
+ return mResponseCode;
+ }
+}
src/main/java/com/keenwrite/io/downloads/events/DownloadStartedEvent.java
+/* Copyright 2022 White Magic Software, Ltd. -- All rights reserved. */
+package com.keenwrite.io.downloads.events;
+
+import java.net.URL;
+
+/**
+ * Collates information about a document that has started downloading.
+ */
+public class DownloadStartedEvent extends DownloadEvent {
+
+ public DownloadStartedEvent( final URL url ) {
+ super( url );
+ }
+
+ public static void fire( final URL url ) {
+ new DownloadStartedEvent( url ).publish();
+ }
+}