Store download-related stuff in the download.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 36768a4..578e86d 100644 (file)
@@ -43,6 +43,7 @@ import net.pterodactylus.xdcc.core.event.BotAdded;
 import net.pterodactylus.xdcc.core.event.CoreStarted;
 import net.pterodactylus.xdcc.data.Bot;
 import net.pterodactylus.xdcc.data.Channel;
+import net.pterodactylus.xdcc.data.Download;
 import net.pterodactylus.xdcc.data.Network;
 import net.pterodactylus.xdcc.data.Pack;
 import net.pterodactylus.xdcc.data.Server;
@@ -72,6 +73,12 @@ public class Core extends AbstractIdleService {
        /** The event bus. */
        private final EventBus eventBus;
 
+       /** The temporary directory to download files to. */
+       private final String temporaryDirectory;
+
+       /** The directory to move finished downloads to. */
+       private final String finalDirectory;
+
        /** The channels that should be monitored. */
        private final Collection<Channel> channels = Sets.newHashSet();
 
@@ -87,6 +94,9 @@ public class Core extends AbstractIdleService {
        /** The currently known bots. */
        private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
 
+       /** The current downloads. */
+       private final Map<String, Download> downloads = Maps.newHashMap();
+
        /** The current DCC receivers. */
        private final Collection<DccReceiver> dccReceivers = Sets.newHashSet();
 
@@ -95,10 +105,16 @@ public class Core extends AbstractIdleService {
         *
         * @param eventBus
         *              The event bus
+        * @param temporaryDirectory
+        *              The directory to download files to
+        * @param finalDirectory
+        *              The directory to move finished files to
         */
        @Inject
-       public Core(EventBus eventBus) {
+       public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
                this.eventBus = eventBus;
+               this.temporaryDirectory = temporaryDirectory;
+               this.finalDirectory = finalDirectory;
        }
 
        //
@@ -179,6 +195,9 @@ public class Core extends AbstractIdleService {
                        return;
                }
 
+               Download download = new Download(bot, pack);
+               downloads.put(pack.name(), download);
+
                try {
                        connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
                } catch (IOException ioe1) {
@@ -325,10 +344,23 @@ public class Core extends AbstractIdleService {
         */
        @Subscribe
        public void dccSendReceived(DccSendReceived dccSendReceived) {
+               Optional<Network> network = getNetwork(dccSendReceived.connection());
+               if (!network.isPresent()) {
+                       return;
+               }
+
+               Download download = downloads.get(dccSendReceived.filename());
+               if (download == null) {
+                       /* unknown download, ignore. */
+                       return;
+               }
+
                logger.info(String.format("Starting download of %s.", dccSendReceived.filename()));
                try {
-                       OutputStream fileOutputStream = new FileOutputStream(new File("/home/bombe/Temp", dccSendReceived.filename()));
+                       File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
+                       OutputStream fileOutputStream = new FileOutputStream(outputFile);
                        DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
+                       download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
                        dccReceivers.add(dccReceiver);
                        dccReceiver.start();
                } catch (FileNotFoundException fnfe1) {