Skip to content

Commit 9af2d95

Browse files
authored
clear core cache (#418)
1 parent 48d7180 commit 9af2d95

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

app/src/androidTest/java/at/tomtasche/reader/test/CoreTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ private static void copy(InputStream src, File dst) throws IOException {
6464
@Test
6565
public void test() {
6666
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
67-
File outputDir = new File(cacheDir, "output");
67+
File outputPath = new File(cacheDir, "core_output");
68+
File cachePath = new File(cacheDir, "core_cache");
6869

6970
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
7071
coreOptions.inputPath = m_testFile.getAbsolutePath();
71-
coreOptions.outputPath = outputDir.getPath();
72+
coreOptions.outputPath = outputPath.getPath();
73+
coreOptions.cachePath = cachePath.getPath();
7274
coreOptions.editable = true;
7375

7476
CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jcl
9898
JNIEXPORT jobject JNICALL
9999
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
100100
jobject options) {
101+
std::error_code ec;
101102
auto logger = std::make_shared<AndroidLogger>();
102103

103104
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
@@ -121,6 +122,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
121122
jboolean editable = env->GetBooleanField(options, editableField);
122123

123124
std::string outputPathCpp = getStringField(env, optionsClass, options, "outputPath");
125+
std::string cachePathCpp = getStringField(env, optionsClass, options, "cachePath");
124126

125127
jclass listClass = env->FindClass("java/util/List");
126128
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
@@ -195,11 +197,11 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
195197

196198
__android_log_print(ANDROID_LOG_VERBOSE, "smn", "Translate to HTML");
197199

198-
std::string output_tmp = outputPathCpp + "/tmp";
199-
std::filesystem::create_directories(output_tmp);
200-
odr::HtmlService service = odr::html::translate(file, output_tmp, htmlConfig, logger);
200+
std::filesystem::remove_all(cachePathCpp, ec);
201+
std::filesystem::create_directories(cachePathCpp);
202+
odr::HtmlService service = odr::html::translate(file, cachePathCpp, htmlConfig, logger);
201203
odr::Html html = service.bring_offline(outputPathCpp);
202-
std::filesystem::remove_all(output_tmp);
204+
std::filesystem::remove_all(cachePathCpp);
203205

204206
for (const odr::HtmlPage &page: html.pages()) {
205207
jstring pageName = env->NewStringUTF(page.name.c_str());
@@ -318,6 +320,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jcla
318320
jobject options) {
319321
__android_log_print(ANDROID_LOG_INFO, "smn", "host file");
320322

323+
std::error_code ec;
321324
auto logger = std::make_shared<AndroidLogger>();
322325

323326
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
@@ -350,6 +353,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jcla
350353
jboolean editable = env->GetBooleanField(options, editableField);
351354

352355
std::string outputPathCpp = getStringField(env, optionsClass, options, "outputPath");
356+
std::string cachePathCpp = getStringField(env, optionsClass, options, "cachePath");
353357

354358
jclass listClass = env->FindClass("java/util/List");
355359
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
@@ -395,9 +399,9 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jcla
395399
htmlConfig.text_document_margin = paging;
396400
htmlConfig.editable = editable;
397401

398-
std::string output_tmp = outputPathCpp + "/tmp";
399-
std::filesystem::create_directories(output_tmp);
400-
odr::HtmlService service = odr::html::translate(file, output_tmp, htmlConfig, logger);
402+
std::filesystem::remove_all(cachePathCpp, ec);
403+
std::filesystem::create_directories(cachePathCpp);
404+
odr::HtmlService service = odr::html::translate(file, cachePathCpp, htmlConfig, logger);
401405
s_server->connect_service(service, prefixCpp);
402406
odr::HtmlViews htmlViews = service.list_views();
403407

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.webkit.MimeTypeMap;
88

99
import java.io.File;
10+
import java.util.Objects;
1011

1112
import at.tomtasche.reader.nonfree.AnalyticsManager;
1213
import at.tomtasche.reader.nonfree.ConfigManager;
@@ -92,9 +93,13 @@ private void translate(Options options, Result result) throws Exception {
9293

9394
File cacheDirectory = AndroidFileCache.getCacheDirectory(cachedFile);
9495

96+
File coreOutputDirectory = new File(cacheDirectory, "core_output");
97+
File coreCacheDirectory = new File(cacheDirectory, "core_cache");
98+
9599
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
96100
coreOptions.inputPath = cachedFile.getPath();
97-
coreOptions.outputPath = cacheDirectory.getPath();
101+
coreOptions.outputPath = coreOutputDirectory.getPath();
102+
coreOptions.cachePath = coreCacheDirectory.getPath();
98103
coreOptions.password = options.password;
99104
coreOptions.editable = options.translatable;
100105
coreOptions.ooxml = doOoxml;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static class CoreOptions {
5757

5858
public String inputPath;
5959
public String outputPath;
60+
public String cachePath;
6061
}
6162

6263
public static CoreResult parse(CoreOptions options) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class RawLoader extends FileLoader {
2121
private static final String[] MIME_BLACKLIST = {"image/vnd.dwg", "image/g3fax", "image/tiff", "image/vnd.djvu", "image/x-eps", "image/x-tga", "image/x-tga", "audio/amr", "video/3gpp", "video/quicktime", "text/calendar", "text/vcard", "text/rtf"};
2222

2323
private CoreWrapper lastCore;
24-
private CoreWrapper.CoreOptions lastCoreOptions;
2524

2625
public RawLoader(Context context) {
2726
super(context, LoaderType.RAW);
@@ -142,8 +141,6 @@ public void loadSync(Options options) {
142141
coreOptions.txt = true;
143142
coreOptions.pdf = false;
144143

145-
lastCoreOptions = coreOptions;
146-
147144
CoreWrapper.CoreResult coreResult = lastCore.parse(coreOptions);
148145
if (coreResult.exception != null) {
149146
throw coreResult.exception;

0 commit comments

Comments
 (0)