Skip to content

Commit cd22b4b

Browse files
committed
Removed patchAsync request when not useful for Onedrive
1 parent 38b4633 commit cd22b4b

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

data/src/apiKey/java/org/cryptomator/data/cloud/onedrive/OnedriveImpl.kt

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import org.cryptomator.domain.usecases.cloud.DataSource
3434
import org.cryptomator.domain.usecases.cloud.DownloadState
3535
import org.cryptomator.domain.usecases.cloud.Progress
3636
import org.cryptomator.domain.usecases.cloud.UploadState
37-
import org.cryptomator.util.Optional
3837
import org.cryptomator.util.SharedPreferencesHandler
3938
import org.cryptomator.util.file.LruFileCacheUtil
4039
import org.cryptomator.util.file.LruFileCacheUtil.Companion.retrieveFromLruCache
@@ -206,19 +205,19 @@ internal class OnedriveImpl(private val cloud: OnedriveCloud, private val client
206205
}
207206
progressAware.onProgress(Progress.completed(UploadState.upload(file)))
208207
return try {
209-
val lastModifiedDate = getLastModifiedDateTime(result.get().fileSystemInfo)
210-
OnedriveCloudNodeFactory.file(file.parent, result.get(), lastModifiedDate)
208+
val driveItem: DriveItem = result.get()
209+
val lastModifiedDate = getLastModifiedDateTime(driveItem) ?: Date()
210+
OnedriveCloudNodeFactory.file(file.parent, driveItem, lastModifiedDate)
211211
} catch (e: ExecutionException) {
212212
throw FatalBackendException(e)
213213
} catch (e: InterruptedException) {
214214
throw FatalBackendException(e)
215215
}
216216
}
217217

218-
private fun getLastModifiedDateTime(fileSystemInfo: FileSystemInfo?): Date {
219-
return fileSystemInfo?.lastModifiedDateTime.let { date ->
220-
Date.from(date?.toInstant())
221-
}?: Date.from(Date().toInstant())
218+
private fun getLastModifiedDateTime(driveItem: DriveItem): Date? {
219+
return driveItem.fileSystemInfo?.lastModifiedDateTime?.let { clientDate -> Date.from(clientDate.toInstant()) }
220+
?: driveItem.lastModifiedDateTime?.let { serverDate -> Date.from(serverDate.toInstant()) }
222221
}
223222

224223
@Throws(NoSuchCloudFileException::class)
@@ -239,14 +238,13 @@ internal class OnedriveImpl(private val cloud: OnedriveCloud, private val client
239238
.putAsync(CopyStream.toByteArray(it)) //
240239
.whenComplete { driveItem, error ->
241240
run {
242-
if (error == null) {
243-
val diffItem = DriveItem()
244-
diffItem.fileSystemInfo = FileSystemInfo()
245-
setLastModifiedDateTime(diffItem.fileSystemInfo, data.modifiedDate(context))
246-
drive(parentNodeInfo.driveId) //
247-
.items(driveItem.id!!) //
248-
.buildRequest(conflictBehaviorOption) //
249-
.patchAsync(diffItem) //
241+
val modifiedDate = data.modifiedDate(context)
242+
if (error != null) {
243+
result.completeExceptionally(error)
244+
return@whenComplete
245+
}
246+
if (modifiedDate.isPresent) {
247+
patchAsyncLastModifiedDate(parentNodeInfo, driveItem, modifiedDate.get())
250248
.whenComplete { driveItem, error ->
251249
if (error == null) {
252250
progressAware.onProgress(Progress.completed(UploadState.upload(file)))
@@ -256,10 +254,11 @@ internal class OnedriveImpl(private val cloud: OnedriveCloud, private val client
256254
result.completeExceptionally(error)
257255
}
258256
}
259-
} else {
260-
result.completeExceptionally(error)
257+
} else { // current date is the default, no need to patch()
258+
progressAware.onProgress(Progress.completed(UploadState.upload(file)))
259+
result.complete(driveItem)
260+
cacheNodeInfo(file, driveItem)
261261
}
262-
263262
}
264263
}
265264
} catch (e: IOException) {
@@ -269,13 +268,27 @@ internal class OnedriveImpl(private val cloud: OnedriveCloud, private val client
269268
} ?: throw FatalBackendException("InputStream shouldn't bee null")
270269
}
271270

271+
private fun patchAsyncLastModifiedDate(parentNodeInfo: OnedriveIdCache.NodeInfo, driveItem: DriveItem, modifiedDate: Date): CompletableFuture<DriveItem> {
272+
val diffItem = DriveItem()
273+
diffItem.fileSystemInfo = FileSystemInfo()
274+
diffItem.fileSystemInfo!!.lastModifiedDateTime = OffsetDateTime.ofInstant(modifiedDate.toInstant(), ZoneId.systemDefault())
275+
return drive(parentNodeInfo.driveId) //
276+
.items(driveItem.id!!) //
277+
.buildRequest() //
278+
.patchAsync(diffItem) //
279+
}
280+
272281
@Throws(IOException::class, NoSuchCloudFileException::class)
273282
private fun chunkedUploadFile(file: OnedriveFile, data: DataSource, progressAware: ProgressAware<UploadState>, result: CompletableFuture<DriveItem>, conflictBehaviorOption: Option, size: Long) {
274283
val parentNodeInfo = requireNodeInfo(file.parent)
275284

276285
val props = DriveItemUploadableProperties()
277-
props.fileSystemInfo = FileSystemInfo()
278-
setLastModifiedDateTime(props.fileSystemInfo, data.modifiedDate(context))
286+
val modifiedDate = data.modifiedDate(context)
287+
288+
if (modifiedDate.isPresent) {
289+
props.fileSystemInfo = FileSystemInfo()
290+
props.fileSystemInfo!!.lastModifiedDateTime = OffsetDateTime.ofInstant(modifiedDate.get().toInstant(), ZoneId.systemDefault())
291+
}
279292

280293
drive(parentNodeInfo.driveId) //
281294
.items(parentNodeInfo.id) //
@@ -304,14 +317,6 @@ internal class OnedriveImpl(private val cloud: OnedriveCloud, private val client
304317
} ?: throw FatalBackendException("Failed to create upload session, response is null")
305318
}
306319

307-
private fun setLastModifiedDateTime(fileSystemInfo: FileSystemInfo?, modifiedDate: Optional<Date>) {
308-
fileSystemInfo?.lastModifiedDateTime = modifiedDate.map { date ->
309-
OffsetDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())
310-
}.orElseGet {
311-
OffsetDateTime.ofInstant(Date().toInstant(), ZoneId.systemDefault())
312-
}
313-
}
314-
315320
@Throws(BackendException::class, IOException::class)
316321
fun read(file: OnedriveFile, encryptedTmpFile: File?, data: OutputStream, progressAware: ProgressAware<DownloadState>) {
317322
progressAware.onProgress(Progress.started(DownloadState.download(file)))

data/src/main/java/org/cryptomator/data/cloud/webdav/network/WebDavClient.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.cryptomator.domain.exception.NotFoundException
1212
import org.cryptomator.domain.exception.ParentFolderDoesNotExistException
1313
import org.cryptomator.domain.exception.TypeMismatchException
1414
import org.cryptomator.domain.exception.UnauthorizedException
15-
import org.cryptomator.util.Optional
1615
import org.xmlpull.v1.XmlPullParserException
1716
import java.io.ByteArrayInputStream
1817
import java.io.IOException

0 commit comments

Comments
 (0)