@@ -34,7 +34,6 @@ import org.cryptomator.domain.usecases.cloud.DataSource
3434import org.cryptomator.domain.usecases.cloud.DownloadState
3535import org.cryptomator.domain.usecases.cloud.Progress
3636import org.cryptomator.domain.usecases.cloud.UploadState
37- import org.cryptomator.util.Optional
3837import org.cryptomator.util.SharedPreferencesHandler
3938import org.cryptomator.util.file.LruFileCacheUtil
4039import 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)))
0 commit comments