@capgo/capacitor-uploader 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -32
- package/android/src/main/java/ee/forgr/capacitor/uploader/Uploader.java +134 -99
- package/android/src/main/java/ee/forgr/capacitor/uploader/UploaderPlugin.java +108 -87
- package/dist/docs.json +74 -39
- package/dist/esm/definitions.d.ts +38 -17
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +5 -7
- package/dist/esm/web.js +84 -10
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +85 -13
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +86 -14
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/UploaderPlugin/Uploader.swift +8 -9
- package/ios/Sources/UploaderPlugin/UploaderPlugin.swift +6 -6
- package/package.json +39 -15
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
This plugin provides a flexible way to upload natively files to various servers, including S3 with presigned URLs.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Can be used in combination with the [Capacitor Camera preview](https://github.com/Cap-go/camera-preview) To upload file in reliable manner instead of reading them in buffer of webview and then upload in JS.
|
|
12
12
|
|
|
13
13
|
## Install
|
|
14
14
|
|
|
@@ -138,16 +138,16 @@ uploadToCustomServer(filePath, serverUrl);
|
|
|
138
138
|
### startUpload(...)
|
|
139
139
|
|
|
140
140
|
```typescript
|
|
141
|
-
startUpload(options: uploadOption) =>
|
|
141
|
+
startUpload(options: uploadOption) => Promise<{ id: string; }>
|
|
142
142
|
```
|
|
143
143
|
|
|
144
|
-
| Param | Type |
|
|
145
|
-
| ------------- | ----------------------------------------------------- |
|
|
146
|
-
| **`options`** | <code><a href="#uploadoption">uploadOption</a></code> |
|
|
144
|
+
| Param | Type | Description |
|
|
145
|
+
| ------------- | ----------------------------------------------------- | ---------------------------------------- |
|
|
146
|
+
| **`options`** | <code><a href="#uploadoption">uploadOption</a></code> | <a href="#uploadoption">uploadOption</a> |
|
|
147
147
|
|
|
148
|
-
**Returns:** <code>
|
|
148
|
+
**Returns:** <code>Promise<{ id: string; }></code>
|
|
149
149
|
|
|
150
|
-
**Since:**
|
|
150
|
+
**Since:** 0.0.1
|
|
151
151
|
|
|
152
152
|
--------------------
|
|
153
153
|
|
|
@@ -155,16 +155,14 @@ startUpload(options: uploadOption) => any
|
|
|
155
155
|
### removeUpload(...)
|
|
156
156
|
|
|
157
157
|
```typescript
|
|
158
|
-
removeUpload(options: { id: string; }) =>
|
|
158
|
+
removeUpload(options: { id: string; }) => Promise<void>
|
|
159
159
|
```
|
|
160
160
|
|
|
161
161
|
| Param | Type |
|
|
162
162
|
| ------------- | ---------------------------- |
|
|
163
163
|
| **`options`** | <code>{ id: string; }</code> |
|
|
164
164
|
|
|
165
|
-
**
|
|
166
|
-
|
|
167
|
-
**Since:** 1.0.0
|
|
165
|
+
**Since:** 0.0.1
|
|
168
166
|
|
|
169
167
|
--------------------
|
|
170
168
|
|
|
@@ -172,7 +170,7 @@ removeUpload(options: { id: string; }) => any
|
|
|
172
170
|
### addListener('events', ...)
|
|
173
171
|
|
|
174
172
|
```typescript
|
|
175
|
-
addListener(eventName:
|
|
173
|
+
addListener(eventName: "events", listenerFunc: (state: UploadEvent) => void) => Promise<PluginListenerHandle>
|
|
176
174
|
```
|
|
177
175
|
|
|
178
176
|
| Param | Type |
|
|
@@ -180,9 +178,9 @@ addListener(eventName: 'events', listenerFunc: (state: UploadEvent) => void) =>
|
|
|
180
178
|
| **`eventName`** | <code>'events'</code> |
|
|
181
179
|
| **`listenerFunc`** | <code>(state: <a href="#uploadevent">UploadEvent</a>) => void</code> |
|
|
182
180
|
|
|
183
|
-
**Returns:** <code>
|
|
181
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
184
182
|
|
|
185
|
-
**Since:**
|
|
183
|
+
**Since:** 0.0.1
|
|
186
184
|
|
|
187
185
|
--------------------
|
|
188
186
|
|
|
@@ -194,30 +192,30 @@ addListener(eventName: 'events', listenerFunc: (state: UploadEvent) => void) =>
|
|
|
194
192
|
|
|
195
193
|
| Prop | Type | Default | Since |
|
|
196
194
|
| ----------------------- | --------------------------------------- | ------------------------ | ----- |
|
|
197
|
-
| **`filePath`** | <code>string</code> | |
|
|
198
|
-
| **`serverUrl`** | <code>string</code> | |
|
|
199
|
-
| **`notificationTitle`** | <code>number</code> | <code>'Uploading'</code> |
|
|
200
|
-
| **`headers`** | <code>{ [key: string]: string; }</code> | |
|
|
201
|
-
| **`method`** | <code>'PUT' \| 'POST'</code> | <code>'POST'</code> |
|
|
202
|
-
| **`mimeType`** | <code>string</code> | |
|
|
203
|
-
| **`parameters`** | <code>{ [key: string]: string; }</code> | |
|
|
204
|
-
| **`maxRetries`** | <code>number</code> | |
|
|
195
|
+
| **`filePath`** | <code>string</code> | | 0.0.1 |
|
|
196
|
+
| **`serverUrl`** | <code>string</code> | | 0.0.1 |
|
|
197
|
+
| **`notificationTitle`** | <code>number</code> | <code>'Uploading'</code> | 0.0.1 |
|
|
198
|
+
| **`headers`** | <code>{ [key: string]: string; }</code> | | 0.0.1 |
|
|
199
|
+
| **`method`** | <code>'PUT' \| 'POST'</code> | <code>'POST'</code> | 0.0.1 |
|
|
200
|
+
| **`mimeType`** | <code>string</code> | | 0.0.1 |
|
|
201
|
+
| **`parameters`** | <code>{ [key: string]: string; }</code> | | 0.0.1 |
|
|
202
|
+
| **`maxRetries`** | <code>number</code> | | 0.0.1 |
|
|
205
203
|
|
|
206
204
|
|
|
207
|
-
####
|
|
205
|
+
#### PluginListenerHandle
|
|
208
206
|
|
|
209
|
-
| Prop
|
|
210
|
-
|
|
|
211
|
-
| **`
|
|
212
|
-
| **`payload`** | <code>{ percent?: number; error?: string; statusCode?: number; }</code> | | 1.0.0 |
|
|
213
|
-
| **`id`** | <code>string</code> | | 1.0.0 |
|
|
207
|
+
| Prop | Type |
|
|
208
|
+
| ------------ | ----------------------------------------- |
|
|
209
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
214
210
|
|
|
215
211
|
|
|
216
|
-
####
|
|
212
|
+
#### UploadEvent
|
|
217
213
|
|
|
218
|
-
| Prop
|
|
219
|
-
|
|
|
220
|
-
| **`
|
|
214
|
+
| Prop | Type | Description | Default | Since |
|
|
215
|
+
| ------------- | ----------------------------------------------------------------------- | -------------------------------------------- | ----------------------------------------------------- | ----- |
|
|
216
|
+
| **`name`** | <code>'uploading' \| 'completed' \| 'failed'</code> | Current status of upload, between 0 and 100. | | 0.0.1 |
|
|
217
|
+
| **`payload`** | <code>{ percent?: number; error?: string; statusCode?: number; }</code> | | <code>{ percent: 0, error: '', statusCode: 0 }</code> | 0.0.1 |
|
|
218
|
+
| **`id`** | <code>string</code> | | | 0.0.1 |
|
|
221
219
|
|
|
222
220
|
</docgen-api>
|
|
223
221
|
|
|
@@ -2,122 +2,157 @@ package ee.forgr.capacitor.uploader;
|
|
|
2
2
|
|
|
3
3
|
import android.app.Application;
|
|
4
4
|
import android.content.Context;
|
|
5
|
+
import android.database.Cursor;
|
|
5
6
|
import android.net.Uri;
|
|
6
7
|
import android.provider.OpenableColumns;
|
|
7
|
-
import
|
|
8
|
-
|
|
8
|
+
import java.util.Map;
|
|
9
9
|
import net.gotev.uploadservice.UploadServiceConfig;
|
|
10
10
|
import net.gotev.uploadservice.data.UploadNotificationConfig;
|
|
11
11
|
import net.gotev.uploadservice.data.UploadNotificationStatusConfig;
|
|
12
|
-
import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest;
|
|
13
12
|
import net.gotev.uploadservice.observer.request.RequestObserverDelegate;
|
|
14
|
-
|
|
15
|
-
import java.util.Map;
|
|
13
|
+
import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest;
|
|
16
14
|
|
|
17
15
|
public class Uploader {
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
private final Context context;
|
|
18
|
+
private final RequestObserverDelegate delegate;
|
|
19
|
+
|
|
20
|
+
public Uploader(Context context, RequestObserverDelegate delegate) {
|
|
21
|
+
this.context = context;
|
|
22
|
+
this.delegate = delegate;
|
|
23
|
+
initializeUploadService(context);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private void initializeUploadService(Context context) {
|
|
27
|
+
Application application = getApplication(context);
|
|
28
|
+
if (application != null) {
|
|
29
|
+
UploadServiceConfig.initialize(
|
|
30
|
+
application,
|
|
31
|
+
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
32
|
+
true
|
|
33
|
+
);
|
|
34
|
+
} else {
|
|
35
|
+
throw new IllegalStateException("Unable to get Application instance");
|
|
26
36
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private Application getApplication(Context context) {
|
|
40
|
+
if (context == null) {
|
|
41
|
+
return null;
|
|
42
|
+
} else if (context instanceof Application) {
|
|
43
|
+
return (Application) context;
|
|
44
|
+
} else {
|
|
45
|
+
return getApplication(context.getApplicationContext());
|
|
35
46
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public String startUpload(
|
|
50
|
+
String filePath,
|
|
51
|
+
String serverUrl,
|
|
52
|
+
Map<String, String> headers,
|
|
53
|
+
Map<String, String> parameters,
|
|
54
|
+
String httpMethod,
|
|
55
|
+
String notificationTitle,
|
|
56
|
+
int maxRetries,
|
|
57
|
+
String mimeType
|
|
58
|
+
) throws Exception {
|
|
59
|
+
UploadNotificationConfig notificationConfig = createNotificationConfig(
|
|
60
|
+
notificationTitle
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
MultipartUploadRequest request = new MultipartUploadRequest(
|
|
64
|
+
context,
|
|
65
|
+
serverUrl
|
|
66
|
+
)
|
|
67
|
+
.setMethod(httpMethod)
|
|
68
|
+
.addFileToUpload(filePath, "file", mimeType) // Updated this line
|
|
69
|
+
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
70
|
+
.setMaxRetries(maxRetries);
|
|
71
|
+
|
|
72
|
+
// Add headers
|
|
73
|
+
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
74
|
+
request.addHeader(entry.getKey(), entry.getValue());
|
|
45
75
|
}
|
|
46
76
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
MultipartUploadRequest request = new MultipartUploadRequest(context, serverUrl)
|
|
52
|
-
.setMethod(httpMethod)
|
|
53
|
-
.addFileToUpload(filePath, "file", mimeType) // Updated this line
|
|
54
|
-
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
55
|
-
.setMaxRetries(maxRetries);
|
|
56
|
-
|
|
57
|
-
// Add headers
|
|
58
|
-
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
59
|
-
request.addHeader(entry.getKey(), entry.getValue());
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Add parameters
|
|
63
|
-
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
64
|
-
request.addParameter(entry.getKey(), entry.getValue());
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Set file name if it's a content URI
|
|
68
|
-
if (filePath.startsWith("content://")) {
|
|
69
|
-
Uri uri = Uri.parse(filePath);
|
|
70
|
-
String fileName = getFileNameFromUri(uri);
|
|
71
|
-
if (fileName != null) {
|
|
72
|
-
request.addParameter("filename", fileName);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Start the upload
|
|
77
|
-
return request.startUpload();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public void removeUpload(String uploadId) {
|
|
81
|
-
net.gotev.uploadservice.UploadService.stopUpload(uploadId);
|
|
77
|
+
// Add parameters
|
|
78
|
+
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
79
|
+
request.addParameter(entry.getKey(), entry.getValue());
|
|
82
80
|
}
|
|
83
81
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
92
|
-
false,
|
|
93
|
-
progress,
|
|
94
|
-
success,
|
|
95
|
-
error,
|
|
96
|
-
cancelled
|
|
97
|
-
);
|
|
82
|
+
// Set file name if it's a content URI
|
|
83
|
+
if (filePath.startsWith("content://")) {
|
|
84
|
+
Uri uri = Uri.parse(filePath);
|
|
85
|
+
String fileName = getFileNameFromUri(uri);
|
|
86
|
+
if (fileName != null) {
|
|
87
|
+
request.addParameter("filename", fileName);
|
|
88
|
+
}
|
|
98
89
|
}
|
|
99
90
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
91
|
+
// Start the upload
|
|
92
|
+
return request.startUpload();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public void removeUpload(String uploadId) {
|
|
96
|
+
net.gotev.uploadservice.UploadService.stopUpload(uploadId);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private UploadNotificationConfig createNotificationConfig(
|
|
100
|
+
String notificationTitle
|
|
101
|
+
) {
|
|
102
|
+
UploadNotificationStatusConfig progress =
|
|
103
|
+
new UploadNotificationStatusConfig(
|
|
104
|
+
notificationTitle,
|
|
105
|
+
notificationTitle + " - In Progress"
|
|
106
|
+
);
|
|
107
|
+
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(
|
|
108
|
+
notificationTitle,
|
|
109
|
+
notificationTitle + " - Completed"
|
|
110
|
+
);
|
|
111
|
+
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(
|
|
112
|
+
notificationTitle,
|
|
113
|
+
notificationTitle + " - Error"
|
|
114
|
+
);
|
|
115
|
+
UploadNotificationStatusConfig cancelled =
|
|
116
|
+
new UploadNotificationStatusConfig(
|
|
117
|
+
notificationTitle,
|
|
118
|
+
notificationTitle + " - Cancelled"
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return new UploadNotificationConfig(
|
|
122
|
+
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
123
|
+
false,
|
|
124
|
+
progress,
|
|
125
|
+
success,
|
|
126
|
+
error,
|
|
127
|
+
cancelled
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private String getFileNameFromUri(Uri uri) {
|
|
132
|
+
String result = null;
|
|
133
|
+
if (uri.getScheme().equals("content")) {
|
|
134
|
+
try (
|
|
135
|
+
Cursor cursor = context
|
|
136
|
+
.getContentResolver()
|
|
137
|
+
.query(uri, null, null, null, null)
|
|
138
|
+
) {
|
|
139
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
140
|
+
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
141
|
+
if (index != -1) {
|
|
142
|
+
result = cursor.getString(index);
|
|
143
|
+
}
|
|
120
144
|
}
|
|
121
|
-
|
|
145
|
+
} catch (Exception e) {
|
|
146
|
+
e.printStackTrace();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (result == null) {
|
|
150
|
+
result = uri.getPath();
|
|
151
|
+
int cut = result.lastIndexOf('/');
|
|
152
|
+
if (cut != -1) {
|
|
153
|
+
result = result.substring(cut + 1);
|
|
154
|
+
}
|
|
122
155
|
}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
123
158
|
}
|
|
@@ -6,111 +6,132 @@ import com.getcapacitor.Plugin;
|
|
|
6
6
|
import com.getcapacitor.PluginCall;
|
|
7
7
|
import com.getcapacitor.PluginMethod;
|
|
8
8
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
9
|
-
|
|
9
|
+
import java.util.HashMap;
|
|
10
|
+
import java.util.Iterator;
|
|
11
|
+
import java.util.Map;
|
|
10
12
|
import net.gotev.uploadservice.data.UploadInfo;
|
|
11
13
|
import net.gotev.uploadservice.network.ServerResponse;
|
|
12
14
|
import net.gotev.uploadservice.observer.request.RequestObserverDelegate;
|
|
13
15
|
|
|
14
|
-
import java.util.Map;
|
|
15
|
-
import java.util.HashMap;
|
|
16
|
-
import java.util.Iterator;
|
|
17
|
-
|
|
18
16
|
@CapacitorPlugin(name = "Uploader")
|
|
19
17
|
public class UploaderPlugin extends Plugin {
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
private Uploader implementation;
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
@Override
|
|
22
|
+
public void load() {
|
|
23
|
+
implementation = new Uploader(
|
|
24
|
+
getContext(),
|
|
25
|
+
new RequestObserverDelegate() {
|
|
26
|
+
@Override
|
|
27
|
+
public void onProgress(Context context, UploadInfo uploadInfo) {
|
|
28
|
+
JSObject event = new JSObject();
|
|
29
|
+
event.put("name", "uploading");
|
|
30
|
+
JSObject payload = new JSObject();
|
|
31
|
+
payload.put("percent", uploadInfo.getProgressPercent());
|
|
32
|
+
event.put("payload", payload);
|
|
33
|
+
event.put("id", uploadInfo.getUploadId());
|
|
34
|
+
notifyListeners("events", event);
|
|
35
|
+
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
@Override
|
|
38
|
+
public void onSuccess(
|
|
39
|
+
Context context,
|
|
40
|
+
UploadInfo uploadInfo,
|
|
41
|
+
ServerResponse serverResponse
|
|
42
|
+
) {
|
|
43
|
+
JSObject event = new JSObject();
|
|
44
|
+
event.put("name", "completed");
|
|
45
|
+
JSObject payload = new JSObject();
|
|
46
|
+
payload.put("statusCode", serverResponse.getCode());
|
|
47
|
+
event.put("payload", payload);
|
|
48
|
+
event.put("id", uploadInfo.getUploadId());
|
|
49
|
+
notifyListeners("events", event);
|
|
50
|
+
}
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
@Override
|
|
53
|
+
public void onError(
|
|
54
|
+
Context context,
|
|
55
|
+
UploadInfo uploadInfo,
|
|
56
|
+
Throwable exception
|
|
57
|
+
) {
|
|
58
|
+
JSObject event = new JSObject();
|
|
59
|
+
event.put("name", "failed");
|
|
60
|
+
JSObject payload = new JSObject();
|
|
61
|
+
payload.put("error", exception.getMessage());
|
|
62
|
+
event.put("payload", payload);
|
|
63
|
+
event.put("id", uploadInfo.getUploadId());
|
|
64
|
+
notifyListeners("events", event);
|
|
65
|
+
}
|
|
58
66
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
@Override
|
|
68
|
+
public void onCompleted(Context context, UploadInfo uploadInfo) {
|
|
69
|
+
// This method is called after onSuccess or onError
|
|
70
|
+
}
|
|
63
71
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
@Override
|
|
73
|
+
public void onCompletedWhileNotObserving() {
|
|
74
|
+
// This method is called when the upload completes while the observer is not registered
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
}
|
|
70
79
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
@PluginMethod
|
|
81
|
+
public void startUpload(PluginCall call) {
|
|
82
|
+
String filePath = call.getString("filePath");
|
|
83
|
+
String serverUrl = call.getString("serverUrl");
|
|
84
|
+
JSObject headersObj = call.getObject("headers", new JSObject());
|
|
85
|
+
JSObject parametersObj = call.getObject("parameters", new JSObject());
|
|
86
|
+
String httpMethod = call.getString("method", "POST");
|
|
87
|
+
String notificationTitle = call.getString(
|
|
88
|
+
"notificationTitle",
|
|
89
|
+
"File Upload"
|
|
90
|
+
);
|
|
91
|
+
int maxRetries = call.getInt("maxRetries", 2);
|
|
92
|
+
String mimeType = call.getString("mimeType"); // Add this line
|
|
81
93
|
|
|
82
|
-
|
|
83
|
-
|
|
94
|
+
Map<String, String> headers = JSObjectToMap(headersObj);
|
|
95
|
+
Map<String, String> parameters = JSObjectToMap(parametersObj);
|
|
84
96
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
try {
|
|
98
|
+
String id = implementation.startUpload(
|
|
99
|
+
filePath,
|
|
100
|
+
serverUrl,
|
|
101
|
+
headers,
|
|
102
|
+
parameters,
|
|
103
|
+
httpMethod,
|
|
104
|
+
notificationTitle,
|
|
105
|
+
maxRetries,
|
|
106
|
+
mimeType
|
|
107
|
+
);
|
|
108
|
+
JSObject result = new JSObject();
|
|
109
|
+
result.put("id", id);
|
|
110
|
+
call.resolve(result);
|
|
111
|
+
} catch (Exception e) {
|
|
112
|
+
call.reject(e.getMessage());
|
|
93
113
|
}
|
|
114
|
+
}
|
|
94
115
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
116
|
+
@PluginMethod
|
|
117
|
+
public void removeUpload(PluginCall call) {
|
|
118
|
+
String id = call.getString("id");
|
|
119
|
+
try {
|
|
120
|
+
implementation.removeUpload(id);
|
|
121
|
+
call.resolve();
|
|
122
|
+
} catch (Exception e) {
|
|
123
|
+
call.reject(e.getMessage());
|
|
104
124
|
}
|
|
125
|
+
}
|
|
105
126
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
return map;
|
|
127
|
+
private Map<String, String> JSObjectToMap(JSObject object) {
|
|
128
|
+
Map<String, String> map = new HashMap<>();
|
|
129
|
+
if (object != null) {
|
|
130
|
+
for (Iterator<String> it = object.keys(); it.hasNext();) {
|
|
131
|
+
String key = it.next();
|
|
132
|
+
map.put(key, object.getString(key));
|
|
133
|
+
}
|
|
115
134
|
}
|
|
135
|
+
return map;
|
|
136
|
+
}
|
|
116
137
|
}
|