@capacitor/filesystem 7.0.0-dev-2287-20241220T140459.0 → 7.0.0-dev-2288-20241220T172526.0

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.
@@ -28,10 +28,6 @@ import java.nio.charset.Charset;
28
28
  import java.nio.charset.StandardCharsets;
29
29
  import java.util.Locale;
30
30
  import org.json.JSONException;
31
- import java.util.concurrent.ExecutorService;
32
- import java.util.concurrent.Executors;
33
- import android.os.Handler;
34
- import android.os.Looper;
35
31
 
36
32
  public class Filesystem {
37
33
 
@@ -105,7 +101,7 @@ public class Filesystem {
105
101
  }
106
102
 
107
103
  public File copy(String from, String directory, String to, String toDirectory, boolean doRename)
108
- throws IOException, CopyFailedException {
104
+ throws IOException, CopyFailedException {
109
105
  if (toDirectory == null) {
110
106
  toDirectory = directory;
111
107
  }
@@ -307,123 +303,81 @@ public class Filesystem {
307
303
  }
308
304
  }
309
305
 
310
- public void downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter,
311
- FilesystemDownloadCallback callback) throws IOException, URISyntaxException, JSONException {
306
+ public JSObject downloadFile(PluginCall call, Bridge bridge, HttpRequestHandler.ProgressEmitter emitter)
307
+ throws IOException, URISyntaxException, JSONException {
312
308
  String urlString = call.getString("url", "");
313
- ExecutorService executor = Executors.newSingleThreadExecutor();
314
- Handler handler = new Handler(Looper.getMainLooper());
315
-
316
- executor.execute(() -> {
317
- AsyncTaskResult result = doDownloadInBackground(urlString, call, bridge, emitter);
318
- handler.post(() -> {
319
- if (result.error != null) {
320
- callback.onError(result.error);
321
- } else {
322
- callback.onSuccess(result.result);
323
- }
324
- });
325
- executor.shutdown();
326
- });
327
- }
309
+ JSObject headers = call.getObject("headers", new JSObject());
310
+ JSObject params = call.getObject("params", new JSObject());
311
+ Integer connectTimeout = call.getInt("connectTimeout");
312
+ Integer readTimeout = call.getInt("readTimeout");
313
+ Boolean disableRedirects = call.getBoolean("disableRedirects");
314
+ Boolean shouldEncode = call.getBoolean("shouldEncodeUrlParams", true);
315
+ Boolean progress = call.getBoolean("progress", false);
316
+
317
+ String method = call.getString("method", "GET").toUpperCase(Locale.ROOT);
318
+ String path = call.getString("path");
319
+ String directory = call.getString("directory", Environment.DIRECTORY_DOWNLOADS);
320
+
321
+ final URL url = new URL(urlString);
322
+ final File file = getFileObject(path, directory);
323
+
324
+ HttpRequestHandler.HttpURLConnectionBuilder connectionBuilder = new HttpRequestHandler.HttpURLConnectionBuilder()
325
+ .setUrl(url)
326
+ .setMethod(method)
327
+ .setHeaders(headers)
328
+ .setUrlParams(params, shouldEncode)
329
+ .setConnectTimeout(connectTimeout)
330
+ .setReadTimeout(readTimeout)
331
+ .setDisableRedirects(disableRedirects)
332
+ .openConnection();
333
+
334
+ CapacitorHttpUrlConnection connection = connectionBuilder.build();
335
+
336
+ connection.setSSLSocketFactory(bridge);
337
+
338
+ InputStream connectionInputStream = connection.getInputStream();
339
+ FileOutputStream fileOutputStream = new FileOutputStream(file, false);
340
+
341
+ String contentLength = connection.getHeaderField("content-length");
342
+ int bytes = 0;
343
+ int maxBytes = 0;
328
344
 
329
- private AsyncTaskResult doDownloadInBackground(String urlString, PluginCall call, Bridge bridge,
330
- HttpRequestHandler.ProgressEmitter emitter) {
331
345
  try {
332
- JSObject headers = call.getObject("headers", new JSObject());
333
- JSObject params = call.getObject("params", new JSObject());
334
- Integer connectTimeout = call.getInt("connectTimeout");
335
- Integer readTimeout = call.getInt("readTimeout");
336
- Boolean disableRedirects = call.getBoolean("disableRedirects");
337
- Boolean shouldEncode = call.getBoolean("shouldEncodeUrlParams", true);
338
- Boolean progress = call.getBoolean("progress", false);
339
-
340
- String method = call.getString("method", "GET").toUpperCase(Locale.ROOT);
341
- String path = call.getString("path");
342
- String directory = call.getString("directory", Environment.DIRECTORY_DOWNLOADS);
343
-
344
- final URL url = new URL(urlString);
345
- final File file = getFileObject(path, directory);
346
-
347
- HttpRequestHandler.HttpURLConnectionBuilder connectionBuilder = new HttpRequestHandler.HttpURLConnectionBuilder()
348
- .setUrl(url)
349
- .setMethod(method)
350
- .setHeaders(headers)
351
- .setUrlParams(params, shouldEncode)
352
- .setConnectTimeout(connectTimeout)
353
- .setReadTimeout(readTimeout)
354
- .setDisableRedirects(disableRedirects)
355
- .openConnection();
356
-
357
- CapacitorHttpUrlConnection connection = connectionBuilder.build();
358
-
359
- connection.setSSLSocketFactory(bridge);
360
-
361
- InputStream connectionInputStream = connection.getInputStream();
362
- FileOutputStream fileOutputStream = new FileOutputStream(file, false);
363
-
364
- String contentLength = connection.getHeaderField("content-length");
365
- int bytes = 0;
366
- int maxBytes = 0;
367
-
368
- try {
369
- maxBytes = contentLength != null ? Integer.parseInt(contentLength) : 0;
370
- } catch (NumberFormatException ignored) {
371
- }
372
-
373
- byte[] buffer = new byte[1024];
374
- int len;
346
+ maxBytes = contentLength != null ? Integer.parseInt(contentLength) : 0;
347
+ } catch (NumberFormatException ignored) {}
375
348
 
376
- // Throttle emitter to 100ms so it doesn't slow down app
377
- long lastEmitTime = System.currentTimeMillis();
378
- long minEmitIntervalMillis = 100;
349
+ byte[] buffer = new byte[1024];
350
+ int len;
379
351
 
380
- while ((len = connectionInputStream.read(buffer)) > 0) {
381
- fileOutputStream.write(buffer, 0, len);
352
+ // Throttle emitter to 100ms so it doesn't slow down app
353
+ long lastEmitTime = System.currentTimeMillis();
354
+ long minEmitIntervalMillis = 100;
382
355
 
383
- bytes += len;
356
+ while ((len = connectionInputStream.read(buffer)) > 0) {
357
+ fileOutputStream.write(buffer, 0, len);
384
358
 
385
- if (progress && null != emitter) {
386
- long currentTime = System.currentTimeMillis();
387
- if (currentTime - lastEmitTime > minEmitIntervalMillis) {
388
- emitter.emit(bytes, maxBytes);
389
- lastEmitTime = currentTime;
390
- }
391
- }
392
- }
359
+ bytes += len;
393
360
 
394
361
  if (progress && null != emitter) {
395
- emitter.emit(bytes, maxBytes);
362
+ long currentTime = System.currentTimeMillis();
363
+ if (currentTime - lastEmitTime > minEmitIntervalMillis) {
364
+ emitter.emit(bytes, maxBytes);
365
+ lastEmitTime = currentTime;
366
+ }
396
367
  }
397
-
398
- connectionInputStream.close();
399
- fileOutputStream.close();
400
-
401
- JSObject ret = new JSObject();
402
- ret.put("path", file.getAbsolutePath());
403
- return new AsyncTaskResult(ret);
404
- } catch (Exception e) {
405
- return new AsyncTaskResult(e);
406
- }
407
- }
408
-
409
- private static class AsyncTaskResult {
410
- final JSObject result;
411
- final Exception error;
412
-
413
- AsyncTaskResult(JSObject result) {
414
- this.result = result;
415
- this.error = null;
416
368
  }
417
369
 
418
- AsyncTaskResult(Exception error) {
419
- this.result = null;
420
- this.error = error;
370
+ if (progress && null != emitter) {
371
+ emitter.emit(bytes, maxBytes);
421
372
  }
422
- }
423
373
 
424
- public interface FilesystemDownloadCallback {
425
- void onSuccess(JSObject result);
374
+ connectionInputStream.close();
375
+ fileOutputStream.close();
426
376
 
427
- void onError(Exception error);
377
+ return new JSObject() {
378
+ {
379
+ put("path", file.getAbsolutePath());
380
+ }
381
+ };
428
382
  }
429
383
  }
@@ -133,7 +133,7 @@ public class FilesystemPlugin extends Plugin {
133
133
  } else {
134
134
  if (
135
135
  fileObject.getParentFile() == null ||
136
- fileObject.getParentFile().exists() ||
136
+ fileObject.getParentFile().exists() ||
137
137
  (recursive && fileObject.getParentFile().mkdirs())
138
138
  ) {
139
139
  saveFile(call, fileObject, data);
@@ -169,7 +169,7 @@ public class FilesystemPlugin extends Plugin {
169
169
  call.resolve(result);
170
170
  } catch (IOException ex) {
171
171
  Logger.error(
172
- getLogTag(),
172
+ getLogTag(),
173
173
  "Creating file '" + file.getPath() + "' with charset '" + charset + "' failed. Error: " + ex.getMessage(),
174
174
  ex
175
175
  );
@@ -388,37 +388,23 @@ public class FilesystemPlugin extends Plugin {
388
388
 
389
389
  if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
390
390
  requestAllPermissions(call, "permissionCallback");
391
- return;
392
- }
391
+ } else {
392
+ HttpRequestHandler.ProgressEmitter emitter = (bytes, contentLength) -> {
393
+ JSObject ret = new JSObject();
394
+ ret.put("url", call.getString("url"));
395
+ ret.put("bytes", bytes);
396
+ ret.put("contentLength", contentLength);
393
397
 
394
- HttpRequestHandler.ProgressEmitter emitter = (bytes, contentLength) -> {
395
- JSObject ret = new JSObject();
396
- ret.put("url", call.getString("url"));
397
- ret.put("bytes", bytes);
398
- ret.put("contentLength", contentLength);
399
- notifyListeners("progress", ret);
400
- };
401
-
402
- implementation.downloadFile(
403
- call,
404
- bridge,
405
- emitter,
406
- new Filesystem.FilesystemDownloadCallback() {
407
- @Override
408
- public void onSuccess(JSObject response) {
409
- // update mediaStore index only if file was written to external storage
410
- if (isPublicDirectory(directory)) {
411
- MediaScannerConnection.scanFile(getContext(), new String[] { response.getString("path") }, null, null);
412
- }
413
- call.resolve(response);
414
- }
398
+ notifyListeners("progress", ret);
399
+ };
415
400
 
416
- @Override
417
- public void onError(Exception error) {
418
- call.reject("Error downloading file: " + error.getLocalizedMessage(), error);
419
- }
401
+ JSObject response = implementation.downloadFile(call, bridge, emitter);
402
+ // update mediaStore index only if file was written to external storage
403
+ if (isPublicDirectory(directory)) {
404
+ MediaScannerConnection.scanFile(getContext(), new String[] { response.getString("path") }, null, null);
420
405
  }
421
- );
406
+ call.resolve(response);
407
+ }
422
408
  } catch (Exception ex) {
423
409
  call.reject("Error downloading file: " + ex.getLocalizedMessage(), ex);
424
410
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/filesystem",
3
- "version": "7.0.0-dev-2287-20241220T140459.0",
3
+ "version": "7.0.0-dev-2288-20241220T172526.0",
4
4
  "description": "The Filesystem API provides a NodeJS-like API for working with files on the device.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -63,7 +63,7 @@
63
63
  "typescript": "~4.1.5"
64
64
  },
65
65
  "peerDependencies": {
66
- "@capacitor/core": "next"
66
+ "@capacitor/core": ">=7.0.0-rc.0"
67
67
  },
68
68
  "prettier": "@ionic/prettier-config",
69
69
  "swiftlint": "@ionic/swiftlint-config",