-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathMetadataLoader.java
More file actions
158 lines (127 loc) · 5.17 KB
/
MetadataLoader.java
File metadata and controls
158 lines (127 loc) · 5.17 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
package at.tomtasche.reader.background;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.webkit.MimeTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
public class MetadataLoader extends FileLoader {
public MetadataLoader(Context context) {
super(context, LoaderType.METADATA);
}
@Override
public boolean isSupported(Options options) {
return true;
}
@Override
public void loadSync(Options options) {
final Result result = new Result();
result.options = options;
result.loaderType = type;
options.fileType = "N/A";
Uri uri = options.originalUri;
try {
// cleanup uri
if ("/./".equals(uri.toString().substring(0, 2))) {
uri = Uri.parse(uri.toString().substring(2));
}
AndroidFileCache.cleanup(context);
// detecting the filename early so we can use it in the catch-block if something goes wrong
String filename = null;
try {
// https://stackoverflow.com/a/38304115/198996
Cursor fileCursor = context.getContentResolver().query(uri, null, null, null, null);
if (fileCursor != null && fileCursor.moveToFirst()) {
int nameIndex = fileCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
filename = fileCursor.getString(nameIndex);
fileCursor.close();
}
} catch (Exception e) {
// "URI does not contain a valid access token." or
// "Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it."
crashManager.log(e);
}
if (filename == null) {
filename = uri.getLastPathSegment();
}
if (filename == null) {
filename = "N/A";
}
options.filename = filename;
File cachedFile;
if (AndroidFileCache.isCached(context, uri)) {
cachedFile = AndroidFileCache.getCacheFile(context, uri);
} else {
cachedFile = AndroidFileCache.createCacheFile(context);
InputStream stream = context.getContentResolver().openInputStream(uri);
StreamUtil.copy(stream, cachedFile);
}
// if file didn't exist an exception would have been thrown by now
options.fileExists = true;
options.cacheUri = AndroidFileCache.getCacheFileUri(context, cachedFile);
String[] fileSplit = options.filename.split("\\.");
String extension = fileSplit.length > 0 ? fileSplit[fileSplit.length - 1] : "N/A";
String mimetype = null;
try {
mimetype = CoreWrapper.mimetype(cachedFile.getAbsolutePath());
} catch (Throwable e) {
crashManager.log(e);
}
if (mimetype == null) {
mimetype = context.getContentResolver().getType(uri);
}
if (mimetype == null) {
try {
mimetype = URLConnection.guessContentTypeFromName(filename);
} catch (Exception e) {
// Samsung S7 Edge crashes with java.lang.StringIndexOutOfBoundsException
crashManager.log(e);
}
}
if (type == null) {
try {
try (InputStream tempStream = new FileInputStream(cachedFile)) {
mimetype = URLConnection.guessContentTypeFromStream(tempStream);
}
} catch (Exception e) {
crashManager.log(e);
}
}
if (type != null) {
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimetype);
} else {
mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(options.fileExtension);
}
if (extension != null) {
options.fileExtension = extension;
}
if (mimetype != null) {
options.fileType = mimetype;
}
if ("inode/x-empty".equals(mimetype)) {
throw new FileNotFoundException();
}
if (options.persistentUri) {
try {
RecentDocumentsUtil.addRecentDocument(context, filename, uri);
} catch (IOException e) {
crashManager.log(e);
}
}
callOnSuccess(result);
} catch (Throwable e) {
options.fileType = "N/A";
try {
RecentDocumentsUtil.removeRecentDocument(context, options.filename, options.originalUri);
} catch (Exception e1) {
crashManager.log(e1);
}
callOnError(result, e);
}
}
}