-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathViewInfoCache.php
More file actions
116 lines (102 loc) · 2.77 KB
/
Copy pathViewInfoCache.php
File metadata and controls
116 lines (102 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Activity;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
class ViewInfoCache {
/** @var array */
protected $cachePath;
/** @var array */
protected $cacheId;
public function __construct(
protected IRootFolder $rootFolder,
) {
}
/**
* @param string $user
* @param int $fileId
* @param string $path
* @return array
*/
public function getInfoById($user, $fileId, $path) {
if (isset($this->cacheId[$user][$fileId])) {
$cache = $this->cacheId[$user][$fileId];
if ($cache['path'] === null) {
$cache['path'] = $path;
}
return $cache;
}
return $this->findInfoById($user, $fileId, $path);
}
/**
* @param string $user
* @param int $fileId
* @param string $filePath
* @return array
*/
protected function findInfoById($user, $fileId, $filePath) {
$cache = [
'path' => $filePath,
'exists' => false,
'is_dir' => false,
'view' => '',
];
try {
$userFolder = $this->rootFolder->getUserFolder($user);
$entries = $userFolder->getById($fileId);
if (empty($entries)) {
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) {
// 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)) {
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) {
$notFound = true;
}
$entry = $userTrashBin->getFirstNodeById($fileId);
if ($entry === null) {
throw new NotFoundException('No entries returned');
}
$cache = [
'path' => $userTrashBin->getRelativePath($entry->getPath()),
'exists' => true,
'is_dir' => $entry instanceof Folder,
'view' => 'trashbin',
'node' => $entry,
];
} catch (NotFoundException) {
// Not found anywhere — cache path as null but return original filePath
$this->cacheId[$user][$fileId] = array_merge($cache, ['path' => null]);
return $cache;
}
$this->cacheId[$user][$fileId] = $cache;
return $cache;
}
}