1 | <?php |
||
41 | class SharedMount extends MountPoint implements MoveableMount { |
||
42 | /** |
||
43 | * @var \OCA\Files_Sharing\SharedStorage $storage |
||
44 | */ |
||
45 | protected $storage = null; |
||
46 | |||
47 | /** |
||
48 | * @var \OC\Files\View |
||
49 | */ |
||
50 | private $recipientView; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $user; |
||
56 | |||
57 | /** @var \OCP\Share\IShare */ |
||
58 | private $superShare; |
||
59 | |||
60 | /** @var \OCP\Share\IShare[] */ |
||
61 | private $groupedShares; |
||
62 | |||
63 | /** |
||
64 | * @param string $storage |
||
65 | * @param SharedMount[] $mountpoints |
||
66 | * @param array $arguments |
||
67 | * @param IStorageFactory $loader |
||
68 | * @param View $recipientView |
||
69 | */ |
||
70 | public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) { |
||
71 | $this->user = $arguments['user']; |
||
72 | $this->recipientView = $recipientView; |
||
73 | |||
74 | $this->superShare = $arguments['superShare']; |
||
75 | $this->groupedShares = $arguments['groupedShares']; |
||
76 | |||
77 | $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); |
||
78 | $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; |
||
79 | parent::__construct($storage, $absMountPoint, $arguments, $loader); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * check if the parent folder exists otherwise move the mount point up |
||
84 | * |
||
85 | * @param \OCP\Share\IShare $share |
||
86 | * @param SharedMount[] $mountpoints |
||
87 | * @return string |
||
88 | */ |
||
89 | private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) { |
||
90 | |||
91 | $mountPoint = basename($share->getTarget()); |
||
92 | $parent = dirname($share->getTarget()); |
||
93 | |||
94 | if ($folderExistCache->hasKey($parent)) { |
||
95 | $parentExists = $folderExistCache->get($parent); |
||
96 | } else { |
||
97 | $parentExists = $this->recipientView->is_dir($parent); |
||
98 | $folderExistCache->set($parent, $parentExists); |
||
99 | } |
||
100 | if (!$parentExists) { |
||
101 | $parent = Helper::getShareFolder($this->recipientView); |
||
102 | } |
||
103 | |||
104 | $newMountPoint = $this->generateUniqueTarget( |
||
105 | \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
||
106 | $this->recipientView, |
||
107 | $mountpoints |
||
108 | ); |
||
109 | |||
110 | if ($newMountPoint !== $share->getTarget()) { |
||
111 | $this->updateFileTarget($newMountPoint, $share); |
||
112 | } |
||
113 | |||
114 | return $newMountPoint; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * update fileTarget in the database if the mount point changed |
||
119 | * |
||
120 | * @param string $newPath |
||
121 | * @param \OCP\Share\IShare $share |
||
122 | * @return bool |
||
123 | */ |
||
124 | private function updateFileTarget($newPath, &$share) { |
||
125 | $share->setTarget($newPath); |
||
126 | |||
127 | foreach ($this->groupedShares as $tmpShare) { |
||
128 | $tmpShare->setTarget($newPath); |
||
129 | \OC::$server->getShareManager()->moveShare($tmpShare, $this->user); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | |||
134 | /** |
||
135 | * @param string $path |
||
136 | * @param View $view |
||
137 | * @param SharedMount[] $mountpoints |
||
138 | * @return mixed |
||
139 | */ |
||
140 | private function generateUniqueTarget($path, $view, array $mountpoints) { |
||
141 | $pathinfo = pathinfo($path); |
||
142 | $ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : ''; |
||
143 | $name = $pathinfo['filename']; |
||
144 | $dir = $pathinfo['dirname']; |
||
145 | |||
146 | $i = 2; |
||
147 | $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
||
148 | while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { |
||
149 | $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); |
||
150 | $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
||
151 | $i++; |
||
152 | } |
||
153 | |||
154 | return $path; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Format a path to be relative to the /user/files/ directory |
||
159 | * |
||
160 | * @param string $path the absolute path |
||
161 | * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
||
162 | * @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
||
163 | */ |
||
164 | protected function stripUserFilesPath($path) { |
||
182 | |||
183 | /** |
||
184 | * Move the mount point to $target |
||
185 | * |
||
186 | * @param string $target the target mount point |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function moveMount($target) { |
||
208 | |||
209 | /** |
||
210 | * Remove the mount points |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function removeMount() { |
||
223 | |||
224 | /** |
||
225 | * @return \OCP\Share\IShare |
||
226 | */ |
||
227 | public function getShare() { |
||
230 | |||
231 | /** |
||
232 | * Get the file id of the root of the storage |
||
233 | * |
||
234 | * @return int |
||
235 | */ |
||
236 | public function getStorageRootId() { |
||
239 | |||
240 | /** |
||
241 | * @return int |
||
242 | */ |
||
243 | public function getNumericStorageId() { |
||
262 | |||
263 | public function getMountType() { |
||
266 | } |
||
267 |