@@ -371,9 +371,31 @@ protected RemoteOperationResult run(OwnCloudClient client) {
371371 remoteParentPath : remoteParentPath + OCFile .PATH_SEPARATOR ;
372372
373373 OCFile parent = getStorageManager ().getFileByPath (remoteParentPath );
374+
375+ // in case of a fresh upload with subfolder, where parent does not exist yet
376+ if (parent == null && mFolderUnlockToken .isEmpty ()) {
377+ // try to create folder
378+ RemoteOperationResult result = grantFolderExistence (remoteParentPath , client );
379+
380+ if (!result .isSuccess ()) {
381+ return result ;
382+ }
383+
384+ parent = getStorageManager ().getFileByPath (remoteParentPath );
385+
386+ if (parent == null ) {
387+ return new RemoteOperationResult (false , "Parent folder not found" , HttpStatus .SC_NOT_FOUND );
388+ }
389+ }
390+
391+ // parent file is not null anymore:
392+ // - it was created on fresh upload or
393+ // - resume of encrypted upload, then parent file exists already as unlock is only for direct parent
394+
374395 mFile .setParentId (parent .getFileId ());
375396
376397 // try to unlock folder with stored token, e.g. when upload needs to be resumed or app crashed
398+ // the parent folder should exist as it is a resume of a broken upload
377399 if (!mFolderUnlockToken .isEmpty ()) {
378400 UnlockFileOperation unlockFileOperation = new UnlockFileOperation (parent .getLocalId (), mFolderUnlockToken );
379401 RemoteOperationResult unlockFileOperationResult = unlockFileOperation .execute (client , true );
@@ -383,13 +405,6 @@ protected RemoteOperationResult run(OwnCloudClient client) {
383405 }
384406 }
385407
386- // check the existence of the parent folder for the file to upload
387- RemoteOperationResult result = grantFolderExistence (remoteParentPath , client );
388-
389- if (!result .isSuccess ()) {
390- return result ;
391- }
392-
393408 // check if any parent is encrypted
394409 encryptedAncestor = FileStorageUtils .checkEncryptionStatus (parent , getStorageManager ());
395410 mFile .setEncrypted (encryptedAncestor );
@@ -590,7 +605,6 @@ private RemoteOperationResult encryptedUpload(OwnCloudClient client, OCFile pare
590605 if (!result .isSuccess () && result .getHttpCode () == HttpStatus .SC_PRECONDITION_FAILED ) {
591606 result = new RemoteOperationResult (ResultCode .SYNC_CONFLICT );
592607 }
593- // }
594608
595609 if (result .isSuccess ()) {
596610 // upload metadata
0 commit comments