@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);
}
}
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);
}
private val failureCache = CacheBuilder.newBuilder().ticker(ticker).expireAfterWrite(30, MINUTES).build<String, Boolean>()
private val imageCache = CacheBuilder.newBuilder().build<String, LinkedElement>()
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) {
@Test
public void requestIsCancelledForNullMimeType() {
verifySnoopCancelsRequestForMimeType(null, true);
- verify(backgroundFetchCallback, never()).cancelForMimeType(eq(uri), ArgumentMatchers.<String>any());
+ verify(backgroundFetchCallback, never()).shouldCancel(eq(uri), ArgumentMatchers.<String>any(), anyLong());
verify(backgroundFetchCallback).failed(uri);
}
}
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> snoopMetadata = forClass(SnoopMetadata.class);
verify(clientGetter).setMetaSnoop(snoopMetadata.capture());
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<FreenetInterface>()
}
@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