AutoCat/AutoCatCore/Extensions/FileManagerExt.swift

24 lines
936 B
Swift

import Foundation
public extension FileManager {
func url(for file: String, in dir: String) throws -> URL {
guard let docUrl = self.urls(for: .documentDirectory, in: .userDomainMask).first else {
throw CocoaError(.fileReadNoSuchFile)
}
let folderUrl = docUrl.appendingPathComponent(dir, isDirectory: true)
if !self.fileExists(atPath: folderUrl.path) {
try self.createDirectory(at: folderUrl, withIntermediateDirectories: true, attributes: nil)
}
return folderUrl.appendingPathComponent(file)
}
func tmpUrl(name: String, ext: String) -> URL {
let formatter = DateFormatter()
formatter.timeStyle = .none
formatter.dateFormat = "dd.MM.yyyy"
let fileName = "\(name)_\(formatter.string(from: Date())).\(ext)"
return URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
}
}