-
Notifications
You must be signed in to change notification settings - Fork 924
/
ShareFile.java
145 lines (131 loc) · 4.87 KB
/
ShareFile.java
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
package cl.json;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.FileProvider;
import android.util.Base64;
import android.webkit.MimeTypeMap;
import com.facebook.react.bridge.ReactApplicationContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by disenodosbbcl on 22-07-16.
*/
public class ShareFile {
private final ReactApplicationContext reactContext;
private String url;
private Uri uri;
private String type;
public ShareFile(String url, String type, ReactApplicationContext reactContext){
this(url, reactContext);
this.type = type;
}
public ShareFile(String url, ReactApplicationContext reactContext){
this.url = url;
this.uri = Uri.parse(this.url);
this.reactContext = reactContext;
}
/**
* Obtain mime type from URL
* @param url {@link String}
* @return {@link String} mime type
*/
private String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
/**
* Return an if the url is a file (local or base64)l
* @return {@link boolean}
*/
public boolean isFile() {
return this.isBase64File() || this.isLocalFile();
}
private boolean isBase64File() {
String scheme = uri.getScheme();
if((scheme != null) && uri.getScheme().equals("data")) {
this.type = this.uri.getSchemeSpecificPart().substring(0, this.uri.getSchemeSpecificPart().indexOf(";"));
return true;
}
return false;
}
private boolean isLocalFile() {
String scheme = uri.getScheme();
if((scheme != null) && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) {
// type is already set
if (this.type != null) {
return true;
}
// try to get mimetype from uri
this.type = this.getMimeType(uri.toString());
// try resolving the file and get the mimetype
if(this.type == null) {
String realPath = this.getRealPathFromURI(uri);
if (realPath != null) {
this.type = this.getMimeType(realPath);
} else {
return false;
}
}
if(this.type == null) {
this.type = "*/*";
}
return true;
}
return false;
}
public String getType() {
if (this.type == null) {
return "*/*";
}
return this.type;
}
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(this.reactContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
String result = null;
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
result = cursor.getString(column_index);
cursor.close();
}
return result;
}
public Uri getURI() {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
String extension = mime.getExtensionFromMimeType(getType());
final String authority = ((ShareApplication) reactContext.getApplicationContext()).getFileProviderAuthority();
if(this.isBase64File()) {
String encodedImg = this.uri.getSchemeSpecificPart().substring(this.uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("mkdirs failed on " + dir.getAbsolutePath());
}
File file = new File(dir, System.nanoTime() + "." + extension);
final FileOutputStream fos = new FileOutputStream(file);
fos.write(Base64.decode(encodedImg, Base64.DEFAULT));
fos.flush();
fos.close();
return FileProvider.getUriForFile(reactContext, authority, file);
} catch (IOException e) {
e.printStackTrace();
}
} else if(this.isLocalFile()) {
Uri uri = Uri.parse(this.url);
if (uri.getPath() == null) {
return null;
}
return FileProvider.getUriForFile(reactContext, authority, new File(uri.getPath()));
}
return null;
}
}