@capgo/capacitor-uploader 0.0.1 → 0.0.5

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 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
- WIP: this is a work in progress still not ready for use
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) => any
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>any</code>
148
+ **Returns:** <code>Promise&lt;{ id: string; }&gt;</code>
149
149
 
150
- **Since:** 1.0.0
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; }) => any
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
- **Returns:** <code>any</code>
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: 'events', listenerFunc: (state: UploadEvent) => void) => any
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>) =&gt; void</code> |
182
180
 
183
- **Returns:** <code>any</code>
181
+ **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
184
182
 
185
- **Since:** 1.0.0
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> | | 1.0.0 |
198
- | **`serverUrl`** | <code>string</code> | | 1.0.0 |
199
- | **`notificationTitle`** | <code>number</code> | <code>'Uploading'</code> | 1.0.0 |
200
- | **`headers`** | <code>{ [key: string]: string; }</code> | | 1.0.0 |
201
- | **`method`** | <code>'PUT' \| 'POST'</code> | <code>'POST'</code> | 1.0.0 |
202
- | **`mimeType`** | <code>string</code> | | 1.0.0 |
203
- | **`parameters`** | <code>{ [key: string]: string; }</code> | | 1.0.0 |
204
- | **`maxRetries`** | <code>number</code> | | 1.0.0 |
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
- #### UploadEvent
205
+ #### PluginListenerHandle
208
206
 
209
- | Prop | Type | Description | Since |
210
- | ------------- | ----------------------------------------------------------------------- | -------------------------------------------- | ----- |
211
- | **`name`** | <code>'uploading' \| 'completed' \| 'failed'</code> | Current status of upload, between 0 and 100. | |
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>() =&gt; Promise&lt;void&gt;</code> |
214
210
 
215
211
 
216
- #### PluginListenerHandle
212
+ #### UploadEvent
217
213
 
218
- | Prop | Type |
219
- | ------------ | ------------------------- |
220
- | **`remove`** | <code>() =&gt; any</code> |
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 android.database.Cursor;
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
- private final Context context;
20
- private final RequestObserverDelegate delegate;
21
-
22
- public Uploader(Context context, RequestObserverDelegate delegate) {
23
- this.context = context;
24
- this.delegate = delegate;
25
- initializeUploadService(context);
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
- private void initializeUploadService(Context context) {
29
- Application application = getApplication(context);
30
- if (application != null) {
31
- UploadServiceConfig.initialize(application, "ee.forgr.capacitor.uploader.notification_channel_id", true);
32
- } else {
33
- throw new IllegalStateException("Unable to get Application instance");
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
- private Application getApplication(Context context) {
38
- if (context == null) {
39
- return null;
40
- } else if (context instanceof Application) {
41
- return (Application) context;
42
- } else {
43
- return getApplication(context.getApplicationContext());
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
- public String startUpload(String filePath, String serverUrl, Map<String, String> headers, Map<String, String> parameters,
48
- String httpMethod, String notificationTitle, int maxRetries, String mimeType) throws Exception {
49
- UploadNotificationConfig notificationConfig = createNotificationConfig(notificationTitle);
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
- private UploadNotificationConfig createNotificationConfig(String notificationTitle) {
85
- UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - In Progress");
86
- UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Completed");
87
- UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Error");
88
- UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Cancelled");
89
-
90
- return new UploadNotificationConfig(
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
- private String getFileNameFromUri(Uri uri) {
101
- String result = null;
102
- if (uri.getScheme().equals("content")) {
103
- try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
104
- if (cursor != null && cursor.moveToFirst()) {
105
- int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
106
- if (index != -1) {
107
- result = cursor.getString(index);
108
- }
109
- }
110
- } catch (Exception e) {
111
- e.printStackTrace();
112
- }
113
- }
114
- if (result == null) {
115
- result = uri.getPath();
116
- int cut = result.lastIndexOf('/');
117
- if (cut != -1) {
118
- result = result.substring(cut + 1);
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
- return result;
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
- private Uploader implementation;
19
+ private Uploader implementation;
22
20
 
23
- @Override
24
- public void load() {
25
- implementation = new Uploader(getContext(), 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
- }
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
- @Override
38
- public void onSuccess(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
39
- JSObject event = new JSObject();
40
- event.put("name", "completed");
41
- JSObject payload = new JSObject();
42
- payload.put("statusCode", serverResponse.getCode());
43
- event.put("payload", payload);
44
- event.put("id", uploadInfo.getUploadId());
45
- notifyListeners("events", event);
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
- @Override
49
- public void onError(Context context, UploadInfo uploadInfo, Throwable exception) {
50
- JSObject event = new JSObject();
51
- event.put("name", "failed");
52
- JSObject payload = new JSObject();
53
- payload.put("error", exception.getMessage());
54
- event.put("payload", payload);
55
- event.put("id", uploadInfo.getUploadId());
56
- notifyListeners("events", event);
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
- @Override
60
- public void onCompleted(Context context, UploadInfo uploadInfo) {
61
- // This method is called after onSuccess or onError
62
- }
67
+ @Override
68
+ public void onCompleted(Context context, UploadInfo uploadInfo) {
69
+ // This method is called after onSuccess or onError
70
+ }
63
71
 
64
- @Override
65
- public void onCompletedWhileNotObserving() {
66
- // This method is called when the upload completes while the observer is not registered
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
- @PluginMethod
72
- public void startUpload(PluginCall call) {
73
- String filePath = call.getString("filePath");
74
- String serverUrl = call.getString("serverUrl");
75
- JSObject headersObj = call.getObject("headers", new JSObject());
76
- JSObject parametersObj = call.getObject("parameters", new JSObject());
77
- String httpMethod = call.getString("method", "POST");
78
- String notificationTitle = call.getString("notificationTitle", "File Upload");
79
- int maxRetries = call.getInt("maxRetries", 2);
80
- String mimeType = call.getString("mimeType"); // Add this line
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
- Map<String, String> headers = JSObjectToMap(headersObj);
83
- Map<String, String> parameters = JSObjectToMap(parametersObj);
94
+ Map<String, String> headers = JSObjectToMap(headersObj);
95
+ Map<String, String> parameters = JSObjectToMap(parametersObj);
84
96
 
85
- try {
86
- String id = implementation.startUpload(filePath, serverUrl, headers, parameters, httpMethod, notificationTitle, maxRetries, mimeType);
87
- JSObject result = new JSObject();
88
- result.put("id", id);
89
- call.resolve(result);
90
- } catch (Exception e) {
91
- call.reject(e.getMessage());
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
- @PluginMethod
96
- public void removeUpload(PluginCall call) {
97
- String id = call.getString("id");
98
- try {
99
- implementation.removeUpload(id);
100
- call.resolve();
101
- } catch (Exception e) {
102
- call.reject(e.getMessage());
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
- private Map<String, String> JSObjectToMap(JSObject object) {
107
- Map<String, String> map = new HashMap<>();
108
- if (object != null) {
109
- for (Iterator<String> it = object.keys(); it.hasNext(); ) {
110
- String key = it.next();
111
- map.put(key, object.getString(key));
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
  }