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