Skip to content

Commit d41b3ea

Browse files
Merge pull request #871 from nextcloud/query-over-9000
Simplify the preview information
2 parents 6f5e565 + 277e536 commit d41b3ea

File tree

8 files changed

+186
-369
lines changed

8 files changed

+186
-369
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Controller/APIv2Controller.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace OCA\Activity\Controller;
2626

27-
use OC\Files\View;
2827
use OCA\Activity\Data;
2928
use OCA\Activity\Exception\InvalidFilterException;
3029
use OCA\Activity\GroupHelper;
@@ -93,28 +92,9 @@ class APIv2Controller extends OCSController {
9392
/** @var IMimeTypeDetector */
9493
protected $mimeTypeDetector;
9594

96-
/** @var View */
97-
protected $view;
98-
9995
/** @var ViewInfoCache */
10096
protected $infoCache;
10197

102-
/**
103-
* OCSEndPoint constructor.
104-
*
105-
* @param string $appName
106-
* @param IRequest $request
107-
* @param IManager $activityManager
108-
* @param Data $data
109-
* @param GroupHelper $helper
110-
* @param UserSettings $settings
111-
* @param IURLGenerator $urlGenerator
112-
* @param IUserSession $userSession
113-
* @param IPreview $preview
114-
* @param IMimeTypeDetector $mimeTypeDetector
115-
* @param View $view
116-
* @param ViewInfoCache $infoCache
117-
*/
11898
public function __construct($appName,
11999
IRequest $request,
120100
IManager $activityManager,
@@ -125,7 +105,6 @@ public function __construct($appName,
125105
IUserSession $userSession,
126106
IPreview $preview,
127107
IMimeTypeDetector $mimeTypeDetector,
128-
View $view,
129108
ViewInfoCache $infoCache) {
130109
parent::__construct($appName, $request);
131110
$this->activityManager = $activityManager;
@@ -136,7 +115,6 @@ public function __construct($appName,
136115
$this->userSession = $userSession;
137116
$this->preview = $preview;
138117
$this->mimeTypeDetector = $mimeTypeDetector;
139-
$this->view = $view;
140118
$this->infoCache = $infoCache;
141119
}
142120

@@ -357,7 +335,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
357335
}
358336

359337
$preview = [
360-
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']),
338+
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]),
361339
'source' => '',
362340
'mimeType' => 'application/octet-stream',
363341
'isMimeTypeIcon' => true,
@@ -371,8 +349,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
371349
$preview['source'] = $this->getPreviewPathFromMimeType('dir');
372350
$preview['mimeType'] = 'dir';
373351
} else {
374-
$this->view->chroot('/' . $owner . '/files');
375-
$fileInfo = $this->view->getFileInfo($info['path']);
352+
$fileInfo = $info['node'] ?? null;
376353
if (!($fileInfo instanceof FileInfo)) {
377354
$preview = $this->getPreviewFromPath($fileId, $filePath, $info);
378355
} elseif ($this->preview->isAvailable($fileInfo)) {
@@ -382,7 +359,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
382359
'x' => 250,
383360
'y' => 250,
384361
'fileId' => $fileId,
385-
'c' => $fileInfo->getEtag()
362+
'c' => $fileInfo->getEtag(),
386363
];
387364

388365
$preview['source'] = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', $params);
@@ -400,7 +377,7 @@ protected function getPreview(string $owner, int $fileId, string $filePath): arr
400377
protected function getPreviewFromPath(int $fileId, string $filePath, array $info): array {
401378
$mimeType = $info['is_dir'] ? 'dir' : $this->mimeTypeDetector->detectPath($filePath);
402379
return [
403-
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view']),
380+
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $fileId]),
404381
'source' => $this->getPreviewPathFromMimeType($mimeType),
405382
'mimeType' => $mimeType,
406383
'isMimeTypeIcon' => true,
@@ -418,18 +395,4 @@ protected function getPreviewPathFromMimeType(string $mimeType): string {
418395

419396
return $this->urlGenerator->getAbsoluteURL($mimeTypeIcon);
420397
}
421-
422-
protected function getPreviewLink(string $path, bool $isDir, string $view): string {
423-
$params = [
424-
'dir' => $path,
425-
];
426-
if (!$isDir) {
427-
$params['dir'] = (substr_count($path, '/') === 1) ? '/' : \dirname($path);
428-
$params['scrollto'] = basename($path);
429-
}
430-
if ($view !== '') {
431-
$params['view'] = $view;
432-
}
433-
return $this->urlGenerator->linkToRouteAbsolute('files.view.index', $params);
434-
}
435398
}

