Skip to content

Commit

Permalink
fix: delete bundle issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Sep 28, 2022
1 parent 2730cef commit a4f0dd8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public Boolean isErrorStatus() {
return BundleStatus.ERROR == this.status;
}

public Boolean isDeleted() {
return BundleStatus.DELETED == this.status;
}

public boolean isDownloaded() {
return !this.isBuiltin() && this.downloaded != null && !this.downloaded.equals("");
return !this.isBuiltin() && this.downloaded != null && !this.downloaded.equals("") && !this.isDeleted();
}

public String getDownloaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum BundleStatus {
SUCCESS("success"),
ERROR("error"),
PENDING("pending"),
DELETED("deleted"),
DOWNLOADING("donwloading");

public final String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ public Boolean delete(final String id, final Boolean removeInfo) throws IOExcept
this.deleteDirectory(bundle);
if (removeInfo) {
this.removeBundleInfo(id);
} else {
this.saveBundleInfo(id, deleted.setStatus(BundleStatus.DELETED));
}
return true;
}
Expand All @@ -283,7 +285,8 @@ private File getBundleDirectory(final String id) {

private boolean bundleExists(final String id) {
final File bundle = this.getBundleDirectory(id);
if (bundle == null || !bundle.exists()) {
final BundleInfo bundleInfo = this.getBundleInfo(id);
if (bundle == null || !bundle.exists() || !bundleInfo.isDeleted()) {
return false;
}
return new File(bundle.getPath(), "/index.html").exists();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void run() {
}
call.resolve(ret);
}
);
);
}
}
);
Expand Down Expand Up @@ -619,11 +619,11 @@ public void run() {

if (
latestVersionName != null &&
!"".equals(latestVersionName) &&
!current.getVersionName().equals(latestVersionName)
!"".equals(latestVersionName) &&
!current.getVersionName().equals(latestVersionName)
) {
final BundleInfo latest =
CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(latestVersionName);
CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(latestVersionName);
if (latest != null) {
if (latest.isErrorStatus()) {
Log.e(
Expand All @@ -646,6 +646,20 @@ public void run() {
CapacitorUpdaterPlugin.this.implementation.setNextBundle(latest.getId());
return;
}
if (latest.isDeleted()) {
Log.i(
CapacitorUpdater.TAG,
"Latest bundle already exists and will be deleted, download will overwrite it."
);
try {
final Boolean deleted = CapacitorUpdaterPlugin.this.implementation.delete(latest.getId(), true);
if (deleted) {
Log.i(CapacitorUpdater.TAG, "Failed bundle deleted: " + latest.getVersionName());
}
} catch (final IOException e) {
Log.e(CapacitorUpdater.TAG, "Failed to delete failed bundle: " + latest.getVersionName(), e);
}
}
}

new Thread(
Expand Down Expand Up @@ -695,7 +709,7 @@ public void run() {
CapacitorUpdaterPlugin.this.notifyListeners("noNeedUpdate", retNoNeed);
}
}
);
);
}
}
)
Expand Down Expand Up @@ -820,8 +834,8 @@ private class DeferredNotifyAppReadyCheck implements Runnable {
public void run() {
try {
Log.i(
CapacitorUpdater.TAG,
"Wait for " + CapacitorUpdaterPlugin.this.appReadyTimeout + "ms, then check for notifyAppReady"
CapacitorUpdater.TAG,
"Wait for " + CapacitorUpdaterPlugin.this.appReadyTimeout + "ms, then check for notifyAppReady"
);
Thread.sleep(CapacitorUpdaterPlugin.this.appReadyTimeout);
CapacitorUpdaterPlugin.this.checkRevert();
Expand Down
6 changes: 5 additions & 1 deletion ios/Plugin/BundleInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ import Foundation
return BundleStatus.ERROR == self.status
}

public func isDeleted() -> Bool {
return BundleStatus.DELETED == self.status
}

public func isDownloaded() -> Bool {
return !self.isBuiltin() && self.downloaded != "" && self.downloaded != BundleInfo.DOWNLOADED_BUILTIN
return !self.isBuiltin() && self.downloaded != "" && self.downloaded != BundleInfo.DOWNLOADED_BUILTIN && !self.isDeleted()
}

public func getDownloaded() -> String {
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/BundleStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum BundleStatus: LocalizedString, Decodable, Encodable {
case SUCCESS = "success"
case ERROR = "error"
case PENDING = "pending"
case DELETED = "deleted"
case DOWNLOADING = "donwloading"

var localizedString: String {
Expand Down
6 changes: 4 additions & 2 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ extension CustomError: LocalizedError {
}
if removeInfo {
self.removeBundleInfo(id: id)
} else {
self.saveBundleInfo(id: id, bundle: deleted.setStatus(status: BundleStatus.DELETED.localizedString))
}
self.removeBundleInfo(id: id)
self.sendStats(action: "delete", versionName: deleted.getVersionName())
return true
}
Expand All @@ -386,7 +387,8 @@ extension CustomError: LocalizedError {
let indexHot = destHot.appendingPathComponent("index.html")
let indexPersist = destHotPersist.appendingPathComponent("index.html")
let url: URL = self.getBundleDirectory(id: id)
if url.isDirectory && destHotPersist.isDirectory && indexHot.exist && indexPersist.exist {
let bundleIndo: BundleInfo = self.getBundleInfo(id: id)
if url.isDirectory && destHotPersist.isDirectory && indexHot.exist && indexPersist.exist && !bundleIndo.isDeleted() {
return true
}
return false
Expand Down
9 changes: 9 additions & 0 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
_ = self.implementation.setNextBundle(next: latest!.getId())
return
}
if latest!.isDeleted() {
print("\(self.implementation.TAG) Latest bundle already exists and will be deleted, download will overwrite it.")
let res = self.implementation.delete(id: latest!.getId(), removeInfo: true)
if !res {
print("\(self.implementation.TAG) Delete version deleted: \(latest!.toString())")
} else {
print("\(self.implementation.TAG) Failed to delete failed bundle: \(latest!.toString())")
}
}
}

do {
Expand Down

0 comments on commit a4f0dd8

Please sign in to comment.