From 0347cedd3f539e8b56df12ecf5937a978ecc597d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 26 Nov 2016 12:18:46 +0100 Subject: [PATCH] Allow to cancel download by size --- .../net/pterodactylus/sone/core/FreenetInterface.java | 4 ++-- .../pterodactylus/sone/core/DefaultElementLoader.kt | 4 ++-- .../pterodactylus/sone/core/FreenetInterfaceTest.java | 4 ++-- .../sone/core/DefaultElementLoaderTest.kt | 19 ++++++++++++++----- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 4b33139..946371b 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -178,7 +178,7 @@ public class FreenetInterface { @Override public boolean snoopMetadata(Metadata meta, ClientContext context) { String mimeType = meta.getMIMEType(); - boolean cancel = (mimeType == null) || backgroundFetchCallback.cancelForMimeType(uri, mimeType); + boolean cancel = (mimeType == null) || backgroundFetchCallback.shouldCancel(uri, mimeType, meta.dataLength()); if (cancel) { backgroundFetchCallback.failed(uri); } @@ -196,7 +196,7 @@ public class FreenetInterface { } public interface BackgroundFetchCallback { - boolean cancelForMimeType(@Nonnull FreenetURI uri, @Nonnull String mimeType); + 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); } diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt index 77cf9a8..e07af93 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt @@ -21,8 +21,8 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke private val failureCache = CacheBuilder.newBuilder().ticker(ticker).expireAfterWrite(30, MINUTES).build() private val imageCache = CacheBuilder.newBuilder().build() private val callback = object : FreenetInterface.BackgroundFetchCallback { - override fun cancelForMimeType(uri: FreenetURI, mimeType: String): Boolean { - return !mimeType.startsWith("image/") + override fun shouldCancel(uri: FreenetURI, mimeType: String, size: Long): Boolean { + return !mimeType.startsWith("image/") || (size > 2097152) } override fun loaded(uri: FreenetURI, mimeType: String, data: ByteArray) { diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index cf69136..59ee67d 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -446,7 +446,7 @@ public class FreenetInterfaceTest { @Test public void requestIsCancelledForNullMimeType() { verifySnoopCancelsRequestForMimeType(null, true); - verify(backgroundFetchCallback, never()).cancelForMimeType(eq(uri), ArgumentMatchers.any()); + verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), ArgumentMatchers.any(), anyLong()); verify(backgroundFetchCallback).failed(uri); } @@ -469,7 +469,7 @@ public class FreenetInterfaceTest { } private void verifySnoopCancelsRequestForMimeType(String mimeType, boolean cancel) { - when(backgroundFetchCallback.cancelForMimeType(uri, mimeType)).thenReturn(cancel); + when(backgroundFetchCallback.shouldCancel(eq(uri), eq(mimeType), anyLong())).thenReturn(cancel); freenetInterface.startFetch(uri, backgroundFetchCallback); ArgumentCaptor snoopMetadata = forClass(SnoopMetadata.class); verify(clientGetter).setMetaSnoop(snoopMetadata.capture()); diff --git a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt index fe3f512..5b84007 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/DefaultElementLoaderTest.kt @@ -27,6 +27,8 @@ class DefaultElementLoaderTest { private val freenetURI = FreenetURI(IMAGE_ID) private const val decomposedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/fru%CC%88hstu%CC%88ck.jpg" private const val normalizedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/frühstück.jpg" + private val sizeOkay = 2097152L; + private val sizeNotOkay = sizeOkay + 1; } private val freenetInterface = mock() @@ -53,31 +55,38 @@ class DefaultElementLoaderTest { } @Test - fun `element loader does not cancel on image mime type`() { + fun `element loader does not cancel on image mime type with 2 mib size`() { elementLoader.loadElement(IMAGE_ID) verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) - assertThat(callback.value.cancelForMimeType(freenetURI, "image/png"), `is`(false)) + assertThat(callback.value.shouldCancel(freenetURI, "image/png", sizeOkay), `is`(false)) + } + + @Test + fun `element loader does cancel on image mime type with more than 2 mib size`() { + elementLoader.loadElement(IMAGE_ID) + verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) + assertThat(callback.value.shouldCancel(freenetURI, "image/png", sizeNotOkay), `is`(true)) } @Test fun `element loader does cancel on audio mime type`() { elementLoader.loadElement(IMAGE_ID) verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) - assertThat(callback.value.cancelForMimeType(freenetURI, "audio/mpeg"), `is`(true)) + assertThat(callback.value.shouldCancel(freenetURI, "audio/mpeg", sizeOkay), `is`(true)) } @Test fun `element loader does not cancel on video mime type`() { elementLoader.loadElement(IMAGE_ID) verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) - assertThat(callback.value.cancelForMimeType(freenetURI, "video/mkv"), `is`(true)) + assertThat(callback.value.shouldCancel(freenetURI, "video/mkv", sizeOkay), `is`(true)) } @Test fun `element loader does not cancel on text mime type`() { elementLoader.loadElement(IMAGE_ID) verify(freenetInterface).startFetch(eq(freenetURI), callback.capture()) - assertThat(callback.value.cancelForMimeType(freenetURI, "text/plain"), `is`(true)) + assertThat(callback.value.shouldCancel(freenetURI, "text/plain", sizeOkay), `is`(true)) } @Test -- 2.7.4