Use save panel for mac image export

This commit is contained in:
jack
2025-10-14 22:21:17 +02:00
parent 1d4bf96f7a
commit a71b8cd545
+14 -28
View File
@@ -2260,39 +2260,25 @@ struct ImagePreviewView: View {
#if os(iOS) #if os(iOS)
showExporter = true showExporter = true
#else #else
do { Task { @MainActor in
guard let downloads = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { let panel = NSSavePanel()
SecureLogger.error("Missing downloads directory for save", category: .session) panel.canCreateDirectories = true
return panel.nameFieldStringValue = url.lastPathComponent
panel.prompt = "save"
if panel.runModal() == .OK, let destination = panel.url {
do {
if FileManager.default.fileExists(atPath: destination.path) {
try FileManager.default.removeItem(at: destination)
}
try FileManager.default.copyItem(at: url, to: destination)
} catch {
SecureLogger.error("Failed to save image preview copy: \(error)", category: .session)
}
} }
let baseName = url.lastPathComponent
let destination = uniqueFileURL(in: downloads, fileName: baseName)
if FileManager.default.fileExists(atPath: destination.path) {
try FileManager.default.removeItem(at: destination)
}
try FileManager.default.copyItem(at: url, to: destination)
} catch {
SecureLogger.error("Failed to save image preview copy: \(error)", category: .session)
} }
#endif #endif
} }
#if !os(iOS)
private func uniqueFileURL(in directory: URL, fileName: String) -> URL {
var candidate = directory.appendingPathComponent(fileName)
let base = (fileName as NSString).deletingPathExtension
let ext = (fileName as NSString).pathExtension
var counter = 1
while FileManager.default.fileExists(atPath: candidate.path) {
let suffix = " (\(counter))"
let name = ext.isEmpty ? base + suffix : base + suffix + "." + ext
candidate = directory.appendingPathComponent(name)
counter += 1
}
return candidate
}
#endif
#if os(iOS) #if os(iOS)
private struct FileExportWrapper: UIViewControllerRepresentable { private struct FileExportWrapper: UIViewControllerRepresentable {
let url: URL let url: URL