77 */
88namespace OCA \Files_Trashbin ;
99
10- use Exception ;
1110use OC \Files \Cache \Cache ;
1211use OC \Files \Cache \CacheEntry ;
1312use OC \Files \Cache \CacheQueryBuilder ;
@@ -459,6 +458,9 @@ private static function copy(View $view, $source, $target) {
459458 */
460459 public static function restore ($ file , $ filename , $ timestamp ) {
461460 $ user = OC_User::getUser ();
461+ if (!$ user ) {
462+ throw new \Exception ('Tried to restore a file while not logged in ' );
463+ }
462464 $ view = new View ('/ ' . $ user );
463465
464466 $ location = '' ;
@@ -495,8 +497,8 @@ public static function restore($file, $filename, $timestamp) {
495497 $ sourcePath = Filesystem::normalizePath ($ file );
496498 $ targetPath = Filesystem::normalizePath ('/ ' . $ location . '/ ' . $ uniqueFilename );
497499
498- $ sourceNode = self ::getNodeForPath ($ sourcePath );
499- $ targetNode = self ::getNodeForPath ($ targetPath );
500+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
501+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
500502 $ run = true ;
501503 $ event = new BeforeNodeRestoredEvent ($ sourceNode , $ targetNode , $ run );
502504 $ dispatcher = Server::get (IEventDispatcher::class);
@@ -516,8 +518,8 @@ public static function restore($file, $filename, $timestamp) {
516518 $ view ->chroot ($ fakeRoot );
517519 Util::emitHook ('\OCA\Files_Trashbin\Trashbin ' , 'post_restore ' , ['filePath ' => $ targetPath , 'trashPath ' => $ sourcePath ]);
518520
519- $ sourceNode = self ::getNodeForPath ($ sourcePath );
520- $ targetNode = self ::getNodeForPath ($ targetPath );
521+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
522+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
521523 $ event = new NodeRestoredEvent ($ sourceNode , $ targetNode );
522524 $ dispatcher = Server::get (IEventDispatcher::class);
523525 $ dispatcher ->dispatchTyped ($ event );
@@ -1163,27 +1165,20 @@ public static function getTrashFilename(string $filename, int $timestamp): strin
11631165 return $ trashFilename ;
11641166 }
11651167
1166- private static function getNodeForPath (string $ path ): Node {
1167- $ user = OC_User::getUser ();
1168+ private static function getNodeForPath (string $ user , string $ path , string $ baseDir = 'files_trashbin/files ' ): Node {
11681169 $ rootFolder = Server::get (IRootFolder::class);
1170+ $ path = ltrim ($ path , '/ ' );
11691171
1170- if ($ user !== false ) {
1171- $ userFolder = $ rootFolder ->getUserFolder ($ user );
1172- /** @var Folder */
1173- $ trashFolder = $ userFolder ->getParent ()->get ('files_trashbin/files ' );
1174- try {
1175- return $ trashFolder ->get ($ path );
1176- } catch (NotFoundException $ ex ) {
1177- }
1172+ $ userFolder = $ rootFolder ->getUserFolder ($ user );
1173+ /** @var Folder $trashFolder */
1174+ $ trashFolder = $ userFolder ->getParent ()->get ($ baseDir );
1175+ try {
1176+ return $ trashFolder ->get ($ path );
1177+ } catch (NotFoundException $ ex ) {
11781178 }
11791179
11801180 $ view = Server::get (View::class);
1181- $ fsView = Filesystem::getView ();
1182- if ($ fsView === null ) {
1183- throw new Exception ('View should not be null ' );
1184- }
1185-
1186- $ fullPath = $ fsView ->getAbsolutePath ($ path );
1181+ $ fullPath = '/ ' . $ user . '/ ' . $ baseDir . '/ ' . $ path ;
11871182
11881183 if (Filesystem::is_dir ($ path )) {
11891184 return new NonExistingFolder ($ rootFolder , $ view , $ fullPath );
0 commit comments