From 0b0224e58ac2f63b5b9a91b94cfaa0f2607f87a2 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sun, 10 Sep 2023 07:15:58 +0800 Subject: [PATCH 1/2] fix: remove require directory instead trash --- xcode/Safari-Extension/Functions.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/xcode/Safari-Extension/Functions.swift b/xcode/Safari-Extension/Functions.swift index b65558cf..73131a80 100644 --- a/xcode/Safari-Extension/Functions.swift +++ b/xcode/Safari-Extension/Functions.swift @@ -822,15 +822,18 @@ func getAllFiles(includeCode: Bool = false) -> [[String: Any]]? { func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: String) -> Bool { let directory = getRequireLocation().appendingPathComponent(filename) - // if file requires no resource but directory exists, trash it - if resources.isEmpty && FileManager.default.fileExists(atPath: directory.path) { - do { - try FileManager.default.trashItem(at: directory, resultingItemURL: nil) - } catch { - // failing to trash item won't break functionality, so log error and move on - err("failed to trash directory in getRequiredCode \(error.localizedDescription)") - return true + // if file requires no resource but directory exists, remove it + // this resource is not user-generated and can be downloaded again, so just remove it instead of moves to the trash + // also in ios, the volume “Data” has no trash and item can only be removed directly + if resources.isEmpty { + if FileManager.default.fileExists(atPath: directory.path) { + do { + try FileManager.default.removeItem(at: directory) + } catch { + err("failed to remove directory in getRequiredCode \(error.localizedDescription)") + } } + return true } // loop through resource urls and attempt to fetch it for resourceUrlString in resources { From cbb4d73a017da1b1ce6ffad48332e56e03db8b12 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Sun, 10 Sep 2023 08:09:50 +0800 Subject: [PATCH 2/2] fix: cleanup resources no longer required --- xcode/Safari-Extension/Functions.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/xcode/Safari-Extension/Functions.swift b/xcode/Safari-Extension/Functions.swift index 73131a80..90669a86 100644 --- a/xcode/Safari-Extension/Functions.swift +++ b/xcode/Safari-Extension/Functions.swift @@ -835,6 +835,8 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri } return true } + // record URLs for subsequent processing + var resourceUrls = Set() // loop through resource urls and attempt to fetch it for resourceUrlString in resources { // get the path of the url string @@ -847,6 +849,7 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri if resourceUrlPath.hasSuffix(fileType) { let resourceFilename = sanitize(resourceUrlString) let fileURL = directory.appendingPathComponent(resourceFilename) + resourceUrls.insert(fileURL) // only attempt to get resource if it does not yet exist if FileManager.default.fileExists(atPath: fileURL.path) {continue} // get the remote file contents @@ -865,6 +868,23 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri } } } + // cleanup downloaded files that are no longer required + do { + // get all downloaded resources url + let downloadedUrls = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: []) + // exclude currently required resources + let abandonedUrls = Set(downloadedUrls).subtracting(resourceUrls) + // loop through abandoned urls and attempt to remove it + for abandonFlieUrl in abandonedUrls { + do { + try FileManager.default.removeItem(at: abandonFlieUrl) + } catch { + err("failed to remove abandoned resource in getRequiredCode \(error.localizedDescription)") + } + } + } catch { + err("failed to cleanup resources in getRequiredCode \(error.localizedDescription)") + } return true }