Move download to final location when it has finished.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:34:35 +0000 (07:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 05:34:35 +0000 (07:34 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index 578e86d..bd60a97 100644 (file)
@@ -36,6 +36,7 @@ import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
 import net.pterodactylus.irc.event.ConnectionEstablished;
+import net.pterodactylus.irc.event.DccDownloadFinished;
 import net.pterodactylus.irc.event.DccSendReceived;
 import net.pterodactylus.irc.util.MessageCleaner;
 import net.pterodactylus.irc.util.RandomNickname;
@@ -368,6 +369,31 @@ public class Core extends AbstractIdleService {
                }
        }
 
+       /**
+        * Closes the output stream of the download and moves the file to the final
+        * location.
+        *
+        * @param dccDownloadFinished
+        *              The DCC download finished event
+        */
+       @Subscribe
+       public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
+               Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
+               if (download == null) {
+                       /* probably shouldn’t happen. */
+                       return;
+               }
+
+               try {
+                       download.outputStream().close();
+                       File file = new File(download.filename());
+                       file.renameTo(new File(finalDirectory, download.filename()));
+               } catch (IOException ioe1) {
+                       /* TODO - handle all the errors. */
+                       logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
+               }
+       }
+
        //
        // PRIVATE METHODS
        //