Skip to content

Commit cb1f723

Browse files
committed
use odrcore magic
1 parent a71ce91 commit cb1f723

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

app/conandeployer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ def deploy(graph, output_folder: str, **kwargs):
5252
f"{output_folder}/fontconfig",
5353
**copytree_kwargs,
5454
)
55+
56+
if "libmagic" in deps:
57+
dep = deps["libmagic"]
58+
conanfile.output.info(f"Deploying libmagic to {output_folder}")
59+
shutil.copytree(
60+
f"{dep.package_folder}/res",
61+
f"{output_folder}/libmagic",
62+
**copytree_kwargs,
63+
)

app/conanfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[requires]
2-
odrcore/5.0.7
2+
odrcore/5.1.0
33

44
[generators]
55
CMakeToolchain

app/src/main/cpp/core_wrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,29 @@ Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jcl
8888
std::string fontconfigDataPath = getStringField(env, paramsClass, params, "fontconfigDataPath");
8989
std::string popplerDataPath = getStringField(env, paramsClass, params, "popplerDataPath");
9090
std::string pdf2htmlexDataPath = getStringField(env, paramsClass, params, "pdf2htmlexDataPath");
91+
std::string libmagicDatabasePath = getStringField(env, paramsClass, params, "libmagicDatabasePath");
9192
std::string customTmpfilePath = getStringField(env, paramsClass, params, "customTmpfilePath");
9293

9394
odr::GlobalParams::set_odr_core_data_path(odrCoreDataPath);
9495
odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath);
9596
odr::GlobalParams::set_poppler_data_path(popplerDataPath);
9697
odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath);
98+
odr::GlobalParams::set_libmagic_database_path(libmagicDatabasePath);
9799

98100
tmpfile_hack::set_tmpfile_directory(customTmpfilePath);
99101
}
100102

103+
JNIEXPORT jstring JNICALL
104+
Java_at_tomtasche_reader_background_CoreWrapper_mimetypeNative(JNIEnv *env, jclass clazz, jstring path) {
105+
auto logger = std::make_shared<AndroidLogger>();
106+
107+
std::string pathCpp = convertString(env, path);
108+
std::string mimetypeCpp = std::string(odr::mimetype(pathCpp));
109+
jstring mimetype = env->NewStringUTF(mimetypeCpp.c_str());
110+
111+
return mimetype;
112+
}
113+
101114
JNIEXPORT jobject JNICALL
102115
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
103116
jobject options) {

app/src/main/cpp/core_wrapper.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ JNIEXPORT void JNICALL
88
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
99
jobject params);
1010

11+
JNIEXPORT jstring JNICALL
12+
Java_at_tomtasche_reader_background_CoreWrapper_mimetypeNative(JNIEnv *env, jclass clazz,
13+
jstring path);
14+
1115
JNIEXPORT jobject JNICALL
1216
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
1317
jobject options);

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public static class GlobalParams {
1818
public String fontconfigDataPath;
1919
public String popplerDataPath;
2020
public String pdf2htmlexDataPath;
21+
public String libmagicDatabasePath;
22+
2123
public String customTmpfilePath;
2224
}
2325

@@ -29,23 +31,32 @@ public static void initialize(Context context) {
2931
File fontconfigDataDirectory = new File(assetsDirectory, "fontconfig");
3032
File popplerDataDirectory = new File(assetsDirectory, "poppler");
3133
File pdf2htmlexDataDirectory = new File(assetsDirectory, "pdf2htmlex");
34+
File libmagicDataDirectory = new File(assetsDirectory, "libmagic");
3235

3336
AssetExtractor ae = new AssetExtractor(context.getAssets());
3437
ae.setOverwrite();
3538
ae.extract(assetsDirectory, "core/odrcore");
3639
ae.extract(assetsDirectory, "core/fontconfig");
3740
ae.extract(assetsDirectory, "core/poppler");
3841
ae.extract(assetsDirectory, "core/pdf2htmlex");
42+
ae.extract(assetsDirectory, "core/libmagic");
3943

4044
CoreWrapper.GlobalParams globalParams = new CoreWrapper.GlobalParams();
4145
globalParams.coreDataPath = odrCoreDataDirectory.getAbsolutePath();
4246
globalParams.fontconfigDataPath = fontconfigDataDirectory.getAbsolutePath();
4347
globalParams.popplerDataPath = popplerDataDirectory.getAbsolutePath();
4448
globalParams.pdf2htmlexDataPath = pdf2htmlexDataDirectory.getAbsolutePath();
49+
globalParams.libmagicDatabasePath = new File(libmagicDataDirectory, "magic.mgc").getAbsolutePath();
4550
globalParams.customTmpfilePath = context.getCacheDir().getAbsolutePath();
4651
CoreWrapper.setGlobalParams(globalParams);
4752
}
4853

54+
public static String mimetype(String path) {
55+
return mimetypeNative(path);
56+
}
57+
58+
private static native String mimetypeNative(String path);
59+
4960
public static class CoreOptions {
5061
public boolean ooxml;
5162
public boolean txt;

app/src/main/java/at/tomtasche/reader/background/MetadataLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ public void loadSync(Options options) {
8989

9090
String mimetype = null;
9191
try {
92-
// todo core get mimetype
93-
//type = MagicApi.magicFile(cachedFile.getAbsolutePath());
92+
mimetype = CoreWrapper.mimetype(cachedFile.getAbsolutePath());
9493
} catch (Throwable e) {
9594
crashManager.log(e);
9695
}

0 commit comments

Comments
 (0)