Rename fetch action methods
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
index c6c4e97..7d1b007 100644 (file)
@@ -32,6 +32,7 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.annotation.Nonnull;
+import javax.inject.Inject;
 
 import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent;
 import net.pterodactylus.sone.core.event.ImageInsertFailedEvent;
@@ -43,7 +44,6 @@ import net.pterodactylus.sone.data.TemporaryImage;
 
 import com.google.common.base.Function;
 import com.google.common.eventbus.EventBus;
-import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
 import freenet.client.ClientMetadata;
@@ -55,12 +55,14 @@ import freenet.client.HighLevelSimpleClient;
 import freenet.client.InsertBlock;
 import freenet.client.InsertContext;
 import freenet.client.InsertException;
+import freenet.client.Metadata;
 import freenet.client.async.BaseClientPutter;
 import freenet.client.async.ClientContext;
 import freenet.client.async.ClientGetCallback;
 import freenet.client.async.ClientGetter;
 import freenet.client.async.ClientPutCallback;
 import freenet.client.async.ClientPutter;
+import freenet.client.async.SnoopMetadata;
 import freenet.client.async.USKCallback;
 import freenet.keys.FreenetURI;
 import freenet.keys.InsertableClientSSK;
@@ -172,15 +174,29 @@ public class FreenetInterface {
                                return imageLoader;
                        }
                };
+               SnoopMetadata snoop = new SnoopMetadata() {
+                       @Override
+                       public boolean snoopMetadata(Metadata meta, ClientContext context) {
+                               String mimeType = meta.getMIMEType();
+                               boolean cancel = (mimeType == null) || backgroundFetchCallback.shouldCancel(uri, mimeType, meta.dataLength());
+                               if (cancel) {
+                                       backgroundFetchCallback.failed(uri);
+                               }
+                               return cancel;
+                       }
+               };
                FetchContext fetchContext = client.getFetchContext();
                try {
-                       client.fetch(uri, 1048576, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
+                       ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
+                       clientGetter.setMetaSnoop(snoop);
+                       clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext);
                } catch (FetchException fe) {
                        /* stupid exception that can not actually be thrown! */
                }
        }
 
        public interface BackgroundFetchCallback {
+               boolean shouldCancel(@Nonnull FreenetURI uri, @Nonnull String mimeType, long size);
                void loaded(@Nonnull FreenetURI uri, @Nonnull String mimeType, @Nonnull byte[] data);
                void failed(@Nonnull FreenetURI uri);
        }
@@ -542,11 +558,18 @@ public class FreenetInterface {
 
        }
 
-       public class InsertTokenSupplier implements Function<Image, InsertToken> {
+       public static class InsertTokenSupplier implements Function<Image, InsertToken> {
+
+               private final FreenetInterface freenetInterface;
+
+               @Inject
+               public InsertTokenSupplier(FreenetInterface freenetInterface) {
+                       this.freenetInterface = freenetInterface;
+               }
 
                @Override
                public InsertToken apply(Image image) {
-                       return new InsertToken(image);
+                       return freenetInterface.new InsertToken(image);
                }
 
        }