From e17163b3da27dfea63341644dabd59b3468e14bd Mon Sep 17 00:00:00 2001 From: jack Date: Thu, 25 Sep 2025 12:53:22 +0200 Subject: [PATCH] Use save panel for mac image export --- bitchat/Views/ContentView.swift | 42 +++++++++++---------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/bitchat/Views/ContentView.swift b/bitchat/Views/ContentView.swift index 1617e19d..f975998f 100644 --- a/bitchat/Views/ContentView.swift +++ b/bitchat/Views/ContentView.swift @@ -2260,39 +2260,25 @@ struct ImagePreviewView: View { #if os(iOS) showExporter = true #else - do { - guard let downloads = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first else { - SecureLogger.error("Missing downloads directory for save", category: .session) - return + Task { @MainActor in + let panel = NSSavePanel() + panel.canCreateDirectories = true + 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 } -#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) private struct FileExportWrapper: UIViewControllerRepresentable { let url: URL