@capgo/capacitor-uploader 8.1.11 → 8.1.12

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.
@@ -63,10 +63,14 @@ public class Uploader {
63
63
  request.addFileToUpload(filePath, fileField, getFileNameFromUri(Uri.parse(filePath)), mimeType);
64
64
 
65
65
  for (Map.Entry<String, String> entry : headers.entrySet()) {
66
- request.addHeader(entry.getKey(), entry.getValue());
66
+ if (entry.getKey() != null && entry.getValue() != null) {
67
+ request.addHeader(entry.getKey(), entry.getValue());
68
+ }
67
69
  }
68
70
  for (Map.Entry<String, String> entry : parameters.entrySet()) {
69
- request.addParameter(entry.getKey(), entry.getValue());
71
+ if (entry.getKey() != null && entry.getValue() != null) {
72
+ request.addParameter(entry.getKey(), entry.getValue());
73
+ }
70
74
  }
71
75
 
72
76
  return request.startUpload();
@@ -91,14 +95,20 @@ public class Uploader {
91
95
  .setNotificationConfig((ctx, uploadId) -> notificationConfig)
92
96
  .setMaxRetries(maxRetries);
93
97
 
94
- request.addHeader("Content-Type", mimeType);
98
+ if (mimeType != null && !mimeType.isEmpty()) {
99
+ request.addHeader("Content-Type", mimeType);
100
+ }
95
101
 
96
102
  for (Map.Entry<String, String> entry : headers.entrySet()) {
97
- request.addHeader(entry.getKey(), entry.getValue());
103
+ if (entry.getKey() != null && entry.getValue() != null) {
104
+ request.addHeader(entry.getKey(), entry.getValue());
105
+ }
98
106
  }
99
107
 
100
108
  for (Map.Entry<String, String> entry : parameters.entrySet()) {
101
- request.addParameter(entry.getKey(), entry.getValue());
109
+ if (entry.getKey() != null && entry.getValue() != null) {
110
+ request.addParameter(entry.getKey(), entry.getValue());
111
+ }
102
112
  }
103
113
 
104
114
  return request.startUpload();
@@ -132,7 +142,7 @@ public class Uploader {
132
142
 
133
143
  private String getFileNameFromUri(Uri uri) {
134
144
  String result = null;
135
- if (uri.getScheme().equals("content")) {
145
+ if ("content".equals(uri.getScheme())) {
136
146
  try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
137
147
  if (cursor != null && cursor.moveToFirst()) {
138
148
  int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
@@ -145,10 +155,10 @@ public class Uploader {
145
155
  }
146
156
  }
147
157
  if (result == null) {
148
- result = uri.getPath();
149
- int cut = result.lastIndexOf('/');
150
- if (cut != -1) {
151
- result = result.substring(cut + 1);
158
+ String path = uri.getPath();
159
+ if (path != null) {
160
+ int cut = path.lastIndexOf('/');
161
+ result = cut != -1 ? path.substring(cut + 1) : path;
152
162
  }
153
163
  }
154
164
  return result;
@@ -3,6 +3,7 @@ package ee.forgr.capacitor.uploader;
3
3
  import android.app.NotificationChannel;
4
4
  import android.app.NotificationManager;
5
5
  import android.content.Context;
6
+ import android.net.Uri;
6
7
  import android.os.Build;
7
8
  import android.webkit.MimeTypeMap;
8
9
  import com.getcapacitor.JSObject;
@@ -21,7 +22,11 @@ import net.gotev.uploadservice.observer.request.RequestObserverDelegate;
21
22
  @CapacitorPlugin(name = "Uploader")
22
23
  public class UploaderPlugin extends Plugin {
23
24
 
24
- private final String pluginVersion = "8.1.11";
25
+ private static final String CAPACITOR_FILE_PATH_PREFIX = "/_capacitor_file_";
26
+
27
+ private static final String CAPACITOR_CONTENT_PATH_PREFIX = "/_capacitor_content_";
28
+
29
+ private final String pluginVersion = "8.1.12";
25
30
 
26
31
  private Uploader implementation;
27
32
 
@@ -123,6 +128,10 @@ public class UploaderPlugin extends Plugin {
123
128
  return;
124
129
  }
125
130
 
131
+ // Convert Capacitor web-accessible URLs to paths native code can open.
132
+ // Capacitor 8+ removed Bridge.getLocalUrl(String); mirror AndroidProtocolHandler logic.
133
+ String localFilePath = resolveCapacitorPath(filePath);
134
+
126
135
  JSObject headersObj = call.getObject("headers", new JSObject());
127
136
  JSObject parametersObj = call.getObject("parameters", new JSObject());
128
137
  String httpMethod = call.getString("method", "POST");
@@ -135,10 +144,10 @@ public class UploaderPlugin extends Plugin {
135
144
  Map<String, String> parameters = JSObjectToMap(parametersObj);
136
145
 
137
146
  try {
138
- String mimeType = call.getString("mimeType", getMimeType(filePath));
147
+ String mimeType = call.getString("mimeType", getMimeType(localFilePath));
139
148
 
140
149
  String id = implementation.startUpload(
141
- filePath,
150
+ localFilePath,
142
151
  serverUrl,
143
152
  headers,
144
153
  parameters,
@@ -172,12 +181,50 @@ public class UploaderPlugin extends Plugin {
172
181
  }
173
182
  }
174
183
 
184
+ /**
185
+ * Maps WebView URLs (e.g. http(s)://localhost/_capacitor_file_/...) to filesystem or content
186
+ * paths, matching {@link com.getcapacitor.AndroidProtocolHandler}. Plain absolute paths and
187
+ * unrecognized URLs are returned unchanged.
188
+ */
189
+ private static String resolveCapacitorPath(String filePath) {
190
+ if (filePath == null || filePath.isEmpty()) {
191
+ return filePath;
192
+ }
193
+ Uri uri = Uri.parse(filePath);
194
+ String path = uri.getPath();
195
+ if (path != null) {
196
+ if (path.startsWith(CAPACITOR_FILE_PATH_PREFIX)) {
197
+ return path.substring(CAPACITOR_FILE_PATH_PREFIX.length());
198
+ }
199
+ if (path.startsWith(CAPACITOR_CONTENT_PATH_PREFIX)) {
200
+ String scheme = uri.getScheme();
201
+ String host = uri.getHost();
202
+ if (scheme != null && host != null) {
203
+ String baseUrl = scheme + "://" + host;
204
+ if (uri.getPort() != -1) {
205
+ baseUrl += ":" + uri.getPort();
206
+ }
207
+ return filePath.replace(baseUrl + CAPACITOR_CONTENT_PATH_PREFIX, "content:/");
208
+ }
209
+ return filePath.replace(CAPACITOR_CONTENT_PATH_PREFIX, "content:/");
210
+ }
211
+ }
212
+ if ("file".equalsIgnoreCase(uri.getScheme()) && uri.getPath() != null) {
213
+ return uri.getPath();
214
+ }
215
+ return filePath;
216
+ }
217
+
175
218
  private Map<String, String> JSObjectToMap(JSObject object) {
176
219
  Map<String, String> map = new HashMap<>();
177
220
  if (object != null) {
178
221
  for (Iterator<String> it = object.keys(); it.hasNext(); ) {
179
222
  String key = it.next();
180
- map.put(key, object.getString(key));
223
+ String value = object.getString(key);
224
+ // Only add non-null and non-empty values to prevent upload service errors
225
+ if (value != null && !value.isEmpty()) {
226
+ map.put(key, value);
227
+ }
181
228
  }
182
229
  }
183
230
  return map;
@@ -3,7 +3,7 @@ import Capacitor
3
3
 
4
4
  @objc(UploaderPlugin)
5
5
  public class UploaderPlugin: CAPPlugin, CAPBridgedPlugin {
6
- private let pluginVersion: String = "8.1.11"
6
+ private let pluginVersion: String = "8.1.12"
7
7
  public let identifier = "UploaderPlugin"
8
8
  public let jsName = "Uploader"
9
9
  public let pluginMethods: [CAPPluginMethod] = [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-uploader",
3
- "version": "8.1.11",
3
+ "version": "8.1.12",
4
4
  "description": "Upload file natively",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",