|
| 1 | +import Foundation |
| 2 | + |
| 3 | + |
| 4 | +public class FileUtils {} |
| 5 | + |
| 6 | +public extension FileUtils { |
| 7 | + |
| 8 | + class func clearFile(_ url: URL?) { |
| 9 | + clearFile(url?.path) |
| 10 | + } |
| 11 | + |
| 12 | + class func clearFile(_ path: String?) { |
| 13 | + guard let path = path else { return } |
| 14 | + do { |
| 15 | + try "".write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8) |
| 16 | + } catch { |
| 17 | + fatalError("CLEAR FILE: Can't write to file \(path)") |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + class func createFile(_ url: URL?) { |
| 22 | + createFile(url?.path) |
| 23 | + } |
| 24 | + |
| 25 | + class func createFile(_ path: String?) { |
| 26 | + guard let path = path else { return } |
| 27 | + FileManager.default.createFile(atPath: path, contents: nil, attributes: nil) |
| 28 | + } |
| 29 | + |
| 30 | + class func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL] { |
| 31 | + var fileURLs: [URL] = .init() |
| 32 | + |
| 33 | + if let documentsURL = FileManager.default.urls(for: directory, in: .userDomainMask).first { |
| 34 | + do { |
| 35 | + fileURLs = try FileManager.default.contentsOfDirectory(at: documentsURL, |
| 36 | + includingPropertiesForKeys: nil, |
| 37 | + options: skipsHiddenFiles ? [.skipsHiddenFiles] : [] ) |
| 38 | + } catch {} |
| 39 | + } |
| 40 | + |
| 41 | + return fileURLs |
| 42 | + } |
| 43 | + |
| 44 | + class func urls(for directory: String, skipsHiddenFiles: Bool = true ) -> [URL] { |
| 45 | + guard let documentsURL = URL(string: directory) else { return [] } |
| 46 | + return urls(for: documentsURL, skipsHiddenFiles: skipsHiddenFiles) |
| 47 | + } |
| 48 | + |
| 49 | + class func urls(for directory: URL, skipsHiddenFiles: Bool = true ) -> [URL] { |
| 50 | + do { |
| 51 | + return try FileManager.default.contentsOfDirectory(at: directory, |
| 52 | + includingPropertiesForKeys: [], |
| 53 | + options: skipsHiddenFiles ? [.skipsHiddenFiles] : [] ) |
| 54 | + } catch { |
| 55 | + return [] |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + class func readDirectory(for directory: URL, skipsHiddenFiles: Bool = true ) -> [URL] { |
| 60 | + urls(for: directory, skipsHiddenFiles: skipsHiddenFiles) |
| 61 | + } |
| 62 | + |
| 63 | + class func readDirectory(for directory: String, skipsHiddenFiles: Bool = true ) -> [URL] { |
| 64 | + urls(for: directory, skipsHiddenFiles: skipsHiddenFiles) |
| 65 | + } |
| 66 | + |
| 67 | + class func isDirectory(_ url: URL) -> Bool { |
| 68 | + (try? url.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false |
| 69 | + } |
| 70 | + |
| 71 | + class func isDirectory(_ path: String) -> Bool { |
| 72 | + guard let url = URL(string: path) else { fatalError("\(path) - can not convert to URL") } |
| 73 | + return isDirectory(url) |
| 74 | + } |
| 75 | + |
| 76 | + class func isFile(_ url: URL) -> Bool { |
| 77 | + !isDirectory(url) |
| 78 | + } |
| 79 | + |
| 80 | + class func isFile(_ path: String) -> Bool { |
| 81 | + guard let url = URL(string: path) else { fatalError("\(path) - can not convert to URL") } |
| 82 | + return isFile(url) |
| 83 | + } |
| 84 | + |
| 85 | + class func fileExist(_ url: URL) -> Bool { |
| 86 | + fileExist(url.path) |
| 87 | + } |
| 88 | + |
| 89 | + class func fileExist(_ path: String) -> Bool { |
| 90 | + FileManager.default.fileExists(atPath: path) |
| 91 | + } |
| 92 | + |
| 93 | + class func directoryExist(_ url: URL) -> Bool { |
| 94 | + directoryExist(url.path) |
| 95 | + } |
| 96 | + |
| 97 | + class func directoryExist(_ path: String) -> Bool { |
| 98 | + var isDirectory = ObjCBool(true) |
| 99 | + let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory) |
| 100 | + return exists && isDirectory.boolValue |
| 101 | + } |
| 102 | +} |
0 commit comments