@@ -560,7 +560,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
560
560
561
561
// Unless the editor is empty (public link) we modify the files as the current editor
562
562
$ editor = $ wopi ->getEditorUid ();
563
- if ($ editor === null && !$ wopi ->isRemoteToken ()) {
563
+ $ isPublic = $ editor === null && !$ wopi ->isRemoteToken ();
564
+ if ($ isPublic ) {
564
565
$ editor = $ wopi ->getOwnerUid ();
565
566
}
566
567
@@ -573,16 +574,10 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
573
574
$ file = $ this ->getFileForWopiToken ($ wopi );
574
575
575
576
$ suggested = $ this ->request ->getHeader ('X-WOPI-RequestedName ' );
576
-
577
577
$ suggested = mb_convert_encoding ($ suggested , 'utf-8 ' , 'utf-7 ' ) . '. ' . $ file ->getExtension ();
578
578
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 );
586
581
587
582
if ($ path === '' ) {
588
583
return new JSONResponse ([
@@ -611,20 +606,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
611
606
$ suggested = $ this ->request ->getHeader ('X-WOPI-SuggestedTarget ' );
612
607
$ suggested = mb_convert_encoding ($ suggested , 'utf-8 ' , 'utf-7 ' );
613
608
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 );
628
611
629
612
// create the folder first
630
613
if (!$ this ->rootFolder ->nodeExists (dirname ($ path ))) {
@@ -638,8 +621,8 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
638
621
639
622
$ content = fopen ('php://input ' , 'rb ' );
640
623
// 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 );
643
626
644
627
try {
645
628
$ this ->wrappedFilesystemOperation ($ wopi , function () use ($ file , $ content ) {
@@ -668,6 +651,13 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
668
651
}
669
652
}
670
653
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
+
671
661
private function lock (Wopi $ wopi , string $ lock ): JSONResponse {
672
662
try {
673
663
$ lock = $ this ->lockManager ->lock (new LockContext (
0 commit comments