@@ -66,538 +66,538 @@ |
||
66 | 66 | |
67 | 67 | class File extends Node implements IFile { |
68 | 68 | |
69 | - protected $request; |
|
70 | - |
|
71 | - /** |
|
72 | - * Sets up the node, expects a full path name |
|
73 | - * |
|
74 | - * @param \OC\Files\View $view |
|
75 | - * @param \OCP\Files\FileInfo $info |
|
76 | - * @param \OCP\Share\IManager $shareManager |
|
77 | - * @param \OC\AppFramework\Http\Request $request |
|
78 | - */ |
|
79 | - public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
|
80 | - parent::__construct($view, $info, $shareManager); |
|
81 | - |
|
82 | - if (isset($request)) { |
|
83 | - $this->request = $request; |
|
84 | - } else { |
|
85 | - $this->request = \OC::$server->getRequest(); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Updates the data |
|
91 | - * |
|
92 | - * The data argument is a readable stream resource. |
|
93 | - * |
|
94 | - * After a successful put operation, you may choose to return an ETag. The |
|
95 | - * etag must always be surrounded by double-quotes. These quotes must |
|
96 | - * appear in the actual string you're returning. |
|
97 | - * |
|
98 | - * Clients may use the ETag from a PUT request to later on make sure that |
|
99 | - * when they update the file, the contents haven't changed in the mean |
|
100 | - * time. |
|
101 | - * |
|
102 | - * If you don't plan to store the file byte-by-byte, and you return a |
|
103 | - * different object on a subsequent GET you are strongly recommended to not |
|
104 | - * return an ETag, and just return null. |
|
105 | - * |
|
106 | - * @param resource $data |
|
107 | - * |
|
108 | - * @throws Forbidden |
|
109 | - * @throws UnsupportedMediaType |
|
110 | - * @throws BadRequest |
|
111 | - * @throws Exception |
|
112 | - * @throws EntityTooLarge |
|
113 | - * @throws ServiceUnavailable |
|
114 | - * @throws FileLocked |
|
115 | - * @return string|null |
|
116 | - */ |
|
117 | - public function put($data) { |
|
118 | - try { |
|
119 | - $exists = $this->fileView->file_exists($this->path); |
|
120 | - if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
121 | - throw new Forbidden(); |
|
122 | - } |
|
123 | - } catch (StorageNotAvailableException $e) { |
|
124 | - throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
125 | - } |
|
126 | - |
|
127 | - // verify path of the target |
|
128 | - $this->verifyPath(); |
|
129 | - |
|
130 | - // chunked handling |
|
131 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
132 | - try { |
|
133 | - return $this->createFileChunked($data); |
|
134 | - } catch (\Exception $e) { |
|
135 | - $this->convertToSabreException($e); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - /** @var Storage $partStorage */ |
|
140 | - list($partStorage) = $this->fileView->resolvePath($this->path); |
|
141 | - $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
|
142 | - |
|
143 | - if ($needsPartFile) { |
|
144 | - // mark file as partial while uploading (ignored by the scanner) |
|
145 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
146 | - } else { |
|
147 | - // upload file directly as the final path |
|
148 | - $partFilePath = $this->path; |
|
149 | - |
|
150 | - $this->emitPreHooks($exists); |
|
151 | - } |
|
152 | - |
|
153 | - $view = \OC\Files\Filesystem::getView(); |
|
154 | - |
|
155 | - // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
156 | - /** @var \OC\Files\Storage\Storage $partStorage */ |
|
157 | - list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
158 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
159 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
160 | - try { |
|
161 | - if (!$needsPartFile) { |
|
162 | - if ($view) { |
|
163 | - $this->emitPreHooks($exists); |
|
164 | - } |
|
165 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
166 | - } |
|
167 | - |
|
168 | - $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
169 | - if ($target === false) { |
|
170 | - \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
|
171 | - // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
172 | - throw new Exception('Could not write file contents'); |
|
173 | - } |
|
174 | - list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
175 | - fclose($target); |
|
176 | - |
|
177 | - if ($result === false) { |
|
178 | - $expected = -1; |
|
179 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
180 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
181 | - } |
|
182 | - throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
183 | - } |
|
184 | - |
|
185 | - // if content length is sent by client: |
|
186 | - // double check if the file was fully received |
|
187 | - // compare expected and actual size |
|
188 | - if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
189 | - $expected = (int) $_SERVER['CONTENT_LENGTH']; |
|
190 | - if ($count !== $expected) { |
|
191 | - throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - } catch (\Exception $e) { |
|
196 | - if ($needsPartFile) { |
|
197 | - $partStorage->unlink($internalPartPath); |
|
198 | - } |
|
199 | - $this->convertToSabreException($e); |
|
200 | - } |
|
201 | - |
|
202 | - try { |
|
203 | - if ($needsPartFile) { |
|
204 | - if ($view) { |
|
205 | - $this->emitPreHooks($exists); |
|
206 | - } |
|
207 | - try { |
|
208 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
209 | - } catch (LockedException $e) { |
|
210 | - if ($needsPartFile) { |
|
211 | - $partStorage->unlink($internalPartPath); |
|
212 | - } |
|
213 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - if ($needsPartFile) { |
|
218 | - // rename to correct path |
|
219 | - try { |
|
220 | - $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
221 | - $fileExists = $storage->file_exists($internalPath); |
|
222 | - if ($renameOkay === false || $fileExists === false) { |
|
223 | - \OC::$server->getLogger()->error('renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', ['app' => 'webdav']); |
|
224 | - throw new Exception('Could not rename part file to final file'); |
|
225 | - } |
|
226 | - } catch (ForbiddenException $ex) { |
|
227 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
228 | - } catch (\Exception $e) { |
|
229 | - $partStorage->unlink($internalPartPath); |
|
230 | - $this->convertToSabreException($e); |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
235 | - $storage->getUpdater()->update($internalPath); |
|
236 | - |
|
237 | - try { |
|
238 | - $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
239 | - } catch (LockedException $e) { |
|
240 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
241 | - } |
|
242 | - |
|
243 | - // allow sync clients to send the mtime along in a header |
|
244 | - if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
245 | - $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
246 | - if ($this->fileView->touch($this->path, $mtime)) { |
|
247 | - $this->header('X-OC-MTime: accepted'); |
|
248 | - } |
|
249 | - } |
|
69 | + protected $request; |
|
70 | + |
|
71 | + /** |
|
72 | + * Sets up the node, expects a full path name |
|
73 | + * |
|
74 | + * @param \OC\Files\View $view |
|
75 | + * @param \OCP\Files\FileInfo $info |
|
76 | + * @param \OCP\Share\IManager $shareManager |
|
77 | + * @param \OC\AppFramework\Http\Request $request |
|
78 | + */ |
|
79 | + public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
|
80 | + parent::__construct($view, $info, $shareManager); |
|
81 | + |
|
82 | + if (isset($request)) { |
|
83 | + $this->request = $request; |
|
84 | + } else { |
|
85 | + $this->request = \OC::$server->getRequest(); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Updates the data |
|
91 | + * |
|
92 | + * The data argument is a readable stream resource. |
|
93 | + * |
|
94 | + * After a successful put operation, you may choose to return an ETag. The |
|
95 | + * etag must always be surrounded by double-quotes. These quotes must |
|
96 | + * appear in the actual string you're returning. |
|
97 | + * |
|
98 | + * Clients may use the ETag from a PUT request to later on make sure that |
|
99 | + * when they update the file, the contents haven't changed in the mean |
|
100 | + * time. |
|
101 | + * |
|
102 | + * If you don't plan to store the file byte-by-byte, and you return a |
|
103 | + * different object on a subsequent GET you are strongly recommended to not |
|
104 | + * return an ETag, and just return null. |
|
105 | + * |
|
106 | + * @param resource $data |
|
107 | + * |
|
108 | + * @throws Forbidden |
|
109 | + * @throws UnsupportedMediaType |
|
110 | + * @throws BadRequest |
|
111 | + * @throws Exception |
|
112 | + * @throws EntityTooLarge |
|
113 | + * @throws ServiceUnavailable |
|
114 | + * @throws FileLocked |
|
115 | + * @return string|null |
|
116 | + */ |
|
117 | + public function put($data) { |
|
118 | + try { |
|
119 | + $exists = $this->fileView->file_exists($this->path); |
|
120 | + if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
121 | + throw new Forbidden(); |
|
122 | + } |
|
123 | + } catch (StorageNotAvailableException $e) { |
|
124 | + throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
125 | + } |
|
126 | + |
|
127 | + // verify path of the target |
|
128 | + $this->verifyPath(); |
|
129 | + |
|
130 | + // chunked handling |
|
131 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
132 | + try { |
|
133 | + return $this->createFileChunked($data); |
|
134 | + } catch (\Exception $e) { |
|
135 | + $this->convertToSabreException($e); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + /** @var Storage $partStorage */ |
|
140 | + list($partStorage) = $this->fileView->resolvePath($this->path); |
|
141 | + $needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
|
142 | + |
|
143 | + if ($needsPartFile) { |
|
144 | + // mark file as partial while uploading (ignored by the scanner) |
|
145 | + $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
146 | + } else { |
|
147 | + // upload file directly as the final path |
|
148 | + $partFilePath = $this->path; |
|
149 | + |
|
150 | + $this->emitPreHooks($exists); |
|
151 | + } |
|
152 | + |
|
153 | + $view = \OC\Files\Filesystem::getView(); |
|
154 | + |
|
155 | + // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
156 | + /** @var \OC\Files\Storage\Storage $partStorage */ |
|
157 | + list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
158 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
159 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
160 | + try { |
|
161 | + if (!$needsPartFile) { |
|
162 | + if ($view) { |
|
163 | + $this->emitPreHooks($exists); |
|
164 | + } |
|
165 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
166 | + } |
|
167 | + |
|
168 | + $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
169 | + if ($target === false) { |
|
170 | + \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
|
171 | + // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
172 | + throw new Exception('Could not write file contents'); |
|
173 | + } |
|
174 | + list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
175 | + fclose($target); |
|
176 | + |
|
177 | + if ($result === false) { |
|
178 | + $expected = -1; |
|
179 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
180 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
181 | + } |
|
182 | + throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
183 | + } |
|
184 | + |
|
185 | + // if content length is sent by client: |
|
186 | + // double check if the file was fully received |
|
187 | + // compare expected and actual size |
|
188 | + if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
189 | + $expected = (int) $_SERVER['CONTENT_LENGTH']; |
|
190 | + if ($count !== $expected) { |
|
191 | + throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + } catch (\Exception $e) { |
|
196 | + if ($needsPartFile) { |
|
197 | + $partStorage->unlink($internalPartPath); |
|
198 | + } |
|
199 | + $this->convertToSabreException($e); |
|
200 | + } |
|
201 | + |
|
202 | + try { |
|
203 | + if ($needsPartFile) { |
|
204 | + if ($view) { |
|
205 | + $this->emitPreHooks($exists); |
|
206 | + } |
|
207 | + try { |
|
208 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
209 | + } catch (LockedException $e) { |
|
210 | + if ($needsPartFile) { |
|
211 | + $partStorage->unlink($internalPartPath); |
|
212 | + } |
|
213 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + if ($needsPartFile) { |
|
218 | + // rename to correct path |
|
219 | + try { |
|
220 | + $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
221 | + $fileExists = $storage->file_exists($internalPath); |
|
222 | + if ($renameOkay === false || $fileExists === false) { |
|
223 | + \OC::$server->getLogger()->error('renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', ['app' => 'webdav']); |
|
224 | + throw new Exception('Could not rename part file to final file'); |
|
225 | + } |
|
226 | + } catch (ForbiddenException $ex) { |
|
227 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
228 | + } catch (\Exception $e) { |
|
229 | + $partStorage->unlink($internalPartPath); |
|
230 | + $this->convertToSabreException($e); |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
235 | + $storage->getUpdater()->update($internalPath); |
|
236 | + |
|
237 | + try { |
|
238 | + $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
239 | + } catch (LockedException $e) { |
|
240 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
241 | + } |
|
242 | + |
|
243 | + // allow sync clients to send the mtime along in a header |
|
244 | + if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
245 | + $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
246 | + if ($this->fileView->touch($this->path, $mtime)) { |
|
247 | + $this->header('X-OC-MTime: accepted'); |
|
248 | + } |
|
249 | + } |
|
250 | 250 | |
251 | - if ($view) { |
|
252 | - $this->emitPostHooks($exists); |
|
253 | - } |
|
254 | - |
|
255 | - $this->refreshInfo(); |
|
256 | - |
|
257 | - if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
258 | - $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
259 | - $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
260 | - $this->refreshInfo(); |
|
261 | - } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
262 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
263 | - $this->refreshInfo(); |
|
264 | - } |
|
265 | - |
|
266 | - } catch (StorageNotAvailableException $e) { |
|
267 | - throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
268 | - } |
|
269 | - |
|
270 | - return '"' . $this->info->getEtag() . '"'; |
|
271 | - } |
|
272 | - |
|
273 | - private function getPartFileBasePath($path) { |
|
274 | - $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
275 | - if ($partFileInStorage) { |
|
276 | - return $path; |
|
277 | - } else { |
|
278 | - return md5($path); // will place it in the root of the view with a unique name |
|
279 | - } |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * @param string $path |
|
284 | - */ |
|
285 | - private function emitPreHooks($exists, $path = null) { |
|
286 | - if (is_null($path)) { |
|
287 | - $path = $this->path; |
|
288 | - } |
|
289 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
290 | - $run = true; |
|
291 | - |
|
292 | - if (!$exists) { |
|
293 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
294 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
295 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
296 | - )); |
|
297 | - } else { |
|
298 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
299 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
300 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
301 | - )); |
|
302 | - } |
|
303 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
304 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
305 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
306 | - )); |
|
307 | - return $run; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @param string $path |
|
312 | - */ |
|
313 | - private function emitPostHooks($exists, $path = null) { |
|
314 | - if (is_null($path)) { |
|
315 | - $path = $this->path; |
|
316 | - } |
|
317 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
318 | - if (!$exists) { |
|
319 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
320 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
321 | - )); |
|
322 | - } else { |
|
323 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
324 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
325 | - )); |
|
326 | - } |
|
327 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
328 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
329 | - )); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Returns the data |
|
334 | - * |
|
335 | - * @return resource |
|
336 | - * @throws Forbidden |
|
337 | - * @throws ServiceUnavailable |
|
338 | - */ |
|
339 | - public function get() { |
|
340 | - //throw exception if encryption is disabled but files are still encrypted |
|
341 | - try { |
|
342 | - if (!$this->info->isReadable()) { |
|
343 | - // do a if the file did not exist |
|
344 | - throw new NotFound(); |
|
345 | - } |
|
346 | - try { |
|
347 | - $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
348 | - } catch (\Exception $e) { |
|
349 | - $this->convertToSabreException($e); |
|
350 | - } |
|
351 | - if ($res === false) { |
|
352 | - throw new ServiceUnavailable("Could not open file"); |
|
353 | - } |
|
354 | - return $res; |
|
355 | - } catch (GenericEncryptionException $e) { |
|
356 | - // returning 503 will allow retry of the operation at a later point in time |
|
357 | - throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
358 | - } catch (StorageNotAvailableException $e) { |
|
359 | - throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
360 | - } catch (ForbiddenException $ex) { |
|
361 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
362 | - } catch (LockedException $e) { |
|
363 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
364 | - } |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * Delete the current file |
|
369 | - * |
|
370 | - * @throws Forbidden |
|
371 | - * @throws ServiceUnavailable |
|
372 | - */ |
|
373 | - public function delete() { |
|
374 | - if (!$this->info->isDeletable()) { |
|
375 | - throw new Forbidden(); |
|
376 | - } |
|
377 | - |
|
378 | - try { |
|
379 | - if (!$this->fileView->unlink($this->path)) { |
|
380 | - // assume it wasn't possible to delete due to permissions |
|
381 | - throw new Forbidden(); |
|
382 | - } |
|
383 | - } catch (StorageNotAvailableException $e) { |
|
384 | - throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
385 | - } catch (ForbiddenException $ex) { |
|
386 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
387 | - } catch (LockedException $e) { |
|
388 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Returns the mime-type for a file |
|
394 | - * |
|
395 | - * If null is returned, we'll assume application/octet-stream |
|
396 | - * |
|
397 | - * @return string |
|
398 | - */ |
|
399 | - public function getContentType() { |
|
400 | - $mimeType = $this->info->getMimetype(); |
|
401 | - |
|
402 | - // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
403 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
404 | - return $mimeType; |
|
405 | - } |
|
406 | - return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * @return array|false |
|
411 | - */ |
|
412 | - public function getDirectDownload() { |
|
413 | - if (\OCP\App::isEnabled('encryption')) { |
|
414 | - return []; |
|
415 | - } |
|
416 | - /** @var \OCP\Files\Storage $storage */ |
|
417 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
418 | - if (is_null($storage)) { |
|
419 | - return []; |
|
420 | - } |
|
421 | - |
|
422 | - return $storage->getDirectDownload($internalPath); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * @param resource $data |
|
427 | - * @return null|string |
|
428 | - * @throws Exception |
|
429 | - * @throws BadRequest |
|
430 | - * @throws NotImplemented |
|
431 | - * @throws ServiceUnavailable |
|
432 | - */ |
|
433 | - private function createFileChunked($data) { |
|
434 | - list($path, $name) = \Sabre\Uri\split($this->path); |
|
435 | - |
|
436 | - $info = \OC_FileChunking::decodeName($name); |
|
437 | - if (empty($info)) { |
|
438 | - throw new NotImplemented('Invalid chunk name'); |
|
439 | - } |
|
440 | - |
|
441 | - $chunk_handler = new \OC_FileChunking($info); |
|
442 | - $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
443 | - |
|
444 | - //detect aborted upload |
|
445 | - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
446 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
447 | - $expected = (int) $_SERVER['CONTENT_LENGTH']; |
|
448 | - if ($bytesWritten !== $expected) { |
|
449 | - $chunk_handler->remove($info['index']); |
|
450 | - throw new BadRequest( |
|
451 | - 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
452 | - } |
|
453 | - } |
|
454 | - } |
|
455 | - |
|
456 | - if ($chunk_handler->isComplete()) { |
|
457 | - /** @var Storage $storage */ |
|
458 | - list($storage,) = $this->fileView->resolvePath($path); |
|
459 | - $needsPartFile = $storage->needsPartFile(); |
|
460 | - $partFile = null; |
|
461 | - |
|
462 | - $targetPath = $path . '/' . $info['name']; |
|
463 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
464 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
465 | - |
|
466 | - $exists = $this->fileView->file_exists($targetPath); |
|
467 | - |
|
468 | - try { |
|
469 | - $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
470 | - |
|
471 | - $this->emitPreHooks($exists, $targetPath); |
|
472 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
473 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
474 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
475 | - |
|
476 | - if ($needsPartFile) { |
|
477 | - // we first assembly the target file as a part file |
|
478 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
479 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
480 | - list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
481 | - |
|
482 | - |
|
483 | - $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
484 | - |
|
485 | - // here is the final atomic rename |
|
486 | - $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
487 | - $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
488 | - if ($renameOkay === false || $fileExists === false) { |
|
489 | - \OC::$server->getLogger()->error('\OC\Files\Filesystem::rename() failed', ['app' => 'webdav']); |
|
490 | - // only delete if an error occurred and the target file was already created |
|
491 | - if ($fileExists) { |
|
492 | - // set to null to avoid double-deletion when handling exception |
|
493 | - // stray part file |
|
494 | - $partFile = null; |
|
495 | - $targetStorage->unlink($targetInternalPath); |
|
496 | - } |
|
497 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
498 | - throw new Exception('Could not rename part file assembled from chunks'); |
|
499 | - } |
|
500 | - } else { |
|
501 | - // assemble directly into the final file |
|
502 | - $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
503 | - } |
|
504 | - |
|
505 | - // allow sync clients to send the mtime along in a header |
|
506 | - if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
507 | - $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
508 | - if ($targetStorage->touch($targetInternalPath, $mtime)) { |
|
509 | - $this->header('X-OC-MTime: accepted'); |
|
510 | - } |
|
511 | - } |
|
512 | - |
|
513 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
514 | - $targetStorage->getUpdater()->update($targetInternalPath); |
|
515 | - |
|
516 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
517 | - |
|
518 | - $this->emitPostHooks($exists, $targetPath); |
|
519 | - |
|
520 | - // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
521 | - $info = $this->fileView->getFileInfo($targetPath); |
|
522 | - |
|
523 | - if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
524 | - $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
525 | - $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
526 | - } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
527 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
528 | - } |
|
529 | - |
|
530 | - $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
531 | - |
|
532 | - return $info->getEtag(); |
|
533 | - } catch (\Exception $e) { |
|
534 | - if ($partFile !== null) { |
|
535 | - $targetStorage->unlink($targetInternalPath); |
|
536 | - } |
|
537 | - $this->convertToSabreException($e); |
|
538 | - } |
|
539 | - } |
|
540 | - |
|
541 | - return null; |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Convert the given exception to a SabreException instance |
|
546 | - * |
|
547 | - * @param \Exception $e |
|
548 | - * |
|
549 | - * @throws \Sabre\DAV\Exception |
|
550 | - */ |
|
551 | - private function convertToSabreException(\Exception $e) { |
|
552 | - if ($e instanceof \Sabre\DAV\Exception) { |
|
553 | - throw $e; |
|
554 | - } |
|
555 | - if ($e instanceof NotPermittedException) { |
|
556 | - // a more general case - due to whatever reason the content could not be written |
|
557 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
558 | - } |
|
559 | - if ($e instanceof ForbiddenException) { |
|
560 | - // the path for the file was forbidden |
|
561 | - throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
562 | - } |
|
563 | - if ($e instanceof EntityTooLargeException) { |
|
564 | - // the file is too big to be stored |
|
565 | - throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
566 | - } |
|
567 | - if ($e instanceof InvalidContentException) { |
|
568 | - // the file content is not permitted |
|
569 | - throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
570 | - } |
|
571 | - if ($e instanceof InvalidPathException) { |
|
572 | - // the path for the file was not valid |
|
573 | - // TODO: find proper http status code for this case |
|
574 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
575 | - } |
|
576 | - if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
577 | - // the file is currently being written to by another process |
|
578 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
579 | - } |
|
580 | - if ($e instanceof GenericEncryptionException) { |
|
581 | - // returning 503 will allow retry of the operation at a later point in time |
|
582 | - throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
583 | - } |
|
584 | - if ($e instanceof StorageNotAvailableException) { |
|
585 | - throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
586 | - } |
|
587 | - |
|
588 | - throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Get the checksum for this file |
|
593 | - * |
|
594 | - * @return string |
|
595 | - */ |
|
596 | - public function getChecksum() { |
|
597 | - return $this->info->getChecksum(); |
|
598 | - } |
|
599 | - |
|
600 | - protected function header($string) { |
|
601 | - \header($string); |
|
602 | - } |
|
251 | + if ($view) { |
|
252 | + $this->emitPostHooks($exists); |
|
253 | + } |
|
254 | + |
|
255 | + $this->refreshInfo(); |
|
256 | + |
|
257 | + if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
258 | + $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
259 | + $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
260 | + $this->refreshInfo(); |
|
261 | + } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
262 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
263 | + $this->refreshInfo(); |
|
264 | + } |
|
265 | + |
|
266 | + } catch (StorageNotAvailableException $e) { |
|
267 | + throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
268 | + } |
|
269 | + |
|
270 | + return '"' . $this->info->getEtag() . '"'; |
|
271 | + } |
|
272 | + |
|
273 | + private function getPartFileBasePath($path) { |
|
274 | + $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
275 | + if ($partFileInStorage) { |
|
276 | + return $path; |
|
277 | + } else { |
|
278 | + return md5($path); // will place it in the root of the view with a unique name |
|
279 | + } |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * @param string $path |
|
284 | + */ |
|
285 | + private function emitPreHooks($exists, $path = null) { |
|
286 | + if (is_null($path)) { |
|
287 | + $path = $this->path; |
|
288 | + } |
|
289 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
290 | + $run = true; |
|
291 | + |
|
292 | + if (!$exists) { |
|
293 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
294 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
295 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
296 | + )); |
|
297 | + } else { |
|
298 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
299 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
300 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
301 | + )); |
|
302 | + } |
|
303 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
304 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
305 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
306 | + )); |
|
307 | + return $run; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @param string $path |
|
312 | + */ |
|
313 | + private function emitPostHooks($exists, $path = null) { |
|
314 | + if (is_null($path)) { |
|
315 | + $path = $this->path; |
|
316 | + } |
|
317 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
318 | + if (!$exists) { |
|
319 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
320 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
321 | + )); |
|
322 | + } else { |
|
323 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
324 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
325 | + )); |
|
326 | + } |
|
327 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
328 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
329 | + )); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Returns the data |
|
334 | + * |
|
335 | + * @return resource |
|
336 | + * @throws Forbidden |
|
337 | + * @throws ServiceUnavailable |
|
338 | + */ |
|
339 | + public function get() { |
|
340 | + //throw exception if encryption is disabled but files are still encrypted |
|
341 | + try { |
|
342 | + if (!$this->info->isReadable()) { |
|
343 | + // do a if the file did not exist |
|
344 | + throw new NotFound(); |
|
345 | + } |
|
346 | + try { |
|
347 | + $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
348 | + } catch (\Exception $e) { |
|
349 | + $this->convertToSabreException($e); |
|
350 | + } |
|
351 | + if ($res === false) { |
|
352 | + throw new ServiceUnavailable("Could not open file"); |
|
353 | + } |
|
354 | + return $res; |
|
355 | + } catch (GenericEncryptionException $e) { |
|
356 | + // returning 503 will allow retry of the operation at a later point in time |
|
357 | + throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
358 | + } catch (StorageNotAvailableException $e) { |
|
359 | + throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
360 | + } catch (ForbiddenException $ex) { |
|
361 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
362 | + } catch (LockedException $e) { |
|
363 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
364 | + } |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * Delete the current file |
|
369 | + * |
|
370 | + * @throws Forbidden |
|
371 | + * @throws ServiceUnavailable |
|
372 | + */ |
|
373 | + public function delete() { |
|
374 | + if (!$this->info->isDeletable()) { |
|
375 | + throw new Forbidden(); |
|
376 | + } |
|
377 | + |
|
378 | + try { |
|
379 | + if (!$this->fileView->unlink($this->path)) { |
|
380 | + // assume it wasn't possible to delete due to permissions |
|
381 | + throw new Forbidden(); |
|
382 | + } |
|
383 | + } catch (StorageNotAvailableException $e) { |
|
384 | + throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
385 | + } catch (ForbiddenException $ex) { |
|
386 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
387 | + } catch (LockedException $e) { |
|
388 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Returns the mime-type for a file |
|
394 | + * |
|
395 | + * If null is returned, we'll assume application/octet-stream |
|
396 | + * |
|
397 | + * @return string |
|
398 | + */ |
|
399 | + public function getContentType() { |
|
400 | + $mimeType = $this->info->getMimetype(); |
|
401 | + |
|
402 | + // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
403 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
404 | + return $mimeType; |
|
405 | + } |
|
406 | + return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * @return array|false |
|
411 | + */ |
|
412 | + public function getDirectDownload() { |
|
413 | + if (\OCP\App::isEnabled('encryption')) { |
|
414 | + return []; |
|
415 | + } |
|
416 | + /** @var \OCP\Files\Storage $storage */ |
|
417 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
418 | + if (is_null($storage)) { |
|
419 | + return []; |
|
420 | + } |
|
421 | + |
|
422 | + return $storage->getDirectDownload($internalPath); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * @param resource $data |
|
427 | + * @return null|string |
|
428 | + * @throws Exception |
|
429 | + * @throws BadRequest |
|
430 | + * @throws NotImplemented |
|
431 | + * @throws ServiceUnavailable |
|
432 | + */ |
|
433 | + private function createFileChunked($data) { |
|
434 | + list($path, $name) = \Sabre\Uri\split($this->path); |
|
435 | + |
|
436 | + $info = \OC_FileChunking::decodeName($name); |
|
437 | + if (empty($info)) { |
|
438 | + throw new NotImplemented('Invalid chunk name'); |
|
439 | + } |
|
440 | + |
|
441 | + $chunk_handler = new \OC_FileChunking($info); |
|
442 | + $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
443 | + |
|
444 | + //detect aborted upload |
|
445 | + if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
446 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
447 | + $expected = (int) $_SERVER['CONTENT_LENGTH']; |
|
448 | + if ($bytesWritten !== $expected) { |
|
449 | + $chunk_handler->remove($info['index']); |
|
450 | + throw new BadRequest( |
|
451 | + 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
452 | + } |
|
453 | + } |
|
454 | + } |
|
455 | + |
|
456 | + if ($chunk_handler->isComplete()) { |
|
457 | + /** @var Storage $storage */ |
|
458 | + list($storage,) = $this->fileView->resolvePath($path); |
|
459 | + $needsPartFile = $storage->needsPartFile(); |
|
460 | + $partFile = null; |
|
461 | + |
|
462 | + $targetPath = $path . '/' . $info['name']; |
|
463 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
464 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
465 | + |
|
466 | + $exists = $this->fileView->file_exists($targetPath); |
|
467 | + |
|
468 | + try { |
|
469 | + $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
470 | + |
|
471 | + $this->emitPreHooks($exists, $targetPath); |
|
472 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
473 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
474 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
475 | + |
|
476 | + if ($needsPartFile) { |
|
477 | + // we first assembly the target file as a part file |
|
478 | + $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
479 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
480 | + list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
481 | + |
|
482 | + |
|
483 | + $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
484 | + |
|
485 | + // here is the final atomic rename |
|
486 | + $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
487 | + $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
488 | + if ($renameOkay === false || $fileExists === false) { |
|
489 | + \OC::$server->getLogger()->error('\OC\Files\Filesystem::rename() failed', ['app' => 'webdav']); |
|
490 | + // only delete if an error occurred and the target file was already created |
|
491 | + if ($fileExists) { |
|
492 | + // set to null to avoid double-deletion when handling exception |
|
493 | + // stray part file |
|
494 | + $partFile = null; |
|
495 | + $targetStorage->unlink($targetInternalPath); |
|
496 | + } |
|
497 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
498 | + throw new Exception('Could not rename part file assembled from chunks'); |
|
499 | + } |
|
500 | + } else { |
|
501 | + // assemble directly into the final file |
|
502 | + $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
503 | + } |
|
504 | + |
|
505 | + // allow sync clients to send the mtime along in a header |
|
506 | + if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
507 | + $mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
508 | + if ($targetStorage->touch($targetInternalPath, $mtime)) { |
|
509 | + $this->header('X-OC-MTime: accepted'); |
|
510 | + } |
|
511 | + } |
|
512 | + |
|
513 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
514 | + $targetStorage->getUpdater()->update($targetInternalPath); |
|
515 | + |
|
516 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
517 | + |
|
518 | + $this->emitPostHooks($exists, $targetPath); |
|
519 | + |
|
520 | + // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
521 | + $info = $this->fileView->getFileInfo($targetPath); |
|
522 | + |
|
523 | + if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
524 | + $checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
525 | + $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
526 | + } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
527 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
528 | + } |
|
529 | + |
|
530 | + $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
531 | + |
|
532 | + return $info->getEtag(); |
|
533 | + } catch (\Exception $e) { |
|
534 | + if ($partFile !== null) { |
|
535 | + $targetStorage->unlink($targetInternalPath); |
|
536 | + } |
|
537 | + $this->convertToSabreException($e); |
|
538 | + } |
|
539 | + } |
|
540 | + |
|
541 | + return null; |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Convert the given exception to a SabreException instance |
|
546 | + * |
|
547 | + * @param \Exception $e |
|
548 | + * |
|
549 | + * @throws \Sabre\DAV\Exception |
|
550 | + */ |
|
551 | + private function convertToSabreException(\Exception $e) { |
|
552 | + if ($e instanceof \Sabre\DAV\Exception) { |
|
553 | + throw $e; |
|
554 | + } |
|
555 | + if ($e instanceof NotPermittedException) { |
|
556 | + // a more general case - due to whatever reason the content could not be written |
|
557 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
558 | + } |
|
559 | + if ($e instanceof ForbiddenException) { |
|
560 | + // the path for the file was forbidden |
|
561 | + throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
562 | + } |
|
563 | + if ($e instanceof EntityTooLargeException) { |
|
564 | + // the file is too big to be stored |
|
565 | + throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
566 | + } |
|
567 | + if ($e instanceof InvalidContentException) { |
|
568 | + // the file content is not permitted |
|
569 | + throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
570 | + } |
|
571 | + if ($e instanceof InvalidPathException) { |
|
572 | + // the path for the file was not valid |
|
573 | + // TODO: find proper http status code for this case |
|
574 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
575 | + } |
|
576 | + if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
577 | + // the file is currently being written to by another process |
|
578 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
579 | + } |
|
580 | + if ($e instanceof GenericEncryptionException) { |
|
581 | + // returning 503 will allow retry of the operation at a later point in time |
|
582 | + throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
583 | + } |
|
584 | + if ($e instanceof StorageNotAvailableException) { |
|
585 | + throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
586 | + } |
|
587 | + |
|
588 | + throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Get the checksum for this file |
|
593 | + * |
|
594 | + * @return string |
|
595 | + */ |
|
596 | + public function getChecksum() { |
|
597 | + return $this->info->getChecksum(); |
|
598 | + } |
|
599 | + |
|
600 | + protected function header($string) { |
|
601 | + \header($string); |
|
602 | + } |
|
603 | 603 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | throw new Forbidden(); |
122 | 122 | } |
123 | 123 | } catch (StorageNotAvailableException $e) { |
124 | - throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
124 | + throw new ServiceUnavailable("File is not updatable: ".$e->getMessage()); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | // verify path of the target |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | |
143 | 143 | if ($needsPartFile) { |
144 | 144 | // mark file as partial while uploading (ignored by the scanner) |
145 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
145 | + $partFilePath = $this->getPartFileBasePath($this->path).'.ocTransferId'.rand().'.part'; |
|
146 | 146 | } else { |
147 | 147 | // upload file directly as the final path |
148 | 148 | $partFilePath = $this->path; |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | if (isset($_SERVER['CONTENT_LENGTH'])) { |
180 | 180 | $expected = $_SERVER['CONTENT_LENGTH']; |
181 | 181 | } |
182 | - throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
182 | + throw new Exception('Error while copying file to target location (copied bytes: '.$count.', expected filesize: '.$expected.' )'); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | // if content length is sent by client: |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
189 | 189 | $expected = (int) $_SERVER['CONTENT_LENGTH']; |
190 | 190 | if ($count !== $expected) { |
191 | - throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
191 | + throw new BadRequest('expected filesize '.$expected.' got '.$count); |
|
192 | 192 | } |
193 | 193 | } |
194 | 194 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
221 | 221 | $fileExists = $storage->file_exists($internalPath); |
222 | 222 | if ($renameOkay === false || $fileExists === false) { |
223 | - \OC::$server->getLogger()->error('renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', ['app' => 'webdav']); |
|
223 | + \OC::$server->getLogger()->error('renaming part file to final file failed ($run: '.($run ? 'true' : 'false').', $renameOkay: '.($renameOkay ? 'true' : 'false').', $fileExists: '.($fileExists ? 'true' : 'false').')', ['app' => 'webdav']); |
|
224 | 224 | throw new Exception('Could not rename part file to final file'); |
225 | 225 | } |
226 | 226 | } catch (ForbiddenException $ex) { |
@@ -264,10 +264,10 @@ discard block |
||
264 | 264 | } |
265 | 265 | |
266 | 266 | } catch (StorageNotAvailableException $e) { |
267 | - throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
267 | + throw new ServiceUnavailable("Failed to check file size: ".$e->getMessage()); |
|
268 | 268 | } |
269 | 269 | |
270 | - return '"' . $this->info->getEtag() . '"'; |
|
270 | + return '"'.$this->info->getEtag().'"'; |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | private function getPartFileBasePath($path) { |
@@ -354,9 +354,9 @@ discard block |
||
354 | 354 | return $res; |
355 | 355 | } catch (GenericEncryptionException $e) { |
356 | 356 | // returning 503 will allow retry of the operation at a later point in time |
357 | - throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
357 | + throw new ServiceUnavailable("Encryption not ready: ".$e->getMessage()); |
|
358 | 358 | } catch (StorageNotAvailableException $e) { |
359 | - throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
359 | + throw new ServiceUnavailable("Failed to open file: ".$e->getMessage()); |
|
360 | 360 | } catch (ForbiddenException $ex) { |
361 | 361 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
362 | 362 | } catch (LockedException $e) { |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | throw new Forbidden(); |
382 | 382 | } |
383 | 383 | } catch (StorageNotAvailableException $e) { |
384 | - throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
384 | + throw new ServiceUnavailable("Failed to unlink: ".$e->getMessage()); |
|
385 | 385 | } catch (ForbiddenException $ex) { |
386 | 386 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
387 | 387 | } catch (LockedException $e) { |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | if ($bytesWritten !== $expected) { |
449 | 449 | $chunk_handler->remove($info['index']); |
450 | 450 | throw new BadRequest( |
451 | - 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
451 | + 'expected filesize '.$expected.' got '.$bytesWritten); |
|
452 | 452 | } |
453 | 453 | } |
454 | 454 | } |
@@ -459,7 +459,7 @@ discard block |
||
459 | 459 | $needsPartFile = $storage->needsPartFile(); |
460 | 460 | $partFile = null; |
461 | 461 | |
462 | - $targetPath = $path . '/' . $info['name']; |
|
462 | + $targetPath = $path.'/'.$info['name']; |
|
463 | 463 | /** @var \OC\Files\Storage\Storage $targetStorage */ |
464 | 464 | list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
465 | 465 | |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | |
476 | 476 | if ($needsPartFile) { |
477 | 477 | // we first assembly the target file as a part file |
478 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
478 | + $partFile = $this->getPartFileBasePath($path.'/'.$info['name']).'.ocTransferId'.$info['transferid'].'.part'; |
|
479 | 479 | /** @var \OC\Files\Storage\Storage $targetStorage */ |
480 | 480 | list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
481 | 481 | |
@@ -579,10 +579,10 @@ discard block |
||
579 | 579 | } |
580 | 580 | if ($e instanceof GenericEncryptionException) { |
581 | 581 | // returning 503 will allow retry of the operation at a later point in time |
582 | - throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
582 | + throw new ServiceUnavailable('Encryption not ready: '.$e->getMessage(), 0, $e); |
|
583 | 583 | } |
584 | 584 | if ($e instanceof StorageNotAvailableException) { |
585 | - throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
585 | + throw new ServiceUnavailable('Failed to write file contents: '.$e->getMessage(), 0, $e); |
|
586 | 586 | } |
587 | 587 | |
588 | 588 | throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |