diff --git a/src/android/kiwixlibrary.cpp b/src/android/kiwixlibrary.cpp index d2d92010e..5b2b6be22 100644 --- a/src/android/kiwixlibrary.cpp +++ b/src/android/kiwixlibrary.cpp @@ -64,7 +64,7 @@ Java_org_kiwix_kiwixlib_JNIKiwixLibrary_addBook(JNIEnv* env, jobject obj, jstrin book.update(reader); ret = LIBRARY->addBook(book); } catch (std::exception& e) { - __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM main page"); + __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to add the book"); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); ret = false; } diff --git a/src/android/kiwixreader.cpp b/src/android/kiwixreader.cpp index 1196f82c5..91dcec78b 100644 --- a/src/android/kiwixreader.cpp +++ b/src/android/kiwixreader.cpp @@ -225,7 +225,7 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType( } JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( - JNIEnv* env, jobject obj, jstring url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) + JNIEnv* env, jobject obj, jobject url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) { /* Default values */ setStringObjValue("", titleObj, env); @@ -234,21 +234,24 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( jbyteArray data = env->NewByteArray(0); /* Retrieve the content */ - std::string cUrl = jni2c(url, env); + std::string cUrl = getStringObjValue(url, env); unsigned int cSize = 0; try { auto entry = READER->getEntryFromEncodedPath(cUrl); + bool isRedirect = entry.isRedirect(); entry = entry.getFinalEntry(); cSize = entry.getSize(); setIntObjValue(cSize, sizeObj, env); - - data = env->NewByteArray(cSize); - env->SetByteArrayRegion( - data, 0, cSize, reinterpret_cast(entry.getBlob().data())); - setStringObjValue(entry.getMimetype(), mimeTypeObj, env); setStringObjValue(entry.getTitle(), titleObj, env); + if (isRedirect) { + setStringObjValue(entry.getPath(), url, env); + } else { + data = env->NewByteArray(cSize); + env->SetByteArrayRegion( + data, 0, cSize, reinterpret_cast(entry.getBlob().data())); + } } catch (std::exception& e) { __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java b/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java index 4a916753b..5ab779767 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixLibrary.java @@ -26,7 +26,7 @@ public class JNIKiwixLibrary { public native boolean addBook(String path) throws JNIKiwixException; - public JNIKiwixLibrary(String filename) + public JNIKiwixLibrary() { nativeHandle = getNativeLibrary(); } diff --git a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java index e405ef0f4..ea3a3d4b3 100644 --- a/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java +++ b/src/android/org/kiwix/kiwixlib/JNIKiwixReader.java @@ -38,7 +38,21 @@ public class JNIKiwixReader public native String getMimeType(String url); - public native byte[] getContent(String url, + /** + * Get the content of a article. + * + * Return a byte array of the content of the article. + * Set the title, mimeType to the title and mimeType of the article. + * Set the size to the size of the returned array. + * + * If the entry doesn't exist : + * - return a empty byte array + * - set all arguments (except url) to empty/0. + * If the entry exist but is a redirection : + * - return an empty byte array + * - set all arguments (including url) to information of the targeted article. + */ + public native byte[] getContent(JNIKiwixString url, JNIKiwixString title, JNIKiwixString mimeType, JNIKiwixInt size); diff --git a/src/android/utils.h b/src/android/utils.h index 3c164e353..aeb5a4d35 100644 --- a/src/android/utils.h +++ b/src/android/utils.h @@ -114,6 +114,13 @@ inline std::string jni2c(const jstring& val, JNIEnv* env) inline int jni2c(const jint val) { return (int)val; } /* Method to deal with variable passed by reference */ +inline std::string getStringObjValue(const jobject obj, JNIEnv* env) +{ + jclass objClass = env->GetObjectClass(obj); + jfieldID objFid = env->GetFieldID(objClass, "value", "Ljava/lang/String;"); + jstring jstr = (jstring)env->GetObjectField(obj, objFid); + return jni2c(jstr, env); +} inline void setStringObjValue(const std::string& value, const jobject obj, JNIEnv* env) diff --git a/src/book.cpp b/src/book.cpp index 8fae56c79..55f1505a0 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -29,7 +29,9 @@ namespace kiwix { /* Constructor */ -Book::Book() : m_readOnly(false) +Book::Book() : + m_pathValid(false), + m_readOnly(false) { } /* Destructor */ @@ -46,6 +48,7 @@ bool Book::update(const kiwix::Book& other) if (m_path.empty()) { m_path = other.m_path; + m_pathValid = other.m_pathValid; } if (m_url.empty()) { @@ -83,6 +86,7 @@ void Book::update(const kiwix::Reader& reader) m_articleCount = reader.getArticleCount(); m_mediaCount = reader.getMediaCount(); m_size = static_cast(reader.getFileSize()) << 10; + m_pathValid = true; reader.getFavicon(m_favicon, m_faviconMimeType); } diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index c48784e76..07eb3d4d5 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -121,22 +121,24 @@ std::string computeAbsolutePath(const std::string& path, const std::string& rela #else char* cRelativePath = strdup(relativePath.c_str()); #endif - char* token = strtok(cRelativePath, "/"); + char* saveptr = nullptr; + char* token = strtok_r(cRelativePath, "/", &saveptr); while (token != NULL) { if (std::string(token) == "..") { absolutePath = removeLastPathElement(absolutePath, true, false); - token = strtok(NULL, "/"); + token = strtok_r(NULL, "/", &saveptr); } else if (strcmp(token, ".") && strcmp(token, "")) { absolutePath += std::string(token); - token = strtok(NULL, "/"); + token = strtok_r(NULL, "/", &saveptr); if (token != NULL) { absolutePath += SEPARATOR; } } else { - token = strtok(NULL, "/"); + token = strtok_r(NULL, "/", &saveptr); } } + free(cRelativePath); return absolutePath; }