Skip to content

Commit 870347c

Browse files
authored
Merge pull request #438 from opendocument-app/replace-magic
Replace magic with odrcore
2 parents 52b3012 + aadd00f commit 870347c

File tree

6 files changed

+49
-57
lines changed

6 files changed

+49
-57
lines changed

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ dependencies {
149149
implementation 'com.google.android.material:material:1.12.0'
150150
implementation 'androidx.webkit:webkit:1.11.0'
151151

152-
implementation 'com.github.huzongyao:AndroidMagic:v1.1.2'
153152
implementation 'com.viliussutkus89:assetextractor-android:1.3.3'
154153

155154
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

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/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: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import android.provider.OpenableColumns;
77
import android.webkit.MimeTypeMap;
88

9-
import com.hzy.libmagic.MagicApi;
10-
119
import java.io.File;
1210
import java.io.FileInputStream;
1311
import java.io.FileNotFoundException;
@@ -21,30 +19,6 @@ public MetadataLoader(Context context) {
2119
super(context, LoaderType.METADATA);
2220
}
2321

24-
private boolean initMagicFromAssets() {
25-
InputStream inputStream = null;
26-
try {
27-
inputStream = context.getAssets().open("magic.mgc");
28-
int length = inputStream.available();
29-
byte[] buffer = new byte[length];
30-
if (inputStream.read(buffer) > 0) {
31-
return MagicApi.loadFromBytes(buffer, MagicApi.MAGIC_MIME_TYPE | MagicApi.MAGIC_COMPRESS_TRANSP) == 0;
32-
}
33-
} catch (Throwable e) {
34-
crashManager.log(e);
35-
} finally {
36-
if (inputStream != null) {
37-
try {
38-
inputStream.close();
39-
} catch (IOException e) {
40-
crashManager.log(e);
41-
}
42-
}
43-
}
44-
45-
return false;
46-
}
47-
4822
@Override
4923
public boolean isSupported(Options options) {
5024
return true;
@@ -113,22 +87,20 @@ public void loadSync(Options options) {
11387
String[] fileSplit = options.filename.split("\\.");
11488
String extension = fileSplit.length > 0 ? fileSplit[fileSplit.length - 1] : "N/A";
11589

116-
String type = null;
90+
String mimetype = null;
11791
try {
118-
if (initMagicFromAssets()) {
119-
type = MagicApi.magicFile(cachedFile.getAbsolutePath());
120-
}
92+
mimetype = CoreWrapper.mimetype(cachedFile.getAbsolutePath());
12193
} catch (Throwable e) {
12294
crashManager.log(e);
12395
}
12496

125-
if (type == null) {
126-
type = context.getContentResolver().getType(uri);
97+
if (mimetype == null) {
98+
mimetype = context.getContentResolver().getType(uri);
12799
}
128100

129-
if (type == null) {
101+
if (mimetype == null) {
130102
try {
131-
type = URLConnection.guessContentTypeFromName(filename);
103+
mimetype = URLConnection.guessContentTypeFromName(filename);
132104
} catch (Exception e) {
133105
// Samsung S7 Edge crashes with java.lang.StringIndexOutOfBoundsException
134106
crashManager.log(e);
@@ -138,27 +110,27 @@ public void loadSync(Options options) {
138110
if (type == null) {
139111
try {
140112
try (InputStream tempStream = new FileInputStream(cachedFile)) {
141-
type = URLConnection.guessContentTypeFromStream(tempStream);
113+
mimetype = URLConnection.guessContentTypeFromStream(tempStream);
142114
}
143115
} catch (Exception e) {
144116
crashManager.log(e);
145117
}
146118
}
147119

148120
if (type != null) {
149-
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
121+
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimetype);
150122
} else {
151-
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(options.fileExtension);
123+
mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(options.fileExtension);
152124
}
153125

154126
if (extension != null) {
155127
options.fileExtension = extension;
156128
}
157-
if (type != null) {
158-
options.fileType = type;
129+
if (mimetype != null) {
130+
options.fileType = mimetype;
159131
}
160132

161-
if ("inode/x-empty".equals(type)) {
133+
if ("inode/x-empty".equals(mimetype)) {
162134
throw new FileNotFoundException();
163135
}
164136

@@ -183,20 +155,4 @@ public void loadSync(Options options) {
183155
callOnError(result, e);
184156
}
185157
}
186-
187-
@Override
188-
public void close() {
189-
super.close();
190-
191-
backgroundHandler.post(new Runnable() {
192-
@Override
193-
public void run() {
194-
try {
195-
MagicApi.close();
196-
} catch (Throwable e) {
197-
crashManager.log(e);
198-
}
199-
}
200-
});
201-
}
202158
}

0 commit comments

Comments
 (0)