Caps-iOS/Caps/Extensions/URL+Extensions.swift

36 lines
911 B
Swift
Raw Permalink Normal View History

2022-06-10 21:20:49 +02:00
import Foundation
extension URL {
var attributes: [FileAttributeKey : Any]? {
do {
return try FileManager.default.attributesOfItem(atPath: path)
} catch let error as NSError {
print("FileAttribute error: \(error)")
}
return nil
}
var fileSize: Int {
return Int(attributes?[.size] as? UInt64 ?? 0)
}
var fileSizeString: String {
return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file)
}
var creationDate: Date? {
return attributes?[.creationDate] as? Date
}
2022-06-21 19:38:51 +02:00
var fileSizeAlt: Int? {
do {
let val = try self.resourceValues(forKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey])
return val.totalFileAllocatedSize ?? val.fileAllocatedSize
} catch {
print(error)
return nil
}
}
2022-06-10 21:20:49 +02:00
}