@@ -28,7 +28,6 @@ |
||
| 28 | 28 | use Icewind\Streams\CallbackWrapper; |
| 29 | 29 | use Icewind\Streams\IteratorDirectory; |
| 30 | 30 | use OC\Files\Cache\CacheEntry; |
| 31 | -use OC\Files\Stream\CountReadStream; |
|
| 32 | 31 | use OCP\Files\NotFoundException; |
| 33 | 32 | use OCP\Files\ObjectStore\IObjectStore; |
| 34 | 33 | |
@@ -33,407 +33,407 @@ |
||
| 33 | 33 | use OCP\Files\ObjectStore\IObjectStore; |
| 34 | 34 | |
| 35 | 35 | class ObjectStoreStorage extends \OC\Files\Storage\Common { |
| 36 | - /** |
|
| 37 | - * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 38 | - */ |
|
| 39 | - protected $objectStore; |
|
| 40 | - /** |
|
| 41 | - * @var string $id |
|
| 42 | - */ |
|
| 43 | - protected $id; |
|
| 44 | - /** |
|
| 45 | - * @var \OC\User\User $user |
|
| 46 | - */ |
|
| 47 | - protected $user; |
|
| 48 | - |
|
| 49 | - private $objectPrefix = 'urn:oid:'; |
|
| 50 | - |
|
| 51 | - private $logger; |
|
| 52 | - |
|
| 53 | - public function __construct($params) { |
|
| 54 | - if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 55 | - $this->objectStore = $params['objectstore']; |
|
| 56 | - } else { |
|
| 57 | - throw new \Exception('missing IObjectStore instance'); |
|
| 58 | - } |
|
| 59 | - if (isset($params['storageid'])) { |
|
| 60 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 61 | - } else { |
|
| 62 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 63 | - } |
|
| 64 | - if (isset($params['objectPrefix'])) { |
|
| 65 | - $this->objectPrefix = $params['objectPrefix']; |
|
| 66 | - } |
|
| 67 | - //initialize cache with root directory in cache |
|
| 68 | - if (!$this->is_dir('/')) { |
|
| 69 | - $this->mkdir('/'); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $this->logger = \OC::$server->getLogger(); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function mkdir($path) { |
|
| 76 | - $path = $this->normalizePath($path); |
|
| 77 | - |
|
| 78 | - if ($this->file_exists($path)) { |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $mTime = time(); |
|
| 83 | - $data = [ |
|
| 84 | - 'mimetype' => 'httpd/unix-directory', |
|
| 85 | - 'size' => 0, |
|
| 86 | - 'mtime' => $mTime, |
|
| 87 | - 'storage_mtime' => $mTime, |
|
| 88 | - 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 89 | - ]; |
|
| 90 | - if ($path === '') { |
|
| 91 | - //create root on the fly |
|
| 92 | - $data['etag'] = $this->getETag(''); |
|
| 93 | - $this->getCache()->put('', $data); |
|
| 94 | - return true; |
|
| 95 | - } else { |
|
| 96 | - // if parent does not exist, create it |
|
| 97 | - $parent = $this->normalizePath(dirname($path)); |
|
| 98 | - $parentType = $this->filetype($parent); |
|
| 99 | - if ($parentType === false) { |
|
| 100 | - if (!$this->mkdir($parent)) { |
|
| 101 | - // something went wrong |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - } else if ($parentType === 'file') { |
|
| 105 | - // parent is a file |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - // finally create the new dir |
|
| 109 | - $mTime = time(); // update mtime |
|
| 110 | - $data['mtime'] = $mTime; |
|
| 111 | - $data['storage_mtime'] = $mTime; |
|
| 112 | - $data['etag'] = $this->getETag($path); |
|
| 113 | - $this->getCache()->put($path, $data); |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param string $path |
|
| 120 | - * @return string |
|
| 121 | - */ |
|
| 122 | - private function normalizePath($path) { |
|
| 123 | - $path = trim($path, '/'); |
|
| 124 | - //FIXME why do we sometimes get a path like 'files//username'? |
|
| 125 | - $path = str_replace('//', '/', $path); |
|
| 126 | - |
|
| 127 | - // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 128 | - if (!$path || $path === '.') { |
|
| 129 | - $path = ''; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $path; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 137 | - * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 138 | - * |
|
| 139 | - * @param string $path |
|
| 140 | - * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 141 | - * @return \OC\Files\ObjectStore\NoopScanner |
|
| 142 | - */ |
|
| 143 | - public function getScanner($path = '', $storage = null) { |
|
| 144 | - if (!$storage) { |
|
| 145 | - $storage = $this; |
|
| 146 | - } |
|
| 147 | - if (!isset($this->scanner)) { |
|
| 148 | - $this->scanner = new NoopScanner($storage); |
|
| 149 | - } |
|
| 150 | - return $this->scanner; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function getId() { |
|
| 154 | - return $this->id; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - public function rmdir($path) { |
|
| 158 | - $path = $this->normalizePath($path); |
|
| 159 | - |
|
| 160 | - if (!$this->is_dir($path)) { |
|
| 161 | - return false; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->rmObjects($path); |
|
| 165 | - |
|
| 166 | - $this->getCache()->remove($path); |
|
| 167 | - |
|
| 168 | - return true; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - private function rmObjects($path) { |
|
| 172 | - $children = $this->getCache()->getFolderContents($path); |
|
| 173 | - foreach ($children as $child) { |
|
| 174 | - if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 175 | - $this->rmObjects($child['path']); |
|
| 176 | - } else { |
|
| 177 | - $this->unlink($child['path']); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - public function unlink($path) { |
|
| 183 | - $path = $this->normalizePath($path); |
|
| 184 | - $stat = $this->stat($path); |
|
| 185 | - |
|
| 186 | - if ($stat && isset($stat['fileid'])) { |
|
| 187 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 188 | - return $this->rmdir($path); |
|
| 189 | - } |
|
| 190 | - try { |
|
| 191 | - $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 192 | - } catch (\Exception $ex) { |
|
| 193 | - if ($ex->getCode() !== 404) { |
|
| 194 | - $this->logger->logException($ex, [ |
|
| 195 | - 'app' => 'objectstore', |
|
| 196 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 197 | - ]); |
|
| 198 | - return false; |
|
| 199 | - } else { |
|
| 200 | - //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - $this->getCache()->remove($path); |
|
| 204 | - return true; |
|
| 205 | - } |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function stat($path) { |
|
| 210 | - $path = $this->normalizePath($path); |
|
| 211 | - $cacheEntry = $this->getCache()->get($path); |
|
| 212 | - if ($cacheEntry instanceof CacheEntry) { |
|
| 213 | - return $cacheEntry->getData(); |
|
| 214 | - } else { |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 221 | - * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 222 | - * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 223 | - * |
|
| 224 | - * @param int $fileId the fileid |
|
| 225 | - * @return null|string the unified resource name used to identify the object |
|
| 226 | - */ |
|
| 227 | - protected function getURN($fileId) { |
|
| 228 | - if (is_numeric($fileId)) { |
|
| 229 | - return $this->objectPrefix . $fileId; |
|
| 230 | - } |
|
| 231 | - return null; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - public function opendir($path) { |
|
| 235 | - $path = $this->normalizePath($path); |
|
| 236 | - |
|
| 237 | - try { |
|
| 238 | - $files = array(); |
|
| 239 | - $folderContents = $this->getCache()->getFolderContents($path); |
|
| 240 | - foreach ($folderContents as $file) { |
|
| 241 | - $files[] = $file['name']; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return IteratorDirectory::wrap($files); |
|
| 245 | - } catch (\Exception $e) { |
|
| 246 | - $this->logger->logException($e); |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - public function filetype($path) { |
|
| 252 | - $path = $this->normalizePath($path); |
|
| 253 | - $stat = $this->stat($path); |
|
| 254 | - if ($stat) { |
|
| 255 | - if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 256 | - return 'dir'; |
|
| 257 | - } |
|
| 258 | - return 'file'; |
|
| 259 | - } else { |
|
| 260 | - return false; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - public function fopen($path, $mode) { |
|
| 265 | - $path = $this->normalizePath($path); |
|
| 266 | - |
|
| 267 | - switch ($mode) { |
|
| 268 | - case 'r': |
|
| 269 | - case 'rb': |
|
| 270 | - $stat = $this->stat($path); |
|
| 271 | - if (is_array($stat)) { |
|
| 272 | - try { |
|
| 273 | - return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 274 | - } catch (NotFoundException $e) { |
|
| 275 | - $this->logger->logException($e, [ |
|
| 276 | - 'app' => 'objectstore', |
|
| 277 | - 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 278 | - ]); |
|
| 279 | - throw $e; |
|
| 280 | - } catch (\Exception $ex) { |
|
| 281 | - $this->logger->logException($ex, [ |
|
| 282 | - 'app' => 'objectstore', |
|
| 283 | - 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 284 | - ]); |
|
| 285 | - return false; |
|
| 286 | - } |
|
| 287 | - } else { |
|
| 288 | - return false; |
|
| 289 | - } |
|
| 290 | - case 'w': |
|
| 291 | - case 'wb': |
|
| 292 | - case 'a': |
|
| 293 | - case 'ab': |
|
| 294 | - case 'r+': |
|
| 295 | - case 'w+': |
|
| 296 | - case 'wb+': |
|
| 297 | - case 'a+': |
|
| 298 | - case 'x': |
|
| 299 | - case 'x+': |
|
| 300 | - case 'c': |
|
| 301 | - case 'c+': |
|
| 302 | - if (strrpos($path, '.') !== false) { |
|
| 303 | - $ext = substr($path, strrpos($path, '.')); |
|
| 304 | - } else { |
|
| 305 | - $ext = ''; |
|
| 306 | - } |
|
| 307 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 308 | - if ($this->file_exists($path)) { |
|
| 309 | - $source = $this->fopen($path, 'r'); |
|
| 310 | - file_put_contents($tmpFile, $source); |
|
| 311 | - } |
|
| 312 | - $handle = fopen($tmpFile, $mode); |
|
| 313 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 314 | - $this->writeBack($tmpFile, $path); |
|
| 315 | - }); |
|
| 316 | - } |
|
| 317 | - return false; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function file_exists($path) { |
|
| 321 | - $path = $this->normalizePath($path); |
|
| 322 | - return (bool)$this->stat($path); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - public function rename($source, $target) { |
|
| 326 | - $source = $this->normalizePath($source); |
|
| 327 | - $target = $this->normalizePath($target); |
|
| 328 | - $this->remove($target); |
|
| 329 | - $this->getCache()->move($source, $target); |
|
| 330 | - $this->touch(dirname($target)); |
|
| 331 | - return true; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - public function getMimeType($path) { |
|
| 335 | - $path = $this->normalizePath($path); |
|
| 336 | - $stat = $this->stat($path); |
|
| 337 | - if (is_array($stat)) { |
|
| 338 | - return $stat['mimetype']; |
|
| 339 | - } else { |
|
| 340 | - return false; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - public function touch($path, $mtime = null) { |
|
| 345 | - if (is_null($mtime)) { |
|
| 346 | - $mtime = time(); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $path = $this->normalizePath($path); |
|
| 350 | - $dirName = dirname($path); |
|
| 351 | - $parentExists = $this->is_dir($dirName); |
|
| 352 | - if (!$parentExists) { |
|
| 353 | - return false; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $stat = $this->stat($path); |
|
| 357 | - if (is_array($stat)) { |
|
| 358 | - // update existing mtime in db |
|
| 359 | - $stat['mtime'] = $mtime; |
|
| 360 | - $this->getCache()->update($stat['fileid'], $stat); |
|
| 361 | - } else { |
|
| 362 | - try { |
|
| 363 | - //create a empty file, need to have at least on char to make it |
|
| 364 | - // work with all object storage implementations |
|
| 365 | - $this->file_put_contents($path, ' '); |
|
| 366 | - $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 367 | - $stat = array( |
|
| 368 | - 'etag' => $this->getETag($path), |
|
| 369 | - 'mimetype' => $mimeType, |
|
| 370 | - 'size' => 0, |
|
| 371 | - 'mtime' => $mtime, |
|
| 372 | - 'storage_mtime' => $mtime, |
|
| 373 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 374 | - ); |
|
| 375 | - $this->getCache()->put($path, $stat); |
|
| 376 | - } catch (\Exception $ex) { |
|
| 377 | - $this->logger->logException($ex, [ |
|
| 378 | - 'app' => 'objectstore', |
|
| 379 | - 'message' => 'Could not create object for ' . $path, |
|
| 380 | - ]); |
|
| 381 | - return false; |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - return true; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - public function writeBack($tmpFile, $path) { |
|
| 388 | - $stat = $this->stat($path); |
|
| 389 | - if (empty($stat)) { |
|
| 390 | - // create new file |
|
| 391 | - $stat = array( |
|
| 392 | - 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 393 | - ); |
|
| 394 | - } |
|
| 395 | - // update stat with new data |
|
| 396 | - $mTime = time(); |
|
| 397 | - $stat['size'] = filesize($tmpFile); |
|
| 398 | - $stat['mtime'] = $mTime; |
|
| 399 | - $stat['storage_mtime'] = $mTime; |
|
| 400 | - |
|
| 401 | - // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 402 | - $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 403 | - $mimetype = $mimetypeDetector->detectPath($path); |
|
| 404 | - if ($mimetype === 'application/octet-stream') { |
|
| 405 | - $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - $stat['mimetype'] = $mimetype; |
|
| 409 | - $stat['etag'] = $this->getETag($path); |
|
| 410 | - |
|
| 411 | - $fileId = $this->getCache()->put($path, $stat); |
|
| 412 | - try { |
|
| 413 | - //upload to object storage |
|
| 414 | - $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 415 | - } catch (\Exception $ex) { |
|
| 416 | - $this->getCache()->remove($path); |
|
| 417 | - $this->logger->logException($ex, [ |
|
| 418 | - 'app' => 'objectstore', |
|
| 419 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 420 | - ]); |
|
| 421 | - throw $ex; // make this bubble up |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * external changes are not supported, exclusive access to the object storage is assumed |
|
| 427 | - * |
|
| 428 | - * @param string $path |
|
| 429 | - * @param int $time |
|
| 430 | - * @return false |
|
| 431 | - */ |
|
| 432 | - public function hasUpdated($path, $time) { |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - public function needsPartFile() { |
|
| 437 | - return false; |
|
| 438 | - } |
|
| 36 | + /** |
|
| 37 | + * @var \OCP\Files\ObjectStore\IObjectStore $objectStore |
|
| 38 | + */ |
|
| 39 | + protected $objectStore; |
|
| 40 | + /** |
|
| 41 | + * @var string $id |
|
| 42 | + */ |
|
| 43 | + protected $id; |
|
| 44 | + /** |
|
| 45 | + * @var \OC\User\User $user |
|
| 46 | + */ |
|
| 47 | + protected $user; |
|
| 48 | + |
|
| 49 | + private $objectPrefix = 'urn:oid:'; |
|
| 50 | + |
|
| 51 | + private $logger; |
|
| 52 | + |
|
| 53 | + public function __construct($params) { |
|
| 54 | + if (isset($params['objectstore']) && $params['objectstore'] instanceof IObjectStore) { |
|
| 55 | + $this->objectStore = $params['objectstore']; |
|
| 56 | + } else { |
|
| 57 | + throw new \Exception('missing IObjectStore instance'); |
|
| 58 | + } |
|
| 59 | + if (isset($params['storageid'])) { |
|
| 60 | + $this->id = 'object::store:' . $params['storageid']; |
|
| 61 | + } else { |
|
| 62 | + $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 63 | + } |
|
| 64 | + if (isset($params['objectPrefix'])) { |
|
| 65 | + $this->objectPrefix = $params['objectPrefix']; |
|
| 66 | + } |
|
| 67 | + //initialize cache with root directory in cache |
|
| 68 | + if (!$this->is_dir('/')) { |
|
| 69 | + $this->mkdir('/'); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $this->logger = \OC::$server->getLogger(); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function mkdir($path) { |
|
| 76 | + $path = $this->normalizePath($path); |
|
| 77 | + |
|
| 78 | + if ($this->file_exists($path)) { |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $mTime = time(); |
|
| 83 | + $data = [ |
|
| 84 | + 'mimetype' => 'httpd/unix-directory', |
|
| 85 | + 'size' => 0, |
|
| 86 | + 'mtime' => $mTime, |
|
| 87 | + 'storage_mtime' => $mTime, |
|
| 88 | + 'permissions' => \OCP\Constants::PERMISSION_ALL, |
|
| 89 | + ]; |
|
| 90 | + if ($path === '') { |
|
| 91 | + //create root on the fly |
|
| 92 | + $data['etag'] = $this->getETag(''); |
|
| 93 | + $this->getCache()->put('', $data); |
|
| 94 | + return true; |
|
| 95 | + } else { |
|
| 96 | + // if parent does not exist, create it |
|
| 97 | + $parent = $this->normalizePath(dirname($path)); |
|
| 98 | + $parentType = $this->filetype($parent); |
|
| 99 | + if ($parentType === false) { |
|
| 100 | + if (!$this->mkdir($parent)) { |
|
| 101 | + // something went wrong |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + } else if ($parentType === 'file') { |
|
| 105 | + // parent is a file |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + // finally create the new dir |
|
| 109 | + $mTime = time(); // update mtime |
|
| 110 | + $data['mtime'] = $mTime; |
|
| 111 | + $data['storage_mtime'] = $mTime; |
|
| 112 | + $data['etag'] = $this->getETag($path); |
|
| 113 | + $this->getCache()->put($path, $data); |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param string $path |
|
| 120 | + * @return string |
|
| 121 | + */ |
|
| 122 | + private function normalizePath($path) { |
|
| 123 | + $path = trim($path, '/'); |
|
| 124 | + //FIXME why do we sometimes get a path like 'files//username'? |
|
| 125 | + $path = str_replace('//', '/', $path); |
|
| 126 | + |
|
| 127 | + // dirname('/folder') returns '.' but internally (in the cache) we store the root as '' |
|
| 128 | + if (!$path || $path === '.') { |
|
| 129 | + $path = ''; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $path; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Object Stores use a NoopScanner because metadata is directly stored in |
|
| 137 | + * the file cache and cannot really scan the filesystem. The storage passed in is not used anywhere. |
|
| 138 | + * |
|
| 139 | + * @param string $path |
|
| 140 | + * @param \OC\Files\Storage\Storage (optional) the storage to pass to the scanner |
|
| 141 | + * @return \OC\Files\ObjectStore\NoopScanner |
|
| 142 | + */ |
|
| 143 | + public function getScanner($path = '', $storage = null) { |
|
| 144 | + if (!$storage) { |
|
| 145 | + $storage = $this; |
|
| 146 | + } |
|
| 147 | + if (!isset($this->scanner)) { |
|
| 148 | + $this->scanner = new NoopScanner($storage); |
|
| 149 | + } |
|
| 150 | + return $this->scanner; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function getId() { |
|
| 154 | + return $this->id; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + public function rmdir($path) { |
|
| 158 | + $path = $this->normalizePath($path); |
|
| 159 | + |
|
| 160 | + if (!$this->is_dir($path)) { |
|
| 161 | + return false; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->rmObjects($path); |
|
| 165 | + |
|
| 166 | + $this->getCache()->remove($path); |
|
| 167 | + |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + private function rmObjects($path) { |
|
| 172 | + $children = $this->getCache()->getFolderContents($path); |
|
| 173 | + foreach ($children as $child) { |
|
| 174 | + if ($child['mimetype'] === 'httpd/unix-directory') { |
|
| 175 | + $this->rmObjects($child['path']); |
|
| 176 | + } else { |
|
| 177 | + $this->unlink($child['path']); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + public function unlink($path) { |
|
| 183 | + $path = $this->normalizePath($path); |
|
| 184 | + $stat = $this->stat($path); |
|
| 185 | + |
|
| 186 | + if ($stat && isset($stat['fileid'])) { |
|
| 187 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 188 | + return $this->rmdir($path); |
|
| 189 | + } |
|
| 190 | + try { |
|
| 191 | + $this->objectStore->deleteObject($this->getURN($stat['fileid'])); |
|
| 192 | + } catch (\Exception $ex) { |
|
| 193 | + if ($ex->getCode() !== 404) { |
|
| 194 | + $this->logger->logException($ex, [ |
|
| 195 | + 'app' => 'objectstore', |
|
| 196 | + 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 197 | + ]); |
|
| 198 | + return false; |
|
| 199 | + } else { |
|
| 200 | + //removing from cache is ok as it does not exist in the objectstore anyway |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + $this->getCache()->remove($path); |
|
| 204 | + return true; |
|
| 205 | + } |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function stat($path) { |
|
| 210 | + $path = $this->normalizePath($path); |
|
| 211 | + $cacheEntry = $this->getCache()->get($path); |
|
| 212 | + if ($cacheEntry instanceof CacheEntry) { |
|
| 213 | + return $cacheEntry->getData(); |
|
| 214 | + } else { |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Override this method if you need a different unique resource identifier for your object storage implementation. |
|
| 221 | + * The default implementations just appends the fileId to 'urn:oid:'. Make sure the URN is unique over all users. |
|
| 222 | + * You may need a mapping table to store your URN if it cannot be generated from the fileid. |
|
| 223 | + * |
|
| 224 | + * @param int $fileId the fileid |
|
| 225 | + * @return null|string the unified resource name used to identify the object |
|
| 226 | + */ |
|
| 227 | + protected function getURN($fileId) { |
|
| 228 | + if (is_numeric($fileId)) { |
|
| 229 | + return $this->objectPrefix . $fileId; |
|
| 230 | + } |
|
| 231 | + return null; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + public function opendir($path) { |
|
| 235 | + $path = $this->normalizePath($path); |
|
| 236 | + |
|
| 237 | + try { |
|
| 238 | + $files = array(); |
|
| 239 | + $folderContents = $this->getCache()->getFolderContents($path); |
|
| 240 | + foreach ($folderContents as $file) { |
|
| 241 | + $files[] = $file['name']; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return IteratorDirectory::wrap($files); |
|
| 245 | + } catch (\Exception $e) { |
|
| 246 | + $this->logger->logException($e); |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + public function filetype($path) { |
|
| 252 | + $path = $this->normalizePath($path); |
|
| 253 | + $stat = $this->stat($path); |
|
| 254 | + if ($stat) { |
|
| 255 | + if ($stat['mimetype'] === 'httpd/unix-directory') { |
|
| 256 | + return 'dir'; |
|
| 257 | + } |
|
| 258 | + return 'file'; |
|
| 259 | + } else { |
|
| 260 | + return false; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + public function fopen($path, $mode) { |
|
| 265 | + $path = $this->normalizePath($path); |
|
| 266 | + |
|
| 267 | + switch ($mode) { |
|
| 268 | + case 'r': |
|
| 269 | + case 'rb': |
|
| 270 | + $stat = $this->stat($path); |
|
| 271 | + if (is_array($stat)) { |
|
| 272 | + try { |
|
| 273 | + return $this->objectStore->readObject($this->getURN($stat['fileid'])); |
|
| 274 | + } catch (NotFoundException $e) { |
|
| 275 | + $this->logger->logException($e, [ |
|
| 276 | + 'app' => 'objectstore', |
|
| 277 | + 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 278 | + ]); |
|
| 279 | + throw $e; |
|
| 280 | + } catch (\Exception $ex) { |
|
| 281 | + $this->logger->logException($ex, [ |
|
| 282 | + 'app' => 'objectstore', |
|
| 283 | + 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 284 | + ]); |
|
| 285 | + return false; |
|
| 286 | + } |
|
| 287 | + } else { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + case 'w': |
|
| 291 | + case 'wb': |
|
| 292 | + case 'a': |
|
| 293 | + case 'ab': |
|
| 294 | + case 'r+': |
|
| 295 | + case 'w+': |
|
| 296 | + case 'wb+': |
|
| 297 | + case 'a+': |
|
| 298 | + case 'x': |
|
| 299 | + case 'x+': |
|
| 300 | + case 'c': |
|
| 301 | + case 'c+': |
|
| 302 | + if (strrpos($path, '.') !== false) { |
|
| 303 | + $ext = substr($path, strrpos($path, '.')); |
|
| 304 | + } else { |
|
| 305 | + $ext = ''; |
|
| 306 | + } |
|
| 307 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext); |
|
| 308 | + if ($this->file_exists($path)) { |
|
| 309 | + $source = $this->fopen($path, 'r'); |
|
| 310 | + file_put_contents($tmpFile, $source); |
|
| 311 | + } |
|
| 312 | + $handle = fopen($tmpFile, $mode); |
|
| 313 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 314 | + $this->writeBack($tmpFile, $path); |
|
| 315 | + }); |
|
| 316 | + } |
|
| 317 | + return false; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function file_exists($path) { |
|
| 321 | + $path = $this->normalizePath($path); |
|
| 322 | + return (bool)$this->stat($path); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + public function rename($source, $target) { |
|
| 326 | + $source = $this->normalizePath($source); |
|
| 327 | + $target = $this->normalizePath($target); |
|
| 328 | + $this->remove($target); |
|
| 329 | + $this->getCache()->move($source, $target); |
|
| 330 | + $this->touch(dirname($target)); |
|
| 331 | + return true; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + public function getMimeType($path) { |
|
| 335 | + $path = $this->normalizePath($path); |
|
| 336 | + $stat = $this->stat($path); |
|
| 337 | + if (is_array($stat)) { |
|
| 338 | + return $stat['mimetype']; |
|
| 339 | + } else { |
|
| 340 | + return false; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + public function touch($path, $mtime = null) { |
|
| 345 | + if (is_null($mtime)) { |
|
| 346 | + $mtime = time(); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $path = $this->normalizePath($path); |
|
| 350 | + $dirName = dirname($path); |
|
| 351 | + $parentExists = $this->is_dir($dirName); |
|
| 352 | + if (!$parentExists) { |
|
| 353 | + return false; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $stat = $this->stat($path); |
|
| 357 | + if (is_array($stat)) { |
|
| 358 | + // update existing mtime in db |
|
| 359 | + $stat['mtime'] = $mtime; |
|
| 360 | + $this->getCache()->update($stat['fileid'], $stat); |
|
| 361 | + } else { |
|
| 362 | + try { |
|
| 363 | + //create a empty file, need to have at least on char to make it |
|
| 364 | + // work with all object storage implementations |
|
| 365 | + $this->file_put_contents($path, ' '); |
|
| 366 | + $mimeType = \OC::$server->getMimeTypeDetector()->detectPath($path); |
|
| 367 | + $stat = array( |
|
| 368 | + 'etag' => $this->getETag($path), |
|
| 369 | + 'mimetype' => $mimeType, |
|
| 370 | + 'size' => 0, |
|
| 371 | + 'mtime' => $mtime, |
|
| 372 | + 'storage_mtime' => $mtime, |
|
| 373 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 374 | + ); |
|
| 375 | + $this->getCache()->put($path, $stat); |
|
| 376 | + } catch (\Exception $ex) { |
|
| 377 | + $this->logger->logException($ex, [ |
|
| 378 | + 'app' => 'objectstore', |
|
| 379 | + 'message' => 'Could not create object for ' . $path, |
|
| 380 | + ]); |
|
| 381 | + return false; |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + return true; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + public function writeBack($tmpFile, $path) { |
|
| 388 | + $stat = $this->stat($path); |
|
| 389 | + if (empty($stat)) { |
|
| 390 | + // create new file |
|
| 391 | + $stat = array( |
|
| 392 | + 'permissions' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE, |
|
| 393 | + ); |
|
| 394 | + } |
|
| 395 | + // update stat with new data |
|
| 396 | + $mTime = time(); |
|
| 397 | + $stat['size'] = filesize($tmpFile); |
|
| 398 | + $stat['mtime'] = $mTime; |
|
| 399 | + $stat['storage_mtime'] = $mTime; |
|
| 400 | + |
|
| 401 | + // run path based detection first, to use file extension because $tmpFile is only a random string |
|
| 402 | + $mimetypeDetector = \OC::$server->getMimeTypeDetector(); |
|
| 403 | + $mimetype = $mimetypeDetector->detectPath($path); |
|
| 404 | + if ($mimetype === 'application/octet-stream') { |
|
| 405 | + $mimetype = $mimetypeDetector->detect($tmpFile); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + $stat['mimetype'] = $mimetype; |
|
| 409 | + $stat['etag'] = $this->getETag($path); |
|
| 410 | + |
|
| 411 | + $fileId = $this->getCache()->put($path, $stat); |
|
| 412 | + try { |
|
| 413 | + //upload to object storage |
|
| 414 | + $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); |
|
| 415 | + } catch (\Exception $ex) { |
|
| 416 | + $this->getCache()->remove($path); |
|
| 417 | + $this->logger->logException($ex, [ |
|
| 418 | + 'app' => 'objectstore', |
|
| 419 | + 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 420 | + ]); |
|
| 421 | + throw $ex; // make this bubble up |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * external changes are not supported, exclusive access to the object storage is assumed |
|
| 427 | + * |
|
| 428 | + * @param string $path |
|
| 429 | + * @param int $time |
|
| 430 | + * @return false |
|
| 431 | + */ |
|
| 432 | + public function hasUpdated($path, $time) { |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + public function needsPartFile() { |
|
| 437 | + return false; |
|
| 438 | + } |
|
| 439 | 439 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | throw new \Exception('missing IObjectStore instance'); |
| 58 | 58 | } |
| 59 | 59 | if (isset($params['storageid'])) { |
| 60 | - $this->id = 'object::store:' . $params['storageid']; |
|
| 60 | + $this->id = 'object::store:'.$params['storageid']; |
|
| 61 | 61 | } else { |
| 62 | - $this->id = 'object::store:' . $this->objectStore->getStorageId(); |
|
| 62 | + $this->id = 'object::store:'.$this->objectStore->getStorageId(); |
|
| 63 | 63 | } |
| 64 | 64 | if (isset($params['objectPrefix'])) { |
| 65 | 65 | $this->objectPrefix = $params['objectPrefix']; |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | if ($ex->getCode() !== 404) { |
| 194 | 194 | $this->logger->logException($ex, [ |
| 195 | 195 | 'app' => 'objectstore', |
| 196 | - 'message' => 'Could not delete object ' . $this->getURN($stat['fileid']) . ' for ' . $path, |
|
| 196 | + 'message' => 'Could not delete object '.$this->getURN($stat['fileid']).' for '.$path, |
|
| 197 | 197 | ]); |
| 198 | 198 | return false; |
| 199 | 199 | } else { |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | */ |
| 227 | 227 | protected function getURN($fileId) { |
| 228 | 228 | if (is_numeric($fileId)) { |
| 229 | - return $this->objectPrefix . $fileId; |
|
| 229 | + return $this->objectPrefix.$fileId; |
|
| 230 | 230 | } |
| 231 | 231 | return null; |
| 232 | 232 | } |
@@ -274,13 +274,13 @@ discard block |
||
| 274 | 274 | } catch (NotFoundException $e) { |
| 275 | 275 | $this->logger->logException($e, [ |
| 276 | 276 | 'app' => 'objectstore', |
| 277 | - 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 277 | + 'message' => 'Could not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 278 | 278 | ]); |
| 279 | 279 | throw $e; |
| 280 | 280 | } catch (\Exception $ex) { |
| 281 | 281 | $this->logger->logException($ex, [ |
| 282 | 282 | 'app' => 'objectstore', |
| 283 | - 'message' => 'Could not get object ' . $this->getURN($stat['fileid']) . ' for file ' . $path, |
|
| 283 | + 'message' => 'Could not get object '.$this->getURN($stat['fileid']).' for file '.$path, |
|
| 284 | 284 | ]); |
| 285 | 285 | return false; |
| 286 | 286 | } |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | file_put_contents($tmpFile, $source); |
| 311 | 311 | } |
| 312 | 312 | $handle = fopen($tmpFile, $mode); |
| 313 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 313 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 314 | 314 | $this->writeBack($tmpFile, $path); |
| 315 | 315 | }); |
| 316 | 316 | } |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | |
| 320 | 320 | public function file_exists($path) { |
| 321 | 321 | $path = $this->normalizePath($path); |
| 322 | - return (bool)$this->stat($path); |
|
| 322 | + return (bool) $this->stat($path); |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | public function rename($source, $target) { |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | } catch (\Exception $ex) { |
| 377 | 377 | $this->logger->logException($ex, [ |
| 378 | 378 | 'app' => 'objectstore', |
| 379 | - 'message' => 'Could not create object for ' . $path, |
|
| 379 | + 'message' => 'Could not create object for '.$path, |
|
| 380 | 380 | ]); |
| 381 | 381 | return false; |
| 382 | 382 | } |
@@ -416,7 +416,7 @@ discard block |
||
| 416 | 416 | $this->getCache()->remove($path); |
| 417 | 417 | $this->logger->logException($ex, [ |
| 418 | 418 | 'app' => 'objectstore', |
| 419 | - 'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path, |
|
| 419 | + 'message' => 'Could not create object '.$this->getURN($fileId).' for '.$path, |
|
| 420 | 420 | ]); |
| 421 | 421 | throw $ex; // make this bubble up |
| 422 | 422 | } |
@@ -40,253 +40,253 @@ |
||
| 40 | 40 | |
| 41 | 41 | class Swift implements IObjectStore { |
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var \OpenCloud\OpenStack |
|
| 45 | - */ |
|
| 46 | - private $client; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - private $params; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var \OpenCloud\ObjectStore\Service |
|
| 55 | - */ |
|
| 56 | - private $objectStoreService; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 60 | - */ |
|
| 61 | - private $container; |
|
| 62 | - |
|
| 63 | - private $memcache; |
|
| 64 | - |
|
| 65 | - public function __construct($params) { |
|
| 66 | - if (isset($params['bucket'])) { |
|
| 67 | - $params['container'] = $params['bucket']; |
|
| 68 | - } |
|
| 69 | - if (!isset($params['container'])) { |
|
| 70 | - $params['container'] = 'owncloud'; |
|
| 71 | - } |
|
| 72 | - if (!isset($params['autocreate'])) { |
|
| 73 | - // should only be true for tests |
|
| 74 | - $params['autocreate'] = false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if (isset($params['apiKey'])) { |
|
| 78 | - $this->client = new Rackspace($params['url'], $params); |
|
| 79 | - $cacheKey = $params['username'] . '@' . $params['url'] . '/' . $params['bucket']; |
|
| 80 | - } else { |
|
| 81 | - $this->client = new OpenStack($params['url'], $params); |
|
| 82 | - $cacheKey = $params['username'] . '@' . $params['url'] . '/' . $params['bucket']; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $cacheFactory = \OC::$server->getMemCacheFactory(); |
|
| 86 | - $this->memcache = $cacheFactory->create('swift::' . $cacheKey); |
|
| 87 | - |
|
| 88 | - $this->params = $params; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - protected function init() { |
|
| 92 | - if ($this->container) { |
|
| 93 | - return; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $this->importToken(); |
|
| 97 | - |
|
| 98 | - /** @var Token $token */ |
|
| 99 | - $token = $this->client->getTokenObject(); |
|
| 100 | - |
|
| 101 | - if (!$token || $token->hasExpired()) { |
|
| 102 | - try { |
|
| 103 | - $this->client->authenticate(); |
|
| 104 | - $this->exportToken(); |
|
| 105 | - } catch (ClientErrorResponseException $e) { |
|
| 106 | - $statusCode = $e->getResponse()->getStatusCode(); |
|
| 107 | - if ($statusCode == 412) { |
|
| 108 | - throw new StorageAuthException('Precondition failed, verify the keystone url', $e); |
|
| 109 | - } else if ($statusCode === 401) { |
|
| 110 | - throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); |
|
| 111 | - } else { |
|
| 112 | - throw new StorageAuthException('Unknown error', $e); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** @var Catalog $catalog */ |
|
| 119 | - $catalog = $this->client->getCatalog(); |
|
| 120 | - |
|
| 121 | - if (isset($this->params['serviceName'])) { |
|
| 122 | - $serviceName = $this->params['serviceName']; |
|
| 123 | - } else { |
|
| 124 | - $serviceName = Service::DEFAULT_NAME; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (isset($this->params['urlType'])) { |
|
| 128 | - $urlType = $this->params['urlType']; |
|
| 129 | - if ($urlType !== 'internalURL' && $urlType !== 'publicURL') { |
|
| 130 | - throw new StorageNotAvailableException('Invalid url type'); |
|
| 131 | - } |
|
| 132 | - } else { |
|
| 133 | - $urlType = Service::DEFAULT_URL_TYPE; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $catalogItem = $this->getCatalogForService($catalog, $serviceName); |
|
| 137 | - if (!$catalogItem) { |
|
| 138 | - $available = implode(', ', $this->getAvailableServiceNames($catalog)); |
|
| 139 | - throw new StorageNotAvailableException( |
|
| 140 | - "Service $serviceName not found in service catalog, available services: $available" |
|
| 141 | - ); |
|
| 142 | - } else if (isset($this->params['region'])) { |
|
| 143 | - $this->validateRegion($catalogItem, $this->params['region']); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType); |
|
| 147 | - |
|
| 148 | - try { |
|
| 149 | - $this->container = $this->objectStoreService->getContainer($this->params['container']); |
|
| 150 | - } catch (ClientErrorResponseException $ex) { |
|
| 151 | - // if the container does not exist and autocreate is true try to create the container on the fly |
|
| 152 | - if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) { |
|
| 153 | - $this->container = $this->objectStoreService->createContainer($this->params['container']); |
|
| 154 | - } else { |
|
| 155 | - throw $ex; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - private function exportToken() { |
|
| 161 | - $export = $this->client->exportCredentials(); |
|
| 162 | - $export['catalog'] = array_map(function (CatalogItem $item) { |
|
| 163 | - return [ |
|
| 164 | - 'name' => $item->getName(), |
|
| 165 | - 'endpoints' => $item->getEndpoints(), |
|
| 166 | - 'type' => $item->getType() |
|
| 167 | - ]; |
|
| 168 | - }, $export['catalog']->getItems()); |
|
| 169 | - $this->memcache->set('token', json_encode($export)); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - private function importToken() { |
|
| 173 | - $cachedTokenString = $this->memcache->get('token'); |
|
| 174 | - if ($cachedTokenString) { |
|
| 175 | - $cachedToken = json_decode($cachedTokenString, true); |
|
| 176 | - $cachedToken['catalog'] = array_map(function (array $item) { |
|
| 177 | - $itemClass = new \stdClass(); |
|
| 178 | - $itemClass->name = $item['name']; |
|
| 179 | - $itemClass->endpoints = array_map(function (array $endpoint) { |
|
| 180 | - return (object) $endpoint; |
|
| 181 | - }, $item['endpoints']); |
|
| 182 | - $itemClass->type = $item['type']; |
|
| 183 | - |
|
| 184 | - return $itemClass; |
|
| 185 | - }, $cachedToken['catalog']); |
|
| 186 | - try { |
|
| 187 | - $this->client->importCredentials($cachedToken); |
|
| 188 | - } catch (\Exception $e) { |
|
| 189 | - $this->client->setTokenObject(new Token()); |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @param Catalog $catalog |
|
| 196 | - * @param $name |
|
| 197 | - * @return null|CatalogItem |
|
| 198 | - */ |
|
| 199 | - private function getCatalogForService(Catalog $catalog, $name) { |
|
| 200 | - foreach ($catalog->getItems() as $item) { |
|
| 201 | - /** @var CatalogItem $item */ |
|
| 202 | - if ($item->hasType(Service::DEFAULT_TYPE) && $item->hasName($name)) { |
|
| 203 | - return $item; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return null; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - private function validateRegion(CatalogItem $item, $region) { |
|
| 211 | - $endPoints = $item->getEndpoints(); |
|
| 212 | - foreach ($endPoints as $endPoint) { |
|
| 213 | - if ($endPoint->region === $region) { |
|
| 214 | - return; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $availableRegions = implode(', ', array_map(function ($endpoint) { |
|
| 219 | - return $endpoint->region; |
|
| 220 | - }, $endPoints)); |
|
| 221 | - |
|
| 222 | - throw new StorageNotAvailableException("Invalid region '$region', available regions: $availableRegions"); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - private function getAvailableServiceNames(Catalog $catalog) { |
|
| 226 | - return array_map(function (CatalogItem $item) { |
|
| 227 | - return $item->getName(); |
|
| 228 | - }, array_filter($catalog->getItems(), function (CatalogItem $item) { |
|
| 229 | - return $item->hasType(Service::DEFAULT_TYPE); |
|
| 230 | - })); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @return string the container name where objects are stored |
|
| 235 | - */ |
|
| 236 | - public function getStorageId() { |
|
| 237 | - return $this->params['container']; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @param string $urn the unified resource name used to identify the object |
|
| 242 | - * @param resource $stream stream with the data to write |
|
| 243 | - * @throws Exception from openstack lib when something goes wrong |
|
| 244 | - */ |
|
| 245 | - public function writeObject($urn, $stream) { |
|
| 246 | - $this->init(); |
|
| 247 | - $this->container->uploadObject($urn, $stream); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param string $urn the unified resource name used to identify the object |
|
| 252 | - * @return resource stream with the read data |
|
| 253 | - * @throws Exception from openstack lib when something goes wrong |
|
| 254 | - */ |
|
| 255 | - public function readObject($urn) { |
|
| 256 | - try { |
|
| 257 | - $this->init(); |
|
| 258 | - $object = $this->container->getObject($urn); |
|
| 259 | - |
|
| 260 | - // we need to keep a reference to objectContent or |
|
| 261 | - // the stream will be closed before we can do anything with it |
|
| 262 | - /** @var $objectContent \Guzzle\Http\EntityBody * */ |
|
| 263 | - $objectContent = $object->getContent(); |
|
| 264 | - } catch (ObjectNotFoundException $e) { |
|
| 265 | - throw new NotFoundException("object $urn not found in object store"); |
|
| 266 | - } |
|
| 267 | - $objectContent->rewind(); |
|
| 268 | - |
|
| 269 | - $stream = $objectContent->getStream(); |
|
| 270 | - // save the object content in the context of the stream to prevent it being gc'd until the stream is closed |
|
| 271 | - stream_context_set_option($stream, 'swift', 'content', $objectContent); |
|
| 272 | - |
|
| 273 | - return RetryWrapper::wrap($stream); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @param string $urn Unified Resource Name |
|
| 278 | - * @return void |
|
| 279 | - * @throws Exception from openstack lib when something goes wrong |
|
| 280 | - */ |
|
| 281 | - public function deleteObject($urn) { |
|
| 282 | - $this->init(); |
|
| 283 | - // see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242 |
|
| 284 | - $this->container->dataObject()->setName($urn)->delete(); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - public function deleteContainer($recursive = false) { |
|
| 288 | - $this->init(); |
|
| 289 | - $this->container->delete($recursive); |
|
| 290 | - } |
|
| 43 | + /** |
|
| 44 | + * @var \OpenCloud\OpenStack |
|
| 45 | + */ |
|
| 46 | + private $client; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + private $params; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var \OpenCloud\ObjectStore\Service |
|
| 55 | + */ |
|
| 56 | + private $objectStoreService; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var \OpenCloud\ObjectStore\Resource\Container |
|
| 60 | + */ |
|
| 61 | + private $container; |
|
| 62 | + |
|
| 63 | + private $memcache; |
|
| 64 | + |
|
| 65 | + public function __construct($params) { |
|
| 66 | + if (isset($params['bucket'])) { |
|
| 67 | + $params['container'] = $params['bucket']; |
|
| 68 | + } |
|
| 69 | + if (!isset($params['container'])) { |
|
| 70 | + $params['container'] = 'owncloud'; |
|
| 71 | + } |
|
| 72 | + if (!isset($params['autocreate'])) { |
|
| 73 | + // should only be true for tests |
|
| 74 | + $params['autocreate'] = false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if (isset($params['apiKey'])) { |
|
| 78 | + $this->client = new Rackspace($params['url'], $params); |
|
| 79 | + $cacheKey = $params['username'] . '@' . $params['url'] . '/' . $params['bucket']; |
|
| 80 | + } else { |
|
| 81 | + $this->client = new OpenStack($params['url'], $params); |
|
| 82 | + $cacheKey = $params['username'] . '@' . $params['url'] . '/' . $params['bucket']; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $cacheFactory = \OC::$server->getMemCacheFactory(); |
|
| 86 | + $this->memcache = $cacheFactory->create('swift::' . $cacheKey); |
|
| 87 | + |
|
| 88 | + $this->params = $params; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + protected function init() { |
|
| 92 | + if ($this->container) { |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $this->importToken(); |
|
| 97 | + |
|
| 98 | + /** @var Token $token */ |
|
| 99 | + $token = $this->client->getTokenObject(); |
|
| 100 | + |
|
| 101 | + if (!$token || $token->hasExpired()) { |
|
| 102 | + try { |
|
| 103 | + $this->client->authenticate(); |
|
| 104 | + $this->exportToken(); |
|
| 105 | + } catch (ClientErrorResponseException $e) { |
|
| 106 | + $statusCode = $e->getResponse()->getStatusCode(); |
|
| 107 | + if ($statusCode == 412) { |
|
| 108 | + throw new StorageAuthException('Precondition failed, verify the keystone url', $e); |
|
| 109 | + } else if ($statusCode === 401) { |
|
| 110 | + throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e); |
|
| 111 | + } else { |
|
| 112 | + throw new StorageAuthException('Unknown error', $e); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** @var Catalog $catalog */ |
|
| 119 | + $catalog = $this->client->getCatalog(); |
|
| 120 | + |
|
| 121 | + if (isset($this->params['serviceName'])) { |
|
| 122 | + $serviceName = $this->params['serviceName']; |
|
| 123 | + } else { |
|
| 124 | + $serviceName = Service::DEFAULT_NAME; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (isset($this->params['urlType'])) { |
|
| 128 | + $urlType = $this->params['urlType']; |
|
| 129 | + if ($urlType !== 'internalURL' && $urlType !== 'publicURL') { |
|
| 130 | + throw new StorageNotAvailableException('Invalid url type'); |
|
| 131 | + } |
|
| 132 | + } else { |
|
| 133 | + $urlType = Service::DEFAULT_URL_TYPE; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $catalogItem = $this->getCatalogForService($catalog, $serviceName); |
|
| 137 | + if (!$catalogItem) { |
|
| 138 | + $available = implode(', ', $this->getAvailableServiceNames($catalog)); |
|
| 139 | + throw new StorageNotAvailableException( |
|
| 140 | + "Service $serviceName not found in service catalog, available services: $available" |
|
| 141 | + ); |
|
| 142 | + } else if (isset($this->params['region'])) { |
|
| 143 | + $this->validateRegion($catalogItem, $this->params['region']); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType); |
|
| 147 | + |
|
| 148 | + try { |
|
| 149 | + $this->container = $this->objectStoreService->getContainer($this->params['container']); |
|
| 150 | + } catch (ClientErrorResponseException $ex) { |
|
| 151 | + // if the container does not exist and autocreate is true try to create the container on the fly |
|
| 152 | + if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) { |
|
| 153 | + $this->container = $this->objectStoreService->createContainer($this->params['container']); |
|
| 154 | + } else { |
|
| 155 | + throw $ex; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + private function exportToken() { |
|
| 161 | + $export = $this->client->exportCredentials(); |
|
| 162 | + $export['catalog'] = array_map(function (CatalogItem $item) { |
|
| 163 | + return [ |
|
| 164 | + 'name' => $item->getName(), |
|
| 165 | + 'endpoints' => $item->getEndpoints(), |
|
| 166 | + 'type' => $item->getType() |
|
| 167 | + ]; |
|
| 168 | + }, $export['catalog']->getItems()); |
|
| 169 | + $this->memcache->set('token', json_encode($export)); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + private function importToken() { |
|
| 173 | + $cachedTokenString = $this->memcache->get('token'); |
|
| 174 | + if ($cachedTokenString) { |
|
| 175 | + $cachedToken = json_decode($cachedTokenString, true); |
|
| 176 | + $cachedToken['catalog'] = array_map(function (array $item) { |
|
| 177 | + $itemClass = new \stdClass(); |
|
| 178 | + $itemClass->name = $item['name']; |
|
| 179 | + $itemClass->endpoints = array_map(function (array $endpoint) { |
|
| 180 | + return (object) $endpoint; |
|
| 181 | + }, $item['endpoints']); |
|
| 182 | + $itemClass->type = $item['type']; |
|
| 183 | + |
|
| 184 | + return $itemClass; |
|
| 185 | + }, $cachedToken['catalog']); |
|
| 186 | + try { |
|
| 187 | + $this->client->importCredentials($cachedToken); |
|
| 188 | + } catch (\Exception $e) { |
|
| 189 | + $this->client->setTokenObject(new Token()); |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @param Catalog $catalog |
|
| 196 | + * @param $name |
|
| 197 | + * @return null|CatalogItem |
|
| 198 | + */ |
|
| 199 | + private function getCatalogForService(Catalog $catalog, $name) { |
|
| 200 | + foreach ($catalog->getItems() as $item) { |
|
| 201 | + /** @var CatalogItem $item */ |
|
| 202 | + if ($item->hasType(Service::DEFAULT_TYPE) && $item->hasName($name)) { |
|
| 203 | + return $item; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return null; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + private function validateRegion(CatalogItem $item, $region) { |
|
| 211 | + $endPoints = $item->getEndpoints(); |
|
| 212 | + foreach ($endPoints as $endPoint) { |
|
| 213 | + if ($endPoint->region === $region) { |
|
| 214 | + return; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $availableRegions = implode(', ', array_map(function ($endpoint) { |
|
| 219 | + return $endpoint->region; |
|
| 220 | + }, $endPoints)); |
|
| 221 | + |
|
| 222 | + throw new StorageNotAvailableException("Invalid region '$region', available regions: $availableRegions"); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + private function getAvailableServiceNames(Catalog $catalog) { |
|
| 226 | + return array_map(function (CatalogItem $item) { |
|
| 227 | + return $item->getName(); |
|
| 228 | + }, array_filter($catalog->getItems(), function (CatalogItem $item) { |
|
| 229 | + return $item->hasType(Service::DEFAULT_TYPE); |
|
| 230 | + })); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @return string the container name where objects are stored |
|
| 235 | + */ |
|
| 236 | + public function getStorageId() { |
|
| 237 | + return $this->params['container']; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @param string $urn the unified resource name used to identify the object |
|
| 242 | + * @param resource $stream stream with the data to write |
|
| 243 | + * @throws Exception from openstack lib when something goes wrong |
|
| 244 | + */ |
|
| 245 | + public function writeObject($urn, $stream) { |
|
| 246 | + $this->init(); |
|
| 247 | + $this->container->uploadObject($urn, $stream); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param string $urn the unified resource name used to identify the object |
|
| 252 | + * @return resource stream with the read data |
|
| 253 | + * @throws Exception from openstack lib when something goes wrong |
|
| 254 | + */ |
|
| 255 | + public function readObject($urn) { |
|
| 256 | + try { |
|
| 257 | + $this->init(); |
|
| 258 | + $object = $this->container->getObject($urn); |
|
| 259 | + |
|
| 260 | + // we need to keep a reference to objectContent or |
|
| 261 | + // the stream will be closed before we can do anything with it |
|
| 262 | + /** @var $objectContent \Guzzle\Http\EntityBody * */ |
|
| 263 | + $objectContent = $object->getContent(); |
|
| 264 | + } catch (ObjectNotFoundException $e) { |
|
| 265 | + throw new NotFoundException("object $urn not found in object store"); |
|
| 266 | + } |
|
| 267 | + $objectContent->rewind(); |
|
| 268 | + |
|
| 269 | + $stream = $objectContent->getStream(); |
|
| 270 | + // save the object content in the context of the stream to prevent it being gc'd until the stream is closed |
|
| 271 | + stream_context_set_option($stream, 'swift', 'content', $objectContent); |
|
| 272 | + |
|
| 273 | + return RetryWrapper::wrap($stream); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @param string $urn Unified Resource Name |
|
| 278 | + * @return void |
|
| 279 | + * @throws Exception from openstack lib when something goes wrong |
|
| 280 | + */ |
|
| 281 | + public function deleteObject($urn) { |
|
| 282 | + $this->init(); |
|
| 283 | + // see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242 |
|
| 284 | + $this->container->dataObject()->setName($urn)->delete(); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + public function deleteContainer($recursive = false) { |
|
| 288 | + $this->init(); |
|
| 289 | + $this->container->delete($recursive); |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | 292 | } |
@@ -59,521 +59,521 @@ |
||
| 59 | 59 | |
| 60 | 60 | class File extends Node implements IFile { |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Updates the data |
|
| 64 | - * |
|
| 65 | - * The data argument is a readable stream resource. |
|
| 66 | - * |
|
| 67 | - * After a successful put operation, you may choose to return an ETag. The |
|
| 68 | - * etag must always be surrounded by double-quotes. These quotes must |
|
| 69 | - * appear in the actual string you're returning. |
|
| 70 | - * |
|
| 71 | - * Clients may use the ETag from a PUT request to later on make sure that |
|
| 72 | - * when they update the file, the contents haven't changed in the mean |
|
| 73 | - * time. |
|
| 74 | - * |
|
| 75 | - * If you don't plan to store the file byte-by-byte, and you return a |
|
| 76 | - * different object on a subsequent GET you are strongly recommended to not |
|
| 77 | - * return an ETag, and just return null. |
|
| 78 | - * |
|
| 79 | - * @param resource $data |
|
| 80 | - * |
|
| 81 | - * @throws Forbidden |
|
| 82 | - * @throws UnsupportedMediaType |
|
| 83 | - * @throws BadRequest |
|
| 84 | - * @throws Exception |
|
| 85 | - * @throws EntityTooLarge |
|
| 86 | - * @throws ServiceUnavailable |
|
| 87 | - * @throws FileLocked |
|
| 88 | - * @return string|null |
|
| 89 | - */ |
|
| 90 | - public function put($data) { |
|
| 91 | - try { |
|
| 92 | - $exists = $this->fileView->file_exists($this->path); |
|
| 93 | - if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 94 | - throw new Forbidden(); |
|
| 95 | - } |
|
| 96 | - } catch (StorageNotAvailableException $e) { |
|
| 97 | - throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // verify path of the target |
|
| 101 | - $this->verifyPath(); |
|
| 102 | - |
|
| 103 | - // chunked handling |
|
| 104 | - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 105 | - try { |
|
| 106 | - return $this->createFileChunked($data); |
|
| 107 | - } catch (\Exception $e) { |
|
| 108 | - $this->convertToSabreException($e); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - list($partStorage) = $this->fileView->resolvePath($this->path); |
|
| 113 | - $needsPartFile = $this->needsPartFile($partStorage) && (strlen($this->path) > 1); |
|
| 114 | - |
|
| 115 | - if ($needsPartFile) { |
|
| 116 | - // mark file as partial while uploading (ignored by the scanner) |
|
| 117 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 118 | - } else { |
|
| 119 | - // upload file directly as the final path |
|
| 120 | - $partFilePath = $this->path; |
|
| 121 | - |
|
| 122 | - $this->emitPreHooks($exists); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 126 | - /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 127 | - list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
| 128 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 129 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 130 | - try { |
|
| 131 | - $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 132 | - if ($target === false) { |
|
| 133 | - \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::fopen() failed', \OCP\Util::ERROR); |
|
| 134 | - // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 135 | - throw new Exception('Could not write file contents'); |
|
| 136 | - } |
|
| 137 | - list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 138 | - fclose($target); |
|
| 139 | - |
|
| 140 | - if ($result === false) { |
|
| 141 | - $expected = -1; |
|
| 142 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 143 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 144 | - } |
|
| 145 | - throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // if content length is sent by client: |
|
| 149 | - // double check if the file was fully received |
|
| 150 | - // compare expected and actual size |
|
| 151 | - if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 152 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 153 | - if ($count != $expected) { |
|
| 154 | - throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - } catch (\Exception $e) { |
|
| 159 | - if ($needsPartFile) { |
|
| 160 | - $partStorage->unlink($internalPartPath); |
|
| 161 | - } |
|
| 162 | - $this->convertToSabreException($e); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - try { |
|
| 166 | - $view = \OC\Files\Filesystem::getView(); |
|
| 167 | - $run = ($view && $needsPartFile) ? $this->emitPreHooks($exists) : true; |
|
| 168 | - |
|
| 169 | - try { |
|
| 170 | - $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 171 | - } catch (LockedException $e) { |
|
| 172 | - if ($needsPartFile) { |
|
| 173 | - $partStorage->unlink($internalPartPath); |
|
| 174 | - } |
|
| 175 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($needsPartFile) { |
|
| 179 | - // rename to correct path |
|
| 180 | - try { |
|
| 181 | - if ($run) { |
|
| 182 | - $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 183 | - $fileExists = $storage->file_exists($internalPath); |
|
| 184 | - } |
|
| 185 | - if (!$run || $renameOkay === false || $fileExists === false) { |
|
| 186 | - \OCP\Util::writeLog('webdav', 'renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', \OCP\Util::ERROR); |
|
| 187 | - throw new Exception('Could not rename part file to final file'); |
|
| 188 | - } |
|
| 189 | - } catch (ForbiddenException $ex) { |
|
| 190 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 191 | - } catch (\Exception $e) { |
|
| 192 | - $partStorage->unlink($internalPartPath); |
|
| 193 | - $this->convertToSabreException($e); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 198 | - $storage->getUpdater()->update($internalPath); |
|
| 199 | - |
|
| 200 | - try { |
|
| 201 | - $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 202 | - } catch (LockedException $e) { |
|
| 203 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - // allow sync clients to send the mtime along in a header |
|
| 207 | - $request = \OC::$server->getRequest(); |
|
| 208 | - if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 209 | - $mtimeStr = $request->server['HTTP_X_OC_MTIME']; |
|
| 210 | - if (!is_numeric($mtimeStr)) { |
|
| 211 | - throw new \InvalidArgumentException('X-OC-Mtime header must be an integer (unix timestamp).'); |
|
| 212 | - } |
|
| 213 | - $mtime = intval($mtimeStr); |
|
| 214 | - if ($this->fileView->touch($this->path, $mtime)) { |
|
| 215 | - header('X-OC-MTime: accepted'); |
|
| 216 | - } |
|
| 217 | - } |
|
| 62 | + /** |
|
| 63 | + * Updates the data |
|
| 64 | + * |
|
| 65 | + * The data argument is a readable stream resource. |
|
| 66 | + * |
|
| 67 | + * After a successful put operation, you may choose to return an ETag. The |
|
| 68 | + * etag must always be surrounded by double-quotes. These quotes must |
|
| 69 | + * appear in the actual string you're returning. |
|
| 70 | + * |
|
| 71 | + * Clients may use the ETag from a PUT request to later on make sure that |
|
| 72 | + * when they update the file, the contents haven't changed in the mean |
|
| 73 | + * time. |
|
| 74 | + * |
|
| 75 | + * If you don't plan to store the file byte-by-byte, and you return a |
|
| 76 | + * different object on a subsequent GET you are strongly recommended to not |
|
| 77 | + * return an ETag, and just return null. |
|
| 78 | + * |
|
| 79 | + * @param resource $data |
|
| 80 | + * |
|
| 81 | + * @throws Forbidden |
|
| 82 | + * @throws UnsupportedMediaType |
|
| 83 | + * @throws BadRequest |
|
| 84 | + * @throws Exception |
|
| 85 | + * @throws EntityTooLarge |
|
| 86 | + * @throws ServiceUnavailable |
|
| 87 | + * @throws FileLocked |
|
| 88 | + * @return string|null |
|
| 89 | + */ |
|
| 90 | + public function put($data) { |
|
| 91 | + try { |
|
| 92 | + $exists = $this->fileView->file_exists($this->path); |
|
| 93 | + if ($this->info && $exists && !$this->info->isUpdateable()) { |
|
| 94 | + throw new Forbidden(); |
|
| 95 | + } |
|
| 96 | + } catch (StorageNotAvailableException $e) { |
|
| 97 | + throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // verify path of the target |
|
| 101 | + $this->verifyPath(); |
|
| 102 | + |
|
| 103 | + // chunked handling |
|
| 104 | + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 105 | + try { |
|
| 106 | + return $this->createFileChunked($data); |
|
| 107 | + } catch (\Exception $e) { |
|
| 108 | + $this->convertToSabreException($e); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + list($partStorage) = $this->fileView->resolvePath($this->path); |
|
| 113 | + $needsPartFile = $this->needsPartFile($partStorage) && (strlen($this->path) > 1); |
|
| 114 | + |
|
| 115 | + if ($needsPartFile) { |
|
| 116 | + // mark file as partial while uploading (ignored by the scanner) |
|
| 117 | + $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 118 | + } else { |
|
| 119 | + // upload file directly as the final path |
|
| 120 | + $partFilePath = $this->path; |
|
| 121 | + |
|
| 122 | + $this->emitPreHooks($exists); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
| 126 | + /** @var \OC\Files\Storage\Storage $partStorage */ |
|
| 127 | + list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); |
|
| 128 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 129 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 130 | + try { |
|
| 131 | + $target = $partStorage->fopen($internalPartPath, 'wb'); |
|
| 132 | + if ($target === false) { |
|
| 133 | + \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::fopen() failed', \OCP\Util::ERROR); |
|
| 134 | + // because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
| 135 | + throw new Exception('Could not write file contents'); |
|
| 136 | + } |
|
| 137 | + list($count, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 138 | + fclose($target); |
|
| 139 | + |
|
| 140 | + if ($result === false) { |
|
| 141 | + $expected = -1; |
|
| 142 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 143 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 144 | + } |
|
| 145 | + throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // if content length is sent by client: |
|
| 149 | + // double check if the file was fully received |
|
| 150 | + // compare expected and actual size |
|
| 151 | + if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 152 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 153 | + if ($count != $expected) { |
|
| 154 | + throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + } catch (\Exception $e) { |
|
| 159 | + if ($needsPartFile) { |
|
| 160 | + $partStorage->unlink($internalPartPath); |
|
| 161 | + } |
|
| 162 | + $this->convertToSabreException($e); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + try { |
|
| 166 | + $view = \OC\Files\Filesystem::getView(); |
|
| 167 | + $run = ($view && $needsPartFile) ? $this->emitPreHooks($exists) : true; |
|
| 168 | + |
|
| 169 | + try { |
|
| 170 | + $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
| 171 | + } catch (LockedException $e) { |
|
| 172 | + if ($needsPartFile) { |
|
| 173 | + $partStorage->unlink($internalPartPath); |
|
| 174 | + } |
|
| 175 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($needsPartFile) { |
|
| 179 | + // rename to correct path |
|
| 180 | + try { |
|
| 181 | + if ($run) { |
|
| 182 | + $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
| 183 | + $fileExists = $storage->file_exists($internalPath); |
|
| 184 | + } |
|
| 185 | + if (!$run || $renameOkay === false || $fileExists === false) { |
|
| 186 | + \OCP\Util::writeLog('webdav', 'renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', \OCP\Util::ERROR); |
|
| 187 | + throw new Exception('Could not rename part file to final file'); |
|
| 188 | + } |
|
| 189 | + } catch (ForbiddenException $ex) { |
|
| 190 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 191 | + } catch (\Exception $e) { |
|
| 192 | + $partStorage->unlink($internalPartPath); |
|
| 193 | + $this->convertToSabreException($e); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 198 | + $storage->getUpdater()->update($internalPath); |
|
| 199 | + |
|
| 200 | + try { |
|
| 201 | + $this->changeLock(ILockingProvider::LOCK_SHARED); |
|
| 202 | + } catch (LockedException $e) { |
|
| 203 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // allow sync clients to send the mtime along in a header |
|
| 207 | + $request = \OC::$server->getRequest(); |
|
| 208 | + if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 209 | + $mtimeStr = $request->server['HTTP_X_OC_MTIME']; |
|
| 210 | + if (!is_numeric($mtimeStr)) { |
|
| 211 | + throw new \InvalidArgumentException('X-OC-Mtime header must be an integer (unix timestamp).'); |
|
| 212 | + } |
|
| 213 | + $mtime = intval($mtimeStr); |
|
| 214 | + if ($this->fileView->touch($this->path, $mtime)) { |
|
| 215 | + header('X-OC-MTime: accepted'); |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - if ($view) { |
|
| 220 | - $this->emitPostHooks($exists); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $this->refreshInfo(); |
|
| 224 | - |
|
| 225 | - if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 226 | - $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 227 | - $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 228 | - $this->refreshInfo(); |
|
| 229 | - } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 230 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 231 | - $this->refreshInfo(); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - } catch (StorageNotAvailableException $e) { |
|
| 235 | - throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return '"' . $this->info->getEtag() . '"'; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - private function getPartFileBasePath($path) { |
|
| 242 | - $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 243 | - if ($partFileInStorage) { |
|
| 244 | - return $path; |
|
| 245 | - } else { |
|
| 246 | - return md5($path); // will place it in the root of the view with a unique name |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param string $path |
|
| 252 | - */ |
|
| 253 | - private function emitPreHooks($exists, $path = null) { |
|
| 254 | - if (is_null($path)) { |
|
| 255 | - $path = $this->path; |
|
| 256 | - } |
|
| 257 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 258 | - $run = true; |
|
| 259 | - |
|
| 260 | - if (!$exists) { |
|
| 261 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
| 262 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 263 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 264 | - )); |
|
| 265 | - } else { |
|
| 266 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
| 267 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 268 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 269 | - )); |
|
| 270 | - } |
|
| 271 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
| 272 | - \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 273 | - \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 274 | - )); |
|
| 275 | - return $run; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * @param string $path |
|
| 280 | - */ |
|
| 281 | - private function emitPostHooks($exists, $path = null) { |
|
| 282 | - if (is_null($path)) { |
|
| 283 | - $path = $this->path; |
|
| 284 | - } |
|
| 285 | - $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 286 | - if (!$exists) { |
|
| 287 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
| 288 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 289 | - )); |
|
| 290 | - } else { |
|
| 291 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
| 292 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 293 | - )); |
|
| 294 | - } |
|
| 295 | - \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
| 296 | - \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 297 | - )); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Returns the data |
|
| 302 | - * |
|
| 303 | - * @return resource |
|
| 304 | - * @throws Forbidden |
|
| 305 | - * @throws ServiceUnavailable |
|
| 306 | - */ |
|
| 307 | - public function get() { |
|
| 308 | - //throw exception if encryption is disabled but files are still encrypted |
|
| 309 | - try { |
|
| 310 | - if (!$this->info->isReadable()) { |
|
| 311 | - // do a if the file did not exist |
|
| 312 | - throw new NotFound(); |
|
| 313 | - } |
|
| 314 | - $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 315 | - if ($res === false) { |
|
| 316 | - throw new ServiceUnavailable("Could not open file"); |
|
| 317 | - } |
|
| 318 | - return $res; |
|
| 319 | - } catch (GenericEncryptionException $e) { |
|
| 320 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 321 | - throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
| 322 | - } catch (StorageNotAvailableException $e) { |
|
| 323 | - throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
| 324 | - } catch (ForbiddenException $ex) { |
|
| 325 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 326 | - } catch (LockedException $e) { |
|
| 327 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 328 | - } catch (NotFoundException $e) { |
|
| 329 | - throw new NotFound('File not found: ' . $e->getMessage(), $e->getCode(), $e); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Delete the current file |
|
| 335 | - * |
|
| 336 | - * @throws Forbidden |
|
| 337 | - * @throws ServiceUnavailable |
|
| 338 | - */ |
|
| 339 | - public function delete() { |
|
| 340 | - if (!$this->info->isDeletable()) { |
|
| 341 | - throw new Forbidden(); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - try { |
|
| 345 | - if (!$this->fileView->unlink($this->path)) { |
|
| 346 | - // assume it wasn't possible to delete due to permissions |
|
| 347 | - throw new Forbidden(); |
|
| 348 | - } |
|
| 349 | - } catch (StorageNotAvailableException $e) { |
|
| 350 | - throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
| 351 | - } catch (ForbiddenException $ex) { |
|
| 352 | - throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 353 | - } catch (LockedException $e) { |
|
| 354 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Returns the mime-type for a file |
|
| 360 | - * |
|
| 361 | - * If null is returned, we'll assume application/octet-stream |
|
| 362 | - * |
|
| 363 | - * @return string |
|
| 364 | - */ |
|
| 365 | - public function getContentType() { |
|
| 366 | - $mimeType = $this->info->getMimetype(); |
|
| 367 | - |
|
| 368 | - // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 369 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 370 | - return $mimeType; |
|
| 371 | - } |
|
| 372 | - return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * @return array|false |
|
| 377 | - */ |
|
| 378 | - public function getDirectDownload() { |
|
| 379 | - if (\OCP\App::isEnabled('encryption')) { |
|
| 380 | - return []; |
|
| 381 | - } |
|
| 382 | - /** @var \OCP\Files\Storage $storage */ |
|
| 383 | - list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 384 | - if (is_null($storage)) { |
|
| 385 | - return []; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - return $storage->getDirectDownload($internalPath); |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * @param resource $data |
|
| 393 | - * @return null|string |
|
| 394 | - * @throws Exception |
|
| 395 | - * @throws BadRequest |
|
| 396 | - * @throws NotImplemented |
|
| 397 | - * @throws ServiceUnavailable |
|
| 398 | - */ |
|
| 399 | - private function createFileChunked($data) { |
|
| 400 | - list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path); |
|
| 401 | - |
|
| 402 | - $info = \OC_FileChunking::decodeName($name); |
|
| 403 | - if (empty($info)) { |
|
| 404 | - throw new NotImplemented('Invalid chunk name'); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - $chunk_handler = new \OC_FileChunking($info); |
|
| 408 | - $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 409 | - |
|
| 410 | - //detect aborted upload |
|
| 411 | - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 412 | - if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 413 | - $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 414 | - if ($bytesWritten != $expected) { |
|
| 415 | - $chunk_handler->remove($info['index']); |
|
| 416 | - throw new BadRequest( |
|
| 417 | - 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - if ($chunk_handler->isComplete()) { |
|
| 423 | - list($storage,) = $this->fileView->resolvePath($path); |
|
| 424 | - $needsPartFile = $this->needsPartFile($storage); |
|
| 425 | - $partFile = null; |
|
| 426 | - |
|
| 427 | - $targetPath = $path . '/' . $info['name']; |
|
| 428 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 429 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 430 | - |
|
| 431 | - $exists = $this->fileView->file_exists($targetPath); |
|
| 432 | - |
|
| 433 | - try { |
|
| 434 | - $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 435 | - |
|
| 436 | - $this->emitPreHooks($exists, $targetPath); |
|
| 437 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 438 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 439 | - list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 440 | - |
|
| 441 | - if ($needsPartFile) { |
|
| 442 | - // we first assembly the target file as a part file |
|
| 443 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 444 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 445 | - list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
| 446 | - |
|
| 447 | - |
|
| 448 | - $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 449 | - |
|
| 450 | - // here is the final atomic rename |
|
| 451 | - $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 452 | - $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 453 | - if ($renameOkay === false || $fileExists === false) { |
|
| 454 | - \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::rename() failed', \OCP\Util::ERROR); |
|
| 455 | - // only delete if an error occurred and the target file was already created |
|
| 456 | - if ($fileExists) { |
|
| 457 | - // set to null to avoid double-deletion when handling exception |
|
| 458 | - // stray part file |
|
| 459 | - $partFile = null; |
|
| 460 | - $targetStorage->unlink($targetInternalPath); |
|
| 461 | - } |
|
| 462 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 463 | - throw new Exception('Could not rename part file assembled from chunks'); |
|
| 464 | - } |
|
| 465 | - } else { |
|
| 466 | - // assemble directly into the final file |
|
| 467 | - $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - // allow sync clients to send the mtime along in a header |
|
| 471 | - $request = \OC::$server->getRequest(); |
|
| 472 | - if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 473 | - if ($targetStorage->touch($targetInternalPath, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 474 | - header('X-OC-MTime: accepted'); |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 479 | - $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 480 | - |
|
| 481 | - $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 482 | - |
|
| 483 | - $this->emitPostHooks($exists, $targetPath); |
|
| 484 | - |
|
| 485 | - // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 486 | - $info = $this->fileView->getFileInfo($targetPath); |
|
| 487 | - |
|
| 488 | - if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 489 | - $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 490 | - $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 491 | - } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 492 | - $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 496 | - |
|
| 497 | - return $info->getEtag(); |
|
| 498 | - } catch (\Exception $e) { |
|
| 499 | - if ($partFile !== null) { |
|
| 500 | - $targetStorage->unlink($targetInternalPath); |
|
| 501 | - } |
|
| 502 | - $this->convertToSabreException($e); |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - return null; |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Returns whether a part file is needed for the given storage |
|
| 511 | - * or whether the file can be assembled/uploaded directly on the |
|
| 512 | - * target storage. |
|
| 513 | - * |
|
| 514 | - * @param \OCP\Files\Storage $storage |
|
| 515 | - * @return bool true if the storage needs part file handling |
|
| 516 | - */ |
|
| 517 | - private function needsPartFile($storage) { |
|
| 518 | - // TODO: in the future use ChunkHandler provided by storage |
|
| 519 | - return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') && |
|
| 520 | - !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud') && |
|
| 521 | - $storage->needsPartFile(); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * Convert the given exception to a SabreException instance |
|
| 526 | - * |
|
| 527 | - * @param \Exception $e |
|
| 528 | - * |
|
| 529 | - * @throws \Sabre\DAV\Exception |
|
| 530 | - */ |
|
| 531 | - private function convertToSabreException(\Exception $e) { |
|
| 532 | - if ($e instanceof \Sabre\DAV\Exception) { |
|
| 533 | - throw $e; |
|
| 534 | - } |
|
| 535 | - if ($e instanceof NotPermittedException) { |
|
| 536 | - // a more general case - due to whatever reason the content could not be written |
|
| 537 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 538 | - } |
|
| 539 | - if ($e instanceof ForbiddenException) { |
|
| 540 | - // the path for the file was forbidden |
|
| 541 | - throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 542 | - } |
|
| 543 | - if ($e instanceof EntityTooLargeException) { |
|
| 544 | - // the file is too big to be stored |
|
| 545 | - throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 546 | - } |
|
| 547 | - if ($e instanceof InvalidContentException) { |
|
| 548 | - // the file content is not permitted |
|
| 549 | - throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 550 | - } |
|
| 551 | - if ($e instanceof InvalidPathException) { |
|
| 552 | - // the path for the file was not valid |
|
| 553 | - // TODO: find proper http status code for this case |
|
| 554 | - throw new Forbidden($e->getMessage(), 0, $e); |
|
| 555 | - } |
|
| 556 | - if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 557 | - // the file is currently being written to by another process |
|
| 558 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 559 | - } |
|
| 560 | - if ($e instanceof GenericEncryptionException) { |
|
| 561 | - // returning 503 will allow retry of the operation at a later point in time |
|
| 562 | - throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
| 563 | - } |
|
| 564 | - if ($e instanceof StorageNotAvailableException) { |
|
| 565 | - throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * Get the checksum for this file |
|
| 573 | - * |
|
| 574 | - * @return string |
|
| 575 | - */ |
|
| 576 | - public function getChecksum() { |
|
| 577 | - return $this->info->getChecksum(); |
|
| 578 | - } |
|
| 219 | + if ($view) { |
|
| 220 | + $this->emitPostHooks($exists); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $this->refreshInfo(); |
|
| 224 | + |
|
| 225 | + if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 226 | + $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 227 | + $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
| 228 | + $this->refreshInfo(); |
|
| 229 | + } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
| 230 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 231 | + $this->refreshInfo(); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + } catch (StorageNotAvailableException $e) { |
|
| 235 | + throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return '"' . $this->info->getEtag() . '"'; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + private function getPartFileBasePath($path) { |
|
| 242 | + $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
| 243 | + if ($partFileInStorage) { |
|
| 244 | + return $path; |
|
| 245 | + } else { |
|
| 246 | + return md5($path); // will place it in the root of the view with a unique name |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param string $path |
|
| 252 | + */ |
|
| 253 | + private function emitPreHooks($exists, $path = null) { |
|
| 254 | + if (is_null($path)) { |
|
| 255 | + $path = $this->path; |
|
| 256 | + } |
|
| 257 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 258 | + $run = true; |
|
| 259 | + |
|
| 260 | + if (!$exists) { |
|
| 261 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array( |
|
| 262 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 263 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 264 | + )); |
|
| 265 | + } else { |
|
| 266 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array( |
|
| 267 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 268 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 269 | + )); |
|
| 270 | + } |
|
| 271 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array( |
|
| 272 | + \OC\Files\Filesystem::signal_param_path => $hookPath, |
|
| 273 | + \OC\Files\Filesystem::signal_param_run => &$run, |
|
| 274 | + )); |
|
| 275 | + return $run; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * @param string $path |
|
| 280 | + */ |
|
| 281 | + private function emitPostHooks($exists, $path = null) { |
|
| 282 | + if (is_null($path)) { |
|
| 283 | + $path = $this->path; |
|
| 284 | + } |
|
| 285 | + $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
| 286 | + if (!$exists) { |
|
| 287 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( |
|
| 288 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 289 | + )); |
|
| 290 | + } else { |
|
| 291 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( |
|
| 292 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 293 | + )); |
|
| 294 | + } |
|
| 295 | + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( |
|
| 296 | + \OC\Files\Filesystem::signal_param_path => $hookPath |
|
| 297 | + )); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Returns the data |
|
| 302 | + * |
|
| 303 | + * @return resource |
|
| 304 | + * @throws Forbidden |
|
| 305 | + * @throws ServiceUnavailable |
|
| 306 | + */ |
|
| 307 | + public function get() { |
|
| 308 | + //throw exception if encryption is disabled but files are still encrypted |
|
| 309 | + try { |
|
| 310 | + if (!$this->info->isReadable()) { |
|
| 311 | + // do a if the file did not exist |
|
| 312 | + throw new NotFound(); |
|
| 313 | + } |
|
| 314 | + $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
| 315 | + if ($res === false) { |
|
| 316 | + throw new ServiceUnavailable("Could not open file"); |
|
| 317 | + } |
|
| 318 | + return $res; |
|
| 319 | + } catch (GenericEncryptionException $e) { |
|
| 320 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 321 | + throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
| 322 | + } catch (StorageNotAvailableException $e) { |
|
| 323 | + throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
| 324 | + } catch (ForbiddenException $ex) { |
|
| 325 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 326 | + } catch (LockedException $e) { |
|
| 327 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 328 | + } catch (NotFoundException $e) { |
|
| 329 | + throw new NotFound('File not found: ' . $e->getMessage(), $e->getCode(), $e); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Delete the current file |
|
| 335 | + * |
|
| 336 | + * @throws Forbidden |
|
| 337 | + * @throws ServiceUnavailable |
|
| 338 | + */ |
|
| 339 | + public function delete() { |
|
| 340 | + if (!$this->info->isDeletable()) { |
|
| 341 | + throw new Forbidden(); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + try { |
|
| 345 | + if (!$this->fileView->unlink($this->path)) { |
|
| 346 | + // assume it wasn't possible to delete due to permissions |
|
| 347 | + throw new Forbidden(); |
|
| 348 | + } |
|
| 349 | + } catch (StorageNotAvailableException $e) { |
|
| 350 | + throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
| 351 | + } catch (ForbiddenException $ex) { |
|
| 352 | + throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
| 353 | + } catch (LockedException $e) { |
|
| 354 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Returns the mime-type for a file |
|
| 360 | + * |
|
| 361 | + * If null is returned, we'll assume application/octet-stream |
|
| 362 | + * |
|
| 363 | + * @return string |
|
| 364 | + */ |
|
| 365 | + public function getContentType() { |
|
| 366 | + $mimeType = $this->info->getMimetype(); |
|
| 367 | + |
|
| 368 | + // PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
| 369 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
| 370 | + return $mimeType; |
|
| 371 | + } |
|
| 372 | + return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * @return array|false |
|
| 377 | + */ |
|
| 378 | + public function getDirectDownload() { |
|
| 379 | + if (\OCP\App::isEnabled('encryption')) { |
|
| 380 | + return []; |
|
| 381 | + } |
|
| 382 | + /** @var \OCP\Files\Storage $storage */ |
|
| 383 | + list($storage, $internalPath) = $this->fileView->resolvePath($this->path); |
|
| 384 | + if (is_null($storage)) { |
|
| 385 | + return []; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + return $storage->getDirectDownload($internalPath); |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * @param resource $data |
|
| 393 | + * @return null|string |
|
| 394 | + * @throws Exception |
|
| 395 | + * @throws BadRequest |
|
| 396 | + * @throws NotImplemented |
|
| 397 | + * @throws ServiceUnavailable |
|
| 398 | + */ |
|
| 399 | + private function createFileChunked($data) { |
|
| 400 | + list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path); |
|
| 401 | + |
|
| 402 | + $info = \OC_FileChunking::decodeName($name); |
|
| 403 | + if (empty($info)) { |
|
| 404 | + throw new NotImplemented('Invalid chunk name'); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + $chunk_handler = new \OC_FileChunking($info); |
|
| 408 | + $bytesWritten = $chunk_handler->store($info['index'], $data); |
|
| 409 | + |
|
| 410 | + //detect aborted upload |
|
| 411 | + if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
| 412 | + if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
| 413 | + $expected = $_SERVER['CONTENT_LENGTH']; |
|
| 414 | + if ($bytesWritten != $expected) { |
|
| 415 | + $chunk_handler->remove($info['index']); |
|
| 416 | + throw new BadRequest( |
|
| 417 | + 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + if ($chunk_handler->isComplete()) { |
|
| 423 | + list($storage,) = $this->fileView->resolvePath($path); |
|
| 424 | + $needsPartFile = $this->needsPartFile($storage); |
|
| 425 | + $partFile = null; |
|
| 426 | + |
|
| 427 | + $targetPath = $path . '/' . $info['name']; |
|
| 428 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 429 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 430 | + |
|
| 431 | + $exists = $this->fileView->file_exists($targetPath); |
|
| 432 | + |
|
| 433 | + try { |
|
| 434 | + $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 435 | + |
|
| 436 | + $this->emitPreHooks($exists, $targetPath); |
|
| 437 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 438 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 439 | + list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
|
| 440 | + |
|
| 441 | + if ($needsPartFile) { |
|
| 442 | + // we first assembly the target file as a part file |
|
| 443 | + $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 444 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 445 | + list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
|
| 446 | + |
|
| 447 | + |
|
| 448 | + $chunk_handler->file_assemble($partStorage, $partInternalPath); |
|
| 449 | + |
|
| 450 | + // here is the final atomic rename |
|
| 451 | + $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath); |
|
| 452 | + $fileExists = $targetStorage->file_exists($targetInternalPath); |
|
| 453 | + if ($renameOkay === false || $fileExists === false) { |
|
| 454 | + \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::rename() failed', \OCP\Util::ERROR); |
|
| 455 | + // only delete if an error occurred and the target file was already created |
|
| 456 | + if ($fileExists) { |
|
| 457 | + // set to null to avoid double-deletion when handling exception |
|
| 458 | + // stray part file |
|
| 459 | + $partFile = null; |
|
| 460 | + $targetStorage->unlink($targetInternalPath); |
|
| 461 | + } |
|
| 462 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 463 | + throw new Exception('Could not rename part file assembled from chunks'); |
|
| 464 | + } |
|
| 465 | + } else { |
|
| 466 | + // assemble directly into the final file |
|
| 467 | + $chunk_handler->file_assemble($targetStorage, $targetInternalPath); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + // allow sync clients to send the mtime along in a header |
|
| 471 | + $request = \OC::$server->getRequest(); |
|
| 472 | + if (isset($request->server['HTTP_X_OC_MTIME'])) { |
|
| 473 | + if ($targetStorage->touch($targetInternalPath, $request->server['HTTP_X_OC_MTIME'])) { |
|
| 474 | + header('X-OC-MTime: accepted'); |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + // since we skipped the view we need to scan and emit the hooks ourselves |
|
| 479 | + $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 480 | + |
|
| 481 | + $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 482 | + |
|
| 483 | + $this->emitPostHooks($exists, $targetPath); |
|
| 484 | + |
|
| 485 | + // FIXME: should call refreshInfo but can't because $this->path is not the of the final file |
|
| 486 | + $info = $this->fileView->getFileInfo($targetPath); |
|
| 487 | + |
|
| 488 | + if (isset($request->server['HTTP_OC_CHECKSUM'])) { |
|
| 489 | + $checksum = trim($request->server['HTTP_OC_CHECKSUM']); |
|
| 490 | + $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]); |
|
| 491 | + } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') { |
|
| 492 | + $this->fileView->putFileInfo($this->path, ['checksum' => '']); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED); |
|
| 496 | + |
|
| 497 | + return $info->getEtag(); |
|
| 498 | + } catch (\Exception $e) { |
|
| 499 | + if ($partFile !== null) { |
|
| 500 | + $targetStorage->unlink($targetInternalPath); |
|
| 501 | + } |
|
| 502 | + $this->convertToSabreException($e); |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + return null; |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Returns whether a part file is needed for the given storage |
|
| 511 | + * or whether the file can be assembled/uploaded directly on the |
|
| 512 | + * target storage. |
|
| 513 | + * |
|
| 514 | + * @param \OCP\Files\Storage $storage |
|
| 515 | + * @return bool true if the storage needs part file handling |
|
| 516 | + */ |
|
| 517 | + private function needsPartFile($storage) { |
|
| 518 | + // TODO: in the future use ChunkHandler provided by storage |
|
| 519 | + return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') && |
|
| 520 | + !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud') && |
|
| 521 | + $storage->needsPartFile(); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * Convert the given exception to a SabreException instance |
|
| 526 | + * |
|
| 527 | + * @param \Exception $e |
|
| 528 | + * |
|
| 529 | + * @throws \Sabre\DAV\Exception |
|
| 530 | + */ |
|
| 531 | + private function convertToSabreException(\Exception $e) { |
|
| 532 | + if ($e instanceof \Sabre\DAV\Exception) { |
|
| 533 | + throw $e; |
|
| 534 | + } |
|
| 535 | + if ($e instanceof NotPermittedException) { |
|
| 536 | + // a more general case - due to whatever reason the content could not be written |
|
| 537 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 538 | + } |
|
| 539 | + if ($e instanceof ForbiddenException) { |
|
| 540 | + // the path for the file was forbidden |
|
| 541 | + throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
| 542 | + } |
|
| 543 | + if ($e instanceof EntityTooLargeException) { |
|
| 544 | + // the file is too big to be stored |
|
| 545 | + throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
| 546 | + } |
|
| 547 | + if ($e instanceof InvalidContentException) { |
|
| 548 | + // the file content is not permitted |
|
| 549 | + throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
| 550 | + } |
|
| 551 | + if ($e instanceof InvalidPathException) { |
|
| 552 | + // the path for the file was not valid |
|
| 553 | + // TODO: find proper http status code for this case |
|
| 554 | + throw new Forbidden($e->getMessage(), 0, $e); |
|
| 555 | + } |
|
| 556 | + if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
| 557 | + // the file is currently being written to by another process |
|
| 558 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 559 | + } |
|
| 560 | + if ($e instanceof GenericEncryptionException) { |
|
| 561 | + // returning 503 will allow retry of the operation at a later point in time |
|
| 562 | + throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
| 563 | + } |
|
| 564 | + if ($e instanceof StorageNotAvailableException) { |
|
| 565 | + throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * Get the checksum for this file |
|
| 573 | + * |
|
| 574 | + * @return string |
|
| 575 | + */ |
|
| 576 | + public function getChecksum() { |
|
| 577 | + return $this->info->getChecksum(); |
|
| 578 | + } |
|
| 579 | 579 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | throw new Forbidden(); |
| 95 | 95 | } |
| 96 | 96 | } catch (StorageNotAvailableException $e) { |
| 97 | - throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); |
|
| 97 | + throw new ServiceUnavailable("File is not updatable: ".$e->getMessage()); |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // verify path of the target |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | if ($needsPartFile) { |
| 116 | 116 | // mark file as partial while uploading (ignored by the scanner) |
| 117 | - $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
| 117 | + $partFilePath = $this->getPartFileBasePath($this->path).'.ocTransferId'.rand().'.part'; |
|
| 118 | 118 | } else { |
| 119 | 119 | // upload file directly as the final path |
| 120 | 120 | $partFilePath = $this->path; |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | if (isset($_SERVER['CONTENT_LENGTH'])) { |
| 143 | 143 | $expected = $_SERVER['CONTENT_LENGTH']; |
| 144 | 144 | } |
| 145 | - throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )'); |
|
| 145 | + throw new Exception('Error while copying file to target location (copied bytes: '.$count.', expected filesize: '.$expected.' )'); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | // if content length is sent by client: |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
| 152 | 152 | $expected = $_SERVER['CONTENT_LENGTH']; |
| 153 | 153 | if ($count != $expected) { |
| 154 | - throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); |
|
| 154 | + throw new BadRequest('expected filesize '.$expected.' got '.$count); |
|
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $fileExists = $storage->file_exists($internalPath); |
| 184 | 184 | } |
| 185 | 185 | if (!$run || $renameOkay === false || $fileExists === false) { |
| 186 | - \OCP\Util::writeLog('webdav', 'renaming part file to final file failed ($run: ' . ( $run ? 'true' : 'false' ) . ', $renameOkay: ' . ( $renameOkay ? 'true' : 'false' ) . ', $fileExists: ' . ( $fileExists ? 'true' : 'false' ) . ')', \OCP\Util::ERROR); |
|
| 186 | + \OCP\Util::writeLog('webdav', 'renaming part file to final file failed ($run: '.($run ? 'true' : 'false').', $renameOkay: '.($renameOkay ? 'true' : 'false').', $fileExists: '.($fileExists ? 'true' : 'false').')', \OCP\Util::ERROR); |
|
| 187 | 187 | throw new Exception('Could not rename part file to final file'); |
| 188 | 188 | } |
| 189 | 189 | } catch (ForbiddenException $ex) { |
@@ -232,10 +232,10 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | } catch (StorageNotAvailableException $e) { |
| 235 | - throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); |
|
| 235 | + throw new ServiceUnavailable("Failed to check file size: ".$e->getMessage()); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - return '"' . $this->info->getEtag() . '"'; |
|
| 238 | + return '"'.$this->info->getEtag().'"'; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | private function getPartFileBasePath($path) { |
@@ -318,15 +318,15 @@ discard block |
||
| 318 | 318 | return $res; |
| 319 | 319 | } catch (GenericEncryptionException $e) { |
| 320 | 320 | // returning 503 will allow retry of the operation at a later point in time |
| 321 | - throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); |
|
| 321 | + throw new ServiceUnavailable("Encryption not ready: ".$e->getMessage()); |
|
| 322 | 322 | } catch (StorageNotAvailableException $e) { |
| 323 | - throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); |
|
| 323 | + throw new ServiceUnavailable("Failed to open file: ".$e->getMessage()); |
|
| 324 | 324 | } catch (ForbiddenException $ex) { |
| 325 | 325 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
| 326 | 326 | } catch (LockedException $e) { |
| 327 | 327 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
| 328 | 328 | } catch (NotFoundException $e) { |
| 329 | - throw new NotFound('File not found: ' . $e->getMessage(), $e->getCode(), $e); |
|
| 329 | + throw new NotFound('File not found: '.$e->getMessage(), $e->getCode(), $e); |
|
| 330 | 330 | } |
| 331 | 331 | } |
| 332 | 332 | |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | throw new Forbidden(); |
| 348 | 348 | } |
| 349 | 349 | } catch (StorageNotAvailableException $e) { |
| 350 | - throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); |
|
| 350 | + throw new ServiceUnavailable("Failed to unlink: ".$e->getMessage()); |
|
| 351 | 351 | } catch (ForbiddenException $ex) { |
| 352 | 352 | throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
| 353 | 353 | } catch (LockedException $e) { |
@@ -414,7 +414,7 @@ discard block |
||
| 414 | 414 | if ($bytesWritten != $expected) { |
| 415 | 415 | $chunk_handler->remove($info['index']); |
| 416 | 416 | throw new BadRequest( |
| 417 | - 'expected filesize ' . $expected . ' got ' . $bytesWritten); |
|
| 417 | + 'expected filesize '.$expected.' got '.$bytesWritten); |
|
| 418 | 418 | } |
| 419 | 419 | } |
| 420 | 420 | } |
@@ -424,7 +424,7 @@ discard block |
||
| 424 | 424 | $needsPartFile = $this->needsPartFile($storage); |
| 425 | 425 | $partFile = null; |
| 426 | 426 | |
| 427 | - $targetPath = $path . '/' . $info['name']; |
|
| 427 | + $targetPath = $path.'/'.$info['name']; |
|
| 428 | 428 | /** @var \OC\Files\Storage\Storage $targetStorage */ |
| 429 | 429 | list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath); |
| 430 | 430 | |
@@ -440,7 +440,7 @@ discard block |
||
| 440 | 440 | |
| 441 | 441 | if ($needsPartFile) { |
| 442 | 442 | // we first assembly the target file as a part file |
| 443 | - $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part'; |
|
| 443 | + $partFile = $this->getPartFileBasePath($path.'/'.$info['name']).'.ocTransferId'.$info['transferid'].'.part'; |
|
| 444 | 444 | /** @var \OC\Files\Storage\Storage $targetStorage */ |
| 445 | 445 | list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile); |
| 446 | 446 | |
@@ -559,10 +559,10 @@ discard block |
||
| 559 | 559 | } |
| 560 | 560 | if ($e instanceof GenericEncryptionException) { |
| 561 | 561 | // returning 503 will allow retry of the operation at a later point in time |
| 562 | - throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e); |
|
| 562 | + throw new ServiceUnavailable('Encryption not ready: '.$e->getMessage(), 0, $e); |
|
| 563 | 563 | } |
| 564 | 564 | if ($e instanceof StorageNotAvailableException) { |
| 565 | - throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e); |
|
| 565 | + throw new ServiceUnavailable('Failed to write file contents: '.$e->getMessage(), 0, $e); |
|
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |