Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
namespace OCA\Files_Trashbin;

use Exception;
use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheEntry;
use OC\Files\Cache\CacheQueryBuilder;
Expand Down Expand Up @@ -459,6 +458,9 @@ private static function copy(View $view, $source, $target) {
*/
public static function restore($file, $filename, $timestamp) {
$user = OC_User::getUser();
if (!$user) {
throw new \Exception('Tried to restore a file while not logged in');
}
$view = new View('/' . $user);

$location = '';
Expand Down Expand Up @@ -495,8 +497,8 @@ public static function restore($file, $filename, $timestamp) {
$sourcePath = Filesystem::normalizePath($file);
$targetPath = Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename);

$sourceNode = self::getNodeForPath($sourcePath);
$targetNode = self::getNodeForPath($targetPath);
$sourceNode = self::getNodeForPath($user, $sourcePath);
$targetNode = self::getNodeForPath($user, $targetPath, 'files');
$run = true;
$event = new BeforeNodeRestoredEvent($sourceNode, $targetNode, $run);
$dispatcher = Server::get(IEventDispatcher::class);
Expand All @@ -516,8 +518,8 @@ public static function restore($file, $filename, $timestamp) {
$view->chroot($fakeRoot);
Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', ['filePath' => $targetPath, 'trashPath' => $sourcePath]);

$sourceNode = self::getNodeForPath($sourcePath);
$targetNode = self::getNodeForPath($targetPath);
$sourceNode = self::getNodeForPath($user, $sourcePath);
$targetNode = self::getNodeForPath($user, $targetPath, 'files');
$event = new NodeRestoredEvent($sourceNode, $targetNode);
$dispatcher = Server::get(IEventDispatcher::class);
$dispatcher->dispatchTyped($event);
Expand Down Expand Up @@ -1163,27 +1165,20 @@ public static function getTrashFilename(string $filename, int $timestamp): strin
return $trashFilename;
}

private static function getNodeForPath(string $path): Node {
$user = OC_User::getUser();
private static function getNodeForPath(string $user, string $path, string $baseDir = 'files_trashbin/files'): Node {
$rootFolder = Server::get(IRootFolder::class);
$path = ltrim($path, '/');

if ($user !== false) {
$userFolder = $rootFolder->getUserFolder($user);
/** @var Folder */
$trashFolder = $userFolder->getParent()->get('files_trashbin/files');
try {
return $trashFolder->get($path);
} catch (NotFoundException $ex) {
}
$userFolder = $rootFolder->getUserFolder($user);
/** @var Folder $trashFolder */
$trashFolder = $userFolder->getParent()->get($baseDir);
try {
return $trashFolder->get($path);
} catch (NotFoundException $ex) {
}

$view = Server::get(View::class);
$fsView = Filesystem::getView();
if ($fsView === null) {
throw new Exception('View should not be null');
}

$fullPath = $fsView->getAbsolutePath($path);
$fullPath = '/' . $user . '/' . $baseDir . '/' . $path;

if (Filesystem::is_dir($path)) {
return new NonExistingFolder($rootFolder, $view, $fullPath);
Expand Down
Loading