2014-05-21 19:41:27 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2014-05-21 19:41:27 -04:00
|
|
|
/**
|
2024-06-06 13:48:28 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-05-21 19:41:27 -04:00
|
|
|
*/
|
2022-04-21 11:43:51 -04:00
|
|
|
|
2014-05-21 19:41:27 -04:00
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
|
2016-04-14 05:50:27 -04:00
|
|
|
use OC\Files\Filesystem;
|
2014-11-24 09:54:42 -05:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2014-05-21 19:41:27 -04:00
|
|
|
use OC\Files\Mount\MoveableMount;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\Files_Sharing\Exceptions\BrokenPath;
|
2020-02-06 11:09:09 -05:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2022-03-24 11:18:34 -04:00
|
|
|
use OCP\Files\Events\InvalidateMountCacheEvent;
|
2018-08-16 15:05:01 -04:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\IDBConnection;
|
2022-03-24 11:18:34 -04:00
|
|
|
use OCP\IUser;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\Server;
|
|
|
|
|
use OCP\Share\IShare;
|
2024-02-08 09:47:39 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-05-21 19:41:27 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-05-28 07:47:50 -04:00
|
|
|
* Shared mount points can be moved by the user
|
2014-05-21 19:41:27 -04:00
|
|
|
*/
|
2023-10-25 12:04:34 -04:00
|
|
|
class SharedMount extends MountPoint implements MoveableMount, ISharedMountPoint {
|
2025-11-20 12:02:21 -05:00
|
|
|
/** @var ?SharedStorage $storage */
|
2014-05-21 19:41:27 -04:00
|
|
|
protected $storage = null;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
/** @var IShare */
|
2016-06-15 03:47:33 -04:00
|
|
|
private $superShare;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
/** @var IShare[] */
|
2016-06-15 03:47:33 -04:00
|
|
|
private $groupedShares;
|
2016-04-15 08:03:48 -04:00
|
|
|
|
2022-03-24 11:18:34 -04:00
|
|
|
public function __construct(
|
|
|
|
|
$storage,
|
|
|
|
|
$arguments,
|
|
|
|
|
IStorageFactory $loader,
|
2024-10-16 04:41:21 -04:00
|
|
|
private IEventDispatcher $eventDispatcher,
|
|
|
|
|
private IUser $user,
|
2022-03-24 11:18:34 -04:00
|
|
|
) {
|
2016-06-15 03:47:33 -04:00
|
|
|
$this->superShare = $arguments['superShare'];
|
|
|
|
|
$this->groupedShares = $arguments['groupedShares'];
|
|
|
|
|
|
2025-04-08 09:36:29 -04:00
|
|
|
$absMountPoint = '/' . $user->getUID() . '/files/' . trim($this->superShare->getTarget(), '/') . '/';
|
|
|
|
|
|
2022-02-02 10:12:57 -05:00
|
|
|
parent::__construct($storage, $absMountPoint, $arguments, $loader, null, null, MountProvider::class);
|
2014-05-27 14:48:41 -04:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 03:01:10 -04:00
|
|
|
/**
|
|
|
|
|
* update fileTarget in the database if the mount point changed
|
|
|
|
|
*
|
|
|
|
|
* @param string $newPath
|
2024-10-18 06:04:22 -04:00
|
|
|
* @param IShare $share
|
2016-04-15 03:01:10 -04:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function updateFileTarget($newPath, &$share) {
|
|
|
|
|
$share->setTarget($newPath);
|
2016-06-15 03:47:33 -04:00
|
|
|
|
2016-12-13 16:28:09 -05:00
|
|
|
foreach ($this->groupedShares as $tmpShare) {
|
|
|
|
|
$tmpShare->setTarget($newPath);
|
2025-02-03 09:34:01 -05:00
|
|
|
Server::get(\OCP\Share\IManager::class)->moveShare($tmpShare, $this->user->getUID());
|
2016-06-15 03:47:33 -04:00
|
|
|
}
|
2022-03-24 11:18:34 -04:00
|
|
|
|
|
|
|
|
$this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent($this->user));
|
2016-04-15 03:01:10 -04:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 19:41:27 -04:00
|
|
|
/**
|
|
|
|
|
* Format a path to be relative to the /user/files/ directory
|
|
|
|
|
*
|
|
|
|
|
* @param string $path the absolute path
|
|
|
|
|
* @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
|
2024-10-10 06:40:31 -04:00
|
|
|
* @throws BrokenPath
|
2014-05-21 19:41:27 -04:00
|
|
|
*/
|
2014-09-29 05:13:01 -04:00
|
|
|
protected function stripUserFilesPath($path) {
|
2014-05-21 19:41:27 -04:00
|
|
|
$trimmed = ltrim($path, '/');
|
|
|
|
|
$split = explode('/', $trimmed);
|
|
|
|
|
|
|
|
|
|
// it is not a file relative to data/user/files
|
|
|
|
|
if (count($split) < 3 || $split[1] !== 'files') {
|
2024-10-10 06:40:31 -04:00
|
|
|
Server::get(LoggerInterface::class)->error('Can not strip userid and "files/" from path: ' . $path, ['app' => 'files_sharing']);
|
|
|
|
|
throw new BrokenPath('Path does not start with /user/files', 10);
|
2014-05-21 19:41:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip 'user' and 'files'
|
|
|
|
|
$sliced = array_slice($split, 2);
|
|
|
|
|
$relPath = implode('/', $sliced);
|
|
|
|
|
|
|
|
|
|
return '/' . $relPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move the mount point to $target
|
|
|
|
|
*
|
|
|
|
|
* @param string $target the target mount point
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function moveMount($target) {
|
|
|
|
|
$relTargetPath = $this->stripUserFilesPath($target);
|
|
|
|
|
$share = $this->storage->getShare();
|
|
|
|
|
|
2014-07-31 05:55:59 -04:00
|
|
|
$result = true;
|
|
|
|
|
|
2016-04-15 03:01:10 -04:00
|
|
|
try {
|
|
|
|
|
$this->updateFileTarget($relTargetPath, $share);
|
2014-05-21 19:41:27 -04:00
|
|
|
$this->setMountPoint($target);
|
|
|
|
|
$this->storage->setMountPoint($relTargetPath);
|
2016-04-15 03:01:10 -04:00
|
|
|
} catch (\Exception $e) {
|
2024-10-10 06:40:31 -04:00
|
|
|
Server::get(LoggerInterface::class)->error(
|
2024-02-08 09:47:39 -05:00
|
|
|
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
|
|
|
|
|
[
|
|
|
|
|
'app' => 'files_sharing',
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]
|
|
|
|
|
);
|
2014-05-21 19:41:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the mount points
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function removeMount() {
|
2024-10-10 06:40:31 -04:00
|
|
|
$mountManager = Filesystem::getMountManager();
|
|
|
|
|
/** @var SharedStorage $storage */
|
2014-05-27 09:07:53 -04:00
|
|
|
$storage = $this->getStorage();
|
2014-07-31 05:55:59 -04:00
|
|
|
$result = $storage->unshareStorage();
|
2014-07-02 08:40:22 -04:00
|
|
|
$mountManager->removeMount($this->mountPoint);
|
2014-05-27 09:07:53 -04:00
|
|
|
|
|
|
|
|
return $result;
|
2014-05-21 19:41:27 -04:00
|
|
|
}
|
2015-03-09 11:20:18 -04:00
|
|
|
|
2015-12-08 03:11:50 -05:00
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @return IShare
|
2015-12-08 03:11:50 -05:00
|
|
|
*/
|
2015-03-09 11:20:18 -04:00
|
|
|
public function getShare() {
|
2016-06-15 03:47:33 -04:00
|
|
|
return $this->superShare;
|
2016-04-15 08:03:48 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-31 12:58:59 -04:00
|
|
|
/**
|
2024-10-18 06:04:22 -04:00
|
|
|
* @return IShare[]
|
2023-03-31 12:58:59 -04:00
|
|
|
*/
|
|
|
|
|
public function getGroupedShares(): array {
|
|
|
|
|
return $this->groupedShares;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 08:03:48 -04:00
|
|
|
/**
|
|
|
|
|
* Get the file id of the root of the storage
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getStorageRootId() {
|
2016-06-15 03:47:33 -04:00
|
|
|
return $this->getShare()->getNodeId();
|
2015-03-09 11:20:18 -04:00
|
|
|
}
|
2016-08-09 09:52:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getNumericStorageId() {
|
2016-12-13 06:30:29 -05:00
|
|
|
if (!is_null($this->getShare()->getNodeCacheEntry())) {
|
|
|
|
|
return $this->getShare()->getNodeCacheEntry()->getStorageId();
|
|
|
|
|
} else {
|
2025-02-03 09:34:01 -05:00
|
|
|
$builder = Server::get(IDBConnection::class)->getQueryBuilder();
|
2016-12-13 06:30:29 -05:00
|
|
|
|
|
|
|
|
$query = $builder->select('storage')
|
|
|
|
|
->from('filecache')
|
|
|
|
|
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId())));
|
|
|
|
|
|
2024-10-16 04:41:21 -04:00
|
|
|
$result = $query->executeQuery();
|
2025-11-17 06:20:11 -05:00
|
|
|
$row = $result->fetchAssociative();
|
2016-12-13 06:30:29 -05:00
|
|
|
$result->closeCursor();
|
|
|
|
|
if ($row) {
|
2017-02-20 22:07:37 -05:00
|
|
|
return (int)$row['storage'];
|
2016-12-13 06:30:29 -05:00
|
|
|
}
|
|
|
|
|
return -1;
|
2016-08-25 12:06:13 -04:00
|
|
|
}
|
2016-08-09 09:52:13 -04:00
|
|
|
}
|
2017-04-26 08:53:11 -04:00
|
|
|
|
|
|
|
|
public function getMountType() {
|
|
|
|
|
return 'shared';
|
|
|
|
|
}
|
2025-12-18 07:34:00 -05:00
|
|
|
|
|
|
|
|
public function getUser(): IUser {
|
|
|
|
|
return $this->user;
|
|
|
|
|
}
|
2014-05-21 19:41:27 -04:00
|
|
|
}
|