diff --git a/Sources/CryptomatorCloudAccess/API/CloudProvider.swift b/Sources/CryptomatorCloudAccess/API/CloudProvider.swift index 07ed669..090c8ec 100644 --- a/Sources/CryptomatorCloudAccess/API/CloudProvider.swift +++ b/Sources/CryptomatorCloudAccess/API/CloudProvider.swift @@ -66,6 +66,7 @@ public protocol CloudProvider { - Parameter replaceExisting: If true, overwrite the existing file at `cloudPath`. - Precondition: `localURL` must be a file URL. - Postcondition: The file is stored at `cloudPath`. + - Postcondition: The `size` property of the returned metadata is set to the size of the uploaded file in the cloud and must not be `nil`. - Returns: Promise with the metadata of the uploaded file. If the upload fails, promise is rejected with: - `CloudProviderError.itemNotFound` if the file does not exist at the `localURL`. - `CloudProviderError.itemAlreadyExists` if the file already exists at the `cloudPath` with `!replaceExisting` or the cloud provider finds a folder instead of a file at `cloudPath` with `replaceExisting`. diff --git a/Sources/CryptomatorCloudAccess/GoogleDrive/GoogleDriveCloudProvider.swift b/Sources/CryptomatorCloudAccess/GoogleDrive/GoogleDriveCloudProvider.swift index a5c279e..33820a4 100644 --- a/Sources/CryptomatorCloudAccess/GoogleDrive/GoogleDriveCloudProvider.swift +++ b/Sources/CryptomatorCloudAccess/GoogleDrive/GoogleDriveCloudProvider.swift @@ -398,7 +398,6 @@ public class GoogleDriveCloudProvider: CloudProvider { return Promise(CloudProviderError.itemAlreadyExists) } let query = GTLRDriveQuery_FilesUpdate.query(withObject: metadata, fileId: item.identifier, uploadParameters: uploadParameters) - query.fields = "id, name, modifiedTime, mimeType" return Promise(query) }.recover { error -> GTLRDriveQuery in guard case CloudProviderError.itemNotFound = error else { @@ -407,6 +406,9 @@ public class GoogleDriveCloudProvider: CloudProvider { metadata.parents = [parentItem.identifier] let query = GTLRDriveQuery_FilesCreate.query(withObject: metadata, uploadParameters: uploadParameters) return query + }.then { query -> GTLRDriveQuery in + query.fields = "id, name, modifiedTime, mimeType, size" + return query } } diff --git a/Tests/CryptomatorCloudAccessIntegrationTests/CloudAccessIntegrationTest.swift b/Tests/CryptomatorCloudAccessIntegrationTests/CloudAccessIntegrationTest.swift index e841652..2af5d95 100644 --- a/Tests/CryptomatorCloudAccessIntegrationTests/CloudAccessIntegrationTest.swift +++ b/Tests/CryptomatorCloudAccessIntegrationTests/CloudAccessIntegrationTest.swift @@ -540,12 +540,14 @@ class CloudAccessIntegrationTest: XCTestCase { print("\(progress.localizedDescription ?? "") (\(progress.localizedAdditionalDescription ?? ""))") } progress.becomeCurrent(withPendingUnitCount: 1) - provider.uploadFile(from: initialLocalURL, to: cloudPath, replaceExisting: false).then { _ -> Promise in + provider.uploadFile(from: initialLocalURL, to: cloudPath, replaceExisting: false).then { cloudItemMetadata -> Promise in XCTAssertTrue(progress.completedUnitCount >= progress.totalUnitCount) + self.assertReceivedCorrectMetadataAfterUploading(file: initialLocalURL, to: cloudPath, metadata: cloudItemMetadata) try overwrittenTestContent.write(to: initialLocalURL, atomically: true, encoding: .utf8) return self.provider.uploadFile(from: initialLocalURL, to: cloudPath, replaceExisting: true) - }.then { _ in - self.provider.downloadFile(from: cloudPath, to: overwrittenLocalURL) + }.then { cloudItemMetadata -> Promise in + self.assertReceivedCorrectMetadataAfterUploading(file: initialLocalURL, to: cloudPath, metadata: cloudItemMetadata) + return self.provider.downloadFile(from: cloudPath, to: overwrittenLocalURL) }.then { _ in self.provider.deleteFile(at: cloudPath) }.then { @@ -969,6 +971,22 @@ class CloudAccessIntegrationTest: XCTestCase { } wait(for: [expectation], timeout: 30.0) } + + private func assertReceivedCorrectMetadataAfterUploading(file localURL: URL, to cloudPath: CloudPath, metadata: CloudItemMetadata) { + let localFileSize: Int? + do { + let attributes = try FileManager.default.attributesOfItem(atPath: localURL.path) + localFileSize = attributes[FileAttributeKey.size] as? Int + } catch { + XCTFail("Get local file size failed with error: \(error)") + return + } + XCTAssertEqual(cloudPath, metadata.cloudPath) + XCTAssertEqual(cloudPath.lastPathComponent, metadata.name) + XCTAssertEqual(localFileSize, metadata.size) + XCTAssertNotNil(metadata.size) + XCTAssertEqual(.file, metadata.itemType) + } } extension CloudItemMetadata: Comparable {