Skip to content
Open
Show file tree
Hide file tree
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
44 changes: 15 additions & 29 deletions lib/ViewInfoCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
Expand All @@ -13,24 +15,14 @@
use OCP\Files\NotFoundException;

class ViewInfoCache {
/** @var array */
protected $cachePath;
/** @var array */
protected $cacheId;

protected array $cacheId = [];

public function __construct(
protected IRootFolder $rootFolder,
) {
}

/**
* @param string $user
* @param int $fileId
* @param string $path
* @return array
*/
public function getInfoById($user, $fileId, $path) {
public function getInfoById(string $user, int $fileId, string $path): array {
if (isset($this->cacheId[$user][$fileId])) {
$cache = $this->cacheId[$user][$fileId];
if ($cache['path'] === null) {
Expand All @@ -43,12 +35,9 @@ public function getInfoById($user, $fileId, $path) {
}

/**
* @param string $user
* @param int $fileId
* @param string $filePath
* @return array
* @return array{path: string, exists: bool, is_dir: bool, view: string, node?: Node}
*/
protected function findInfoById($user, $fileId, $filePath) {
protected function findInfoById(string $user, int $fileId, string $filePath): array {
$cache = [
'path' => $filePath,
'exists' => false,
Expand All @@ -59,39 +48,36 @@ protected function findInfoById($user, $fileId, $filePath) {
$notFound = false;
try {
$userFolder = $this->rootFolder->getUserFolder($user);
$entries = $userFolder->getById($fileId);
if (empty($entries)) {
$entry = $userFolder->getFirstNodeById($fileId);
if ($entry === null) {
throw new NotFoundException('No entries returned');
}
/** @var Node $entry */
$entry = array_shift($entries);

$cache['path'] = $userFolder->getRelativePath($entry->getPath());
$cache['is_dir'] = $entry instanceof Folder;
$cache['exists'] = true;
$cache['node'] = $entry;
} catch (NotFoundException $e) {
} catch (NotFoundException) {
// The file was not found in the normal view,
// maybe it is in the trashbin?
try {
/** @var Folder $userTrashBin */
$userTrashBin = $this->rootFolder->get('/' . $user . '/files_trashbin');
$entries = $userTrashBin->getById($fileId);
if (empty($entries)) {
if (!$userTrashBin instanceof Folder) {
throw new NotFoundException('No trash bin found for user: ' . $user);
}
$entry = $userTrashBin->getFirstNodeById($fileId);
if ($entry === null) {
throw new NotFoundException('No entries returned');
}

/** @var Node $entry */
$entry = array_shift($entries);

$cache = [
'path' => $userTrashBin->getRelativePath($entry->getPath()),
'exists' => true,
'is_dir' => $entry instanceof Folder,
'view' => 'trashbin',
'node' => $entry,
];
} catch (NotFoundException $e) {
} catch (NotFoundException) {
$notFound = true;
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/ViewInfoCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
->willReturn($userFolder);
if ($path === null) {
$userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with($fileId)
->willThrowException(new NotFoundException());
->willReturn(null);

$userTrashBin = $this->createMock(Folder::class);
$this->rootFolder->expects($this->once())
Expand All @@ -250,9 +250,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
->willReturn($userTrashBin);
if ($pathTrash === null) {
$userTrashBin->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with($fileId)
->willThrowException(new NotFoundException());
->willReturn(null);
} else {
$node = $this->createMock($isDir ? Folder::class : File::class);
$node
Expand All @@ -264,9 +264,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
->willReturn($pathTrash);

$userTrashBin->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with($fileId)
->willReturn([2 => $node]);
->willReturn($node);
$expected['node'] = $node;
$expectedCache[$user][$fileId]['node'] = $node;
}
Expand All @@ -281,9 +281,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
->willReturn($path);

$userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with($fileId)
->willReturn([3 => $node]);
->willReturn($node);
$expected['node'] = $node;
$expectedCache[$user][$fileId]['node'] = $node;
}
Expand Down
14 changes: 7 additions & 7 deletions vendor-bin/psalm/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading