aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Shared/URLAdditions.swift
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-01-08 14:45:58 -0800
committerDustin Mierau <dustin@mierau.me>2024-01-08 14:45:58 -0800
commite9bae0328801d5a056f33beb0f764c6d35e518a2 (patch)
treefcf853330f8de68948b089a5b15e2ef4c6dbb47c /Hotline/Shared/URLAdditions.swift
parent4349f50167f50b30d7cd71854882ab33ac6ea5ea (diff)
Move to a standard SwiftUI window for preview windows so I can add toolbar items from SwiftUI. Not ideal. Added support for previewing text files too.
Diffstat (limited to 'Hotline/Shared/URLAdditions.swift')
-rw-r--r--Hotline/Shared/URLAdditions.swift42
1 files changed, 42 insertions, 0 deletions
diff --git a/Hotline/Shared/URLAdditions.swift b/Hotline/Shared/URLAdditions.swift
new file mode 100644
index 0000000..7eebb46
--- /dev/null
+++ b/Hotline/Shared/URLAdditions.swift
@@ -0,0 +1,42 @@
+import Foundation
+
+extension URL {
+ func generateUniqueFilePath(filename base: String) -> String {
+ let fileManager = FileManager.default
+ var finalName = base
+ var counter = 2
+
+ // Helper function to generate a new filename with a counter
+ func makeFileName() -> String {
+ let baseName = (base as NSString).deletingPathExtension
+ let extensionName = (base as NSString).pathExtension
+ return extensionName.isEmpty ? "\(baseName) \(counter)" : "\(baseName) \(counter).\(extensionName)"
+ }
+
+ // Check if file exists and append counter until a unique name is found
+ var filePath = self.appending(component: finalName).path(percentEncoded: false)
+ while fileManager.fileExists(atPath: filePath) {
+ finalName = makeFileName()
+ filePath = self.appending(component: finalName).path(percentEncoded: false)
+ counter += 1
+ }
+
+ return filePath
+ }
+
+// private func prepareDownloadFile(name: String) -> Bool {
+// let folderURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)[0]
+// let filePath = findUniqueFilePath(base: name, at: folderURL)
+//
+// if FileManager.default.createFile(atPath: filePath, contents: nil) {
+// if let h = FileHandle(forWritingAtPath: filePath) {
+// self.filePath = filePath
+// self.fileHandle = h
+// self.fileProgress?.fileURL = URL(filePath: filePath).resolvingSymlinksInPath()
+// return true
+// }
+// }
+//
+// return false
+// }
+}