Skip to content

Commit 03f6d82

Browse files
authored
fix: properly clean up resource files no longer required (#538)
* fix: remove require directory instead trash * fix: cleanup resources no longer required
1 parent 040a3b5 commit 03f6d82

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

xcode/Safari-Extension/Functions.swift

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,21 @@ func getAllFiles(includeCode: Bool = false) -> [[String: Any]]? {
822822

823823
func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: String) -> Bool {
824824
let directory = getRequireLocation().appendingPathComponent(filename)
825-
// if file requires no resource but directory exists, trash it
826-
if resources.isEmpty && FileManager.default.fileExists(atPath: directory.path) {
827-
do {
828-
try FileManager.default.trashItem(at: directory, resultingItemURL: nil)
829-
} catch {
830-
// failing to trash item won't break functionality, so log error and move on
831-
err("failed to trash directory in getRequiredCode \(error.localizedDescription)")
832-
return true
825+
// if file requires no resource but directory exists, remove it
826+
// this resource is not user-generated and can be downloaded again, so just remove it instead of moves to the trash
827+
// also in ios, the volume “Data” has no trash and item can only be removed directly
828+
if resources.isEmpty {
829+
if FileManager.default.fileExists(atPath: directory.path) {
830+
do {
831+
try FileManager.default.removeItem(at: directory)
832+
} catch {
833+
err("failed to remove directory in getRequiredCode \(error.localizedDescription)")
834+
}
833835
}
836+
return true
834837
}
838+
// record URLs for subsequent processing
839+
var resourceUrls = Set<URL>()
835840
// loop through resource urls and attempt to fetch it
836841
for resourceUrlString in resources {
837842
// get the path of the url string
@@ -844,6 +849,7 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri
844849
if resourceUrlPath.hasSuffix(fileType) {
845850
let resourceFilename = sanitize(resourceUrlString)
846851
let fileURL = directory.appendingPathComponent(resourceFilename)
852+
resourceUrls.insert(fileURL)
847853
// only attempt to get resource if it does not yet exist
848854
if FileManager.default.fileExists(atPath: fileURL.path) {continue}
849855
// get the remote file contents
@@ -862,6 +868,23 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri
862868
}
863869
}
864870
}
871+
// cleanup downloaded files that are no longer required
872+
do {
873+
// get all downloaded resources url
874+
let downloadedUrls = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: [])
875+
// exclude currently required resources
876+
let abandonedUrls = Set(downloadedUrls).subtracting(resourceUrls)
877+
// loop through abandoned urls and attempt to remove it
878+
for abandonFlieUrl in abandonedUrls {
879+
do {
880+
try FileManager.default.removeItem(at: abandonFlieUrl)
881+
} catch {
882+
err("failed to remove abandoned resource in getRequiredCode \(error.localizedDescription)")
883+
}
884+
}
885+
} catch {
886+
err("failed to cleanup resources in getRequiredCode \(error.localizedDescription)")
887+
}
865888
return true
866889
}
867890

0 commit comments

Comments
 (0)