/
URL+Unused.swift
37 lines (31 loc) · 1.18 KB
/
URL+Unused.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// URL+Unused.swift
// eduVPN
//
import Foundation
extension URL {
/// Gives the next unused file URL by appending a counter if needed
///
/// For example: if 'Image.png' exists returns 'Image 2.png', or if that one exists too 'Image 3.png'
///
/// - Returns: URL
func nextUnusedFileURL() throws -> URL {
var candidate = self
let fileExtension = pathExtension
while FileManager.default.fileExists(atPath: candidate.path) {
candidate = candidate.deletingPathExtension()
var lastPathComponent = candidate.lastPathComponent
candidate = candidate.deletingLastPathComponent()
let parts = lastPathComponent.split(separator: " ")
if let last = parts.last, let counter = Int(last), counter > 0 {
lastPathComponent = parts.dropLast().joined(separator: " ") + " \(counter + 1)"
} else {
lastPathComponent += " 2"
}
candidate = candidate
.appendingPathComponent(lastPathComponent)
.appendingPathExtension(fileExtension)
}
return candidate
}
}