X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=0fb34b54b98a9abd7accfb177ee7d13445ac173c;hp=c6c4e97e3002a42e1a00b5e87cf30a135c173ad1;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=4115cb08fe44c1ad7b62ca4771020e21fbb02a4d diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index c6c4e97..0fb34b5 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -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; @@ -76,8 +78,6 @@ import freenet.support.io.ResumeFailedException; /** * Contains all necessary functionality for interacting with the Freenet node. - * - * @author David ‘Bombe’ Roden */ @Singleton public class FreenetInterface { @@ -172,15 +172,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); } @@ -339,59 +353,7 @@ public class FreenetInterface { } /** - * Container for a fetched URI and the {@link FetchResult}. - * - * @author David Roden - */ - public static class Fetched { - - /** The fetched URI. */ - private final FreenetURI freenetUri; - - /** The fetch result. */ - private final FetchResult fetchResult; - - /** - * Creates a new fetched URI. - * - * @param freenetUri - * The URI that was fetched - * @param fetchResult - * The fetch result - */ - public Fetched(FreenetURI freenetUri, FetchResult fetchResult) { - this.freenetUri = freenetUri; - this.fetchResult = fetchResult; - } - - // - // ACCESSORS - // - - /** - * Returns the fetched URI. - * - * @return The fetched URI - */ - public FreenetURI getFreenetUri() { - return freenetUri; - } - - /** - * Returns the fetch result. - * - * @return The fetch result - */ - public FetchResult getFetchResult() { - return fetchResult; - } - - } - - /** * Callback for USK watcher events. - * - * @author David ‘Bombe’ Roden */ public static interface Callback { @@ -419,7 +381,6 @@ public class FreenetInterface { * @see ImageInsertStartedEvent * @see ImageInsertFailedEvent * @see ImageInsertFinishedEvent - * @author David ‘Bombe’ Roden */ public class InsertToken implements ClientPutCallback { @@ -542,11 +503,18 @@ public class FreenetInterface { } - public class InsertTokenSupplier implements Function { + public static class InsertTokenSupplier implements Function { + + 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); } }