forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploader.java
More file actions
245 lines (214 loc) · 7.9 KB
/
FileUploader.java
File metadata and controls
245 lines (214 loc) · 7.9 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
*
*/
package com.beetight;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import android.net.Uri;
import java.net.URL;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.webkit.CookieManager;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
/**
* @author matt
*
*/
public class FileUploader extends Plugin {
/* (non-Javadoc)
* @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String)
*/
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
String server = args.getString(0);
String file = args.getString(1);
JSONObject params = args.getJSONObject(2);
String fileKey = "file";
String fileName = "image.jpg";
String mimeType = "image/jpeg";
if(args.length() > 3) {
fileKey = args.getString(3);
}
if(args.length() > 4) {
fileName = args.getString(4);
}
if(args.length() > 5) {
mimeType = args.getString(5);
}
if (action.equals("upload")) {
upload(file, server, params, fileKey, fileName, mimeType, callbackId);
} else if (action.equals("uploadByUri")) {
Uri uri = Uri.parse(file);
upload(uri, server, params, fileKey, fileName, mimeType, callbackId);
} else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
public void upload(Uri uri, String server, JSONObject params, final String fileKey, final String fileName, final String mimeType, String callbackId) {
try {
InputStream fileInputStream=this.ctx.getContentResolver().openInputStream(uri);
upload(fileInputStream, server, params, fileKey, fileName, mimeType, callbackId);
} catch (FileNotFoundException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
}
}
public void upload(String filename, String server, JSONObject params, final String fileKey, final String fileName, final String mimeType, String callbackId) {
File uploadFile = new File(filename);
try {
FileInputStream fileInputStream = new FileInputStream(uploadFile);
upload(fileInputStream, server, params, fileKey, fileName, mimeType, callbackId);
} catch (FileNotFoundException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
}
}
public void upload(InputStream fileInputStream, String server, JSONObject params, final String fileKey, final String fileName, final String mimeType, final String callbackId) {
try {
String lineEnd = "\r\n";
String td = "--";
String boundary = "*****com.beetight.formBoundary";
URL url = new URL(server);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Get cookies that have been set in our webview
CookieManager cm = CookieManager.getInstance();
String cookie = cm.getCookie(server);
// allow inputs
conn.setDoInput(true);
// allow outputs
conn.setDoOutput(true);
// don't use a cached copy
conn.setUseCaches(false);
// use a post method
conn.setRequestMethod("POST");
// set post headers
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary);
conn.setRequestProperty("Cookie", cookie);
// open data output stream
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
try {
for (Iterator iter = params.keys(); iter.hasNext();) {
Object key = iter.next();
dos.writeBytes(td + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"; ");
dos.writeBytes(lineEnd + lineEnd);
dos.writeBytes(params.getString(key.toString()));
dos.writeBytes(lineEnd);
}
} catch (JSONException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
}
dos.writeBytes(td + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"" + fileKey + "\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes("Content-Type: " + mimeType + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
final int total = bytesAvailable;
Log.e("PhoneGapLog", "available: " + bytesAvailable);
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
int progress = bytesRead;
int send = 0;
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
progress += bytesRead;
final int prog = progress;
Log.e("PhoneGapLog", "read " + progress + " of " + total);
// Sending every progress event is overkill
if (send++ % 20 == 0) {
ctx.runOnUiThread(new Runnable () {
public void run() {
try {
JSONObject result = new JSONObject();
result.put("status", FileUploader.Status.PROGRESS);
result.put("progress", prog);
result.put("total", total);
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, result);
progressResult.setKeepCallback(true);
success(progressResult, callbackId);
} catch (JSONException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
}
}
});
// Give a chance for the progress to be sent to javascript
Thread.sleep(100);
}
}
// send multipart form data necessary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(td + boundary + td + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
b.append( (char)ch );
}
String s=b.toString();
dos.close();
JSONObject result = new JSONObject();
result.put("status", FileUploader.Status.COMPLETE);
result.put("progress", progress);
result.put("total", total);
result.put("result", s);
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, result);
progressResult.setKeepCallback(true);
success(progressResult, callbackId);
}
catch (MalformedURLException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
PluginResult result = new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, e.getMessage());
error(result, callbackId);
}
catch (FileNotFoundException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
PluginResult result = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
error(result, callbackId);
}
catch (IOException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
PluginResult result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
error(result, callbackId);
} catch (InterruptedException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
PluginResult result = new PluginResult(PluginResult.Status.ERROR, e.getMessage());
error(result, callbackId);
} catch (JSONException e) {
Log.e("PhoneGapLog", "error: " + e.getMessage(), e);
PluginResult result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
error(result, callbackId);
}
}
public enum Status {
PROGRESS,
COMPLETE
}
}