@capgo/capacitor-updater 7.15.3 → 7.16.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.
@@ -33,9 +33,11 @@ import java.net.URL;
33
33
  import java.util.ArrayList;
34
34
  import java.util.Arrays;
35
35
  import java.util.Date;
36
+ import java.util.HashSet;
36
37
  import java.util.List;
37
38
  import java.util.Map;
38
39
  import java.util.Objects;
40
+ import java.util.Set;
39
41
  import java.util.Timer;
40
42
  import java.util.TimerTask;
41
43
  import java.util.UUID;
@@ -58,7 +60,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
58
60
  private static final String statsUrlDefault = "https://plugin.capgo.app/stats";
59
61
  private static final String channelUrlDefault = "https://plugin.capgo.app/channel_self";
60
62
 
61
- private final String PLUGIN_VERSION = "7.15.3";
63
+ private final String PLUGIN_VERSION = "7.16.0";
62
64
  private static final String DELAY_CONDITION_PREFERENCES = "";
63
65
 
64
66
  private SharedPreferences.Editor editor;
@@ -443,6 +445,14 @@ public class CapacitorUpdaterPlugin extends Plugin {
443
445
  logger.error("Failed to delete: " + bundle.getId() + " " + e.getMessage());
444
446
  }
445
447
  }
448
+ final List<BundleInfo> storedBundles = this.implementation.list(true);
449
+ final Set<String> allowedIds = new HashSet<>();
450
+ for (final BundleInfo info : storedBundles) {
451
+ if (info != null && info.getId() != null && !info.getId().isEmpty()) {
452
+ allowedIds.add(info.getId());
453
+ }
454
+ }
455
+ this.implementation.cleanupDownloadDirectories(allowedIds);
446
456
  }
447
457
  this.editor.putString("LatestNativeBuildVersion", this.currentBuildVersion);
448
458
  this.editor.apply();
@@ -32,6 +32,7 @@ import java.util.Iterator;
32
32
  import java.util.List;
33
33
  import java.util.Map;
34
34
  import java.util.Objects;
35
+ import java.util.Set;
35
36
  import java.util.concurrent.CompletableFuture;
36
37
  import java.util.concurrent.ConcurrentHashMap;
37
38
  import java.util.concurrent.TimeUnit;
@@ -466,6 +467,41 @@ public class CapgoUpdater {
466
467
  }
467
468
  }
468
469
 
470
+ public void cleanupDownloadDirectories(final Set<String> allowedIds) {
471
+ if (this.documentsDir == null) {
472
+ logger.warn("Documents directory is null, skipping download cleanup");
473
+ return;
474
+ }
475
+
476
+ final File bundleRoot = new File(this.documentsDir, bundleDirectory);
477
+ if (!bundleRoot.exists() || !bundleRoot.isDirectory()) {
478
+ return;
479
+ }
480
+
481
+ final File[] entries = bundleRoot.listFiles();
482
+ if (entries != null) {
483
+ for (final File entry : entries) {
484
+ if (!entry.isDirectory()) {
485
+ continue;
486
+ }
487
+
488
+ final String id = entry.getName();
489
+
490
+ if (allowedIds != null && allowedIds.contains(id)) {
491
+ continue;
492
+ }
493
+
494
+ try {
495
+ this.deleteDirectory(entry);
496
+ this.removeBundleInfo(id);
497
+ logger.info("Deleted orphan bundle directory: " + id);
498
+ } catch (IOException e) {
499
+ logger.error("Failed to delete orphan bundle directory: " + id + " " + e.getMessage());
500
+ }
501
+ }
502
+ }
503
+ }
504
+
469
505
  private void safeDelete(final File target) {
470
506
  if (target == null || !target.exists()) {
471
507
  return;
@@ -50,7 +50,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
50
50
  CAPPluginMethod(name: "isShakeMenuEnabled", returnType: CAPPluginReturnPromise)
51
51
  ]
52
52
  public var implementation = CapgoUpdater()
53
- private let PLUGIN_VERSION: String = "7.15.3"
53
+ private let PLUGIN_VERSION: String = "7.16.0"
54
54
  static let updateUrlDefault = "https://plugin.capgo.app/updates"
55
55
  static let statsUrlDefault = "https://plugin.capgo.app/stats"
56
56
  static let channelUrlDefault = "https://plugin.capgo.app/channel_self"
@@ -248,6 +248,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
248
248
  logger.error("Delete failed, id \(version.getId()) doesn't exist")
249
249
  }
250
250
  }
251
+
252
+ let storedBundles = implementation.list(raw: true)
253
+ let allowedIds = Set(storedBundles.compactMap { info -> String? in
254
+ let id = info.getId()
255
+ return id.isEmpty ? nil : id
256
+ })
257
+ implementation.cleanupDownloadDirectories(allowedIds: allowedIds)
251
258
  }
252
259
  UserDefaults.standard.set(self.currentBuildVersion, forKey: "LatestNativeBuildVersion")
253
260
  UserDefaults.standard.synchronize()
@@ -886,6 +886,42 @@ import UIKit
886
886
  return self.delete(id: id, removeInfo: true)
887
887
  }
888
888
 
889
+ public func cleanupDownloadDirectories(allowedIds: Set<String>) {
890
+ let bundleRoot = libraryDir.appendingPathComponent(bundleDirectory)
891
+ let fileManager = FileManager.default
892
+
893
+ guard fileManager.fileExists(atPath: bundleRoot.path) else {
894
+ return
895
+ }
896
+
897
+ do {
898
+ let contents = try fileManager.contentsOfDirectory(at: bundleRoot, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles])
899
+
900
+ for url in contents {
901
+ let resourceValues = try url.resourceValues(forKeys: [.isDirectoryKey])
902
+ if resourceValues.isDirectory != true {
903
+ continue
904
+ }
905
+
906
+ let id = url.lastPathComponent
907
+
908
+ if allowedIds.contains(id) {
909
+ continue
910
+ }
911
+
912
+ do {
913
+ try fileManager.removeItem(at: url)
914
+ self.removeBundleInfo(id: id)
915
+ logger.info("Deleted orphan bundle directory: \(id)")
916
+ } catch {
917
+ logger.error("Failed to delete orphan bundle directory: \(id) \(error.localizedDescription)")
918
+ }
919
+ }
920
+ } catch {
921
+ logger.error("Failed to enumerate bundle directory for cleanup: \(error.localizedDescription)")
922
+ }
923
+ }
924
+
889
925
  public func getBundleDirectory(id: String) -> URL {
890
926
  return libraryDir.appendingPathComponent(self.bundleDirectory).appendingPathComponent(id)
891
927
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "7.15.3",
3
+ "version": "7.16.0",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Live update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",