Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not put binary illustration data into a string. #74

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/src/main/cpp/libkiwix/illustration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ METHOD0(jstring, url) {
return TO_JNI(THIS->url);
} CATCH_EXCEPTION(nullptr)

GETTER(jstring, getData)
METHOD0(jbyteArray, getData) {
auto data = THIS->getData();
return cArray2jni(data.data(), data.size(), env);
} CATCH_EXCEPTION(nullptr)
2 changes: 1 addition & 1 deletion lib/src/main/java/org/kiwix/libkiwix/Illustration.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Illustration
public native String mimeType();
public native String url();

public native String getData();
public native byte[] getData();
@Override
protected void finalize() { dispose(); }

Expand Down
7 changes: 4 additions & 3 deletions lib/src/test/org/kiwix/test/libkiwix/TestBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
package org.kiwix.test.libkiwix;

import org.kiwix.libkiwix.Book;
import org.kiwix.libkiwix.Illustration;
import org.kiwix.test.libkiwix.TestIllustration;
import org.kiwix.test.libzim.TestArchive;
import java.util.stream.Stream;

public class TestBook
{
Expand Down Expand Up @@ -35,6 +36,6 @@ public class TestBook
public long getMediaCount() { return inner.getMediaCount(); }
public long getSize() { return inner.getSize(); }

public Illustration[] getIllustrations() { return inner.getIllustrations(); }
public Illustration getIllustration(int size) { return inner.getIllustration(size); }
public TestIllustration[] getIllustrations() { return Stream.of(inner.getIllustrations()).map(i -> new TestIllustration(i)).toArray(TestIllustration[]::new); }
public TestIllustration getIllustration(int size) { return new TestIllustration(inner.getIllustration(size)); }
}
16 changes: 16 additions & 0 deletions lib/src/test/org/kiwix/test/libkiwix/TestIllustration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

package org.kiwix.test.libkiwix;

import org.kiwix.libkiwix.Illustration;

public class TestIllustration
{
private Illustration inner;
public TestIllustration(Illustration _inner) { inner = _inner; }

public int width() { return inner.width(); }
public int height() { return inner.height(); }
public String mimeType() { return inner.mimeType(); }
public String url() { return inner.url(); }
public byte[] getData() { return inner.getData(); }
}
44 changes: 38 additions & 6 deletions lib/src/test/test.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private void testLibrary(TestLibrary lib)
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getTitle(), "Test ZIM file");
assertEquals(book.getTags(), "_category:Category;_ftindex:yes;_ftindex:yes;_pictures:yes;_videos:yes;_details:yes");
assertEquals(book.getIllustration(48).width(), 48);

assertEquals(book.getUrl(), "http://localhost/small.zim");
assertEquals(book.getDescription(), "Description");
assertEquals(book.getCreator(), "Creator");
Expand All @@ -313,7 +313,7 @@ private void testLibrary(TestLibrary lib)
assertEquals(book.getArticleCount(), 1);
assertEquals(book.getMediaCount(), 1);
assertEquals(book.getSize(), 66560);
Illustration[] illustrations = book.getIllustrations();
TestIllustration[] illustrations = book.getIllustrations();
assertEquals(1, illustrations.length);

assertEquals(book.getTagStr("video"), "");
Expand All @@ -328,7 +328,15 @@ public void testLibrarySimple() throws IOException {
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");

TestIllustration illustration = book.getIllustration(48);
assertEquals(illustration.width(), 48);
assertEquals(illustration.height(), 48);
assertEquals(illustration.mimeType(), "image/png");
assertEquals(illustration.url(), "");
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertTrue(Arrays.equals(faviconData, illustration.getData()));

assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
Expand All @@ -351,7 +359,15 @@ public void testLibraryXml() throws IOException {
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");

TestIllustration illustration = book.getIllustration(48);
assertEquals(illustration.width(), 48);
assertEquals(illustration.height(), 48);
assertEquals(illustration.mimeType(), "image/png");
assertEquals(illustration.url(), "");
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertTrue(Arrays.equals(faviconData, illustration.getData()));

assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
Expand All @@ -370,7 +386,15 @@ public void testLibraryXmlContent() throws IOException {
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "");

TestIllustration illustration = book.getIllustration(48);
assertEquals(illustration.width(), 48);
assertEquals(illustration.height(), 48);
assertEquals(illustration.mimeType(), "image/png");
assertEquals(illustration.url(), "");
byte[] faviconData = getFileContent("small_zimfile_data/favicon.png");
assertTrue(Arrays.equals(faviconData, illustration.getData()));

assertEquals(book.getPath(), new File("small.zim").getAbsolutePath());
assertEquals(book.getHumanReadableIdFromPath(), "small");
assertTrue(book.isPathValid());
Expand All @@ -389,7 +413,15 @@ public void testLibraryOPDS() throws IOException {
testLibrary(lib);
String[] bookIds = lib.getBooksIds();
TestBook book = lib.getBookById(bookIds[0]);
assertEquals(book.getIllustration(48).url(), "http://localhost/meta?name=favicon&content=small");

TestIllustration illustration = book.getIllustration(48);
assertEquals(illustration.width(), 48);
assertEquals(illustration.height(), 48);
assertEquals(illustration.mimeType(), "image/png");
assertEquals(illustration.url(), "http://localhost/meta?name=favicon&content=small");
// This will try to downoald to the data, but we have no local server. So return empty array.
assertTrue(Arrays.equals(illustration.getData(), new byte[0]));

assertEquals(book.getPath(), "");
assertEquals(book.getHumanReadableIdFromPath(), "");
assertFalse(book.isPathValid());
Expand Down
Loading