Skip to content

Commit

Permalink
Remove the trashed state from fileId endpoint (#554)
Browse files Browse the repository at this point in the history
* Remove the trashed state from fileId endpoint

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

* refactor tests

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

* fix unit tests

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

* refactor tests

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

* refactor tests

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

* Address reviews

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>

---------

Signed-off-by: Swikriti Tripathi <swikriti808@gmail.com>
  • Loading branch information
SwikritiT committed Jan 12, 2024
1 parent e15a7bd commit a88dbdc
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 339 deletions.
53 changes: 10 additions & 43 deletions lib/Controller/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@

namespace OCA\OpenProject\Controller;

use Exception;
use OCA\Activity\Data;
use OCA\Activity\GroupHelperDisabled;
use OCA\Activity\UserSettings;
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\AppFramework\QueryException;
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\FileInfo;
Expand All @@ -33,7 +29,6 @@
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use Throwable;
use OCP\Files\DavUtil;

class FilesController extends OCSController {
Expand Down Expand Up @@ -67,14 +62,6 @@ class FilesController extends OCSController {
* @var IUserManager
*/
private $userManager;
/**
* @var IAppManager
*/
private $appManager;
/**
* @var ITrashManager
*/
private $trashManager = null;
/**
* @var DavUtil
*/
Expand All @@ -87,7 +74,6 @@ class FilesController extends OCSController {
* @param IUserSession $userSession
* @param IMountProviderCollection $mountCollection
* @param IManager $activityManager
* @param IAppManager $appManager
* @param IDBConnection $connection
* @param ILogger $logger
* @param IUserManager $userManager
Expand All @@ -100,7 +86,6 @@ public function __construct(string $appName,
IUserSession $userSession,
IMountProviderCollection $mountCollection,
IManager $activityManager,
IAppManager $appManager,
IDBConnection $connection,
ILogger $logger,
IUserManager $userManager,
Expand All @@ -114,7 +99,6 @@ public function __construct(string $appName,
$this->connection = $connection;
$this->logger = $logger;
$this->userManager = $userManager;
$this->appManager = $appManager;
$this->davUtils = $davUtils;
}

Expand Down Expand Up @@ -155,20 +139,6 @@ public function getFilesInfo(?array $fileIds): DataResponse {
return new DataResponse($result);
}

/**
* Function to make the trash-manager testable
*
* @param ITrashManager|null $trashManager
* @return void
* @throws QueryException
*/
public function setTrashManager(ITrashManager $trashManager = null): void {
if ($trashManager !== null) {
$this->trashManager = $trashManager;
} elseif ($this->trashManager === null) {
$this->trashManager = \OC::$server->get(ITrashManager::class);
}
}
/**
* @param int $fileId
* @return array<mixed>
Expand All @@ -178,24 +148,12 @@ public function setTrashManager(ITrashManager $trashManager = null): void {
*/
private function compileFileInfo($fileId) {
$file = null;
$trashed = false;

$userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
$files = $userFolder->getById($fileId);
if (is_array($files) && count($files) > 0) {
$file = $files[0];
} elseif ($this->appManager->isEnabledForUser('files_trashbin')) {
try {
$this->setTrashManager();
$file = $this->trashManager->getTrashNodeById(
$this->user, $fileId
);
$trashed = true;
} catch (Exception | Throwable $e) {
$this->logger->error('failed to use the trashManager', ['exception' => $e]);
}
}

$mounts = $this->mountCollection->getMountCache()->getMountsForFileId($fileId);

if ($file !== null && is_array($mounts) && count($mounts) > 0) {
Expand Down Expand Up @@ -249,7 +207,6 @@ private function compileFileInfo($fileId) {
'size' => $file->getSize(),
'owner_name' => $owner->getDisplayName(),
'owner_id' => $owner->getUID(),
'trashed' => $trashed,
'modifier_name' => $modifierName,
'modifier_id' => $modifierId,
'dav_permissions' => $davPermission,
Expand All @@ -258,6 +215,16 @@ private function compileFileInfo($fileId) {
}

if (is_array($mounts) && count($mounts) > 0) {
// if the file is in trashbin send 404
foreach ($mounts as $mount) {
if (str_starts_with($mount->getInternalPath(), 'files_trashbin')) {
return [
'status' => 'Not Found',
'statuscode' => 404,
];
}
}
// if the file is of another user send 403
return [
'status' => 'Forbidden',
'statuscode' => 403,
Expand Down
Loading

0 comments on commit a88dbdc

Please sign in to comment.