1<?php
2
3declare(strict_types=1);
4
5
6/**
7 * Circles - Bring cloud-users closer together.
8 *
9 * This file is licensed under the Affero General Public License version 3 or
10 * later. See the COPYING file.
11 *
12 * @author Maxence Lange <maxence@artificial-owl.com>
13 * @copyright 2020
14 * @license GNU AGPL version 3 or any later version
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as
18 * published by the Free Software Foundation, either version 3 of the
19 * License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU Affero General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31
32namespace OCA\Circles\GlobalScale\GSMount;
33
34use OCA\Circles\Db\GSSharesRequest;
35use OCA\Circles\Model\GlobalScale\GSShareMountpoint;
36use OCP\Share\Exceptions\ShareNotFound;
37
38/**
39 * Class MountManager
40 *
41 * @package OCA\Circles\GlobalScale\GSMount
42 */
43class MountManager {
44
45
46	/** @var string */
47	private $userId;
48
49	/** @var GSSharesRequest */
50	private $gsSharesRequest;
51
52
53	/**
54	 * MountManager constructor.
55	 *
56	 * @param string $userId
57	 * @param GSSharesRequest $gsSharesRequest
58	 */
59	public function __construct($userId, GSSharesRequest $gsSharesRequest) {
60		$this->userId = $userId;
61		$this->gsSharesRequest = $gsSharesRequest;
62	}
63
64
65	/**
66	 * @param int $gsShareId
67	 * @param string $target
68	 *
69	 * @return bool
70	 */
71	public function renameShare(int $gsShareId, string $target) {
72		try {
73			if ($target !== '-') {
74				$target = $this->stripPath($target);
75				$this->gsSharesRequest->getShareMountPointByPath($this->userId, $target);
76
77				return false;
78			}
79		} catch (ShareNotFound $e) {
80		}
81
82		$mountPoint = new GSShareMountpoint($gsShareId, $this->userId, $target);
83		try {
84			$this->gsSharesRequest->getShareMountPointById($gsShareId, $this->userId);
85			$this->gsSharesRequest->updateShareMountPoint($mountPoint);
86		} catch (ShareNotFound $e) {
87			$this->gsSharesRequest->generateShareMountPoint($mountPoint);
88		}
89
90		return true;
91	}
92
93
94	// TODO: implement !
95	public function getMountManager() {
96		return $this;
97	}
98
99	// TODO: implement !
100	public function removeShare($mountPoint) {
101	}
102
103	// TODO: implement !
104	public function removeMount($mountPoint) {
105	}
106
107
108	/**
109	 * @param int $gsShareId
110	 *
111	 * @return bool
112	 */
113	public function unshare(int $gsShareId) {
114		return $this->renameShare($gsShareId, '-');
115	}
116
117
118	/**
119	 * remove '/user/files' from the path and trailing slashes
120	 *
121	 * @param string $path
122	 *
123	 * @return string
124	 */
125	protected function stripPath($path) {
126		$prefix = '/' . $this->userId . '/files';
127
128		return rtrim(substr($path, strlen($prefix)), '/');
129	}
130}
131