@@ -560,7 +560,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
560560
561561 // Unless the editor is empty (public link) we modify the files as the current editor
562562 $ editor = $ wopi ->getEditorUid ();
563- if ($ editor === null && !$ wopi ->isRemoteToken ()) {
563+ $ isPublic = $ editor === null && !$ wopi ->isRemoteToken ();
564+ if ($ isPublic ) {
564565 $ editor = $ wopi ->getOwnerUid ();
565566 }
566567
@@ -573,16 +574,10 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
573574 $ file = $ this ->getFileForWopiToken ($ wopi );
574575
575576 $ suggested = $ this ->request ->getHeader ('X-WOPI-RequestedName ' );
576-
577577 $ suggested = mb_convert_encoding ($ suggested , 'utf-8 ' , 'utf-7 ' ) . '. ' . $ file ->getExtension ();
578578
579- if (strpos ($ suggested , '. ' ) === 0 ) {
580- $ path = dirname ($ file ->getPath ()) . '/New File ' . $ suggested ;
581- } elseif (strpos ($ suggested , '/ ' ) !== 0 ) {
582- $ path = dirname ($ file ->getPath ()) . '/ ' . $ suggested ;
583- } else {
584- $ path = $ userFolder ->getPath () . $ suggested ;
585- }
579+ $ parent = $ isPublic ? dirname ($ file ->getPath ()) : $ userFolder ->getPath ();
580+ $ path = $ this ->normalizePath ($ suggested , $ parent );
586581
587582 if ($ path === '' ) {
588583 return new JSONResponse ([
@@ -611,20 +606,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
611606 $ suggested = $ this ->request ->getHeader ('X-WOPI-SuggestedTarget ' );
612607 $ suggested = mb_convert_encoding ($ suggested , 'utf-8 ' , 'utf-7 ' );
613608
614- if ($ suggested [0 ] === '. ' ) {
615- $ path = dirname ($ file ->getPath ()) . '/New File ' . $ suggested ;
616- } elseif ($ suggested [0 ] !== '/ ' ) {
617- $ path = dirname ($ file ->getPath ()) . '/ ' . $ suggested ;
618- } else {
619- $ path = $ userFolder ->getPath () . $ suggested ;
620- }
621-
622- if ($ path === '' ) {
623- return new JSONResponse ([
624- 'status ' => 'error ' ,
625- 'message ' => 'Cannot create the file '
626- ]);
627- }
609+ $ parent = $ isPublic ? dirname ($ file ->getPath ()) : $ userFolder ->getPath ();
610+ $ path = $ this ->normalizePath ($ suggested , $ parent );
628611
629612 // create the folder first
630613 if (!$ this ->rootFolder ->nodeExists (dirname ($ path ))) {
@@ -638,8 +621,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
638621
639622 $ content = fopen ('php://input ' , 'rb ' );
640623 // Set the user to register the change under his name
641- $ this ->userScopeService ->setUserScope ($ wopi -> getEditorUid () );
642- $ this ->userScopeService ->setFilesystemScope ($ wopi -> getEditorUid () );
624+ $ this ->userScopeService ->setUserScope ($ editor );
625+ $ this ->userScopeService ->setFilesystemScope ($ editor );
643626
644627 try {
645628 $ this ->wrappedFilesystemOperation ($ wopi , function () use ($ file , $ content ) {
@@ -668,6 +651,13 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
668651 }
669652 }
670653
654+ private function normalizePath (string $ path , ?string $ parent = null ): string {
655+ $ path = str_starts_with ($ path , '/ ' ) ? $ path : '/ ' . $ path ;
656+ $ parent = is_null ($ parent ) ? '' : rtrim ($ parent , '/ ' );
657+
658+ return $ parent . $ path ;
659+ }
660+
671661 private function lock (Wopi $ wopi , string $ lock ): JSONResponse {
672662 try {
673663 $ lock = $ this ->lockManager ->lock (new LockContext (
0 commit comments