lib/ViewInfoCache.php

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
namespace OCA\Activity;
2424

25-
use OC\Files\View;
25+
use OCP\Files\Folder;
26+
use OCP\Files\IRootFolder;
27+
use OCP\Files\Node;
2628
use OCP\Files\NotFoundException;
2729

2830
class ViewInfoCache {
@@ -32,27 +34,11 @@ class ViewInfoCache {
3234
/** @var array */
3335
protected $cacheId;
3436

35-
/** @var \OC\Files\View */
36-
protected $view;
37+
/** @var IRootFolder */
38+
protected $rootFolder;
3739

38-
/**
39-
* @param View $view
40-
*/
41-
public function __construct(View $view) {
42-
$this->view = $view;
43-
}
44-
45-
/**
46-
* @param string $user
47-
* @param string $path
48-
* @return array
49-
*/
50-
public function getInfoByPath($user, $path) {
51-
if (isset($this->cachePath[$user][$path])) {
52-
return $this->cachePath[$user][$path];
53-
}
54-
55-
return $this->findInfoByPath($user, $path);
40+
public function __construct(IRootFolder $rootFolder) {
41+
$this->rootFolder = $rootFolder;
5642
}
5743

5844
/**
@@ -73,35 +59,13 @@ public function getInfoById($user, $fileId, $path) {
7359
return $this->findInfoById($user, $fileId, $path);
7460
}
7561

76-
/**
77-
* @param string $user
78-
* @param string $path
79-
* @return array
80-
*/
81-
protected function findInfoByPath($user, $path) {
82-
$this->view->chroot('/' . $user . '/files');
83-
84-
$exists = $this->view->file_exists($path);
85-
86-
$this->cachePath[$user][$path] = [
87-
'path' => $path,
88-
'exists' => $exists,
89-
'is_dir' => $exists ? (bool)$this->view->is_dir($path) : false,
90-
'view' => '',
91-
];
92-
93-
return $this->cachePath[$user][$path];
94-
}
95-
9662
/**
9763
* @param string $user
9864
* @param int $fileId
9965
* @param string $filePath
10066
* @return array
10167
*/
10268
protected function findInfoById($user, $fileId, $filePath) {
103-
$this->view->chroot('/' . $user . '/files');
104-
10569
$cache = [
10670
'path' => $filePath,
10771
'exists' => false,
@@ -111,24 +75,38 @@ protected function findInfoById($user, $fileId, $filePath) {
11175

11276
$notFound = false;
11377
try {
114-
$path = $this->view->getPath($fileId);
78+
$userFolder = $this->rootFolder->getUserFolder($user);
79+
$entries = $userFolder->getById($fileId);
80+
if (empty($entries)) {
81+
throw new NotFoundException('No entries returned');
82+
}
83+
/** @var Node $entry */
84+
$entry = array_shift($entries);
11585

116-
$cache['path'] = $path;
117-
$cache['is_dir'] = $this->view->is_dir($path);
86+
$cache['path'] = $userFolder->getRelativePath($entry->getPath());
87+
$cache['is_dir'] = $entry instanceof Folder;
11888
$cache['exists'] = true;
89+
$cache['node'] = $entry;
11990
} catch (NotFoundException $e) {
120-
// The file was not found in the normal view, maybe it is in
121-
// the trashbin?
122-
$this->view->chroot('/' . $user . '/files_trashbin');
123-
91+
// The file was not found in the normal view,
92+
// maybe it is in the trashbin?
12493
try {
125-
$path = $this->view->getPath($fileId);
94+
/** @var Folder $userTrashBin */
95+
$userTrashBin = $this->rootFolder->get('/' . $user . '/files_trashbin');
96+
$entries = $userTrashBin->getById($fileId);
97+
if (empty($entries)) {
98+
throw new NotFoundException('No entries returned');
99+
}
100+
101+
/** @var Node $entry */
102+
$entry = array_shift($entries);
126103

127104
$cache = [
128-
'path' => substr($path, strlen('/files')),
105+
'path' => $userTrashBin->getRelativePath($entry->getPath()),
129106
'exists' => true,
130-
'is_dir' => (bool)$this->view->is_dir($path),
107+
'is_dir' => $entry instanceof Folder,
131108
'view' => 'trashbin',
109+
'node' => $entry,
132110
];
133111
} catch (NotFoundException $e) {
134112
$notFound = true;

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@
4343
</errorLevel>
4444
</UndefinedDocblockClass>
4545
</issueHandlers>
46+
<stubs>
47+
<file name="tests/stubs/oc_hooks_emitter.php" />
48+
</stubs>
4649
</psalm>

0 commit comments

Comments
 (0)