Skip to content

Commit

Permalink
fixed fetchItemList: CloudPath was the same for all ItemMetadata. Add…
Browse files Browse the repository at this point in the history
…itionally it is now relative to the rootURL.
  • Loading branch information
phil1995 committed Sep 15, 2020
1 parent 71d8a3e commit 484eb4b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion CloudAccess/LocalFileSystem/LocalFileSystemProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public class LocalFileSystemProvider: CloudProvider {
let size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize
let lastModifiedDate = (try? url.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
let itemType = getItemType(from: (try? url.resourceValues(forKeys: [.fileResourceTypeKey]))?.fileResourceType)
return CloudItemMetadata(name: name, cloudPath: cloudPath, itemType: itemType, lastModifiedDate: lastModifiedDate, size: size)
let itemCloudPath = getRelativeCloudPath(from: url)
return CloudItemMetadata(name: name, cloudPath: itemCloudPath, itemType: itemType, lastModifiedDate: lastModifiedDate, size: size)
}
promise = Promise(CloudItemList(items: metadatas, nextPageToken: nil))
} catch CocoaError.fileReadNoSuchFile {
Expand Down Expand Up @@ -356,4 +357,17 @@ public class LocalFileSystemProvider: CloudProvider {
private func validateItemType(at url: URL, with itemType: CloudItemType) -> Bool {
return url.hasDirectoryPath == (itemType == .folder) || !url.hasDirectoryPath == (itemType == .file)
}

private func getRelativeCloudPath(from url: URL) -> CloudPath {
assert(url.isFileURL)
let relativePath = url.path.deletingPrefix(rootURL.path)
return CloudPath(relativePath + (url.hasDirectoryPath ? "/" : ""))
}
}

private extension String {
func deletingPrefix(_ prefix: String) -> String {
guard hasPrefix(prefix) else { return self }
return String(dropFirst(prefix.count))
}
}
4 changes: 2 additions & 2 deletions CloudAccessTests/LocalFileSystem/LocalFileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class LocalFileSystemTests: XCTestCase {
FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
provider.fetchItemList(forFolderAt: CloudPath("/"), withPageToken: nil).then { itemList in
XCTAssertEqual(2, itemList.items.count)
XCTAssertTrue(itemList.items.contains(where: { $0.name == "dir" }))
XCTAssertTrue(itemList.items.contains(where: { $0.name == "file" }))
XCTAssertTrue(itemList.items.contains(where: { $0.name == "dir" && $0.cloudPath == CloudPath("/dir/") }))
XCTAssertTrue(itemList.items.contains(where: { $0.name == "file" && $0.cloudPath == CloudPath("/file") }))
}.catch { error in
XCTFail("Error in promise: \(error)")
}.always {
Expand Down

0 comments on commit 484eb4b

Please sign in to comment.