From: David ‘Bombe’ Roden Date: Thu, 11 Apr 2013 05:34:35 +0000 (+0200) Subject: Move download to final location when it has finished. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=774744bb2cd1944021b096fa4c0447d67b44389f;p=xudocci.git Move download to final location when it has finished. --- diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 578e86d..bd60a97 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -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 //