From 3c6ff4ad9b6ef6f06cf97173cb10bd616c3fe9f5 Mon Sep 17 00:00:00 2001 From: Tony Arnold Date: Fri, 17 Jul 2020 13:16:28 +1000 Subject: [PATCH 1/2] Add `size` property to types that conform to Location --- Sources/Files.swift | 11 +++++++++++ Tests/FilesTests/FilesTests.swift | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/Sources/Files.swift b/Sources/Files.swift index f52db5d..72855b6 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -105,6 +105,17 @@ public extension Location { return storage.attributes[.modificationDate] as? Date } + typealias ByteCount = Int + + /// 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 ae02771..0c5e9a0 100644 --- a/Tests/FilesTests/FilesTests.swift +++ b/Tests/FilesTests/FilesTests.swift @@ -520,6 +520,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 { @@ -885,6 +895,7 @@ class FilesTests: XCTestCase { ("testFirstAndLastInFileSequence", testFirstAndLastInFileSequence), ("testConvertingFileSequenceToRecursive", testConvertingFileSequenceToRecursive), ("testModificationDate", testModificationDate), + ("testSize", testSize), ("testParent", testParent), ("testRootFolderParentIsNil", testRootFolderParentIsNil), ("testRootSubfolderParentIsRoot", testRootSubfolderParentIsRoot), From b80d31c59e6f965d5a858f4d07bc22f16eb93db2 Mon Sep 17 00:00:00 2001 From: Tony Arnold Date: Thu, 26 Aug 2021 12:01:06 +1000 Subject: [PATCH 2/2] Add `size` property to types that conform to Location --- Sources/Files.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Files.swift b/Sources/Files.swift index 6a16113..ca8b163 100644 --- a/Sources/Files.swift +++ b/Sources/Files.swift @@ -105,7 +105,7 @@ public extension Location { return storage.attributes[.modificationDate] as? Date } - typealias ByteCount = Int + typealias ByteCount = Int64 /// The size of the item in bytes, or `nil` if the information is not available. var size: ByteCount? {