diff --git a/Sources/Files.swift b/Sources/Files.swift index 830f1ba..ca8b163 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -105,6 +105,17 @@ public extension Location { return storage.attributes[.modificationDate] as? Date } + typealias ByteCount = Int64 + + /// The size of the item in bytes, or `nil` if the information is not available. + var size: ByteCount? { + guard let size = storage.attributes[.size] as? UInt64 else { + return nil + } + + return ByteCount(size) + } + /// Initialize an instance of an existing location at a given path. /// - parameter path: The absolute path of the location. /// - throws: `LocationError` if the item couldn't be found. diff --git a/Tests/FilesTests/FilesTests.swift b/Tests/FilesTests/FilesTests.swift index 040f60e..95b64a7 100644 --- a/Tests/FilesTests/FilesTests.swift +++ b/Tests/FilesTests/FilesTests.swift @@ -540,6 +540,16 @@ class FilesTests: XCTestCase { XCTAssertTrue(file.modificationDate.map(Calendar.current.isDateInToday) ?? false) } } + + func testSize() { + performTest { + let subfolder = try folder.createSubfolder(named: "Folder") + XCTAssertNotNil(subfolder.size) + + let file = try folder.createFile(named: "File") + XCTAssertNotNil(file.size) + } + } func testParent() { performTest { @@ -906,6 +916,7 @@ class FilesTests: XCTestCase { ("testFirstAndLastInFileSequence", testFirstAndLastInFileSequence), ("testConvertingFileSequenceToRecursive", testConvertingFileSequenceToRecursive), ("testModificationDate", testModificationDate), + ("testSize", testSize), ("testParent", testParent), ("testRootFolderParentIsNil", testRootFolderParentIsNil), ("testRootSubfolderParentIsRoot", testRootSubfolderParentIsRoot),