6
6
*/
7
7
namespace OCA \Files_Trashbin ;
8
8
9
- use Exception ;
10
9
use OC \Files \Cache \Cache ;
11
10
use OC \Files \Cache \CacheEntry ;
12
11
use OC \Files \Cache \CacheQueryBuilder ;
@@ -458,6 +457,9 @@ private static function copy(View $view, $source, $target) {
458
457
*/
459
458
public static function restore ($ file , $ filename , $ timestamp ) {
460
459
$ user = OC_User::getUser ();
460
+ if (!$ user ) {
461
+ throw new \Exception ('Tried to restore a file while not logged in ' );
462
+ }
461
463
$ view = new View ('/ ' . $ user );
462
464
463
465
$ location = '' ;
@@ -494,8 +496,8 @@ public static function restore($file, $filename, $timestamp) {
494
496
$ sourcePath = Filesystem::normalizePath ($ file );
495
497
$ targetPath = Filesystem::normalizePath ('/ ' . $ location . '/ ' . $ uniqueFilename );
496
498
497
- $ sourceNode = self ::getNodeForPath ($ sourcePath );
498
- $ targetNode = self ::getNodeForPath ($ targetPath );
499
+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
500
+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
499
501
$ run = true ;
500
502
$ event = new BeforeNodeRestoredEvent ($ sourceNode , $ targetNode , $ run );
501
503
$ dispatcher = Server::get (IEventDispatcher::class);
@@ -515,8 +517,8 @@ public static function restore($file, $filename, $timestamp) {
515
517
$ view ->chroot ($ fakeRoot );
516
518
Util::emitHook ('\OCA\Files_Trashbin\Trashbin ' , 'post_restore ' , ['filePath ' => $ targetPath , 'trashPath ' => $ sourcePath ]);
517
519
518
- $ sourceNode = self ::getNodeForPath ($ sourcePath );
519
- $ targetNode = self ::getNodeForPath ($ targetPath );
520
+ $ sourceNode = self ::getNodeForPath ($ user , $ sourcePath );
521
+ $ targetNode = self ::getNodeForPath ($ user , $ targetPath, ' files ' );
520
522
$ event = new NodeRestoredEvent ($ sourceNode , $ targetNode );
521
523
$ dispatcher = Server::get (IEventDispatcher::class);
522
524
$ dispatcher ->dispatchTyped ($ event );
@@ -1162,27 +1164,20 @@ public static function getTrashFilename(string $filename, int $timestamp): strin
1162
1164
return $ trashFilename ;
1163
1165
}
1164
1166
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 {
1167
1168
$ rootFolder = Server::get (IRootFolder::class);
1169
+ $ path = ltrim ($ path , '/ ' );
1168
1170
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 ) {
1177
1177
}
1178
1178
1179
1179
$ 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 ;
1186
1181
1187
1182
if (Filesystem::is_dir ($ path )) {
1188
1183
return new NonExistingFolder ($ rootFolder , $ view , $ fullPath );
0 commit comments