From 77a6a3868bcfa2eebaa077f7fd211e21e4fde700 Mon Sep 17 00:00:00 2001 From: ACTCD <101378590+ACTCD@users.noreply.github.com> Date: Mon, 11 Sep 2023 09:45:33 +0800 Subject: [PATCH] fix: `@require` resources injection in ios --- xcode/Safari-Extension/Functions.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/xcode/Safari-Extension/Functions.swift b/xcode/Safari-Extension/Functions.swift index 90669a86..9704b74c 100644 --- a/xcode/Safari-Extension/Functions.swift +++ b/xcode/Safari-Extension/Functions.swift @@ -849,7 +849,8 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri if resourceUrlPath.hasSuffix(fileType) { let resourceFilename = sanitize(resourceUrlString) let fileURL = directory.appendingPathComponent(resourceFilename) - resourceUrls.insert(fileURL) + // insert url to resolve symlink into set + resourceUrls.insert(fileURL.resolvingSymlinksInPath()) // only attempt to get resource if it does not yet exist if FileManager.default.fileExists(atPath: fileURL.path) {continue} // get the remote file contents @@ -870,14 +871,18 @@ func getRequiredCode(_ filename: String, _ resources: [String], _ fileType: Stri } // cleanup downloaded files that are no longer required do { + var downloadedUrls = Set() // get all downloaded resources url - let downloadedUrls = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: []) + let fileUrls = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: []) + // insert url to resolve symlink into set + for url in fileUrls { downloadedUrls.insert(url.resolvingSymlinksInPath()) } // exclude currently required resources let abandonedUrls = Set(downloadedUrls).subtracting(resourceUrls) // loop through abandoned urls and attempt to remove it - for abandonFlieUrl in abandonedUrls { + for abandonFileUrl in abandonedUrls { do { - try FileManager.default.removeItem(at: abandonFlieUrl) + try FileManager.default.removeItem(at: abandonFileUrl) + logText("cleanup abandoned resource: \(unsanitize(abandonFileUrl.lastPathComponent))") } catch { err("failed to remove abandoned resource in getRequiredCode \(error.localizedDescription)") }