-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathRawLoader.java
More file actions
198 lines (156 loc) · 7.84 KB
/
RawLoader.java
File metadata and controls
198 lines (156 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package at.tomtasche.reader.background;
import android.content.Context;
import android.net.Uri;
import android.util.Base64;
import android.util.Base64OutputStream;
import android.webkit.MimeTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class RawLoader extends FileLoader {
private static final boolean USE_CORE_TXT = true;
private static final String[] MIME_WHITELIST = {"text/", "image/", "video/", "audio/", "application/json", "application/xml", "application/zip"};
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"};
private CoreWrapper lastCore;
public RawLoader(Context context) {
super(context, LoaderType.RAW);
}
@Override
public boolean isSupported(Options options) {
String fileType = options.fileType;
for (String mime : MIME_WHITELIST) {
if (!fileType.startsWith(mime)) {
continue;
}
for (String blackMime : MIME_BLACKLIST) {
if (fileType.startsWith(blackMime)) {
return false;
}
}
return true;
}
return false;
}
@Override
public void loadSync(Options options) {
final Result result = new Result();
result.options = options;
result.loaderType = type;
try {
String fileType = options.fileType;
String extension = options.fileExtension;
File cacheFile = AndroidFileCache.getCacheFile(context, options.cacheUri);
File cacheDirectory = AndroidFileCache.getCacheDirectory(cacheFile);
Uri finalUri;
if (fileType.startsWith("image/")) {
File htmlFile = new File(cacheDirectory, "image.html");
InputStream htmlStream = context.getAssets().open("image.html");
StreamUtil.copy(htmlStream, htmlFile);
// use jpg as a workaround for most images
extension = "jpg";
if (fileType.contains("svg")) {
// browser does not recognize SVG if it's not called ".svg"
extension = "svg";
}
File imageFile = new File(cacheDirectory, "image." + extension);
StreamUtil.copy(cacheFile, imageFile);
finalUri = Uri.fromFile(htmlFile).buildUpon().appendQueryParameter("ext", extension).build();
} else if (fileType.startsWith("audio/")) {
File htmlFile = new File(cacheDirectory, "audio.html");
InputStream htmlStream = context.getAssets().open("audio.html");
StreamUtil.copy(htmlStream, htmlFile);
// use mp3 as a workaround for most images
extension = "mp3";
File audioFile = new File(cacheDirectory, "audio." + extension);
StreamUtil.copy(cacheFile, audioFile);
finalUri = Uri.fromFile(htmlFile).buildUpon().appendQueryParameter("ext", extension).build();
} else if (fileType.startsWith("video/")) {
File htmlFile = new File(cacheDirectory, "video.html");
InputStream htmlStream = context.getAssets().open("video.html");
StreamUtil.copy(htmlStream, htmlFile);
// use mp4 as a workaround for most images
extension = "mp4";
File videoFile = new File(cacheDirectory, "video." + extension);
StreamUtil.copy(cacheFile, videoFile);
finalUri = Uri.fromFile(htmlFile).buildUpon().appendQueryParameter("ext", extension).build();
} else if (extension.equals("csv")) {
File htmlFile = new File(cacheDirectory, "text.html");
InputStream htmlPrefixStream = context.getAssets().open("text-prefix.html");
InputStream htmlSuffixStream = context.getAssets().open("text-suffix.html");
OutputStream outputStream = new FileOutputStream(htmlFile);
try {
StreamUtil.copy(htmlPrefixStream, outputStream);
writeBase64(cacheFile, cacheDirectory, outputStream);
StreamUtil.copy(htmlSuffixStream, outputStream);
} finally {
outputStream.close();
}
File fontFile = new File(cacheDirectory, "text.ttf");
InputStream fontStream = context.getAssets().open("text.ttf");
StreamUtil.copy(fontStream, fontFile);
finalUri = Uri.fromFile(htmlFile).buildUpon().appendQueryParameter("ext", extension).build();
} else if (fileType.startsWith("text/")) {
if (lastCore != null) {
lastCore.close();
}
CoreWrapper core = new CoreWrapper();
try {
lastCore = core;
} catch (Throwable e) {
crashManager.log(e);
}
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
coreOptions.inputPath = cacheFile.getPath();
coreOptions.outputPath = cacheDirectory.getPath();
coreOptions.ooxml = false;
coreOptions.txt = true;
coreOptions.pdf = false;
CoreWrapper.CoreResult coreResult = lastCore.parse(coreOptions);
if (coreResult.exception != null) {
throw coreResult.exception;
}
File entryFile = new File(coreResult.pagePaths.get(0));
finalUri = Uri.fromFile(entryFile);
} else if (fileType.startsWith("application/zip")) {
File htmlFile = new File(cacheDirectory, "zip.html");
InputStream htmlPrefixStream = context.getAssets().open("zip-prefix.html");
InputStream htmlSuffixStream = context.getAssets().open("zip-suffix.html");
OutputStream outputStream = new FileOutputStream(htmlFile);
try {
StreamUtil.copy(htmlPrefixStream, outputStream);
writeBase64(cacheFile, cacheDirectory, outputStream);
StreamUtil.copy(htmlSuffixStream, outputStream);
} finally {
outputStream.close();
}
finalUri = Uri.fromFile(htmlFile);
} else {
File renamedFile = new File(cacheDirectory, "temp." + extension);
StreamUtil.copy(cacheFile, renamedFile);
finalUri = Uri.fromFile(renamedFile);
}
result.partTitles.add(null);
result.partUris.add(finalUri);
callOnSuccess(result);
} catch (Throwable e) {
callOnError(result, e);
}
}
private void writeBase64(File cacheFile, File cacheDirectory, OutputStream outputStream) throws IOException {
// need to store it in a separate file first because BaseStream writes characters on close
FileInputStream fileInputStream = new FileInputStream(cacheFile);
File baseFile = new File(cacheDirectory, "tmp");
OutputStream baseFileOutputStream = new FileOutputStream(baseFile);
Base64OutputStream baseOutputStream = new Base64OutputStream(baseFileOutputStream, Base64.NO_WRAP);
try {
StreamUtil.copy(fileInputStream, baseOutputStream);
} finally {
baseOutputStream.close();
}
InputStream baseFileInputStream = new FileInputStream(baseFile);
StreamUtil.copy(baseFileInputStream, outputStream);
}
}