@capgo/capacitor-uploader 7.1.14 → 7.1.20
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 +1 -1
- package/android/src/main/java/ee/forgr/capacitor/uploader/Uploader.java +130 -167
- package/android/src/main/java/ee/forgr/capacitor/uploader/UploaderPlugin.java +134 -153
- package/dist/docs.json +1 -1
- package/dist/esm/definitions.d.ts +5 -5
- 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 +2 -2
- package/dist/esm/web.js +16 -16
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +16 -16
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +16 -16
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -222,7 +222,7 @@ removeUpload(options: { id: string; }) => Promise<void>
|
|
|
222
222
|
### addListener('events', ...)
|
|
223
223
|
|
|
224
224
|
```typescript
|
|
225
|
-
addListener(eventName:
|
|
225
|
+
addListener(eventName: 'events', listenerFunc: (state: UploadEvent) => void) => Promise<PluginListenerHandle>
|
|
226
226
|
```
|
|
227
227
|
|
|
228
228
|
| Param | Type |
|
|
@@ -14,180 +14,143 @@ import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest;
|
|
|
14
14
|
|
|
15
15
|
public class Uploader {
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
private void initializeUploadService(Context context) {
|
|
25
|
-
Application application = getApplication(context);
|
|
26
|
-
if (application != null) {
|
|
27
|
-
UploadServiceConfig.initialize(
|
|
28
|
-
application,
|
|
29
|
-
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
30
|
-
true
|
|
31
|
-
);
|
|
32
|
-
} else {
|
|
33
|
-
throw new IllegalStateException("Unable to get Application instance");
|
|
34
|
-
}
|
|
35
|
-
}
|
|
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());
|
|
17
|
+
private final Context context;
|
|
18
|
+
|
|
19
|
+
public Uploader(Context context) {
|
|
20
|
+
this.context = context;
|
|
21
|
+
initializeUploadService(context);
|
|
44
22
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
String notificationTitle,
|
|
54
|
-
int maxRetries,
|
|
55
|
-
String mimeType,
|
|
56
|
-
String uploadType,
|
|
57
|
-
String fileField
|
|
58
|
-
) throws Exception {
|
|
59
|
-
UploadNotificationConfig notificationConfig = createNotificationConfig(
|
|
60
|
-
notificationTitle
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
if ("multipart".equals(uploadType)) {
|
|
64
|
-
MultipartUploadRequest request = new MultipartUploadRequest(
|
|
65
|
-
context,
|
|
66
|
-
serverUrl
|
|
67
|
-
)
|
|
68
|
-
.setMethod(httpMethod)
|
|
69
|
-
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
70
|
-
.setMaxRetries(maxRetries);
|
|
71
|
-
|
|
72
|
-
request.addFileToUpload(
|
|
73
|
-
filePath,
|
|
74
|
-
fileField,
|
|
75
|
-
getFileNameFromUri(Uri.parse(filePath)),
|
|
76
|
-
mimeType
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
80
|
-
request.addHeader(entry.getKey(), entry.getValue());
|
|
81
|
-
}
|
|
82
|
-
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
83
|
-
request.addParameter(entry.getKey(), entry.getValue());
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return request.startUpload();
|
|
87
|
-
} else {
|
|
88
|
-
return startBinaryUpload(
|
|
89
|
-
filePath,
|
|
90
|
-
serverUrl,
|
|
91
|
-
headers,
|
|
92
|
-
parameters,
|
|
93
|
-
httpMethod,
|
|
94
|
-
notificationConfig,
|
|
95
|
-
maxRetries,
|
|
96
|
-
mimeType
|
|
97
|
-
);
|
|
23
|
+
|
|
24
|
+
private void initializeUploadService(Context context) {
|
|
25
|
+
Application application = getApplication(context);
|
|
26
|
+
if (application != null) {
|
|
27
|
+
UploadServiceConfig.initialize(application, "ee.forgr.capacitor.uploader.notification_channel_id", true);
|
|
28
|
+
} else {
|
|
29
|
+
throw new IllegalStateException("Unable to get Application instance");
|
|
30
|
+
}
|
|
98
31
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
int maxRetries,
|
|
109
|
-
String mimeType
|
|
110
|
-
) throws Exception {
|
|
111
|
-
BinaryUploadRequest request = new BinaryUploadRequest(context, serverUrl)
|
|
112
|
-
.setMethod(httpMethod)
|
|
113
|
-
.setFileToUpload(filePath)
|
|
114
|
-
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
115
|
-
.setMaxRetries(maxRetries);
|
|
116
|
-
|
|
117
|
-
request.addHeader("Content-Type", mimeType);
|
|
118
|
-
|
|
119
|
-
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
120
|
-
request.addHeader(entry.getKey(), entry.getValue());
|
|
32
|
+
|
|
33
|
+
private Application getApplication(Context context) {
|
|
34
|
+
if (context == null) {
|
|
35
|
+
return null;
|
|
36
|
+
} else if (context instanceof Application) {
|
|
37
|
+
return (Application) context;
|
|
38
|
+
} else {
|
|
39
|
+
return getApplication(context.getApplicationContext());
|
|
40
|
+
}
|
|
121
41
|
}
|
|
122
42
|
|
|
123
|
-
|
|
124
|
-
|
|
43
|
+
public String startUpload(
|
|
44
|
+
String filePath,
|
|
45
|
+
String serverUrl,
|
|
46
|
+
Map<String, String> headers,
|
|
47
|
+
Map<String, String> parameters,
|
|
48
|
+
String httpMethod,
|
|
49
|
+
String notificationTitle,
|
|
50
|
+
int maxRetries,
|
|
51
|
+
String mimeType,
|
|
52
|
+
String uploadType,
|
|
53
|
+
String fileField
|
|
54
|
+
) throws Exception {
|
|
55
|
+
UploadNotificationConfig notificationConfig = createNotificationConfig(notificationTitle);
|
|
56
|
+
|
|
57
|
+
if ("multipart".equals(uploadType)) {
|
|
58
|
+
MultipartUploadRequest request = new MultipartUploadRequest(context, serverUrl)
|
|
59
|
+
.setMethod(httpMethod)
|
|
60
|
+
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
61
|
+
.setMaxRetries(maxRetries);
|
|
62
|
+
|
|
63
|
+
request.addFileToUpload(filePath, fileField, getFileNameFromUri(Uri.parse(filePath)), mimeType);
|
|
64
|
+
|
|
65
|
+
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
66
|
+
request.addHeader(entry.getKey(), entry.getValue());
|
|
67
|
+
}
|
|
68
|
+
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
69
|
+
request.addParameter(entry.getKey(), entry.getValue());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return request.startUpload();
|
|
73
|
+
} else {
|
|
74
|
+
return startBinaryUpload(filePath, serverUrl, headers, parameters, httpMethod, notificationConfig, maxRetries, mimeType);
|
|
75
|
+
}
|
|
125
76
|
}
|
|
126
77
|
|
|
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
|
-
new UploadNotificationStatusConfig(
|
|
152
|
-
notificationTitle,
|
|
153
|
-
notificationTitle + " - Cancelled"
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
return new UploadNotificationConfig(
|
|
157
|
-
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
158
|
-
false,
|
|
159
|
-
progress,
|
|
160
|
-
success,
|
|
161
|
-
error,
|
|
162
|
-
cancelled
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
private String getFileNameFromUri(Uri uri) {
|
|
167
|
-
String result = null;
|
|
168
|
-
if (uri.getScheme().equals("content")) {
|
|
169
|
-
try (
|
|
170
|
-
Cursor cursor = context
|
|
171
|
-
.getContentResolver()
|
|
172
|
-
.query(uri, null, null, null, null)
|
|
173
|
-
) {
|
|
174
|
-
if (cursor != null && cursor.moveToFirst()) {
|
|
175
|
-
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
176
|
-
if (index != -1) {
|
|
177
|
-
result = cursor.getString(index);
|
|
178
|
-
}
|
|
78
|
+
private String startBinaryUpload(
|
|
79
|
+
String filePath,
|
|
80
|
+
String serverUrl,
|
|
81
|
+
Map<String, String> headers,
|
|
82
|
+
Map<String, String> parameters,
|
|
83
|
+
String httpMethod,
|
|
84
|
+
UploadNotificationConfig notificationConfig,
|
|
85
|
+
int maxRetries,
|
|
86
|
+
String mimeType
|
|
87
|
+
) throws Exception {
|
|
88
|
+
BinaryUploadRequest request = new BinaryUploadRequest(context, serverUrl)
|
|
89
|
+
.setMethod(httpMethod)
|
|
90
|
+
.setFileToUpload(filePath)
|
|
91
|
+
.setNotificationConfig((ctx, uploadId) -> notificationConfig)
|
|
92
|
+
.setMaxRetries(maxRetries);
|
|
93
|
+
|
|
94
|
+
request.addHeader("Content-Type", mimeType);
|
|
95
|
+
|
|
96
|
+
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
97
|
+
request.addHeader(entry.getKey(), entry.getValue());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
|
101
|
+
request.addParameter(entry.getKey(), entry.getValue());
|
|
179
102
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
103
|
+
|
|
104
|
+
return request.startUpload();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public void removeUpload(String uploadId) {
|
|
108
|
+
net.gotev.uploadservice.UploadService.stopUpload(uploadId);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private UploadNotificationConfig createNotificationConfig(String notificationTitle) {
|
|
112
|
+
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(
|
|
113
|
+
notificationTitle,
|
|
114
|
+
notificationTitle + " - In Progress"
|
|
115
|
+
);
|
|
116
|
+
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Completed");
|
|
117
|
+
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Error");
|
|
118
|
+
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(
|
|
119
|
+
notificationTitle,
|
|
120
|
+
notificationTitle + " - Cancelled"
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return new UploadNotificationConfig(
|
|
124
|
+
"ee.forgr.capacitor.uploader.notification_channel_id",
|
|
125
|
+
false,
|
|
126
|
+
progress,
|
|
127
|
+
success,
|
|
128
|
+
error,
|
|
129
|
+
cancelled
|
|
130
|
+
);
|
|
183
131
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
132
|
+
|
|
133
|
+
private String getFileNameFromUri(Uri uri) {
|
|
134
|
+
String result = null;
|
|
135
|
+
if (uri.getScheme().equals("content")) {
|
|
136
|
+
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
|
|
137
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
138
|
+
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
139
|
+
if (index != -1) {
|
|
140
|
+
result = cursor.getString(index);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
} catch (Exception e) {
|
|
144
|
+
e.printStackTrace();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (result == null) {
|
|
148
|
+
result = uri.getPath();
|
|
149
|
+
int cut = result.lastIndexOf('/');
|
|
150
|
+
if (cut != -1) {
|
|
151
|
+
result = result.substring(cut + 1);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
190
155
|
}
|
|
191
|
-
return result;
|
|
192
|
-
}
|
|
193
156
|
}
|
|
@@ -21,168 +21,149 @@ import net.gotev.uploadservice.observer.request.RequestObserverDelegate;
|
|
|
21
21
|
@CapacitorPlugin(name = "Uploader")
|
|
22
22
|
public class UploaderPlugin extends Plugin {
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
private static final String CHANNEL_ID =
|
|
27
|
-
"ee.forgr.capacitor.uploader.notification_channel_id";
|
|
28
|
-
private static final String CHANNEL_NAME = "Uploader Notifications";
|
|
29
|
-
private static final String CHANNEL_DESCRIPTION =
|
|
30
|
-
"Notifications for file uploads";
|
|
31
|
-
|
|
32
|
-
private void createNotificationChannel() {
|
|
33
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
34
|
-
NotificationManager notificationManager =
|
|
35
|
-
(NotificationManager) getContext()
|
|
36
|
-
.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
37
|
-
|
|
38
|
-
NotificationChannel channel = new NotificationChannel(
|
|
39
|
-
CHANNEL_ID,
|
|
40
|
-
CHANNEL_NAME,
|
|
41
|
-
NotificationManager.IMPORTANCE_DEFAULT
|
|
42
|
-
);
|
|
43
|
-
channel.setDescription(CHANNEL_DESCRIPTION);
|
|
44
|
-
|
|
45
|
-
notificationManager.createNotificationChannel(channel);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
@Override
|
|
50
|
-
public void load() {
|
|
51
|
-
createNotificationChannel();
|
|
52
|
-
|
|
53
|
-
// Create a request observer for all uploads
|
|
54
|
-
RequestObserver observer = new RequestObserver(
|
|
55
|
-
getContext().getApplicationContext(),
|
|
56
|
-
getActivity(),
|
|
57
|
-
new RequestObserverDelegate() {
|
|
58
|
-
@Override
|
|
59
|
-
public void onProgress(Context context, UploadInfo uploadInfo) {
|
|
60
|
-
JSObject event = new JSObject();
|
|
61
|
-
event.put("name", "uploading");
|
|
62
|
-
JSObject payload = new JSObject();
|
|
63
|
-
payload.put("percent", uploadInfo.getProgressPercent());
|
|
64
|
-
event.put("payload", payload);
|
|
65
|
-
event.put("id", uploadInfo.getUploadId());
|
|
66
|
-
notifyListeners("events", event);
|
|
67
|
-
}
|
|
24
|
+
private Uploader implementation;
|
|
68
25
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
UploadInfo uploadInfo,
|
|
73
|
-
ServerResponse serverResponse
|
|
74
|
-
) {
|
|
75
|
-
JSObject event = new JSObject();
|
|
76
|
-
event.put("name", "completed");
|
|
77
|
-
JSObject payload = new JSObject();
|
|
78
|
-
payload.put("statusCode", serverResponse.getCode());
|
|
79
|
-
event.put("payload", payload);
|
|
80
|
-
event.put("id", uploadInfo.getUploadId());
|
|
81
|
-
notifyListeners("events", event);
|
|
82
|
-
}
|
|
26
|
+
private static final String CHANNEL_ID = "ee.forgr.capacitor.uploader.notification_channel_id";
|
|
27
|
+
private static final String CHANNEL_NAME = "Uploader Notifications";
|
|
28
|
+
private static final String CHANNEL_DESCRIPTION = "Notifications for file uploads";
|
|
83
29
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
UploadInfo uploadInfo,
|
|
88
|
-
Throwable exception
|
|
89
|
-
) {
|
|
90
|
-
JSObject event = new JSObject();
|
|
91
|
-
event.put("name", "failed");
|
|
92
|
-
JSObject payload = new JSObject();
|
|
93
|
-
payload.put("error", exception.getMessage());
|
|
94
|
-
event.put("payload", payload);
|
|
95
|
-
event.put("id", uploadInfo.getUploadId());
|
|
96
|
-
notifyListeners("events", event);
|
|
97
|
-
}
|
|
30
|
+
private void createNotificationChannel() {
|
|
31
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
32
|
+
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
98
33
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
JSObject event = new JSObject();
|
|
102
|
-
event.put("name", "finished");
|
|
103
|
-
event.put("id", uploadInfo.getUploadId());
|
|
104
|
-
notifyListeners("events", event);
|
|
105
|
-
}
|
|
34
|
+
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
|
|
35
|
+
channel.setDescription(CHANNEL_DESCRIPTION);
|
|
106
36
|
|
|
107
|
-
|
|
108
|
-
public void onCompletedWhileNotObserving() {
|
|
109
|
-
// Handle completion while not observing if needed
|
|
37
|
+
notificationManager.createNotificationChannel(channel);
|
|
110
38
|
}
|
|
111
|
-
|
|
112
|
-
);
|
|
39
|
+
}
|
|
113
40
|
|
|
114
|
-
|
|
115
|
-
|
|
41
|
+
@Override
|
|
42
|
+
public void load() {
|
|
43
|
+
createNotificationChannel();
|
|
44
|
+
|
|
45
|
+
// Create a request observer for all uploads
|
|
46
|
+
RequestObserver observer = new RequestObserver(
|
|
47
|
+
getContext().getApplicationContext(),
|
|
48
|
+
getActivity(),
|
|
49
|
+
new RequestObserverDelegate() {
|
|
50
|
+
@Override
|
|
51
|
+
public void onProgress(Context context, UploadInfo uploadInfo) {
|
|
52
|
+
JSObject event = new JSObject();
|
|
53
|
+
event.put("name", "uploading");
|
|
54
|
+
JSObject payload = new JSObject();
|
|
55
|
+
payload.put("percent", uploadInfo.getProgressPercent());
|
|
56
|
+
event.put("payload", payload);
|
|
57
|
+
event.put("id", uploadInfo.getUploadId());
|
|
58
|
+
notifyListeners("events", event);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Override
|
|
62
|
+
public void onSuccess(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
|
|
63
|
+
JSObject event = new JSObject();
|
|
64
|
+
event.put("name", "completed");
|
|
65
|
+
JSObject payload = new JSObject();
|
|
66
|
+
payload.put("statusCode", serverResponse.getCode());
|
|
67
|
+
event.put("payload", payload);
|
|
68
|
+
event.put("id", uploadInfo.getUploadId());
|
|
69
|
+
notifyListeners("events", event);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Override
|
|
73
|
+
public void onError(Context context, UploadInfo uploadInfo, Throwable exception) {
|
|
74
|
+
JSObject event = new JSObject();
|
|
75
|
+
event.put("name", "failed");
|
|
76
|
+
JSObject payload = new JSObject();
|
|
77
|
+
payload.put("error", exception.getMessage());
|
|
78
|
+
event.put("payload", payload);
|
|
79
|
+
event.put("id", uploadInfo.getUploadId());
|
|
80
|
+
notifyListeners("events", event);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@Override
|
|
84
|
+
public void onCompleted(Context context, UploadInfo uploadInfo) {
|
|
85
|
+
JSObject event = new JSObject();
|
|
86
|
+
event.put("name", "finished");
|
|
87
|
+
event.put("id", uploadInfo.getUploadId());
|
|
88
|
+
notifyListeners("events", event);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@Override
|
|
92
|
+
public void onCompletedWhileNotObserving() {
|
|
93
|
+
// Handle completion while not observing if needed
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
implementation = new Uploader(getContext().getApplicationContext());
|
|
99
|
+
}
|
|
116
100
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
101
|
+
public static String getMimeType(String url) {
|
|
102
|
+
String type = null;
|
|
103
|
+
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
|
|
104
|
+
if (extension != null) {
|
|
105
|
+
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
106
|
+
}
|
|
107
|
+
return type;
|
|
122
108
|
}
|
|
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
|
-
result.put("id", id);
|
|
161
|
-
call.resolve(result);
|
|
162
|
-
} catch (Exception e) {
|
|
163
|
-
call.reject(e.getMessage());
|
|
109
|
+
|
|
110
|
+
@PluginMethod
|
|
111
|
+
public void startUpload(PluginCall call) {
|
|
112
|
+
String filePath = call.getString("filePath");
|
|
113
|
+
String serverUrl = call.getString("serverUrl");
|
|
114
|
+
JSObject headersObj = call.getObject("headers", new JSObject());
|
|
115
|
+
JSObject parametersObj = call.getObject("parameters", new JSObject());
|
|
116
|
+
String httpMethod = call.getString("method", "POST");
|
|
117
|
+
String notificationTitle = call.getString("notificationTitle", "File Upload");
|
|
118
|
+
int maxRetries = call.getInt("maxRetries", 2);
|
|
119
|
+
String uploadType = call.getString("uploadType", "binary");
|
|
120
|
+
String fileField = call.getString("fileField", "file");
|
|
121
|
+
|
|
122
|
+
Map<String, String> headers = JSObjectToMap(headersObj);
|
|
123
|
+
Map<String, String> parameters = JSObjectToMap(parametersObj);
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
String mimeType = call.getString("mimeType", getMimeType(filePath));
|
|
127
|
+
|
|
128
|
+
String id = implementation.startUpload(
|
|
129
|
+
filePath,
|
|
130
|
+
serverUrl,
|
|
131
|
+
headers,
|
|
132
|
+
parameters,
|
|
133
|
+
httpMethod,
|
|
134
|
+
notificationTitle,
|
|
135
|
+
maxRetries,
|
|
136
|
+
mimeType,
|
|
137
|
+
uploadType,
|
|
138
|
+
fileField
|
|
139
|
+
);
|
|
140
|
+
JSObject result = new JSObject();
|
|
141
|
+
result.put("id", id);
|
|
142
|
+
call.resolve(result);
|
|
143
|
+
} catch (Exception e) {
|
|
144
|
+
call.reject(e.getMessage());
|
|
145
|
+
}
|
|
164
146
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
147
|
+
|
|
148
|
+
@PluginMethod
|
|
149
|
+
public void removeUpload(PluginCall call) {
|
|
150
|
+
String id = call.getString("id");
|
|
151
|
+
try {
|
|
152
|
+
implementation.removeUpload(id);
|
|
153
|
+
call.resolve();
|
|
154
|
+
} catch (Exception e) {
|
|
155
|
+
call.reject(e.getMessage());
|
|
156
|
+
}
|
|
175
157
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
158
|
+
|
|
159
|
+
private Map<String, String> JSObjectToMap(JSObject object) {
|
|
160
|
+
Map<String, String> map = new HashMap<>();
|
|
161
|
+
if (object != null) {
|
|
162
|
+
for (Iterator<String> it = object.keys(); it.hasNext();) {
|
|
163
|
+
String key = it.next();
|
|
164
|
+
map.put(key, object.getString(key));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return map;
|
|
185
168
|
}
|
|
186
|
-
return map;
|
|
187
|
-
}
|
|
188
169
|
}
|
package/dist/docs.json
CHANGED
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
},
|
|
75
75
|
{
|
|
76
76
|
"name": "addListener",
|
|
77
|
-
"signature": "(eventName:
|
|
77
|
+
"signature": "(eventName: 'events', listenerFunc: (state: UploadEvent) => void) => Promise<PluginListenerHandle>",
|
|
78
78
|
"parameters": [
|
|
79
79
|
{
|
|
80
80
|
"name": "eventName",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PluginListenerHandle } from
|
|
1
|
+
import type { PluginListenerHandle } from '@capacitor/core';
|
|
2
2
|
export interface uploadOption {
|
|
3
3
|
/**
|
|
4
4
|
* @since 0.0.1
|
|
@@ -29,7 +29,7 @@ export interface uploadOption {
|
|
|
29
29
|
* @description The method to use for the request
|
|
30
30
|
* @default 'POST'
|
|
31
31
|
*/
|
|
32
|
-
method?:
|
|
32
|
+
method?: 'PUT' | 'POST';
|
|
33
33
|
/**
|
|
34
34
|
* @since 0.0.1
|
|
35
35
|
* @description The mime type to use for the request
|
|
@@ -52,7 +52,7 @@ export interface uploadOption {
|
|
|
52
52
|
* @description The type of upload to use
|
|
53
53
|
* @default 'binary'
|
|
54
54
|
*/
|
|
55
|
-
uploadType?:
|
|
55
|
+
uploadType?: 'binary' | 'multipart';
|
|
56
56
|
/**
|
|
57
57
|
* @since 0.0.2
|
|
58
58
|
* @description The form field name for the file when using multipart
|
|
@@ -66,7 +66,7 @@ export interface UploadEvent {
|
|
|
66
66
|
*
|
|
67
67
|
* @since 0.0.1
|
|
68
68
|
*/
|
|
69
|
-
name:
|
|
69
|
+
name: 'uploading' | 'completed' | 'failed';
|
|
70
70
|
/**
|
|
71
71
|
* @since 0.0.1
|
|
72
72
|
* @description The payload of the event
|
|
@@ -121,5 +121,5 @@ export interface UploaderPlugin {
|
|
|
121
121
|
* @param listenerFunc
|
|
122
122
|
* @returns { PluginListenerHandle }
|
|
123
123
|
*/
|
|
124
|
-
addListener(eventName:
|
|
124
|
+
addListener(eventName: 'events', listenerFunc: (state: UploadEvent) => void): Promise<PluginListenerHandle>;
|
|
125
125
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"","sourcesContent":["import type { PluginListenerHandle } from '@capacitor/core';\n\nexport interface uploadOption {\n /**\n * @since 0.0.1\n * @description The file path of the file to upload\n */\n filePath: string;\n /**\n * @since 0.0.1\n * @description The url of the server\n */\n serverUrl: string;\n /**\n * @since 0.0.1\n * @default 'Uploading'\n * @description The title of the notification\n * Android only\n */\n notificationTitle?: number;\n /**\n * @since 0.0.1\n * @description The headers to send with the request\n */\n headers: {\n [key: string]: string;\n };\n /**\n * @since 0.0.1\n * @description The method to use for the request\n * @default 'POST'\n */\n method?: 'PUT' | 'POST';\n /**\n * @since 0.0.1\n * @description The mime type to use for the request\n */\n mimeType?: string;\n /**\n * @since 0.0.1\n * @description The parameters to send with the request\n */\n parameters?: { [key: string]: string };\n /**\n * @since 0.0.1\n * @description The maximum number of retries\n */\n maxRetries?: number;\n /**\n * @since 0.0.2\n * @description The type of upload to use\n * @default 'binary'\n */\n uploadType?: 'binary' | 'multipart';\n /**\n * @since 0.0.2\n * @description The form field name for the file when using multipart\n * @default 'file'\n */\n fileField?: string;\n}\nexport interface UploadEvent {\n /**\n * Current status of upload, between 0 and 100.\n *\n * @since 0.0.1\n */\n name: 'uploading' | 'completed' | 'failed';\n /**\n * @since 0.0.1\n * @description The payload of the event\n * @default { percent: 0, error: '', statusCode: 0 }\n */\n payload: {\n /**\n * @since 0.0.1\n * @description The percent of the upload\n */\n percent?: number;\n /**\n * @since 0.0.1\n * @description The error of the upload\n */\n error?: string;\n /**\n * @since 0.0.1\n * @description The status code of the upload\n */\n statusCode?: number;\n };\n /**\n * @since 0.0.1\n * @description The id of the upload\n */\n id: string;\n}\n\nexport interface UploaderPlugin {\n /**\n * @since 0.0.1\n * @description Start the upload\n * @param options uploadOption\n * @returns { id: string }\n */\n startUpload(options: uploadOption): Promise<{ id: string }>;\n /**\n * @since 0.0.1\n * @description Remove the upload\n * @param options\n * @returns { void }\n */\n removeUpload(options: { id: string }): Promise<void>;\n /**\n * @since 0.0.1\n * @description Add a listener for the upload events\n * @param eventName\n * @param listenerFunc\n * @returns { PluginListenerHandle }\n */\n addListener(eventName: 'events', listenerFunc: (state: UploadEvent) => void): Promise<PluginListenerHandle>;\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { registerPlugin } from
|
|
2
|
-
const Uploader = registerPlugin(
|
|
3
|
-
web: () => import(
|
|
1
|
+
import { registerPlugin } from '@capacitor/core';
|
|
2
|
+
const Uploader = registerPlugin('Uploader', {
|
|
3
|
+
web: () => import('./web').then((m) => new m.UploaderWeb()),
|
|
4
4
|
});
|
|
5
|
-
export * from
|
|
5
|
+
export * from './definitions';
|
|
6
6
|
export { Uploader };
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,QAAQ,GAAG,cAAc,CAAiB,UAAU,EAAE;IAC1D,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;CAC5D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,CAAC","sourcesContent":["import { registerPlugin } from
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,QAAQ,GAAG,cAAc,CAAiB,UAAU,EAAE;IAC1D,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;CAC5D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,QAAQ,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { UploaderPlugin } from './definitions';\n\nconst Uploader = registerPlugin<UploaderPlugin>('Uploader', {\n web: () => import('./web').then((m) => new m.UploaderWeb()),\n});\n\nexport * from './definitions';\nexport { Uploader };\n"]}
|
package/dist/esm/web.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { WebPlugin } from
|
|
2
|
-
import type { UploaderPlugin, uploadOption } from
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import type { UploaderPlugin, uploadOption } from './definitions';
|
|
3
3
|
export declare class UploaderWeb extends WebPlugin implements UploaderPlugin {
|
|
4
4
|
private uploads;
|
|
5
5
|
startUpload(options: uploadOption): Promise<{
|
package/dist/esm/web.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { WebPlugin } from
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
2
|
export class UploaderWeb extends WebPlugin {
|
|
3
3
|
constructor() {
|
|
4
4
|
super(...arguments);
|
|
5
5
|
this.uploads = new Map();
|
|
6
6
|
}
|
|
7
7
|
async startUpload(options) {
|
|
8
|
-
console.log(
|
|
8
|
+
console.log('startUpload', options);
|
|
9
9
|
const id = Math.random().toString(36).substring(2, 15);
|
|
10
10
|
const controller = new AbortController();
|
|
11
11
|
const maxRetries = options.maxRetries || 3;
|
|
@@ -14,49 +14,49 @@ export class UploaderWeb extends WebPlugin {
|
|
|
14
14
|
return { id };
|
|
15
15
|
}
|
|
16
16
|
async removeUpload(options) {
|
|
17
|
-
console.log(
|
|
17
|
+
console.log('removeUpload', options);
|
|
18
18
|
const upload = this.uploads.get(options.id);
|
|
19
19
|
if (upload) {
|
|
20
20
|
upload.controller.abort();
|
|
21
21
|
this.uploads.delete(options.id);
|
|
22
|
-
this.notifyListeners(
|
|
23
|
-
name:
|
|
22
|
+
this.notifyListeners('events', {
|
|
23
|
+
name: 'cancelled',
|
|
24
24
|
id: options.id,
|
|
25
25
|
payload: {},
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
async doUpload(id, options) {
|
|
30
|
-
const { filePath, serverUrl, headers = {}, method =
|
|
30
|
+
const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;
|
|
31
31
|
const upload = this.uploads.get(id);
|
|
32
32
|
if (!upload)
|
|
33
33
|
return;
|
|
34
34
|
try {
|
|
35
35
|
const file = await this.getFileFromPath(filePath);
|
|
36
36
|
if (!file)
|
|
37
|
-
throw new Error(
|
|
37
|
+
throw new Error('File not found');
|
|
38
38
|
const formData = new FormData();
|
|
39
|
-
formData.append(
|
|
39
|
+
formData.append('file', file);
|
|
40
40
|
for (const [key, value] of Object.entries(parameters)) {
|
|
41
41
|
formData.append(key, value);
|
|
42
42
|
}
|
|
43
43
|
const response = await fetch(serverUrl, {
|
|
44
44
|
method,
|
|
45
45
|
headers,
|
|
46
|
-
body: method ===
|
|
46
|
+
body: method === 'PUT' ? file : formData,
|
|
47
47
|
signal: upload.controller.signal,
|
|
48
48
|
});
|
|
49
49
|
if (!response.ok)
|
|
50
50
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
51
|
-
this.notifyListeners(
|
|
52
|
-
name:
|
|
51
|
+
this.notifyListeners('events', {
|
|
52
|
+
name: 'completed',
|
|
53
53
|
id,
|
|
54
54
|
payload: { statusCode: response.status },
|
|
55
55
|
});
|
|
56
56
|
this.uploads.delete(id);
|
|
57
57
|
}
|
|
58
58
|
catch (error) {
|
|
59
|
-
if (error.name ===
|
|
59
|
+
if (error.name === 'AbortError')
|
|
60
60
|
return;
|
|
61
61
|
if (upload.retries > 0) {
|
|
62
62
|
upload.retries--;
|
|
@@ -64,8 +64,8 @@ export class UploaderWeb extends WebPlugin {
|
|
|
64
64
|
setTimeout(() => this.doUpload(id, options), 1000);
|
|
65
65
|
}
|
|
66
66
|
else {
|
|
67
|
-
this.notifyListeners(
|
|
68
|
-
name:
|
|
67
|
+
this.notifyListeners('events', {
|
|
68
|
+
name: 'failed',
|
|
69
69
|
id,
|
|
70
70
|
payload: { error: error.message },
|
|
71
71
|
});
|
|
@@ -79,12 +79,12 @@ export class UploaderWeb extends WebPlugin {
|
|
|
79
79
|
try {
|
|
80
80
|
const response = await fetch(filePath);
|
|
81
81
|
const blob = await response.blob();
|
|
82
|
-
return new File([blob], filePath.split(
|
|
82
|
+
return new File([blob], filePath.split('/').pop() || 'file', {
|
|
83
83
|
type: blob.type,
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
catch (error) {
|
|
87
|
-
console.error(
|
|
87
|
+
console.error('Error getting file:', error);
|
|
88
88
|
return null;
|
|
89
89
|
}
|
|
90
90
|
}
|
package/dist/esm/web.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,WAAY,SAAQ,SAAS;IAA1C;;QACU,YAAO,
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,OAAO,WAAY,SAAQ,SAAS;IAA1C;;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\nimport type { UploaderPlugin, uploadOption } from './definitions';\n\nexport class UploaderWeb extends WebPlugin implements UploaderPlugin {\n private uploads: Map<string, { controller: AbortController; retries: number }> = new Map();\n\n async startUpload(options: uploadOption): Promise<{ id: string }> {\n console.log('startUpload', options);\n\n const id = Math.random().toString(36).substring(2, 15);\n const controller = new AbortController();\n const maxRetries = options.maxRetries || 3;\n this.uploads.set(id, { controller, retries: maxRetries });\n\n this.doUpload(id, options);\n\n return { id };\n }\n\n async removeUpload(options: { id: string }): Promise<void> {\n console.log('removeUpload', options);\n const upload = this.uploads.get(options.id);\n if (upload) {\n upload.controller.abort();\n this.uploads.delete(options.id);\n this.notifyListeners('events', {\n name: 'cancelled',\n id: options.id,\n payload: {},\n });\n }\n }\n\n private async doUpload(id: string, options: uploadOption) {\n const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;\n const upload = this.uploads.get(id);\n\n if (!upload) return;\n\n try {\n const file = await this.getFileFromPath(filePath);\n if (!file) throw new Error('File not found');\n\n const formData = new FormData();\n formData.append('file', file);\n\n for (const [key, value] of Object.entries(parameters)) {\n formData.append(key, value);\n }\n\n const response = await fetch(serverUrl, {\n method,\n headers,\n body: method === 'PUT' ? file : formData,\n signal: upload.controller.signal,\n });\n\n if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);\n\n this.notifyListeners('events', {\n name: 'completed',\n id,\n payload: { statusCode: response.status },\n });\n\n this.uploads.delete(id);\n } catch (error) {\n if ((error as Error).name === 'AbortError') return;\n\n if (upload.retries > 0) {\n upload.retries--;\n console.log(`Retrying upload (retries left: ${upload.retries})`);\n setTimeout(() => this.doUpload(id, options), 1000);\n } else {\n this.notifyListeners('events', {\n name: 'failed',\n id,\n payload: { error: (error as Error).message },\n });\n this.uploads.delete(id);\n }\n }\n }\n\n private async getFileFromPath(filePath: string): Promise<File | null> {\n // This is a simplified version. In a real-world scenario,\n // you might need to handle different types of paths or use a file system API.\n try {\n const response = await fetch(filePath);\n const blob = await response.blob();\n return new File([blob], filePath.split('/').pop() || 'file', {\n type: blob.type,\n });\n } catch (error) {\n console.error('Error getting file:', error);\n return null;\n }\n }\n}\n"]}
|
package/dist/plugin.cjs.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@capacitor/core');
|
|
4
4
|
|
|
5
|
-
const Uploader = core.registerPlugin(
|
|
5
|
+
const Uploader = core.registerPlugin('Uploader', {
|
|
6
6
|
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.UploaderWeb()),
|
|
7
7
|
});
|
|
8
8
|
|
|
@@ -12,7 +12,7 @@ class UploaderWeb extends core.WebPlugin {
|
|
|
12
12
|
this.uploads = new Map();
|
|
13
13
|
}
|
|
14
14
|
async startUpload(options) {
|
|
15
|
-
console.log(
|
|
15
|
+
console.log('startUpload', options);
|
|
16
16
|
const id = Math.random().toString(36).substring(2, 15);
|
|
17
17
|
const controller = new AbortController();
|
|
18
18
|
const maxRetries = options.maxRetries || 3;
|
|
@@ -21,49 +21,49 @@ class UploaderWeb extends core.WebPlugin {
|
|
|
21
21
|
return { id };
|
|
22
22
|
}
|
|
23
23
|
async removeUpload(options) {
|
|
24
|
-
console.log(
|
|
24
|
+
console.log('removeUpload', options);
|
|
25
25
|
const upload = this.uploads.get(options.id);
|
|
26
26
|
if (upload) {
|
|
27
27
|
upload.controller.abort();
|
|
28
28
|
this.uploads.delete(options.id);
|
|
29
|
-
this.notifyListeners(
|
|
30
|
-
name:
|
|
29
|
+
this.notifyListeners('events', {
|
|
30
|
+
name: 'cancelled',
|
|
31
31
|
id: options.id,
|
|
32
32
|
payload: {},
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
async doUpload(id, options) {
|
|
37
|
-
const { filePath, serverUrl, headers = {}, method =
|
|
37
|
+
const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;
|
|
38
38
|
const upload = this.uploads.get(id);
|
|
39
39
|
if (!upload)
|
|
40
40
|
return;
|
|
41
41
|
try {
|
|
42
42
|
const file = await this.getFileFromPath(filePath);
|
|
43
43
|
if (!file)
|
|
44
|
-
throw new Error(
|
|
44
|
+
throw new Error('File not found');
|
|
45
45
|
const formData = new FormData();
|
|
46
|
-
formData.append(
|
|
46
|
+
formData.append('file', file);
|
|
47
47
|
for (const [key, value] of Object.entries(parameters)) {
|
|
48
48
|
formData.append(key, value);
|
|
49
49
|
}
|
|
50
50
|
const response = await fetch(serverUrl, {
|
|
51
51
|
method,
|
|
52
52
|
headers,
|
|
53
|
-
body: method ===
|
|
53
|
+
body: method === 'PUT' ? file : formData,
|
|
54
54
|
signal: upload.controller.signal,
|
|
55
55
|
});
|
|
56
56
|
if (!response.ok)
|
|
57
57
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
58
|
-
this.notifyListeners(
|
|
59
|
-
name:
|
|
58
|
+
this.notifyListeners('events', {
|
|
59
|
+
name: 'completed',
|
|
60
60
|
id,
|
|
61
61
|
payload: { statusCode: response.status },
|
|
62
62
|
});
|
|
63
63
|
this.uploads.delete(id);
|
|
64
64
|
}
|
|
65
65
|
catch (error) {
|
|
66
|
-
if (error.name ===
|
|
66
|
+
if (error.name === 'AbortError')
|
|
67
67
|
return;
|
|
68
68
|
if (upload.retries > 0) {
|
|
69
69
|
upload.retries--;
|
|
@@ -71,8 +71,8 @@ class UploaderWeb extends core.WebPlugin {
|
|
|
71
71
|
setTimeout(() => this.doUpload(id, options), 1000);
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
|
-
this.notifyListeners(
|
|
75
|
-
name:
|
|
74
|
+
this.notifyListeners('events', {
|
|
75
|
+
name: 'failed',
|
|
76
76
|
id,
|
|
77
77
|
payload: { error: error.message },
|
|
78
78
|
});
|
|
@@ -86,12 +86,12 @@ class UploaderWeb extends core.WebPlugin {
|
|
|
86
86
|
try {
|
|
87
87
|
const response = await fetch(filePath);
|
|
88
88
|
const blob = await response.blob();
|
|
89
|
-
return new File([blob], filePath.split(
|
|
89
|
+
return new File([blob], filePath.split('/').pop() || 'file', {
|
|
90
90
|
type: blob.type,
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
catch (error) {
|
|
94
|
-
console.error(
|
|
94
|
+
console.error('Error getting file:', error);
|
|
95
95
|
return null;
|
|
96
96
|
}
|
|
97
97
|
}
|
package/dist/plugin.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Uploader = registerPlugin('Uploader', {\n web: () => import('./web').then((m) => new m.UploaderWeb()),\n});\nexport * from './definitions';\nexport { Uploader };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class UploaderWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.uploads = new Map();\n }\n async startUpload(options) {\n console.log('startUpload', options);\n const id = Math.random().toString(36).substring(2, 15);\n const controller = new AbortController();\n const maxRetries = options.maxRetries || 3;\n this.uploads.set(id, { controller, retries: maxRetries });\n this.doUpload(id, options);\n return { id };\n }\n async removeUpload(options) {\n console.log('removeUpload', options);\n const upload = this.uploads.get(options.id);\n if (upload) {\n upload.controller.abort();\n this.uploads.delete(options.id);\n this.notifyListeners('events', {\n name: 'cancelled',\n id: options.id,\n payload: {},\n });\n }\n }\n async doUpload(id, options) {\n const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;\n const upload = this.uploads.get(id);\n if (!upload)\n return;\n try {\n const file = await this.getFileFromPath(filePath);\n if (!file)\n throw new Error('File not found');\n const formData = new FormData();\n formData.append('file', file);\n for (const [key, value] of Object.entries(parameters)) {\n formData.append(key, value);\n }\n const response = await fetch(serverUrl, {\n method,\n headers,\n body: method === 'PUT' ? file : formData,\n signal: upload.controller.signal,\n });\n if (!response.ok)\n throw new Error(`HTTP error! status: ${response.status}`);\n this.notifyListeners('events', {\n name: 'completed',\n id,\n payload: { statusCode: response.status },\n });\n this.uploads.delete(id);\n }\n catch (error) {\n if (error.name === 'AbortError')\n return;\n if (upload.retries > 0) {\n upload.retries--;\n console.log(`Retrying upload (retries left: ${upload.retries})`);\n setTimeout(() => this.doUpload(id, options), 1000);\n }\n else {\n this.notifyListeners('events', {\n name: 'failed',\n id,\n payload: { error: error.message },\n });\n this.uploads.delete(id);\n }\n }\n }\n async getFileFromPath(filePath) {\n // This is a simplified version. In a real-world scenario,\n // you might need to handle different types of paths or use a file system API.\n try {\n const response = await fetch(filePath);\n const blob = await response.blob();\n return new File([blob], filePath.split('/').pop() || 'file', {\n type: blob.type,\n });\n }\n catch (error) {\n console.error('Error getting file:', error);\n return null;\n }\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AACK,MAAC,QAAQ,GAAGA,mBAAc,CAAC,UAAU,EAAE;AAC5C,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AAC/D,CAAC;;ACFM,MAAM,WAAW,SAASC,cAAS,CAAC;AAC3C,IAAI,WAAW,GAAG;AAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;AAC3B,QAAQ,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE;AAChC;AACA,IAAI,MAAM,WAAW,CAAC,OAAO,EAAE;AAC/B,QAAQ,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,CAAC;AAC3C,QAAQ,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;AAC9D,QAAQ,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE;AAChD,QAAQ,MAAM,UAAU,GAAG,OAAO,CAAC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C,CAAC,CAAC;AAChF,gBAAgB,UAAU,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC;AAClE;AACA,iBAAiB;AACjB,gBAAgB,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE;AAC/C,oBAAoB,IAAI,EAAE,QAAQ;AAClC,oBAAoB,EAAE;AACtB,oBAAoB,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,OAAO,EAAE;AACrD,iBAAiB,CAAC;AAClB,gBAAgB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;AACvC;AACA;AACA;AACA,IAAI,MAAM,eAAe,CAAC,QAAQ,EAAE;AACpC;AACA;AACA,QAAQ,IAAI;AACZ,YAAY,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,QAAQ,CAAC;AAClD,YAAY,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE;AAC9C,YAAY,OAAO,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,MAAM,EAAE;AACzE,gBAAgB,IAAI,EAAE,IAAI,CAAC,IAAI;AAC/B,aAAa,CAAC;AACd;AACA,QAAQ,OAAO,KAAK,EAAE;AACtB,YAAY,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC;AACvD,YAAY,OAAO,IAAI;AACvB;AACA;AACA;;;;;;;;;"}
|
package/dist/plugin.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var capacitorCapacitorUpdater = (function (exports, core) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const Uploader = core.registerPlugin(
|
|
4
|
+
const Uploader = core.registerPlugin('Uploader', {
|
|
5
5
|
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.UploaderWeb()),
|
|
6
6
|
});
|
|
7
7
|
|
|
@@ -11,7 +11,7 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
11
11
|
this.uploads = new Map();
|
|
12
12
|
}
|
|
13
13
|
async startUpload(options) {
|
|
14
|
-
console.log(
|
|
14
|
+
console.log('startUpload', options);
|
|
15
15
|
const id = Math.random().toString(36).substring(2, 15);
|
|
16
16
|
const controller = new AbortController();
|
|
17
17
|
const maxRetries = options.maxRetries || 3;
|
|
@@ -20,49 +20,49 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
20
20
|
return { id };
|
|
21
21
|
}
|
|
22
22
|
async removeUpload(options) {
|
|
23
|
-
console.log(
|
|
23
|
+
console.log('removeUpload', options);
|
|
24
24
|
const upload = this.uploads.get(options.id);
|
|
25
25
|
if (upload) {
|
|
26
26
|
upload.controller.abort();
|
|
27
27
|
this.uploads.delete(options.id);
|
|
28
|
-
this.notifyListeners(
|
|
29
|
-
name:
|
|
28
|
+
this.notifyListeners('events', {
|
|
29
|
+
name: 'cancelled',
|
|
30
30
|
id: options.id,
|
|
31
31
|
payload: {},
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
async doUpload(id, options) {
|
|
36
|
-
const { filePath, serverUrl, headers = {}, method =
|
|
36
|
+
const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;
|
|
37
37
|
const upload = this.uploads.get(id);
|
|
38
38
|
if (!upload)
|
|
39
39
|
return;
|
|
40
40
|
try {
|
|
41
41
|
const file = await this.getFileFromPath(filePath);
|
|
42
42
|
if (!file)
|
|
43
|
-
throw new Error(
|
|
43
|
+
throw new Error('File not found');
|
|
44
44
|
const formData = new FormData();
|
|
45
|
-
formData.append(
|
|
45
|
+
formData.append('file', file);
|
|
46
46
|
for (const [key, value] of Object.entries(parameters)) {
|
|
47
47
|
formData.append(key, value);
|
|
48
48
|
}
|
|
49
49
|
const response = await fetch(serverUrl, {
|
|
50
50
|
method,
|
|
51
51
|
headers,
|
|
52
|
-
body: method ===
|
|
52
|
+
body: method === 'PUT' ? file : formData,
|
|
53
53
|
signal: upload.controller.signal,
|
|
54
54
|
});
|
|
55
55
|
if (!response.ok)
|
|
56
56
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
57
|
-
this.notifyListeners(
|
|
58
|
-
name:
|
|
57
|
+
this.notifyListeners('events', {
|
|
58
|
+
name: 'completed',
|
|
59
59
|
id,
|
|
60
60
|
payload: { statusCode: response.status },
|
|
61
61
|
});
|
|
62
62
|
this.uploads.delete(id);
|
|
63
63
|
}
|
|
64
64
|
catch (error) {
|
|
65
|
-
if (error.name ===
|
|
65
|
+
if (error.name === 'AbortError')
|
|
66
66
|
return;
|
|
67
67
|
if (upload.retries > 0) {
|
|
68
68
|
upload.retries--;
|
|
@@ -70,8 +70,8 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
70
70
|
setTimeout(() => this.doUpload(id, options), 1000);
|
|
71
71
|
}
|
|
72
72
|
else {
|
|
73
|
-
this.notifyListeners(
|
|
74
|
-
name:
|
|
73
|
+
this.notifyListeners('events', {
|
|
74
|
+
name: 'failed',
|
|
75
75
|
id,
|
|
76
76
|
payload: { error: error.message },
|
|
77
77
|
});
|
|
@@ -85,12 +85,12 @@ var capacitorCapacitorUpdater = (function (exports, core) {
|
|
|
85
85
|
try {
|
|
86
86
|
const response = await fetch(filePath);
|
|
87
87
|
const blob = await response.blob();
|
|
88
|
-
return new File([blob], filePath.split(
|
|
88
|
+
return new File([blob], filePath.split('/').pop() || 'file', {
|
|
89
89
|
type: blob.type,
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
catch (error) {
|
|
93
|
-
console.error(
|
|
93
|
+
console.error('Error getting file:', error);
|
|
94
94
|
return null;
|
|
95
95
|
}
|
|
96
96
|
}
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from '@capacitor/core';\nconst Uploader = registerPlugin('Uploader', {\n web: () => import('./web').then((m) => new m.UploaderWeb()),\n});\nexport * from './definitions';\nexport { Uploader };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class UploaderWeb extends WebPlugin {\n constructor() {\n super(...arguments);\n this.uploads = new Map();\n }\n async startUpload(options) {\n console.log('startUpload', options);\n const id = Math.random().toString(36).substring(2, 15);\n const controller = new AbortController();\n const maxRetries = options.maxRetries || 3;\n this.uploads.set(id, { controller, retries: maxRetries });\n this.doUpload(id, options);\n return { id };\n }\n async removeUpload(options) {\n console.log('removeUpload', options);\n const upload = this.uploads.get(options.id);\n if (upload) {\n upload.controller.abort();\n this.uploads.delete(options.id);\n this.notifyListeners('events', {\n name: 'cancelled',\n id: options.id,\n payload: {},\n });\n }\n }\n async doUpload(id, options) {\n const { filePath, serverUrl, headers = {}, method = 'POST', parameters = {} } = options;\n const upload = this.uploads.get(id);\n if (!upload)\n return;\n try {\n const file = await this.getFileFromPath(filePath);\n if (!file)\n throw new Error('File not found');\n const formData = new FormData();\n formData.append('file', file);\n for (const [key, value] of Object.entries(parameters)) {\n formData.append(key, value);\n }\n const response = await fetch(serverUrl, {\n method,\n headers,\n body: method === 'PUT' ? file : formData,\n signal: upload.controller.signal,\n });\n if (!response.ok)\n throw new Error(`HTTP error! status: ${response.status}`);\n this.notifyListeners('events', {\n name: 'completed',\n id,\n payload: { statusCode: response.status },\n });\n this.uploads.delete(id);\n }\n catch (error) {\n if (error.name === 'AbortError')\n return;\n if (upload.retries > 0) {\n upload.retries--;\n console.log(`Retrying upload (retries left: ${upload.retries})`);\n setTimeout(() => this.doUpload(id, options), 1000);\n }\n else {\n this.notifyListeners('events', {\n name: 'failed',\n id,\n payload: { error: error.message },\n });\n this.uploads.delete(id);\n }\n }\n }\n async getFileFromPath(filePath) {\n // This is a simplified version. In a real-world scenario,\n // you might need to handle different types of paths or use a file system API.\n try {\n const response = await fetch(filePath);\n const blob = await response.blob();\n return new File([blob], filePath.split('/').pop() || 'file', {\n type: blob.type,\n });\n }\n catch (error) {\n console.error('Error getting file:', error);\n return null;\n }\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AACK,UAAC,QAAQ,GAAGA,mBAAc,CAAC,UAAU,EAAE;IAC5C,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;IAC/D,CAAC;;ICFM,MAAM,WAAW,SAASC,cAAS,CAAC;IAC3C,IAAI,WAAW,GAAG;IAClB,QAAQ,KAAK,CAAC,GAAG,SAAS,CAAC;IAC3B,QAAQ,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE;IAChC;IACA,IAAI,MAAM,WAAW,CAAC,OAAO,EAAE;IAC/B,QAAQ,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,CAAC;IAC3C,QAAQ,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC;IAC9D,QAAQ,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE;IAChD,QAAQ,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC;IAClD,QAAQ,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IACjE,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IAClC,QAAQ,OAAO,EAAE,EAAE,EAAE;IACrB;IACA,IAAI,MAAM,YAAY,CAAC,OAAO,EAAE;IAChC,QAAQ,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,OAAO,CAAC;IAC5C,QAAQ,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AAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;IACvC;IACA;IACA;IACA,IAAI,MAAM,eAAe,CAAC,QAAQ,EAAE;IACpC;IACA;IACA,QAAQ,IAAI;IACZ,YAAY,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,QAAQ,CAAC;IAClD,YAAY,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE;IAC9C,YAAY,OAAO,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,MAAM,EAAE;IACzE,gBAAgB,IAAI,EAAE,IAAI,CAAC,IAAI;IAC/B,aAAa,CAAC;IACd;IACA,QAAQ,OAAO,KAAK,EAAE;IACtB,YAAY,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC;IACvD,YAAY,OAAO,IAAI;IACvB;IACA;IACA;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/capacitor-uploader",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.20",
|
|
4
4
|
"description": "Upload file natively",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
55
55
|
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --autocorrect --format",
|
|
56
56
|
"eslint": "eslint .",
|
|
57
|
-
"prettier": "prettier
|
|
57
|
+
"prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
|
|
58
58
|
"swiftlint": "node-swiftlint",
|
|
59
59
|
"docgen": "docgen --api UploaderPlugin --output-readme README.md --output-json dist/docs.json",
|
|
60
60
|
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
|