Passed
Push — master ( 3eb7c9...54532b )
by John
13:37 queued 13s
created
apps/files_sharing/lib/Cache.php 1 patch
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -47,172 +47,172 @@
 block discarded – undo
47 47
  * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
48 48
  */
49 49
 class Cache extends CacheJail {
50
-	/** @var \OCA\Files_Sharing\SharedStorage */
51
-	private $storage;
52
-	/** @var ICacheEntry */
53
-	private $sourceRootInfo;
54
-	/** @var IUserManager */
55
-	private $userManager;
56
-
57
-	private $rootUnchanged = true;
58
-
59
-	private $ownerDisplayName;
60
-
61
-	private $numericId;
62
-
63
-	/**
64
-	 * @param \OCA\Files_Sharing\SharedStorage $storage
65
-	 */
66
-	public function __construct($storage, ICacheEntry $sourceRootInfo, IUserManager $userManager) {
67
-		$this->storage = $storage;
68
-		$this->sourceRootInfo = $sourceRootInfo;
69
-		$this->userManager = $userManager;
70
-		$this->numericId = $sourceRootInfo->getStorageId();
71
-
72
-		parent::__construct(
73
-			null,
74
-			''
75
-		);
76
-	}
77
-
78
-	protected function getRoot() {
79
-		if ($this->root === '') {
80
-			$absoluteRoot = $this->sourceRootInfo->getPath();
81
-
82
-			// the sourceRootInfo path is the absolute path of the folder in the "real" storage
83
-			// in the case where a folder is shared from a Jail we need to ensure that the share Jail
84
-			// has it's root set relative to the source Jail
85
-			$currentStorage = $this->storage->getSourceStorage();
86
-			if ($currentStorage->instanceOfStorage(Jail::class)) {
87
-				/** @var Jail $currentStorage */
88
-				$absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
89
-			}
90
-			$this->root = $absoluteRoot;
91
-		}
92
-		return $this->root;
93
-	}
94
-
95
-	protected function getGetUnjailedRoot() {
96
-		return $this->sourceRootInfo->getPath();
97
-	}
98
-
99
-	public function getCache() {
100
-		if (is_null($this->cache)) {
101
-			$sourceStorage = $this->storage->getSourceStorage();
102
-			if ($sourceStorage) {
103
-				$this->cache = $sourceStorage->getCache();
104
-			} else {
105
-				// don't set $this->cache here since sourceStorage will be set later
106
-				return new FailedCache();
107
-			}
108
-		}
109
-		return $this->cache;
110
-	}
111
-
112
-	public function getNumericStorageId() {
113
-		if (isset($this->numericId)) {
114
-			return $this->numericId;
115
-		} else {
116
-			return false;
117
-		}
118
-	}
119
-
120
-	public function get($file) {
121
-		if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
122
-			return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
123
-		}
124
-		return parent::get($file);
125
-	}
126
-
127
-	public function update($id, array $data) {
128
-		$this->rootUnchanged = false;
129
-		parent::update($id, $data);
130
-	}
131
-
132
-	public function insert($file, array $data) {
133
-		$this->rootUnchanged = false;
134
-		return parent::insert($file, $data);
135
-	}
136
-
137
-	public function remove($file) {
138
-		$this->rootUnchanged = false;
139
-		parent::remove($file);
140
-	}
141
-
142
-	public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
143
-		$this->rootUnchanged = false;
144
-		return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
145
-	}
146
-
147
-	protected function formatCacheEntry($entry, $path = null) {
148
-		if (is_null($path)) {
149
-			$path = $entry['path'] ?? '';
150
-			$entry['path'] = $this->getJailedPath($path);
151
-		} else {
152
-			$entry['path'] = $path;
153
-		}
154
-
155
-		try {
156
-			if (isset($entry['permissions'])) {
157
-				$entry['permissions'] &= $this->storage->getShare()->getPermissions();
158
-			} else {
159
-				$entry['permissions'] = $this->storage->getPermissions($entry['path']);
160
-			}
161
-		} catch (StorageNotAvailableException $e) {
162
-			// thrown by FailedStorage e.g. when the sharer does not exist anymore
163
-			// (IDE may say the exception is never thrown – false negative)
164
-			$sharePermissions = 0;
165
-		}
166
-		$entry['uid_owner'] = $this->storage->getOwner('');
167
-		$entry['displayname_owner'] = $this->getOwnerDisplayName();
168
-		if ($path === '') {
169
-			$entry['is_share_mount_point'] = true;
170
-		}
171
-		return $entry;
172
-	}
173
-
174
-	private function getOwnerDisplayName() {
175
-		if (!$this->ownerDisplayName) {
176
-			/** @var ICacheFactory $cacheFactory */
177
-			$cacheFactory = \OC::$server->get(ICacheFactory::class);
178
-			$memcache = $cacheFactory->createLocal('share_owner_name');
179
-			$uid = $this->storage->getOwner('');
180
-			$cached = $memcache->get($uid);
181
-			if ($cached) {
182
-				$this->ownerDisplayName = $cached;
183
-			} else {
184
-				$user = $this->userManager->get($uid);
185
-				if ($user) {
186
-					$this->ownerDisplayName = $user->getDisplayName();
187
-				} else {
188
-					$this->ownerDisplayName = $uid;
189
-				}
190
-				$memcache->set($uid, $this->ownerDisplayName, 60 * 60);
191
-			}
192
-		}
193
-		return $this->ownerDisplayName;
194
-	}
195
-
196
-	/**
197
-	 * remove all entries for files that are stored on the storage from the cache
198
-	 */
199
-	public function clear() {
200
-		// Not a valid action for Shared Cache
201
-	}
202
-
203
-	public function getQueryFilterForStorage(): ISearchOperator {
204
-		// Do the normal jail behavior for non files
205
-		if ($this->storage->getItemType() !== 'file') {
206
-			return parent::getQueryFilterForStorage();
207
-		}
208
-
209
-		// for single file shares we don't need to do the LIKE
210
-		return new SearchBinaryOperator(
211
-			ISearchBinaryOperator::OPERATOR_AND,
212
-			[
213
-				\OC\Files\Cache\Cache::getQueryFilterForStorage(),
214
-				new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
215
-			]
216
-		);
217
-	}
50
+    /** @var \OCA\Files_Sharing\SharedStorage */
51
+    private $storage;
52
+    /** @var ICacheEntry */
53
+    private $sourceRootInfo;
54
+    /** @var IUserManager */
55
+    private $userManager;
56
+
57
+    private $rootUnchanged = true;
58
+
59
+    private $ownerDisplayName;
60
+
61
+    private $numericId;
62
+
63
+    /**
64
+     * @param \OCA\Files_Sharing\SharedStorage $storage
65
+     */
66
+    public function __construct($storage, ICacheEntry $sourceRootInfo, IUserManager $userManager) {
67
+        $this->storage = $storage;
68
+        $this->sourceRootInfo = $sourceRootInfo;
69
+        $this->userManager = $userManager;
70
+        $this->numericId = $sourceRootInfo->getStorageId();
71
+
72
+        parent::__construct(
73
+            null,
74
+            ''
75
+        );
76
+    }
77
+
78
+    protected function getRoot() {
79
+        if ($this->root === '') {
80
+            $absoluteRoot = $this->sourceRootInfo->getPath();
81
+
82
+            // the sourceRootInfo path is the absolute path of the folder in the "real" storage
83
+            // in the case where a folder is shared from a Jail we need to ensure that the share Jail
84
+            // has it's root set relative to the source Jail
85
+            $currentStorage = $this->storage->getSourceStorage();
86
+            if ($currentStorage->instanceOfStorage(Jail::class)) {
87
+                /** @var Jail $currentStorage */
88
+                $absoluteRoot = $currentStorage->getJailedPath($absoluteRoot);
89
+            }
90
+            $this->root = $absoluteRoot;
91
+        }
92
+        return $this->root;
93
+    }
94
+
95
+    protected function getGetUnjailedRoot() {
96
+        return $this->sourceRootInfo->getPath();
97
+    }
98
+
99
+    public function getCache() {
100
+        if (is_null($this->cache)) {
101
+            $sourceStorage = $this->storage->getSourceStorage();
102
+            if ($sourceStorage) {
103
+                $this->cache = $sourceStorage->getCache();
104
+            } else {
105
+                // don't set $this->cache here since sourceStorage will be set later
106
+                return new FailedCache();
107
+            }
108
+        }
109
+        return $this->cache;
110
+    }
111
+
112
+    public function getNumericStorageId() {
113
+        if (isset($this->numericId)) {
114
+            return $this->numericId;
115
+        } else {
116
+            return false;
117
+        }
118
+    }
119
+
120
+    public function get($file) {
121
+        if ($this->rootUnchanged && ($file === '' || $file === $this->sourceRootInfo->getId())) {
122
+            return $this->formatCacheEntry(clone $this->sourceRootInfo, '');
123
+        }
124
+        return parent::get($file);
125
+    }
126
+
127
+    public function update($id, array $data) {
128
+        $this->rootUnchanged = false;
129
+        parent::update($id, $data);
130
+    }
131
+
132
+    public function insert($file, array $data) {
133
+        $this->rootUnchanged = false;
134
+        return parent::insert($file, $data);
135
+    }
136
+
137
+    public function remove($file) {
138
+        $this->rootUnchanged = false;
139
+        parent::remove($file);
140
+    }
141
+
142
+    public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) {
143
+        $this->rootUnchanged = false;
144
+        return parent::moveFromCache($sourceCache, $sourcePath, $targetPath);
145
+    }
146
+
147
+    protected function formatCacheEntry($entry, $path = null) {
148
+        if (is_null($path)) {
149
+            $path = $entry['path'] ?? '';
150
+            $entry['path'] = $this->getJailedPath($path);
151
+        } else {
152
+            $entry['path'] = $path;
153
+        }
154
+
155
+        try {
156
+            if (isset($entry['permissions'])) {
157
+                $entry['permissions'] &= $this->storage->getShare()->getPermissions();
158
+            } else {
159
+                $entry['permissions'] = $this->storage->getPermissions($entry['path']);
160
+            }
161
+        } catch (StorageNotAvailableException $e) {
162
+            // thrown by FailedStorage e.g. when the sharer does not exist anymore
163
+            // (IDE may say the exception is never thrown – false negative)
164
+            $sharePermissions = 0;
165
+        }
166
+        $entry['uid_owner'] = $this->storage->getOwner('');
167
+        $entry['displayname_owner'] = $this->getOwnerDisplayName();
168
+        if ($path === '') {
169
+            $entry['is_share_mount_point'] = true;
170
+        }
171
+        return $entry;
172
+    }
173
+
174
+    private function getOwnerDisplayName() {
175
+        if (!$this->ownerDisplayName) {
176
+            /** @var ICacheFactory $cacheFactory */
177
+            $cacheFactory = \OC::$server->get(ICacheFactory::class);
178
+            $memcache = $cacheFactory->createLocal('share_owner_name');
179
+            $uid = $this->storage->getOwner('');
180
+            $cached = $memcache->get($uid);
181
+            if ($cached) {
182
+                $this->ownerDisplayName = $cached;
183
+            } else {
184
+                $user = $this->userManager->get($uid);
185
+                if ($user) {
186
+                    $this->ownerDisplayName = $user->getDisplayName();
187
+                } else {
188
+                    $this->ownerDisplayName = $uid;
189
+                }
190
+                $memcache->set($uid, $this->ownerDisplayName, 60 * 60);
191
+            }
192
+        }
193
+        return $this->ownerDisplayName;
194
+    }
195
+
196
+    /**
197
+     * remove all entries for files that are stored on the storage from the cache
198
+     */
199
+    public function clear() {
200
+        // Not a valid action for Shared Cache
201
+    }
202
+
203
+    public function getQueryFilterForStorage(): ISearchOperator {
204
+        // Do the normal jail behavior for non files
205
+        if ($this->storage->getItemType() !== 'file') {
206
+            return parent::getQueryFilterForStorage();
207
+        }
208
+
209
+        // for single file shares we don't need to do the LIKE
210
+        return new SearchBinaryOperator(
211
+            ISearchBinaryOperator::OPERATOR_AND,
212
+            [
213
+                \OC\Files\Cache\Cache::getQueryFilterForStorage(),
214
+                new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'path', $this->getGetUnjailedRoot()),
215
+            ]
216
+        );
217
+    }
218 218
 }
Please login to merge, or discard this patch.