Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions xcode/Safari-Extension/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -822,16 +822,21 @@ 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
}
// record URLs for subsequent processing
var resourceUrls = Set<URL>()
// loop through resource urls and attempt to fetch it
for resourceUrlString in resources {
// get the path of the url string
Expand All @@ -844,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
Expand All @@ -862,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
}

Expand Down