66 */
77namespace OCA \Files_Trashbin ;
88
9- use Exception ;
109use OC \Files \Cache \Cache ;
1110use OC \Files \Cache \CacheEntry ;
1211use OC \Files \Cache \CacheQueryBuilder ;
@@ -458,6 +457,9 @@ private static function copy(View $view, $source, $target) {
458457 */
459458 public static function restore ($ file , $ filename , $ timestamp ) {
460459 $ user = OC_User::getUser ();
460+ if (!$ user ) {
461+ throw new \Exception ('Tried to restore a file while not logged in ' );
462+ }
461463 $ view = new View ('/ ' . $ user );
462464
463465 $ location = '' ;
@@ -494,8 +496,8 @@ public static function restore($file, $filename, $timestamp) {
494496 $ sourcePath = Filesystem::normalizePath ($ file );
495497 $ targetPath = Filesystem::normalizePath ('/ ' . $ location . '/ ' . $ uniqueFilename );
496498
497- $ sourceNode = self ::getNodeForPath ($ sourcePath );
498- $ targetNode = self ::getNodeForPath ($ targetPath );
499+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
500+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
499501 $ run = true ;
500502 $ event = new BeforeNodeRestoredEvent ($ sourceNode , $ targetNode , $ run );
501503 $ dispatcher = Server::get (IEventDispatcher::class);
@@ -515,8 +517,8 @@ public static function restore($file, $filename, $timestamp) {
515517 $ view ->chroot ($ fakeRoot );
516518 Util::emitHook ('\OCA\Files_Trashbin\Trashbin ' , 'post_restore ' , ['filePath ' => $ targetPath , 'trashPath ' => $ sourcePath ]);
517519
518- $ sourceNode = self ::getNodeForPath ($ sourcePath );
519- $ targetNode = self ::getNodeForPath ($ targetPath );
520+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
521+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
520522 $ event = new NodeRestoredEvent ($ sourceNode , $ targetNode );
521523 $ dispatcher = Server::get (IEventDispatcher::class);
522524 $ dispatcher ->dispatchTyped ($ event );
@@ -1162,27 +1164,20 @@ public static function getTrashFilename(string $filename, int $timestamp): strin
11621164 return $ trashFilename ;
11631165 }
11641166
1165- private static function getNodeForPath (string $ path ): Node {
1166- $ user = OC_User::getUser ();
1167+ private static function getNodeForPath (string $ user , string $ path , string $ baseDir = 'files_trashbin/files ' ): Node {
11671168 $ rootFolder = Server::get (IRootFolder::class);
1169+ $ path = ltrim ($ path , '/ ' );
11681170
1169- if ($ user !== false ) {
1170- $ userFolder = $ rootFolder ->getUserFolder ($ user );
1171- /** @var Folder */
1172- $ trashFolder = $ userFolder ->getParent ()->get ('files_trashbin/files ' );
1173- try {
1174- return $ trashFolder ->get ($ path );
1175- } catch (NotFoundException $ ex ) {
1176- }
1171+ $ userFolder = $ rootFolder ->getUserFolder ($ user );
1172+ /** @var Folder $trashFolder */
1173+ $ trashFolder = $ userFolder ->getParent ()->get ($ baseDir );
1174+ try {
1175+ return $ trashFolder ->get ($ path );
1176+ } catch (NotFoundException $ ex ) {
11771177 }
11781178
11791179 $ view = Server::get (View::class);
1180- $ fsView = Filesystem::getView ();
1181- if ($ fsView === null ) {
1182- throw new Exception ('View should not be null ' );
1183- }
1184-
1185- $ fullPath = $ fsView ->getAbsolutePath ($ path );
1180+ $ fullPath = '/ ' . $ user . '/ ' . $ baseDir . '/ ' . $ path ;
11861181
11871182 if (Filesystem::is_dir ($ path )) {
11881183 return new NonExistingFolder ($ rootFolder , $ view , $ fullPath );
0 commit comments