@@ -481,7 +481,6 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
481481 E2EFiles e2eFiles = new E2EFiles (parentFile , null , new File (mOriginalStoragePath ), null , null );
482482 FileLock fileLock = null ;
483483 long size ;
484- boolean folderWasLocked = false ;
485484 boolean metadataExists = false ;
486485 String token = null ;
487486 Object object = null ;
@@ -505,7 +504,6 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
505504 Log_OC .e (TAG , "Failed to obtain folder lock token for encrypted upload" );
506505 return new RemoteOperationResult <>(new IllegalStateException ("Cannot proceed: folder lock token is null or empty" ));
507506 }
508- folderWasLocked = true ;
509507 Log_OC .d (TAG , "folder successfully locked" );
510508 } catch (Exception e ) {
511509 Log_OC .e (TAG , "Failed to lock folder" , e );
@@ -612,13 +610,20 @@ private long getE2ECounter(OCFile parentFile) {
612610
613611 private String getFolderUnlockTokenOrLockFolder (OwnCloudClient client , OCFile parentFile , long counter ) throws UploadException {
614612 if (mFolderUnlockToken != null && !mFolderUnlockToken .isEmpty ()) {
613+ Log_OC .d (TAG , "Reusing existing folder unlock token from previous upload attempt" );
615614 return mFolderUnlockToken ;
616615 }
617616
618617 String token = EncryptionUtils .lockFolder (parentFile , client , counter );
618+ if (token == null || token .isEmpty ()) {
619+ Log_OC .e (TAG , "Lock folder returned null or empty token" );
620+ throw new UploadException ("Failed to lock folder: token is null or empty" );
621+ }
622+
619623 mUpload .setFolderUnlockToken (token );
620624 uploadsStorageManager .updateUpload (mUpload );
621625
626+ Log_OC .d (TAG , "Folder locked successfully, token saved" );
622627 return token ;
623628 }
624629
@@ -763,12 +768,12 @@ private RemoteOperationResult performE2EUpload(E2EClientData data) throws Operat
763768 throw new OperationCancelledException ();
764769 }
765770
766- RemoteOperationResult result = mUploadOperation .execute (data .getClient ());
771+ var result = mUploadOperation .execute (data .getClient ());
767772
768773 /// move local temporal file or original file to its corresponding
769774 // location in the Nextcloud local folder
770775 if (!result .isSuccess () && result .getHttpCode () == HttpStatus .SC_PRECONDITION_FAILED ) {
771- result = new RemoteOperationResult (ResultCode .SYNC_CONFLICT );
776+ result = new RemoteOperationResult <> (ResultCode .SYNC_CONFLICT );
772777 }
773778
774779 return result ;
@@ -906,17 +911,36 @@ private RemoteOperationResult cleanupE2EUpload(FileLock fileLock, E2EFiles e2eFi
906911 e2eFiles .deleteTemporalFileWithOriginalFileComparison ();
907912
908913 if (result == null ) {
909- result = new RemoteOperationResult (ResultCode .UNKNOWN_ERROR );
914+ result = new RemoteOperationResult <> (ResultCode .UNKNOWN_ERROR );
910915 }
911916
912917 logResult (result , mFile .getStoragePath (), mFile .getRemotePath ());
913918
919+ if (token == null || token .isEmpty ()) {
920+ Log_OC .e (TAG , "CRITICAL ERROR: Folder was locked but token is null/empty. Cannot unlock! " +
921+ "Folder: " + e2eFiles .getParentFile ().getFileName ());
922+ RemoteOperationResult <Void > tokenError = new RemoteOperationResult <>(
923+ new IllegalStateException ("Folder locked but token lost - manual intervention may be required" )
924+ );
925+
926+ // Override result only if original operation succeeded
927+ if (result .isSuccess ()) {
928+ result = tokenError ;
929+ }
930+ return result ;
931+ }
932+
914933 // Unlock must be done otherwise folder stays locked and user can't upload any file
915934 RemoteOperationResult <Void > unlockFolderResult ;
916- if (object instanceof DecryptedFolderMetadataFileV1 ) {
917- unlockFolderResult = EncryptionUtils .unlockFolderV1 (e2eFiles .getParentFile (), client , token );
918- } else {
919- unlockFolderResult = EncryptionUtils .unlockFolder (e2eFiles .getParentFile (), client , token );
935+ try {
936+ if (object instanceof DecryptedFolderMetadataFileV1 ) {
937+ unlockFolderResult = EncryptionUtils .unlockFolderV1 (e2eFiles .getParentFile (), client , token );
938+ } else {
939+ unlockFolderResult = EncryptionUtils .unlockFolder (e2eFiles .getParentFile (), client , token );
940+ }
941+ } catch (Exception e ) {
942+ Log_OC .e (TAG , "CRITICAL ERROR: Exception during folder unlock" , e );
943+ unlockFolderResult = new RemoteOperationResult <>(e );
920944 }
921945
922946 if (unlockFolderResult != null && !unlockFolderResult .isSuccess ()) {
@@ -932,6 +956,10 @@ private RemoteOperationResult cleanupE2EUpload(FileLock fileLock, E2EFiles e2eFi
932956 return Unit .INSTANCE ;
933957 });
934958 }
959+
960+ // Clear the saved token since folder is now unlocked
961+ mUpload .setFolderUnlockToken (null );
962+ uploadsStorageManager .updateUpload (mUpload );
935963 }
936964
937965 e2eFiles .deleteEncryptedTempFile ();
0 commit comments