mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-12-27 17:40:38 +00:00
QuickLook: change + cleanup cache folder on close
This commit is contained in:
parent
cdbfd5db00
commit
26e09d230b
1 changed files with 21 additions and 3 deletions
|
@ -3,7 +3,13 @@ import SwiftUI
|
|||
|
||||
@MainActor
|
||||
public class QuickLook: ObservableObject {
|
||||
@Published public var url: URL?
|
||||
@Published public var url: URL? {
|
||||
didSet {
|
||||
if url == nil {
|
||||
cleanup(urls: urls)
|
||||
}
|
||||
}
|
||||
}
|
||||
@Published public private(set) var urls: [URL] = []
|
||||
@Published public private(set) var isPreparing: Bool = false
|
||||
@Published public private(set) var latestError: Error?
|
||||
|
@ -50,12 +56,24 @@ public class QuickLook: ObservableObject {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var quickLookDir: URL {
|
||||
try! FileManager.default.url(for: .cachesDirectory,
|
||||
in: .userDomainMask,
|
||||
appropriateFor: nil,
|
||||
create: false)
|
||||
.appending(component: "quicklook")
|
||||
}
|
||||
|
||||
private func localPathFor(url: URL) async throws -> URL {
|
||||
let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
|
||||
let path = tempDir.appendingPathComponent(url.lastPathComponent)
|
||||
try? FileManager.default.createDirectory(at: quickLookDir, withIntermediateDirectories: true)
|
||||
let path = quickLookDir.appendingPathComponent(url.lastPathComponent)
|
||||
let data = try await URLSession.shared.data(from: url).0
|
||||
try data.write(to: path)
|
||||
return path
|
||||
}
|
||||
|
||||
private func cleanup(urls: [URL]) {
|
||||
try? FileManager.default.removeItem(at: quickLookDir)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue