mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-02-09 16:08:47 +01:00
QuickLook: change + cleanup cache folder on close
This commit is contained in:
parent
cdbfd5db00
commit
26e09d230b
@ -3,7 +3,13 @@ import SwiftUI
|
|||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
public class QuickLook: ObservableObject {
|
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 urls: [URL] = []
|
||||||
@Published public private(set) var isPreparing: Bool = false
|
@Published public private(set) var isPreparing: Bool = false
|
||||||
@Published public private(set) var latestError: Error?
|
@Published public private(set) var latestError: Error?
|
||||||
@ -51,11 +57,23 @@ 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 {
|
private func localPathFor(url: URL) async throws -> URL {
|
||||||
let tempDir = URL(fileURLWithPath: NSTemporaryDirectory())
|
try? FileManager.default.createDirectory(at: quickLookDir, withIntermediateDirectories: true)
|
||||||
let path = tempDir.appendingPathComponent(url.lastPathComponent)
|
let path = quickLookDir.appendingPathComponent(url.lastPathComponent)
|
||||||
let data = try await URLSession.shared.data(from: url).0
|
let data = try await URLSession.shared.data(from: url).0
|
||||||
try data.write(to: path)
|
try data.write(to: path)
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func cleanup(urls: [URL]) {
|
||||||
|
try? FileManager.default.removeItem(at: quickLookDir)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user