Completed
Push — master ( b12b38...e67fe8 )
by
unknown
42:17
created
apps/dav/lib/Connector/Sabre/Node.php 1 patch
Indentation   +355 added lines, -355 removed lines patch added patch discarded remove patch
@@ -25,364 +25,364 @@
 block discarded – undo
25 25
 use OCP\Share\IManager;
26 26
 
27 27
 abstract class Node implements \Sabre\DAV\INode {
28
-	/**
29
-	 * The path to the current node
30
-	 *
31
-	 * @var string
32
-	 */
33
-	protected $path;
34
-
35
-	protected FileInfo $info;
36
-
37
-	/**
38
-	 * @var IManager
39
-	 */
40
-	protected $shareManager;
41
-
42
-	protected \OCP\Files\Node $node;
43
-
44
-	/**
45
-	 * Sets up the node, expects a full path name
46
-	 */
47
-	public function __construct(
48
-		protected View $fileView,
49
-		FileInfo $info,
50
-		?IManager $shareManager = null,
51
-	) {
52
-		$this->path = $this->fileView->getRelativePath($info->getPath());
53
-		$this->info = $info;
54
-		if ($shareManager) {
55
-			$this->shareManager = $shareManager;
56
-		} else {
57
-			$this->shareManager = Server::get(\OCP\Share\IManager::class);
58
-		}
59
-		if ($info instanceof Folder || $info instanceof File) {
60
-			$this->node = $info;
61
-		} else {
62
-			// The Node API assumes that the view passed doesn't have a fake root
63
-			$rootView = Server::get(View::class);
64
-			$root = Server::get(IRootFolder::class);
65
-			if ($info->getType() === FileInfo::TYPE_FOLDER) {
66
-				$this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
67
-			} else {
68
-				$this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
69
-			}
70
-		}
71
-	}
72
-
73
-	protected function refreshInfo(): void {
74
-		$info = $this->fileView->getFileInfo($this->path);
75
-		if ($info === false) {
76
-			throw new \Sabre\DAV\Exception('Failed to get fileinfo for ' . $this->path);
77
-		}
78
-		$this->info = $info;
79
-		$root = Server::get(IRootFolder::class);
80
-		$rootView = Server::get(View::class);
81
-		if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
82
-			$this->node = new Folder($root, $rootView, $this->path, $this->info);
83
-		} else {
84
-			$this->node = new File($root, $rootView, $this->path, $this->info);
85
-		}
86
-	}
87
-
88
-	/**
89
-	 *  Returns the name of the node
90
-	 *
91
-	 * @return string
92
-	 */
93
-	public function getName() {
94
-		return $this->info->getName();
95
-	}
96
-
97
-	/**
98
-	 * Returns the full path
99
-	 *
100
-	 * @return string
101
-	 */
102
-	public function getPath() {
103
-		return $this->path;
104
-	}
105
-
106
-	/**
107
-	 * Renames the node
108
-	 *
109
-	 * @param string $name The new name
110
-	 * @throws \Sabre\DAV\Exception\BadRequest
111
-	 * @throws \Sabre\DAV\Exception\Forbidden
112
-	 */
113
-	public function setName($name) {
114
-		// rename is only allowed if the delete privilege is granted
115
-		// (basically rename is a copy with delete of the original node)
116
-		if (!($this->info->isDeletable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) {
117
-			throw new \Sabre\DAV\Exception\Forbidden();
118
-		}
119
-
120
-		[$parentPath,] = \Sabre\Uri\split($this->path);
121
-		[, $newName] = \Sabre\Uri\split($name);
122
-		$newPath = $parentPath . '/' . $newName;
123
-
124
-		// verify path of the target
125
-		$this->verifyPath($newPath);
126
-
127
-		if (!$this->fileView->rename($this->path, $newPath)) {
128
-			throw new \Sabre\DAV\Exception('Failed to rename ' . $this->path . ' to ' . $newPath);
129
-		}
130
-
131
-		$this->path = $newPath;
132
-
133
-		$this->refreshInfo();
134
-	}
135
-
136
-	/**
137
-	 * Returns the last modification time, as a unix timestamp
138
-	 *
139
-	 * @return int timestamp as integer
140
-	 */
141
-	public function getLastModified() {
142
-		$timestamp = $this->info->getMtime();
143
-		if (!empty($timestamp)) {
144
-			return (int)$timestamp;
145
-		}
146
-		return $timestamp;
147
-	}
148
-
149
-	/**
150
-	 *  sets the last modification time of the file (mtime) to the value given
151
-	 *  in the second parameter or to now if the second param is empty.
152
-	 *  Even if the modification time is set to a custom value the access time is set to now.
153
-	 */
154
-	public function touch($mtime) {
155
-		$mtime = $this->sanitizeMtime($mtime);
156
-		$this->fileView->touch($this->path, $mtime);
157
-		$this->refreshInfo();
158
-	}
159
-
160
-	/**
161
-	 * Returns the ETag for a file
162
-	 *
163
-	 * An ETag is a unique identifier representing the current version of the
164
-	 * file. If the file changes, the ETag MUST change.  The ETag is an
165
-	 * arbitrary string, but MUST be surrounded by double-quotes.
166
-	 *
167
-	 * Return null if the ETag can not effectively be determined
168
-	 *
169
-	 * @return string
170
-	 */
171
-	public function getETag() {
172
-		return '"' . $this->info->getEtag() . '"';
173
-	}
174
-
175
-	/**
176
-	 * Sets the ETag
177
-	 *
178
-	 * @param string $etag
179
-	 *
180
-	 * @return int file id of updated file or -1 on failure
181
-	 */
182
-	public function setETag($etag) {
183
-		return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
184
-	}
185
-
186
-	public function setCreationTime(int $time) {
187
-		return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
188
-	}
189
-
190
-	public function setUploadTime(int $time) {
191
-		return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
192
-	}
193
-
194
-	/**
195
-	 * Returns the size of the node, in bytes
196
-	 *
197
-	 * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
198
-	 * @return int|float
199
-	 */
200
-	public function getSize(): int|float {
201
-		return $this->info->getSize();
202
-	}
203
-
204
-	/**
205
-	 * Returns the cache's file id
206
-	 *
207
-	 * @return int
208
-	 */
209
-	public function getId() {
210
-		return $this->info->getId();
211
-	}
212
-
213
-	/**
214
-	 * @return string|null
215
-	 */
216
-	public function getFileId() {
217
-		if ($id = $this->info->getId()) {
218
-			return DavUtil::getDavFileId($id);
219
-		}
220
-
221
-		return null;
222
-	}
223
-
224
-	/**
225
-	 * @return integer
226
-	 */
227
-	public function getInternalFileId() {
228
-		return $this->info->getId();
229
-	}
230
-
231
-	public function getInternalPath(): string {
232
-		return $this->info->getInternalPath();
233
-	}
234
-
235
-	/**
236
-	 * @param string $user
237
-	 * @return int
238
-	 */
239
-	public function getSharePermissions($user) {
240
-		// check of we access a federated share
241
-		if ($user !== null) {
242
-			try {
243
-				$share = $this->shareManager->getShareByToken($user);
244
-				return $share->getPermissions();
245
-			} catch (ShareNotFound $e) {
246
-				// ignore
247
-			}
248
-		}
249
-
250
-		try {
251
-			$storage = $this->info->getStorage();
252
-		} catch (StorageNotAvailableException $e) {
253
-			$storage = null;
254
-		}
255
-
256
-		if ($storage && $storage->instanceOfStorage(ISharedStorage::class)) {
257
-			/** @var ISharedStorage $storage */
258
-			$permissions = (int)$storage->getShare()->getPermissions();
259
-		} else {
260
-			$permissions = $this->info->getPermissions();
261
-		}
262
-
263
-		/*
28
+    /**
29
+     * The path to the current node
30
+     *
31
+     * @var string
32
+     */
33
+    protected $path;
34
+
35
+    protected FileInfo $info;
36
+
37
+    /**
38
+     * @var IManager
39
+     */
40
+    protected $shareManager;
41
+
42
+    protected \OCP\Files\Node $node;
43
+
44
+    /**
45
+     * Sets up the node, expects a full path name
46
+     */
47
+    public function __construct(
48
+        protected View $fileView,
49
+        FileInfo $info,
50
+        ?IManager $shareManager = null,
51
+    ) {
52
+        $this->path = $this->fileView->getRelativePath($info->getPath());
53
+        $this->info = $info;
54
+        if ($shareManager) {
55
+            $this->shareManager = $shareManager;
56
+        } else {
57
+            $this->shareManager = Server::get(\OCP\Share\IManager::class);
58
+        }
59
+        if ($info instanceof Folder || $info instanceof File) {
60
+            $this->node = $info;
61
+        } else {
62
+            // The Node API assumes that the view passed doesn't have a fake root
63
+            $rootView = Server::get(View::class);
64
+            $root = Server::get(IRootFolder::class);
65
+            if ($info->getType() === FileInfo::TYPE_FOLDER) {
66
+                $this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
67
+            } else {
68
+                $this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
69
+            }
70
+        }
71
+    }
72
+
73
+    protected function refreshInfo(): void {
74
+        $info = $this->fileView->getFileInfo($this->path);
75
+        if ($info === false) {
76
+            throw new \Sabre\DAV\Exception('Failed to get fileinfo for ' . $this->path);
77
+        }
78
+        $this->info = $info;
79
+        $root = Server::get(IRootFolder::class);
80
+        $rootView = Server::get(View::class);
81
+        if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
82
+            $this->node = new Folder($root, $rootView, $this->path, $this->info);
83
+        } else {
84
+            $this->node = new File($root, $rootView, $this->path, $this->info);
85
+        }
86
+    }
87
+
88
+    /**
89
+     *  Returns the name of the node
90
+     *
91
+     * @return string
92
+     */
93
+    public function getName() {
94
+        return $this->info->getName();
95
+    }
96
+
97
+    /**
98
+     * Returns the full path
99
+     *
100
+     * @return string
101
+     */
102
+    public function getPath() {
103
+        return $this->path;
104
+    }
105
+
106
+    /**
107
+     * Renames the node
108
+     *
109
+     * @param string $name The new name
110
+     * @throws \Sabre\DAV\Exception\BadRequest
111
+     * @throws \Sabre\DAV\Exception\Forbidden
112
+     */
113
+    public function setName($name) {
114
+        // rename is only allowed if the delete privilege is granted
115
+        // (basically rename is a copy with delete of the original node)
116
+        if (!($this->info->isDeletable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) {
117
+            throw new \Sabre\DAV\Exception\Forbidden();
118
+        }
119
+
120
+        [$parentPath,] = \Sabre\Uri\split($this->path);
121
+        [, $newName] = \Sabre\Uri\split($name);
122
+        $newPath = $parentPath . '/' . $newName;
123
+
124
+        // verify path of the target
125
+        $this->verifyPath($newPath);
126
+
127
+        if (!$this->fileView->rename($this->path, $newPath)) {
128
+            throw new \Sabre\DAV\Exception('Failed to rename ' . $this->path . ' to ' . $newPath);
129
+        }
130
+
131
+        $this->path = $newPath;
132
+
133
+        $this->refreshInfo();
134
+    }
135
+
136
+    /**
137
+     * Returns the last modification time, as a unix timestamp
138
+     *
139
+     * @return int timestamp as integer
140
+     */
141
+    public function getLastModified() {
142
+        $timestamp = $this->info->getMtime();
143
+        if (!empty($timestamp)) {
144
+            return (int)$timestamp;
145
+        }
146
+        return $timestamp;
147
+    }
148
+
149
+    /**
150
+     *  sets the last modification time of the file (mtime) to the value given
151
+     *  in the second parameter or to now if the second param is empty.
152
+     *  Even if the modification time is set to a custom value the access time is set to now.
153
+     */
154
+    public function touch($mtime) {
155
+        $mtime = $this->sanitizeMtime($mtime);
156
+        $this->fileView->touch($this->path, $mtime);
157
+        $this->refreshInfo();
158
+    }
159
+
160
+    /**
161
+     * Returns the ETag for a file
162
+     *
163
+     * An ETag is a unique identifier representing the current version of the
164
+     * file. If the file changes, the ETag MUST change.  The ETag is an
165
+     * arbitrary string, but MUST be surrounded by double-quotes.
166
+     *
167
+     * Return null if the ETag can not effectively be determined
168
+     *
169
+     * @return string
170
+     */
171
+    public function getETag() {
172
+        return '"' . $this->info->getEtag() . '"';
173
+    }
174
+
175
+    /**
176
+     * Sets the ETag
177
+     *
178
+     * @param string $etag
179
+     *
180
+     * @return int file id of updated file or -1 on failure
181
+     */
182
+    public function setETag($etag) {
183
+        return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
184
+    }
185
+
186
+    public function setCreationTime(int $time) {
187
+        return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
188
+    }
189
+
190
+    public function setUploadTime(int $time) {
191
+        return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
192
+    }
193
+
194
+    /**
195
+     * Returns the size of the node, in bytes
196
+     *
197
+     * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
198
+     * @return int|float
199
+     */
200
+    public function getSize(): int|float {
201
+        return $this->info->getSize();
202
+    }
203
+
204
+    /**
205
+     * Returns the cache's file id
206
+     *
207
+     * @return int
208
+     */
209
+    public function getId() {
210
+        return $this->info->getId();
211
+    }
212
+
213
+    /**
214
+     * @return string|null
215
+     */
216
+    public function getFileId() {
217
+        if ($id = $this->info->getId()) {
218
+            return DavUtil::getDavFileId($id);
219
+        }
220
+
221
+        return null;
222
+    }
223
+
224
+    /**
225
+     * @return integer
226
+     */
227
+    public function getInternalFileId() {
228
+        return $this->info->getId();
229
+    }
230
+
231
+    public function getInternalPath(): string {
232
+        return $this->info->getInternalPath();
233
+    }
234
+
235
+    /**
236
+     * @param string $user
237
+     * @return int
238
+     */
239
+    public function getSharePermissions($user) {
240
+        // check of we access a federated share
241
+        if ($user !== null) {
242
+            try {
243
+                $share = $this->shareManager->getShareByToken($user);
244
+                return $share->getPermissions();
245
+            } catch (ShareNotFound $e) {
246
+                // ignore
247
+            }
248
+        }
249
+
250
+        try {
251
+            $storage = $this->info->getStorage();
252
+        } catch (StorageNotAvailableException $e) {
253
+            $storage = null;
254
+        }
255
+
256
+        if ($storage && $storage->instanceOfStorage(ISharedStorage::class)) {
257
+            /** @var ISharedStorage $storage */
258
+            $permissions = (int)$storage->getShare()->getPermissions();
259
+        } else {
260
+            $permissions = $this->info->getPermissions();
261
+        }
262
+
263
+        /*
264 264
 		 * We can always share non moveable mount points with DELETE and UPDATE
265 265
 		 * Eventually we need to do this properly
266 266
 		 */
267
-		$mountpoint = $this->info->getMountPoint();
268
-		if (!($mountpoint instanceof MoveableMount)) {
269
-			$mountpointpath = $mountpoint->getMountPoint();
270
-			if (str_ends_with($mountpointpath, '/')) {
271
-				$mountpointpath = substr($mountpointpath, 0, -1);
272
-			}
273
-
274
-			if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
275
-				$permissions |= Constants::PERMISSION_DELETE | Constants::PERMISSION_UPDATE;
276
-			}
277
-		}
278
-
279
-		/*
267
+        $mountpoint = $this->info->getMountPoint();
268
+        if (!($mountpoint instanceof MoveableMount)) {
269
+            $mountpointpath = $mountpoint->getMountPoint();
270
+            if (str_ends_with($mountpointpath, '/')) {
271
+                $mountpointpath = substr($mountpointpath, 0, -1);
272
+            }
273
+
274
+            if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
275
+                $permissions |= Constants::PERMISSION_DELETE | Constants::PERMISSION_UPDATE;
276
+            }
277
+        }
278
+
279
+        /*
280 280
 		 * Files can't have create or delete permissions
281 281
 		 */
282
-		if ($this->info->getType() === FileInfo::TYPE_FILE) {
283
-			$permissions &= ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE);
284
-		}
285
-
286
-		return $permissions;
287
-	}
288
-
289
-	/**
290
-	 * @return array
291
-	 */
292
-	public function getShareAttributes(): array {
293
-		try {
294
-			$storage = $this->node->getStorage();
295
-		} catch (NotFoundException $e) {
296
-			return [];
297
-		}
298
-
299
-		$attributes = [];
300
-		if ($storage->instanceOfStorage(ISharedStorage::class)) {
301
-			/** @var ISharedStorage $storage */
302
-			$attributes = $storage->getShare()->getAttributes();
303
-			if ($attributes === null) {
304
-				return [];
305
-			} else {
306
-				return $attributes->toArray();
307
-			}
308
-		}
309
-
310
-		return $attributes;
311
-	}
312
-
313
-	public function getNoteFromShare(?string $user): ?string {
314
-		try {
315
-			$storage = $this->node->getStorage();
316
-		} catch (NotFoundException) {
317
-			return null;
318
-		}
319
-
320
-		if ($storage->instanceOfStorage(ISharedStorage::class)) {
321
-			/** @var ISharedStorage $storage */
322
-			$share = $storage->getShare();
323
-			if ($user === $share->getShareOwner()) {
324
-				// Note is only for recipient not the owner
325
-				return null;
326
-			}
327
-			return $share->getNote();
328
-		}
329
-
330
-		return null;
331
-	}
332
-
333
-	/**
334
-	 * @return string
335
-	 */
336
-	public function getDavPermissions() {
337
-		return DavUtil::getDavPermissions($this->info);
338
-	}
339
-
340
-	public function getOwner() {
341
-		return $this->info->getOwner();
342
-	}
343
-
344
-	protected function verifyPath(?string $path = null): void {
345
-		try {
346
-			$path = $path ?? $this->info->getPath();
347
-			$this->fileView->verifyPath(
348
-				dirname($path),
349
-				basename($path),
350
-			);
351
-		} catch (InvalidPathException $ex) {
352
-			throw new InvalidPath($ex->getMessage());
353
-		}
354
-	}
355
-
356
-	/**
357
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
358
-	 */
359
-	public function acquireLock($type) {
360
-		$this->fileView->lockFile($this->path, $type);
361
-	}
362
-
363
-	/**
364
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
365
-	 */
366
-	public function releaseLock($type) {
367
-		$this->fileView->unlockFile($this->path, $type);
368
-	}
369
-
370
-	/**
371
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
372
-	 */
373
-	public function changeLock($type) {
374
-		$this->fileView->changeLock($this->path, $type);
375
-	}
376
-
377
-	public function getFileInfo() {
378
-		return $this->info;
379
-	}
380
-
381
-	public function getNode(): \OCP\Files\Node {
382
-		return $this->node;
383
-	}
384
-
385
-	protected function sanitizeMtime(string $mtimeFromRequest): int {
386
-		return MtimeSanitizer::sanitizeMtime($mtimeFromRequest);
387
-	}
282
+        if ($this->info->getType() === FileInfo::TYPE_FILE) {
283
+            $permissions &= ~(Constants::PERMISSION_CREATE | Constants::PERMISSION_DELETE);
284
+        }
285
+
286
+        return $permissions;
287
+    }
288
+
289
+    /**
290
+     * @return array
291
+     */
292
+    public function getShareAttributes(): array {
293
+        try {
294
+            $storage = $this->node->getStorage();
295
+        } catch (NotFoundException $e) {
296
+            return [];
297
+        }
298
+
299
+        $attributes = [];
300
+        if ($storage->instanceOfStorage(ISharedStorage::class)) {
301
+            /** @var ISharedStorage $storage */
302
+            $attributes = $storage->getShare()->getAttributes();
303
+            if ($attributes === null) {
304
+                return [];
305
+            } else {
306
+                return $attributes->toArray();
307
+            }
308
+        }
309
+
310
+        return $attributes;
311
+    }
312
+
313
+    public function getNoteFromShare(?string $user): ?string {
314
+        try {
315
+            $storage = $this->node->getStorage();
316
+        } catch (NotFoundException) {
317
+            return null;
318
+        }
319
+
320
+        if ($storage->instanceOfStorage(ISharedStorage::class)) {
321
+            /** @var ISharedStorage $storage */
322
+            $share = $storage->getShare();
323
+            if ($user === $share->getShareOwner()) {
324
+                // Note is only for recipient not the owner
325
+                return null;
326
+            }
327
+            return $share->getNote();
328
+        }
329
+
330
+        return null;
331
+    }
332
+
333
+    /**
334
+     * @return string
335
+     */
336
+    public function getDavPermissions() {
337
+        return DavUtil::getDavPermissions($this->info);
338
+    }
339
+
340
+    public function getOwner() {
341
+        return $this->info->getOwner();
342
+    }
343
+
344
+    protected function verifyPath(?string $path = null): void {
345
+        try {
346
+            $path = $path ?? $this->info->getPath();
347
+            $this->fileView->verifyPath(
348
+                dirname($path),
349
+                basename($path),
350
+            );
351
+        } catch (InvalidPathException $ex) {
352
+            throw new InvalidPath($ex->getMessage());
353
+        }
354
+    }
355
+
356
+    /**
357
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
358
+     */
359
+    public function acquireLock($type) {
360
+        $this->fileView->lockFile($this->path, $type);
361
+    }
362
+
363
+    /**
364
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
365
+     */
366
+    public function releaseLock($type) {
367
+        $this->fileView->unlockFile($this->path, $type);
368
+    }
369
+
370
+    /**
371
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
372
+     */
373
+    public function changeLock($type) {
374
+        $this->fileView->changeLock($this->path, $type);
375
+    }
376
+
377
+    public function getFileInfo() {
378
+        return $this->info;
379
+    }
380
+
381
+    public function getNode(): \OCP\Files\Node {
382
+        return $this->node;
383
+    }
384
+
385
+    protected function sanitizeMtime(string $mtimeFromRequest): int {
386
+        return MtimeSanitizer::sanitizeMtime($mtimeFromRequest);
387
+    }
388 388
 }
Please login to merge, or discard this patch.