@@ -55,805 +55,805 @@ |
||
| 55 | 55 | |
| 56 | 56 | class Storage { |
| 57 | 57 | |
| 58 | - const DEFAULTENABLED=true; |
|
| 59 | - const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota |
|
| 60 | - const VERSIONS_ROOT = 'files_versions/'; |
|
| 61 | - |
|
| 62 | - const DELETE_TRIGGER_MASTER_REMOVED = 0; |
|
| 63 | - const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1; |
|
| 64 | - const DELETE_TRIGGER_QUOTA_EXCEEDED = 2; |
|
| 65 | - |
|
| 66 | - // files for which we can remove the versions after the delete operation was successful |
|
| 67 | - private static $deletedFiles = array(); |
|
| 68 | - |
|
| 69 | - private static $sourcePathAndUser = array(); |
|
| 70 | - |
|
| 71 | - private static $max_versions_per_interval = array( |
|
| 72 | - //first 10sec, one version every 2sec |
|
| 73 | - 1 => array('intervalEndsAfter' => 10, 'step' => 2), |
|
| 74 | - //next minute, one version every 10sec |
|
| 75 | - 2 => array('intervalEndsAfter' => 60, 'step' => 10), |
|
| 76 | - //next hour, one version every minute |
|
| 77 | - 3 => array('intervalEndsAfter' => 3600, 'step' => 60), |
|
| 78 | - //next 24h, one version every hour |
|
| 79 | - 4 => array('intervalEndsAfter' => 86400, 'step' => 3600), |
|
| 80 | - //next 30days, one version per day |
|
| 81 | - 5 => array('intervalEndsAfter' => 2592000, 'step' => 86400), |
|
| 82 | - //until the end one version per week |
|
| 83 | - 6 => array('intervalEndsAfter' => -1, 'step' => 604800), |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - /** @var \OCA\Files_Versions\AppInfo\Application */ |
|
| 87 | - private static $application; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * get the UID of the owner of the file and the path to the file relative to |
|
| 91 | - * owners files folder |
|
| 92 | - * |
|
| 93 | - * @param string $filename |
|
| 94 | - * @return array |
|
| 95 | - * @throws \OC\User\NoUserException |
|
| 96 | - */ |
|
| 97 | - public static function getUidAndFilename($filename) { |
|
| 98 | - $uid = Filesystem::getOwner($filename); |
|
| 99 | - $userManager = \OC::$server->getUserManager(); |
|
| 100 | - // if the user with the UID doesn't exists, e.g. because the UID points |
|
| 101 | - // to a remote user with a federated cloud ID we use the current logged-in |
|
| 102 | - // user. We need a valid local user to create the versions |
|
| 103 | - if (!$userManager->userExists($uid)) { |
|
| 104 | - $uid = User::getUser(); |
|
| 105 | - } |
|
| 106 | - Filesystem::initMountPoints($uid); |
|
| 107 | - if ( $uid !== User::getUser() ) { |
|
| 108 | - $info = Filesystem::getFileInfo($filename); |
|
| 109 | - $ownerView = new View('/'.$uid.'/files'); |
|
| 110 | - try { |
|
| 111 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 112 | - // make sure that the file name doesn't end with a trailing slash |
|
| 113 | - // can for example happen single files shared across servers |
|
| 114 | - $filename = rtrim($filename, '/'); |
|
| 115 | - } catch (NotFoundException $e) { |
|
| 116 | - $filename = null; |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - return [$uid, $filename]; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Remember the owner and the owner path of the source file |
|
| 124 | - * |
|
| 125 | - * @param string $source source path |
|
| 126 | - */ |
|
| 127 | - public static function setSourcePathAndUser($source) { |
|
| 128 | - list($uid, $path) = self::getUidAndFilename($source); |
|
| 129 | - self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Gets the owner and the owner path from the source path |
|
| 134 | - * |
|
| 135 | - * @param string $source source path |
|
| 136 | - * @return array with user id and path |
|
| 137 | - */ |
|
| 138 | - public static function getSourcePathAndUser($source) { |
|
| 139 | - |
|
| 140 | - if (isset(self::$sourcePathAndUser[$source])) { |
|
| 141 | - $uid = self::$sourcePathAndUser[$source]['uid']; |
|
| 142 | - $path = self::$sourcePathAndUser[$source]['path']; |
|
| 143 | - unset(self::$sourcePathAndUser[$source]); |
|
| 144 | - } else { |
|
| 145 | - $uid = $path = false; |
|
| 146 | - } |
|
| 147 | - return array($uid, $path); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * get current size of all versions from a given user |
|
| 152 | - * |
|
| 153 | - * @param string $user user who owns the versions |
|
| 154 | - * @return int versions size |
|
| 155 | - */ |
|
| 156 | - private static function getVersionsSize($user) { |
|
| 157 | - $view = new View('/' . $user); |
|
| 158 | - $fileInfo = $view->getFileInfo('/files_versions'); |
|
| 159 | - return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * store a new version of a file. |
|
| 164 | - */ |
|
| 165 | - public static function store($filename) { |
|
| 166 | - |
|
| 167 | - // if the file gets streamed we need to remove the .part extension |
|
| 168 | - // to get the right target |
|
| 169 | - $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
| 170 | - if ($ext === 'part') { |
|
| 171 | - $filename = substr($filename, 0, -5); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // we only handle existing files |
|
| 175 | - if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
| 176 | - return false; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - list($uid, $filename) = self::getUidAndFilename($filename); |
|
| 180 | - |
|
| 181 | - $files_view = new View('/'.$uid .'/files'); |
|
| 182 | - |
|
| 183 | - $eventDispatcher = \OC::$server->getEventDispatcher(); |
|
| 184 | - $fileInfo = $files_view->getFileInfo($filename); |
|
| 185 | - $id = $fileInfo->getId(); |
|
| 186 | - $nodes = \OC::$server->getRootFolder()->getById($id); |
|
| 187 | - foreach ($nodes as $node) { |
|
| 188 | - $event = new CreateVersionEvent($node); |
|
| 189 | - $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event); |
|
| 190 | - if ($event->shouldCreateVersion() === false) { |
|
| 191 | - return false; |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - // no use making versions for empty files |
|
| 196 | - if ($fileInfo->getSize() === 0) { |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** @var IVersionManager $versionManager */ |
|
| 201 | - $versionManager = \OC::$server->query(IVersionManager::class); |
|
| 202 | - $userManager = \OC::$server->getUserManager(); |
|
| 203 | - $user = $userManager->get($uid); |
|
| 204 | - |
|
| 205 | - $versionManager->createVersion($user, $fileInfo); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * mark file as deleted so that we can remove the versions if the file is gone |
|
| 211 | - * @param string $path |
|
| 212 | - */ |
|
| 213 | - public static function markDeletedFile($path) { |
|
| 214 | - list($uid, $filename) = self::getUidAndFilename($path); |
|
| 215 | - self::$deletedFiles[$path] = array( |
|
| 216 | - 'uid' => $uid, |
|
| 217 | - 'filename' => $filename); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * delete the version from the storage and cache |
|
| 222 | - * |
|
| 223 | - * @param View $view |
|
| 224 | - * @param string $path |
|
| 225 | - */ |
|
| 226 | - protected static function deleteVersion($view, $path) { |
|
| 227 | - $view->unlink($path); |
|
| 228 | - /** |
|
| 229 | - * @var \OC\Files\Storage\Storage $storage |
|
| 230 | - * @var string $internalPath |
|
| 231 | - */ |
|
| 232 | - list($storage, $internalPath) = $view->resolvePath($path); |
|
| 233 | - $cache = $storage->getCache($internalPath); |
|
| 234 | - $cache->remove($internalPath); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Delete versions of a file |
|
| 239 | - */ |
|
| 240 | - public static function delete($path) { |
|
| 241 | - |
|
| 242 | - $deletedFile = self::$deletedFiles[$path]; |
|
| 243 | - $uid = $deletedFile['uid']; |
|
| 244 | - $filename = $deletedFile['filename']; |
|
| 245 | - |
|
| 246 | - if (!Filesystem::file_exists($path)) { |
|
| 247 | - |
|
| 248 | - $view = new View('/' . $uid . '/files_versions'); |
|
| 249 | - |
|
| 250 | - $versions = self::getVersions($uid, $filename); |
|
| 251 | - if (!empty($versions)) { |
|
| 252 | - foreach ($versions as $v) { |
|
| 253 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 254 | - self::deleteVersion($view, $filename . '.v' . $v['version']); |
|
| 255 | - \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - unset(self::$deletedFiles[$path]); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Rename or copy versions of a file of the given paths |
|
| 264 | - * |
|
| 265 | - * @param string $sourcePath source path of the file to move, relative to |
|
| 266 | - * the currently logged in user's "files" folder |
|
| 267 | - * @param string $targetPath target path of the file to move, relative to |
|
| 268 | - * the currently logged in user's "files" folder |
|
| 269 | - * @param string $operation can be 'copy' or 'rename' |
|
| 270 | - */ |
|
| 271 | - public static function renameOrCopy($sourcePath, $targetPath, $operation) { |
|
| 272 | - list($sourceOwner, $sourcePath) = self::getSourcePathAndUser($sourcePath); |
|
| 273 | - |
|
| 274 | - // it was a upload of a existing file if no old path exists |
|
| 275 | - // in this case the pre-hook already called the store method and we can |
|
| 276 | - // stop here |
|
| 277 | - if ($sourcePath === false) { |
|
| 278 | - return true; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - list($targetOwner, $targetPath) = self::getUidAndFilename($targetPath); |
|
| 282 | - |
|
| 283 | - $sourcePath = ltrim($sourcePath, '/'); |
|
| 284 | - $targetPath = ltrim($targetPath, '/'); |
|
| 285 | - |
|
| 286 | - $rootView = new View(''); |
|
| 287 | - |
|
| 288 | - // did we move a directory ? |
|
| 289 | - if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 290 | - // does the directory exists for versions too ? |
|
| 291 | - if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) { |
|
| 292 | - // create missing dirs if necessary |
|
| 293 | - self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 294 | - |
|
| 295 | - // move the directory containing the versions |
|
| 296 | - $rootView->$operation( |
|
| 297 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath, |
|
| 298 | - '/' . $targetOwner . '/files_versions/' . $targetPath |
|
| 299 | - ); |
|
| 300 | - } |
|
| 301 | - } else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) { |
|
| 302 | - // create missing dirs if necessary |
|
| 303 | - self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 304 | - |
|
| 305 | - foreach ($versions as $v) { |
|
| 306 | - // move each version one by one to the target directory |
|
| 307 | - $rootView->$operation( |
|
| 308 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'], |
|
| 309 | - '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version'] |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - // if we moved versions directly for a file, schedule expiration check for that file |
|
| 315 | - if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 316 | - self::scheduleExpire($targetOwner, $targetPath); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Rollback to an old version of a file. |
|
| 323 | - * |
|
| 324 | - * @param string $file file name |
|
| 325 | - * @param int $revision revision timestamp |
|
| 326 | - * @return bool |
|
| 327 | - */ |
|
| 328 | - public static function rollback($file, $revision) { |
|
| 329 | - |
|
| 330 | - // add expected leading slash |
|
| 331 | - $file = '/' . ltrim($file, '/'); |
|
| 332 | - list($uid, $filename) = self::getUidAndFilename($file); |
|
| 333 | - if ($uid === null || trim($filename, '/') === '') { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // Fetch the userfolder to trigger view hooks |
|
| 338 | - $userFolder = \OC::$server->getUserFolder($uid); |
|
| 339 | - |
|
| 340 | - $users_view = new View('/'.$uid); |
|
| 341 | - $files_view = new View('/'. User::getUser().'/files'); |
|
| 342 | - |
|
| 343 | - $versionCreated = false; |
|
| 344 | - |
|
| 345 | - $fileInfo = $files_view->getFileInfo($file); |
|
| 346 | - |
|
| 347 | - // check if user has the permissions to revert a version |
|
| 348 | - if (!$fileInfo->isUpdateable()) { |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - //first create a new version |
|
| 353 | - $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename); |
|
| 354 | - if (!$users_view->file_exists($version)) { |
|
| 355 | - $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename)); |
|
| 356 | - $versionCreated = true; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $fileToRestore = 'files_versions' . $filename . '.v' . $revision; |
|
| 360 | - |
|
| 361 | - // Restore encrypted version of the old file for the newly restored file |
|
| 362 | - // This has to happen manually here since the file is manually copied below |
|
| 363 | - $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion(); |
|
| 364 | - $oldFileInfo = $users_view->getFileInfo($fileToRestore); |
|
| 365 | - $cache = $fileInfo->getStorage()->getCache(); |
|
| 366 | - $cache->update( |
|
| 367 | - $fileInfo->getId(), [ |
|
| 368 | - 'encrypted' => $oldVersion, |
|
| 369 | - 'encryptedVersion' => $oldVersion, |
|
| 370 | - 'size' => $oldFileInfo->getSize() |
|
| 371 | - ] |
|
| 372 | - ); |
|
| 373 | - |
|
| 374 | - // rollback |
|
| 375 | - if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { |
|
| 376 | - $files_view->touch($file, $revision); |
|
| 377 | - Storage::scheduleExpire($uid, $file); |
|
| 378 | - |
|
| 379 | - $node = $userFolder->get($file); |
|
| 380 | - |
|
| 381 | - // TODO: move away from those legacy hooks! |
|
| 382 | - \OC_Hook::emit('\OCP\Versions', 'rollback', array( |
|
| 383 | - 'path' => $filename, |
|
| 384 | - 'revision' => $revision, |
|
| 385 | - 'node' => $node, |
|
| 386 | - )); |
|
| 387 | - return true; |
|
| 388 | - } else if ($versionCreated) { |
|
| 389 | - self::deleteVersion($users_view, $version); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return false; |
|
| 393 | - |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Stream copy file contents from $path1 to $path2 |
|
| 398 | - * |
|
| 399 | - * @param View $view view to use for copying |
|
| 400 | - * @param string $path1 source file to copy |
|
| 401 | - * @param string $path2 target file |
|
| 402 | - * |
|
| 403 | - * @return bool true for success, false otherwise |
|
| 404 | - */ |
|
| 405 | - private static function copyFileContents($view, $path1, $path2) { |
|
| 406 | - /** @var \OC\Files\Storage\Storage $storage1 */ |
|
| 407 | - list($storage1, $internalPath1) = $view->resolvePath($path1); |
|
| 408 | - /** @var \OC\Files\Storage\Storage $storage2 */ |
|
| 409 | - list($storage2, $internalPath2) = $view->resolvePath($path2); |
|
| 410 | - |
|
| 411 | - $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 412 | - $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 413 | - |
|
| 414 | - // TODO add a proper way of overwriting a file while maintaining file ids |
|
| 415 | - if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) { |
|
| 416 | - $source = $storage1->fopen($internalPath1, 'r'); |
|
| 417 | - $target = $storage2->fopen($internalPath2, 'w'); |
|
| 418 | - list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 419 | - fclose($source); |
|
| 420 | - fclose($target); |
|
| 421 | - |
|
| 422 | - if ($result !== false) { |
|
| 423 | - $storage1->unlink($internalPath1); |
|
| 424 | - } |
|
| 425 | - } else { |
|
| 426 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 430 | - $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 431 | - |
|
| 432 | - return ($result !== false); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * get a list of all available versions of a file in descending chronological order |
|
| 437 | - * @param string $uid user id from the owner of the file |
|
| 438 | - * @param string $filename file to find versions of, relative to the user files dir |
|
| 439 | - * @param string $userFullPath |
|
| 440 | - * @return array versions newest version first |
|
| 441 | - */ |
|
| 442 | - public static function getVersions($uid, $filename, $userFullPath = '') { |
|
| 443 | - $versions = array(); |
|
| 444 | - if (empty($filename)) { |
|
| 445 | - return $versions; |
|
| 446 | - } |
|
| 447 | - // fetch for old versions |
|
| 448 | - $view = new View('/' . $uid . '/'); |
|
| 449 | - |
|
| 450 | - $pathinfo = pathinfo($filename); |
|
| 451 | - $versionedFile = $pathinfo['basename']; |
|
| 452 | - |
|
| 453 | - $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']); |
|
| 454 | - |
|
| 455 | - $dirContent = false; |
|
| 456 | - if ($view->is_dir($dir)) { |
|
| 457 | - $dirContent = $view->opendir($dir); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - if ($dirContent === false) { |
|
| 461 | - return $versions; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - if (is_resource($dirContent)) { |
|
| 465 | - while (($entryName = readdir($dirContent)) !== false) { |
|
| 466 | - if (!Filesystem::isIgnoredDir($entryName)) { |
|
| 467 | - $pathparts = pathinfo($entryName); |
|
| 468 | - $filename = $pathparts['filename']; |
|
| 469 | - if ($filename === $versionedFile) { |
|
| 470 | - $pathparts = pathinfo($entryName); |
|
| 471 | - $timestamp = substr($pathparts['extension'], 1); |
|
| 472 | - $filename = $pathparts['filename']; |
|
| 473 | - $key = $timestamp . '#' . $filename; |
|
| 474 | - $versions[$key]['version'] = $timestamp; |
|
| 475 | - $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp); |
|
| 476 | - if (empty($userFullPath)) { |
|
| 477 | - $versions[$key]['preview'] = ''; |
|
| 478 | - } else { |
|
| 479 | - $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]); |
|
| 480 | - } |
|
| 481 | - $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); |
|
| 482 | - $versions[$key]['name'] = $versionedFile; |
|
| 483 | - $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName); |
|
| 484 | - $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile); |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - closedir($dirContent); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - // sort with newest version first |
|
| 492 | - krsort($versions); |
|
| 493 | - |
|
| 494 | - return $versions; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Expire versions that older than max version retention time |
|
| 499 | - * @param string $uid |
|
| 500 | - */ |
|
| 501 | - public static function expireOlderThanMaxForUser($uid){ |
|
| 502 | - $expiration = self::getExpiration(); |
|
| 503 | - $threshold = $expiration->getMaxAgeAsTimestamp(); |
|
| 504 | - $versions = self::getAllVersions($uid); |
|
| 505 | - if (!$threshold || !array_key_exists('all', $versions)) { |
|
| 506 | - return; |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - $toDelete = []; |
|
| 510 | - foreach (array_reverse($versions['all']) as $key => $version) { |
|
| 511 | - if ((int)$version['version'] <$threshold) { |
|
| 512 | - $toDelete[$key] = $version; |
|
| 513 | - } else { |
|
| 514 | - //Versions are sorted by time - nothing mo to iterate. |
|
| 515 | - break; |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - $view = new View('/' . $uid . '/files_versions'); |
|
| 520 | - if (!empty($toDelete)) { |
|
| 521 | - foreach ($toDelete as $version) { |
|
| 522 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
|
| 523 | - self::deleteVersion($view, $version['path'] . '.v' . $version['version']); |
|
| 524 | - \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
|
| 525 | - } |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * translate a timestamp into a string like "5 days ago" |
|
| 531 | - * @param int $timestamp |
|
| 532 | - * @return string for example "5 days ago" |
|
| 533 | - */ |
|
| 534 | - private static function getHumanReadableTimestamp($timestamp) { |
|
| 535 | - |
|
| 536 | - $diff = time() - $timestamp; |
|
| 537 | - |
|
| 538 | - if ($diff < 60) { // first minute |
|
| 539 | - return $diff . " seconds ago"; |
|
| 540 | - } elseif ($diff < 3600) { //first hour |
|
| 541 | - return round($diff / 60) . " minutes ago"; |
|
| 542 | - } elseif ($diff < 86400) { // first day |
|
| 543 | - return round($diff / 3600) . " hours ago"; |
|
| 544 | - } elseif ($diff < 604800) { //first week |
|
| 545 | - return round($diff / 86400) . " days ago"; |
|
| 546 | - } elseif ($diff < 2419200) { //first month |
|
| 547 | - return round($diff / 604800) . " weeks ago"; |
|
| 548 | - } elseif ($diff < 29030400) { // first year |
|
| 549 | - return round($diff / 2419200) . " months ago"; |
|
| 550 | - } else { |
|
| 551 | - return round($diff / 29030400) . " years ago"; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * returns all stored file versions from a given user |
|
| 558 | - * @param string $uid id of the user |
|
| 559 | - * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename |
|
| 560 | - */ |
|
| 561 | - private static function getAllVersions($uid) { |
|
| 562 | - $view = new View('/' . $uid . '/'); |
|
| 563 | - $dirs = array(self::VERSIONS_ROOT); |
|
| 564 | - $versions = array(); |
|
| 565 | - |
|
| 566 | - while (!empty($dirs)) { |
|
| 567 | - $dir = array_pop($dirs); |
|
| 568 | - $files = $view->getDirectoryContent($dir); |
|
| 569 | - |
|
| 570 | - foreach ($files as $file) { |
|
| 571 | - $fileData = $file->getData(); |
|
| 572 | - $filePath = $dir . '/' . $fileData['name']; |
|
| 573 | - if ($file['type'] === 'dir') { |
|
| 574 | - $dirs[] = $filePath; |
|
| 575 | - } else { |
|
| 576 | - $versionsBegin = strrpos($filePath, '.v'); |
|
| 577 | - $relPathStart = strlen(self::VERSIONS_ROOT); |
|
| 578 | - $version = substr($filePath, $versionsBegin + 2); |
|
| 579 | - $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart); |
|
| 580 | - $key = $version . '#' . $relpath; |
|
| 581 | - $versions[$key] = array('path' => $relpath, 'timestamp' => $version); |
|
| 582 | - } |
|
| 583 | - } |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - // newest version first |
|
| 587 | - krsort($versions); |
|
| 588 | - |
|
| 589 | - $result = array(); |
|
| 590 | - |
|
| 591 | - foreach ($versions as $key => $value) { |
|
| 592 | - $size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']); |
|
| 593 | - $filename = $value['path']; |
|
| 594 | - |
|
| 595 | - $result['all'][$key]['version'] = $value['timestamp']; |
|
| 596 | - $result['all'][$key]['path'] = $filename; |
|
| 597 | - $result['all'][$key]['size'] = $size; |
|
| 598 | - |
|
| 599 | - $result['by_file'][$filename][$key]['version'] = $value['timestamp']; |
|
| 600 | - $result['by_file'][$filename][$key]['path'] = $filename; |
|
| 601 | - $result['by_file'][$filename][$key]['size'] = $size; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return $result; |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * get list of files we want to expire |
|
| 609 | - * @param array $versions list of versions |
|
| 610 | - * @param integer $time |
|
| 611 | - * @param bool $quotaExceeded is versions storage limit reached |
|
| 612 | - * @return array containing the list of to deleted versions and the size of them |
|
| 613 | - */ |
|
| 614 | - protected static function getExpireList($time, $versions, $quotaExceeded = false) { |
|
| 615 | - $expiration = self::getExpiration(); |
|
| 616 | - |
|
| 617 | - if ($expiration->shouldAutoExpire()) { |
|
| 618 | - list($toDelete, $size) = self::getAutoExpireList($time, $versions); |
|
| 619 | - } else { |
|
| 620 | - $size = 0; |
|
| 621 | - $toDelete = []; // versions we want to delete |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - foreach ($versions as $key => $version) { |
|
| 625 | - if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) { |
|
| 626 | - $size += $version['size']; |
|
| 627 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - return [$toDelete, $size]; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * get list of files we want to expire |
|
| 636 | - * @param array $versions list of versions |
|
| 637 | - * @param integer $time |
|
| 638 | - * @return array containing the list of to deleted versions and the size of them |
|
| 639 | - */ |
|
| 640 | - protected static function getAutoExpireList($time, $versions) { |
|
| 641 | - $size = 0; |
|
| 642 | - $toDelete = array(); // versions we want to delete |
|
| 643 | - |
|
| 644 | - $interval = 1; |
|
| 645 | - $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
| 646 | - if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
| 647 | - $nextInterval = -1; |
|
| 648 | - } else { |
|
| 649 | - $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - $firstVersion = reset($versions); |
|
| 653 | - $firstKey = key($versions); |
|
| 654 | - $prevTimestamp = $firstVersion['version']; |
|
| 655 | - $nextVersion = $firstVersion['version'] - $step; |
|
| 656 | - unset($versions[$firstKey]); |
|
| 657 | - |
|
| 658 | - foreach ($versions as $key => $version) { |
|
| 659 | - $newInterval = true; |
|
| 660 | - while ($newInterval) { |
|
| 661 | - if ($nextInterval === -1 || $prevTimestamp > $nextInterval) { |
|
| 662 | - if ($version['version'] > $nextVersion) { |
|
| 663 | - //distance between two version too small, mark to delete |
|
| 664 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 665 | - $size += $version['size']; |
|
| 666 | - \OC::$server->getLogger()->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']); |
|
| 667 | - } else { |
|
| 668 | - $nextVersion = $version['version'] - $step; |
|
| 669 | - $prevTimestamp = $version['version']; |
|
| 670 | - } |
|
| 671 | - $newInterval = false; // version checked so we can move to the next one |
|
| 672 | - } else { // time to move on to the next interval |
|
| 673 | - $interval++; |
|
| 674 | - $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
| 675 | - $nextVersion = $prevTimestamp - $step; |
|
| 676 | - if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
| 677 | - $nextInterval = -1; |
|
| 678 | - } else { |
|
| 679 | - $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
| 680 | - } |
|
| 681 | - $newInterval = true; // we changed the interval -> check same version with new interval |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - return array($toDelete, $size); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * Schedule versions expiration for the given file |
|
| 691 | - * |
|
| 692 | - * @param string $uid owner of the file |
|
| 693 | - * @param string $fileName file/folder for which to schedule expiration |
|
| 694 | - */ |
|
| 695 | - public static function scheduleExpire($uid, $fileName) { |
|
| 696 | - // let the admin disable auto expire |
|
| 697 | - $expiration = self::getExpiration(); |
|
| 698 | - if ($expiration->isEnabled()) { |
|
| 699 | - $command = new Expire($uid, $fileName); |
|
| 700 | - \OC::$server->getCommandBus()->push($command); |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - /** |
|
| 705 | - * Expire versions which exceed the quota. |
|
| 706 | - * |
|
| 707 | - * This will setup the filesystem for the given user but will not |
|
| 708 | - * tear it down afterwards. |
|
| 709 | - * |
|
| 710 | - * @param string $filename path to file to expire |
|
| 711 | - * @param string $uid user for which to expire the version |
|
| 712 | - * @return bool|int|null |
|
| 713 | - */ |
|
| 714 | - public static function expire($filename, $uid) { |
|
| 715 | - $expiration = self::getExpiration(); |
|
| 716 | - |
|
| 717 | - if ($expiration->isEnabled()) { |
|
| 718 | - // get available disk space for user |
|
| 719 | - $user = \OC::$server->getUserManager()->get($uid); |
|
| 720 | - if (is_null($user)) { |
|
| 721 | - \OC::$server->getLogger()->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']); |
|
| 722 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid); |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - \OC_Util::setupFS($uid); |
|
| 726 | - |
|
| 727 | - if (!Filesystem::file_exists($filename)) { |
|
| 728 | - return false; |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - if (empty($filename)) { |
|
| 732 | - // file maybe renamed or deleted |
|
| 733 | - return false; |
|
| 734 | - } |
|
| 735 | - $versionsFileview = new View('/'.$uid.'/files_versions'); |
|
| 736 | - |
|
| 737 | - $softQuota = true; |
|
| 738 | - $quota = $user->getQuota(); |
|
| 739 | - if ( $quota === null || $quota === 'none' ) { |
|
| 740 | - $quota = Filesystem::free_space('/'); |
|
| 741 | - $softQuota = false; |
|
| 742 | - } else { |
|
| 743 | - $quota = \OCP\Util::computerFileSize($quota); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - // make sure that we have the current size of the version history |
|
| 747 | - $versionsSize = self::getVersionsSize($uid); |
|
| 748 | - |
|
| 749 | - // calculate available space for version history |
|
| 750 | - // subtract size of files and current versions size from quota |
|
| 751 | - if ($quota >= 0) { |
|
| 752 | - if ($softQuota) { |
|
| 753 | - $userFolder = \OC::$server->getUserFolder($uid); |
|
| 754 | - if(is_null($userFolder)) { |
|
| 755 | - $availableSpace = 0; |
|
| 756 | - } else { |
|
| 757 | - $free = $quota - $userFolder->getSize(false); // remaining free space for user |
|
| 758 | - if ($free > 0) { |
|
| 759 | - $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions |
|
| 760 | - } else { |
|
| 761 | - $availableSpace = $free - $versionsSize; |
|
| 762 | - } |
|
| 763 | - } |
|
| 764 | - } else { |
|
| 765 | - $availableSpace = $quota; |
|
| 766 | - } |
|
| 767 | - } else { |
|
| 768 | - $availableSpace = PHP_INT_MAX; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - $allVersions = Storage::getVersions($uid, $filename); |
|
| 772 | - |
|
| 773 | - $time = time(); |
|
| 774 | - list($toDelete, $sizeOfDeletedVersions) = self::getExpireList($time, $allVersions, $availableSpace <= 0); |
|
| 775 | - |
|
| 776 | - $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
| 777 | - $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
| 778 | - |
|
| 779 | - // if still not enough free space we rearrange the versions from all files |
|
| 780 | - if ($availableSpace <= 0) { |
|
| 781 | - $result = Storage::getAllVersions($uid); |
|
| 782 | - $allVersions = $result['all']; |
|
| 783 | - |
|
| 784 | - foreach ($result['by_file'] as $versions) { |
|
| 785 | - list($toDeleteNew, $size) = self::getExpireList($time, $versions, $availableSpace <= 0); |
|
| 786 | - $toDelete = array_merge($toDelete, $toDeleteNew); |
|
| 787 | - $sizeOfDeletedVersions += $size; |
|
| 788 | - } |
|
| 789 | - $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
| 790 | - $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - $logger = \OC::$server->getLogger(); |
|
| 794 | - foreach($toDelete as $key => $path) { |
|
| 795 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 796 | - self::deleteVersion($versionsFileview, $path); |
|
| 797 | - \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 798 | - unset($allVersions[$key]); // update array with the versions we keep |
|
| 799 | - $logger->info('Expire: ' . $path, ['app' => 'files_versions']); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - // Check if enough space is available after versions are rearranged. |
|
| 803 | - // If not we delete the oldest versions until we meet the size limit for versions, |
|
| 804 | - // but always keep the two latest versions |
|
| 805 | - $numOfVersions = count($allVersions) -2 ; |
|
| 806 | - $i = 0; |
|
| 807 | - // sort oldest first and make sure that we start at the first element |
|
| 808 | - ksort($allVersions); |
|
| 809 | - reset($allVersions); |
|
| 810 | - while ($availableSpace < 0 && $i < $numOfVersions) { |
|
| 811 | - $version = current($allVersions); |
|
| 812 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 813 | - self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']); |
|
| 814 | - \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 815 | - \OC::$server->getLogger()->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']); |
|
| 816 | - $versionsSize -= $version['size']; |
|
| 817 | - $availableSpace += $version['size']; |
|
| 818 | - next($allVersions); |
|
| 819 | - $i++; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - return $versionsSize; // finally return the new size of the version history |
|
| 823 | - } |
|
| 824 | - |
|
| 825 | - return false; |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - /** |
|
| 829 | - * Create recursively missing directories inside of files_versions |
|
| 830 | - * that match the given path to a file. |
|
| 831 | - * |
|
| 832 | - * @param string $filename $path to a file, relative to the user's |
|
| 833 | - * "files" folder |
|
| 834 | - * @param View $view view on data/user/ |
|
| 835 | - */ |
|
| 836 | - public static function createMissingDirectories($filename, $view) { |
|
| 837 | - $dirname = Filesystem::normalizePath(dirname($filename)); |
|
| 838 | - $dirParts = explode('/', $dirname); |
|
| 839 | - $dir = "/files_versions"; |
|
| 840 | - foreach ($dirParts as $part) { |
|
| 841 | - $dir = $dir . '/' . $part; |
|
| 842 | - if (!$view->file_exists($dir)) { |
|
| 843 | - $view->mkdir($dir); |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /** |
|
| 849 | - * Static workaround |
|
| 850 | - * @return Expiration |
|
| 851 | - */ |
|
| 852 | - protected static function getExpiration(){ |
|
| 853 | - if (is_null(self::$application)) { |
|
| 854 | - self::$application = new Application(); |
|
| 855 | - } |
|
| 856 | - return self::$application->getContainer()->query(Expiration::class); |
|
| 857 | - } |
|
| 58 | + const DEFAULTENABLED=true; |
|
| 59 | + const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota |
|
| 60 | + const VERSIONS_ROOT = 'files_versions/'; |
|
| 61 | + |
|
| 62 | + const DELETE_TRIGGER_MASTER_REMOVED = 0; |
|
| 63 | + const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1; |
|
| 64 | + const DELETE_TRIGGER_QUOTA_EXCEEDED = 2; |
|
| 65 | + |
|
| 66 | + // files for which we can remove the versions after the delete operation was successful |
|
| 67 | + private static $deletedFiles = array(); |
|
| 68 | + |
|
| 69 | + private static $sourcePathAndUser = array(); |
|
| 70 | + |
|
| 71 | + private static $max_versions_per_interval = array( |
|
| 72 | + //first 10sec, one version every 2sec |
|
| 73 | + 1 => array('intervalEndsAfter' => 10, 'step' => 2), |
|
| 74 | + //next minute, one version every 10sec |
|
| 75 | + 2 => array('intervalEndsAfter' => 60, 'step' => 10), |
|
| 76 | + //next hour, one version every minute |
|
| 77 | + 3 => array('intervalEndsAfter' => 3600, 'step' => 60), |
|
| 78 | + //next 24h, one version every hour |
|
| 79 | + 4 => array('intervalEndsAfter' => 86400, 'step' => 3600), |
|
| 80 | + //next 30days, one version per day |
|
| 81 | + 5 => array('intervalEndsAfter' => 2592000, 'step' => 86400), |
|
| 82 | + //until the end one version per week |
|
| 83 | + 6 => array('intervalEndsAfter' => -1, 'step' => 604800), |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + /** @var \OCA\Files_Versions\AppInfo\Application */ |
|
| 87 | + private static $application; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * get the UID of the owner of the file and the path to the file relative to |
|
| 91 | + * owners files folder |
|
| 92 | + * |
|
| 93 | + * @param string $filename |
|
| 94 | + * @return array |
|
| 95 | + * @throws \OC\User\NoUserException |
|
| 96 | + */ |
|
| 97 | + public static function getUidAndFilename($filename) { |
|
| 98 | + $uid = Filesystem::getOwner($filename); |
|
| 99 | + $userManager = \OC::$server->getUserManager(); |
|
| 100 | + // if the user with the UID doesn't exists, e.g. because the UID points |
|
| 101 | + // to a remote user with a federated cloud ID we use the current logged-in |
|
| 102 | + // user. We need a valid local user to create the versions |
|
| 103 | + if (!$userManager->userExists($uid)) { |
|
| 104 | + $uid = User::getUser(); |
|
| 105 | + } |
|
| 106 | + Filesystem::initMountPoints($uid); |
|
| 107 | + if ( $uid !== User::getUser() ) { |
|
| 108 | + $info = Filesystem::getFileInfo($filename); |
|
| 109 | + $ownerView = new View('/'.$uid.'/files'); |
|
| 110 | + try { |
|
| 111 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 112 | + // make sure that the file name doesn't end with a trailing slash |
|
| 113 | + // can for example happen single files shared across servers |
|
| 114 | + $filename = rtrim($filename, '/'); |
|
| 115 | + } catch (NotFoundException $e) { |
|
| 116 | + $filename = null; |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + return [$uid, $filename]; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Remember the owner and the owner path of the source file |
|
| 124 | + * |
|
| 125 | + * @param string $source source path |
|
| 126 | + */ |
|
| 127 | + public static function setSourcePathAndUser($source) { |
|
| 128 | + list($uid, $path) = self::getUidAndFilename($source); |
|
| 129 | + self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Gets the owner and the owner path from the source path |
|
| 134 | + * |
|
| 135 | + * @param string $source source path |
|
| 136 | + * @return array with user id and path |
|
| 137 | + */ |
|
| 138 | + public static function getSourcePathAndUser($source) { |
|
| 139 | + |
|
| 140 | + if (isset(self::$sourcePathAndUser[$source])) { |
|
| 141 | + $uid = self::$sourcePathAndUser[$source]['uid']; |
|
| 142 | + $path = self::$sourcePathAndUser[$source]['path']; |
|
| 143 | + unset(self::$sourcePathAndUser[$source]); |
|
| 144 | + } else { |
|
| 145 | + $uid = $path = false; |
|
| 146 | + } |
|
| 147 | + return array($uid, $path); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * get current size of all versions from a given user |
|
| 152 | + * |
|
| 153 | + * @param string $user user who owns the versions |
|
| 154 | + * @return int versions size |
|
| 155 | + */ |
|
| 156 | + private static function getVersionsSize($user) { |
|
| 157 | + $view = new View('/' . $user); |
|
| 158 | + $fileInfo = $view->getFileInfo('/files_versions'); |
|
| 159 | + return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * store a new version of a file. |
|
| 164 | + */ |
|
| 165 | + public static function store($filename) { |
|
| 166 | + |
|
| 167 | + // if the file gets streamed we need to remove the .part extension |
|
| 168 | + // to get the right target |
|
| 169 | + $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
| 170 | + if ($ext === 'part') { |
|
| 171 | + $filename = substr($filename, 0, -5); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // we only handle existing files |
|
| 175 | + if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
| 176 | + return false; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + list($uid, $filename) = self::getUidAndFilename($filename); |
|
| 180 | + |
|
| 181 | + $files_view = new View('/'.$uid .'/files'); |
|
| 182 | + |
|
| 183 | + $eventDispatcher = \OC::$server->getEventDispatcher(); |
|
| 184 | + $fileInfo = $files_view->getFileInfo($filename); |
|
| 185 | + $id = $fileInfo->getId(); |
|
| 186 | + $nodes = \OC::$server->getRootFolder()->getById($id); |
|
| 187 | + foreach ($nodes as $node) { |
|
| 188 | + $event = new CreateVersionEvent($node); |
|
| 189 | + $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event); |
|
| 190 | + if ($event->shouldCreateVersion() === false) { |
|
| 191 | + return false; |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + // no use making versions for empty files |
|
| 196 | + if ($fileInfo->getSize() === 0) { |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** @var IVersionManager $versionManager */ |
|
| 201 | + $versionManager = \OC::$server->query(IVersionManager::class); |
|
| 202 | + $userManager = \OC::$server->getUserManager(); |
|
| 203 | + $user = $userManager->get($uid); |
|
| 204 | + |
|
| 205 | + $versionManager->createVersion($user, $fileInfo); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * mark file as deleted so that we can remove the versions if the file is gone |
|
| 211 | + * @param string $path |
|
| 212 | + */ |
|
| 213 | + public static function markDeletedFile($path) { |
|
| 214 | + list($uid, $filename) = self::getUidAndFilename($path); |
|
| 215 | + self::$deletedFiles[$path] = array( |
|
| 216 | + 'uid' => $uid, |
|
| 217 | + 'filename' => $filename); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * delete the version from the storage and cache |
|
| 222 | + * |
|
| 223 | + * @param View $view |
|
| 224 | + * @param string $path |
|
| 225 | + */ |
|
| 226 | + protected static function deleteVersion($view, $path) { |
|
| 227 | + $view->unlink($path); |
|
| 228 | + /** |
|
| 229 | + * @var \OC\Files\Storage\Storage $storage |
|
| 230 | + * @var string $internalPath |
|
| 231 | + */ |
|
| 232 | + list($storage, $internalPath) = $view->resolvePath($path); |
|
| 233 | + $cache = $storage->getCache($internalPath); |
|
| 234 | + $cache->remove($internalPath); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Delete versions of a file |
|
| 239 | + */ |
|
| 240 | + public static function delete($path) { |
|
| 241 | + |
|
| 242 | + $deletedFile = self::$deletedFiles[$path]; |
|
| 243 | + $uid = $deletedFile['uid']; |
|
| 244 | + $filename = $deletedFile['filename']; |
|
| 245 | + |
|
| 246 | + if (!Filesystem::file_exists($path)) { |
|
| 247 | + |
|
| 248 | + $view = new View('/' . $uid . '/files_versions'); |
|
| 249 | + |
|
| 250 | + $versions = self::getVersions($uid, $filename); |
|
| 251 | + if (!empty($versions)) { |
|
| 252 | + foreach ($versions as $v) { |
|
| 253 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 254 | + self::deleteVersion($view, $filename . '.v' . $v['version']); |
|
| 255 | + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + unset(self::$deletedFiles[$path]); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Rename or copy versions of a file of the given paths |
|
| 264 | + * |
|
| 265 | + * @param string $sourcePath source path of the file to move, relative to |
|
| 266 | + * the currently logged in user's "files" folder |
|
| 267 | + * @param string $targetPath target path of the file to move, relative to |
|
| 268 | + * the currently logged in user's "files" folder |
|
| 269 | + * @param string $operation can be 'copy' or 'rename' |
|
| 270 | + */ |
|
| 271 | + public static function renameOrCopy($sourcePath, $targetPath, $operation) { |
|
| 272 | + list($sourceOwner, $sourcePath) = self::getSourcePathAndUser($sourcePath); |
|
| 273 | + |
|
| 274 | + // it was a upload of a existing file if no old path exists |
|
| 275 | + // in this case the pre-hook already called the store method and we can |
|
| 276 | + // stop here |
|
| 277 | + if ($sourcePath === false) { |
|
| 278 | + return true; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + list($targetOwner, $targetPath) = self::getUidAndFilename($targetPath); |
|
| 282 | + |
|
| 283 | + $sourcePath = ltrim($sourcePath, '/'); |
|
| 284 | + $targetPath = ltrim($targetPath, '/'); |
|
| 285 | + |
|
| 286 | + $rootView = new View(''); |
|
| 287 | + |
|
| 288 | + // did we move a directory ? |
|
| 289 | + if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 290 | + // does the directory exists for versions too ? |
|
| 291 | + if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) { |
|
| 292 | + // create missing dirs if necessary |
|
| 293 | + self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 294 | + |
|
| 295 | + // move the directory containing the versions |
|
| 296 | + $rootView->$operation( |
|
| 297 | + '/' . $sourceOwner . '/files_versions/' . $sourcePath, |
|
| 298 | + '/' . $targetOwner . '/files_versions/' . $targetPath |
|
| 299 | + ); |
|
| 300 | + } |
|
| 301 | + } else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) { |
|
| 302 | + // create missing dirs if necessary |
|
| 303 | + self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 304 | + |
|
| 305 | + foreach ($versions as $v) { |
|
| 306 | + // move each version one by one to the target directory |
|
| 307 | + $rootView->$operation( |
|
| 308 | + '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'], |
|
| 309 | + '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version'] |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + // if we moved versions directly for a file, schedule expiration check for that file |
|
| 315 | + if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 316 | + self::scheduleExpire($targetOwner, $targetPath); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Rollback to an old version of a file. |
|
| 323 | + * |
|
| 324 | + * @param string $file file name |
|
| 325 | + * @param int $revision revision timestamp |
|
| 326 | + * @return bool |
|
| 327 | + */ |
|
| 328 | + public static function rollback($file, $revision) { |
|
| 329 | + |
|
| 330 | + // add expected leading slash |
|
| 331 | + $file = '/' . ltrim($file, '/'); |
|
| 332 | + list($uid, $filename) = self::getUidAndFilename($file); |
|
| 333 | + if ($uid === null || trim($filename, '/') === '') { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // Fetch the userfolder to trigger view hooks |
|
| 338 | + $userFolder = \OC::$server->getUserFolder($uid); |
|
| 339 | + |
|
| 340 | + $users_view = new View('/'.$uid); |
|
| 341 | + $files_view = new View('/'. User::getUser().'/files'); |
|
| 342 | + |
|
| 343 | + $versionCreated = false; |
|
| 344 | + |
|
| 345 | + $fileInfo = $files_view->getFileInfo($file); |
|
| 346 | + |
|
| 347 | + // check if user has the permissions to revert a version |
|
| 348 | + if (!$fileInfo->isUpdateable()) { |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + //first create a new version |
|
| 353 | + $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename); |
|
| 354 | + if (!$users_view->file_exists($version)) { |
|
| 355 | + $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename)); |
|
| 356 | + $versionCreated = true; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $fileToRestore = 'files_versions' . $filename . '.v' . $revision; |
|
| 360 | + |
|
| 361 | + // Restore encrypted version of the old file for the newly restored file |
|
| 362 | + // This has to happen manually here since the file is manually copied below |
|
| 363 | + $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion(); |
|
| 364 | + $oldFileInfo = $users_view->getFileInfo($fileToRestore); |
|
| 365 | + $cache = $fileInfo->getStorage()->getCache(); |
|
| 366 | + $cache->update( |
|
| 367 | + $fileInfo->getId(), [ |
|
| 368 | + 'encrypted' => $oldVersion, |
|
| 369 | + 'encryptedVersion' => $oldVersion, |
|
| 370 | + 'size' => $oldFileInfo->getSize() |
|
| 371 | + ] |
|
| 372 | + ); |
|
| 373 | + |
|
| 374 | + // rollback |
|
| 375 | + if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { |
|
| 376 | + $files_view->touch($file, $revision); |
|
| 377 | + Storage::scheduleExpire($uid, $file); |
|
| 378 | + |
|
| 379 | + $node = $userFolder->get($file); |
|
| 380 | + |
|
| 381 | + // TODO: move away from those legacy hooks! |
|
| 382 | + \OC_Hook::emit('\OCP\Versions', 'rollback', array( |
|
| 383 | + 'path' => $filename, |
|
| 384 | + 'revision' => $revision, |
|
| 385 | + 'node' => $node, |
|
| 386 | + )); |
|
| 387 | + return true; |
|
| 388 | + } else if ($versionCreated) { |
|
| 389 | + self::deleteVersion($users_view, $version); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return false; |
|
| 393 | + |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Stream copy file contents from $path1 to $path2 |
|
| 398 | + * |
|
| 399 | + * @param View $view view to use for copying |
|
| 400 | + * @param string $path1 source file to copy |
|
| 401 | + * @param string $path2 target file |
|
| 402 | + * |
|
| 403 | + * @return bool true for success, false otherwise |
|
| 404 | + */ |
|
| 405 | + private static function copyFileContents($view, $path1, $path2) { |
|
| 406 | + /** @var \OC\Files\Storage\Storage $storage1 */ |
|
| 407 | + list($storage1, $internalPath1) = $view->resolvePath($path1); |
|
| 408 | + /** @var \OC\Files\Storage\Storage $storage2 */ |
|
| 409 | + list($storage2, $internalPath2) = $view->resolvePath($path2); |
|
| 410 | + |
|
| 411 | + $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 412 | + $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 413 | + |
|
| 414 | + // TODO add a proper way of overwriting a file while maintaining file ids |
|
| 415 | + if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) { |
|
| 416 | + $source = $storage1->fopen($internalPath1, 'r'); |
|
| 417 | + $target = $storage2->fopen($internalPath2, 'w'); |
|
| 418 | + list(, $result) = \OC_Helper::streamCopy($source, $target); |
|
| 419 | + fclose($source); |
|
| 420 | + fclose($target); |
|
| 421 | + |
|
| 422 | + if ($result !== false) { |
|
| 423 | + $storage1->unlink($internalPath1); |
|
| 424 | + } |
|
| 425 | + } else { |
|
| 426 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 430 | + $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 431 | + |
|
| 432 | + return ($result !== false); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * get a list of all available versions of a file in descending chronological order |
|
| 437 | + * @param string $uid user id from the owner of the file |
|
| 438 | + * @param string $filename file to find versions of, relative to the user files dir |
|
| 439 | + * @param string $userFullPath |
|
| 440 | + * @return array versions newest version first |
|
| 441 | + */ |
|
| 442 | + public static function getVersions($uid, $filename, $userFullPath = '') { |
|
| 443 | + $versions = array(); |
|
| 444 | + if (empty($filename)) { |
|
| 445 | + return $versions; |
|
| 446 | + } |
|
| 447 | + // fetch for old versions |
|
| 448 | + $view = new View('/' . $uid . '/'); |
|
| 449 | + |
|
| 450 | + $pathinfo = pathinfo($filename); |
|
| 451 | + $versionedFile = $pathinfo['basename']; |
|
| 452 | + |
|
| 453 | + $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']); |
|
| 454 | + |
|
| 455 | + $dirContent = false; |
|
| 456 | + if ($view->is_dir($dir)) { |
|
| 457 | + $dirContent = $view->opendir($dir); |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + if ($dirContent === false) { |
|
| 461 | + return $versions; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + if (is_resource($dirContent)) { |
|
| 465 | + while (($entryName = readdir($dirContent)) !== false) { |
|
| 466 | + if (!Filesystem::isIgnoredDir($entryName)) { |
|
| 467 | + $pathparts = pathinfo($entryName); |
|
| 468 | + $filename = $pathparts['filename']; |
|
| 469 | + if ($filename === $versionedFile) { |
|
| 470 | + $pathparts = pathinfo($entryName); |
|
| 471 | + $timestamp = substr($pathparts['extension'], 1); |
|
| 472 | + $filename = $pathparts['filename']; |
|
| 473 | + $key = $timestamp . '#' . $filename; |
|
| 474 | + $versions[$key]['version'] = $timestamp; |
|
| 475 | + $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp); |
|
| 476 | + if (empty($userFullPath)) { |
|
| 477 | + $versions[$key]['preview'] = ''; |
|
| 478 | + } else { |
|
| 479 | + $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]); |
|
| 480 | + } |
|
| 481 | + $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); |
|
| 482 | + $versions[$key]['name'] = $versionedFile; |
|
| 483 | + $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName); |
|
| 484 | + $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile); |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + closedir($dirContent); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + // sort with newest version first |
|
| 492 | + krsort($versions); |
|
| 493 | + |
|
| 494 | + return $versions; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Expire versions that older than max version retention time |
|
| 499 | + * @param string $uid |
|
| 500 | + */ |
|
| 501 | + public static function expireOlderThanMaxForUser($uid){ |
|
| 502 | + $expiration = self::getExpiration(); |
|
| 503 | + $threshold = $expiration->getMaxAgeAsTimestamp(); |
|
| 504 | + $versions = self::getAllVersions($uid); |
|
| 505 | + if (!$threshold || !array_key_exists('all', $versions)) { |
|
| 506 | + return; |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + $toDelete = []; |
|
| 510 | + foreach (array_reverse($versions['all']) as $key => $version) { |
|
| 511 | + if ((int)$version['version'] <$threshold) { |
|
| 512 | + $toDelete[$key] = $version; |
|
| 513 | + } else { |
|
| 514 | + //Versions are sorted by time - nothing mo to iterate. |
|
| 515 | + break; |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + $view = new View('/' . $uid . '/files_versions'); |
|
| 520 | + if (!empty($toDelete)) { |
|
| 521 | + foreach ($toDelete as $version) { |
|
| 522 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
|
| 523 | + self::deleteVersion($view, $version['path'] . '.v' . $version['version']); |
|
| 524 | + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
|
| 525 | + } |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * translate a timestamp into a string like "5 days ago" |
|
| 531 | + * @param int $timestamp |
|
| 532 | + * @return string for example "5 days ago" |
|
| 533 | + */ |
|
| 534 | + private static function getHumanReadableTimestamp($timestamp) { |
|
| 535 | + |
|
| 536 | + $diff = time() - $timestamp; |
|
| 537 | + |
|
| 538 | + if ($diff < 60) { // first minute |
|
| 539 | + return $diff . " seconds ago"; |
|
| 540 | + } elseif ($diff < 3600) { //first hour |
|
| 541 | + return round($diff / 60) . " minutes ago"; |
|
| 542 | + } elseif ($diff < 86400) { // first day |
|
| 543 | + return round($diff / 3600) . " hours ago"; |
|
| 544 | + } elseif ($diff < 604800) { //first week |
|
| 545 | + return round($diff / 86400) . " days ago"; |
|
| 546 | + } elseif ($diff < 2419200) { //first month |
|
| 547 | + return round($diff / 604800) . " weeks ago"; |
|
| 548 | + } elseif ($diff < 29030400) { // first year |
|
| 549 | + return round($diff / 2419200) . " months ago"; |
|
| 550 | + } else { |
|
| 551 | + return round($diff / 29030400) . " years ago"; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * returns all stored file versions from a given user |
|
| 558 | + * @param string $uid id of the user |
|
| 559 | + * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename |
|
| 560 | + */ |
|
| 561 | + private static function getAllVersions($uid) { |
|
| 562 | + $view = new View('/' . $uid . '/'); |
|
| 563 | + $dirs = array(self::VERSIONS_ROOT); |
|
| 564 | + $versions = array(); |
|
| 565 | + |
|
| 566 | + while (!empty($dirs)) { |
|
| 567 | + $dir = array_pop($dirs); |
|
| 568 | + $files = $view->getDirectoryContent($dir); |
|
| 569 | + |
|
| 570 | + foreach ($files as $file) { |
|
| 571 | + $fileData = $file->getData(); |
|
| 572 | + $filePath = $dir . '/' . $fileData['name']; |
|
| 573 | + if ($file['type'] === 'dir') { |
|
| 574 | + $dirs[] = $filePath; |
|
| 575 | + } else { |
|
| 576 | + $versionsBegin = strrpos($filePath, '.v'); |
|
| 577 | + $relPathStart = strlen(self::VERSIONS_ROOT); |
|
| 578 | + $version = substr($filePath, $versionsBegin + 2); |
|
| 579 | + $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart); |
|
| 580 | + $key = $version . '#' . $relpath; |
|
| 581 | + $versions[$key] = array('path' => $relpath, 'timestamp' => $version); |
|
| 582 | + } |
|
| 583 | + } |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + // newest version first |
|
| 587 | + krsort($versions); |
|
| 588 | + |
|
| 589 | + $result = array(); |
|
| 590 | + |
|
| 591 | + foreach ($versions as $key => $value) { |
|
| 592 | + $size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']); |
|
| 593 | + $filename = $value['path']; |
|
| 594 | + |
|
| 595 | + $result['all'][$key]['version'] = $value['timestamp']; |
|
| 596 | + $result['all'][$key]['path'] = $filename; |
|
| 597 | + $result['all'][$key]['size'] = $size; |
|
| 598 | + |
|
| 599 | + $result['by_file'][$filename][$key]['version'] = $value['timestamp']; |
|
| 600 | + $result['by_file'][$filename][$key]['path'] = $filename; |
|
| 601 | + $result['by_file'][$filename][$key]['size'] = $size; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return $result; |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * get list of files we want to expire |
|
| 609 | + * @param array $versions list of versions |
|
| 610 | + * @param integer $time |
|
| 611 | + * @param bool $quotaExceeded is versions storage limit reached |
|
| 612 | + * @return array containing the list of to deleted versions and the size of them |
|
| 613 | + */ |
|
| 614 | + protected static function getExpireList($time, $versions, $quotaExceeded = false) { |
|
| 615 | + $expiration = self::getExpiration(); |
|
| 616 | + |
|
| 617 | + if ($expiration->shouldAutoExpire()) { |
|
| 618 | + list($toDelete, $size) = self::getAutoExpireList($time, $versions); |
|
| 619 | + } else { |
|
| 620 | + $size = 0; |
|
| 621 | + $toDelete = []; // versions we want to delete |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + foreach ($versions as $key => $version) { |
|
| 625 | + if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) { |
|
| 626 | + $size += $version['size']; |
|
| 627 | + $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + return [$toDelete, $size]; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * get list of files we want to expire |
|
| 636 | + * @param array $versions list of versions |
|
| 637 | + * @param integer $time |
|
| 638 | + * @return array containing the list of to deleted versions and the size of them |
|
| 639 | + */ |
|
| 640 | + protected static function getAutoExpireList($time, $versions) { |
|
| 641 | + $size = 0; |
|
| 642 | + $toDelete = array(); // versions we want to delete |
|
| 643 | + |
|
| 644 | + $interval = 1; |
|
| 645 | + $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
| 646 | + if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
| 647 | + $nextInterval = -1; |
|
| 648 | + } else { |
|
| 649 | + $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + $firstVersion = reset($versions); |
|
| 653 | + $firstKey = key($versions); |
|
| 654 | + $prevTimestamp = $firstVersion['version']; |
|
| 655 | + $nextVersion = $firstVersion['version'] - $step; |
|
| 656 | + unset($versions[$firstKey]); |
|
| 657 | + |
|
| 658 | + foreach ($versions as $key => $version) { |
|
| 659 | + $newInterval = true; |
|
| 660 | + while ($newInterval) { |
|
| 661 | + if ($nextInterval === -1 || $prevTimestamp > $nextInterval) { |
|
| 662 | + if ($version['version'] > $nextVersion) { |
|
| 663 | + //distance between two version too small, mark to delete |
|
| 664 | + $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 665 | + $size += $version['size']; |
|
| 666 | + \OC::$server->getLogger()->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']); |
|
| 667 | + } else { |
|
| 668 | + $nextVersion = $version['version'] - $step; |
|
| 669 | + $prevTimestamp = $version['version']; |
|
| 670 | + } |
|
| 671 | + $newInterval = false; // version checked so we can move to the next one |
|
| 672 | + } else { // time to move on to the next interval |
|
| 673 | + $interval++; |
|
| 674 | + $step = Storage::$max_versions_per_interval[$interval]['step']; |
|
| 675 | + $nextVersion = $prevTimestamp - $step; |
|
| 676 | + if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) { |
|
| 677 | + $nextInterval = -1; |
|
| 678 | + } else { |
|
| 679 | + $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter']; |
|
| 680 | + } |
|
| 681 | + $newInterval = true; // we changed the interval -> check same version with new interval |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + return array($toDelete, $size); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * Schedule versions expiration for the given file |
|
| 691 | + * |
|
| 692 | + * @param string $uid owner of the file |
|
| 693 | + * @param string $fileName file/folder for which to schedule expiration |
|
| 694 | + */ |
|
| 695 | + public static function scheduleExpire($uid, $fileName) { |
|
| 696 | + // let the admin disable auto expire |
|
| 697 | + $expiration = self::getExpiration(); |
|
| 698 | + if ($expiration->isEnabled()) { |
|
| 699 | + $command = new Expire($uid, $fileName); |
|
| 700 | + \OC::$server->getCommandBus()->push($command); |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + /** |
|
| 705 | + * Expire versions which exceed the quota. |
|
| 706 | + * |
|
| 707 | + * This will setup the filesystem for the given user but will not |
|
| 708 | + * tear it down afterwards. |
|
| 709 | + * |
|
| 710 | + * @param string $filename path to file to expire |
|
| 711 | + * @param string $uid user for which to expire the version |
|
| 712 | + * @return bool|int|null |
|
| 713 | + */ |
|
| 714 | + public static function expire($filename, $uid) { |
|
| 715 | + $expiration = self::getExpiration(); |
|
| 716 | + |
|
| 717 | + if ($expiration->isEnabled()) { |
|
| 718 | + // get available disk space for user |
|
| 719 | + $user = \OC::$server->getUserManager()->get($uid); |
|
| 720 | + if (is_null($user)) { |
|
| 721 | + \OC::$server->getLogger()->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']); |
|
| 722 | + throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid); |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + \OC_Util::setupFS($uid); |
|
| 726 | + |
|
| 727 | + if (!Filesystem::file_exists($filename)) { |
|
| 728 | + return false; |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + if (empty($filename)) { |
|
| 732 | + // file maybe renamed or deleted |
|
| 733 | + return false; |
|
| 734 | + } |
|
| 735 | + $versionsFileview = new View('/'.$uid.'/files_versions'); |
|
| 736 | + |
|
| 737 | + $softQuota = true; |
|
| 738 | + $quota = $user->getQuota(); |
|
| 739 | + if ( $quota === null || $quota === 'none' ) { |
|
| 740 | + $quota = Filesystem::free_space('/'); |
|
| 741 | + $softQuota = false; |
|
| 742 | + } else { |
|
| 743 | + $quota = \OCP\Util::computerFileSize($quota); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + // make sure that we have the current size of the version history |
|
| 747 | + $versionsSize = self::getVersionsSize($uid); |
|
| 748 | + |
|
| 749 | + // calculate available space for version history |
|
| 750 | + // subtract size of files and current versions size from quota |
|
| 751 | + if ($quota >= 0) { |
|
| 752 | + if ($softQuota) { |
|
| 753 | + $userFolder = \OC::$server->getUserFolder($uid); |
|
| 754 | + if(is_null($userFolder)) { |
|
| 755 | + $availableSpace = 0; |
|
| 756 | + } else { |
|
| 757 | + $free = $quota - $userFolder->getSize(false); // remaining free space for user |
|
| 758 | + if ($free > 0) { |
|
| 759 | + $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions |
|
| 760 | + } else { |
|
| 761 | + $availableSpace = $free - $versionsSize; |
|
| 762 | + } |
|
| 763 | + } |
|
| 764 | + } else { |
|
| 765 | + $availableSpace = $quota; |
|
| 766 | + } |
|
| 767 | + } else { |
|
| 768 | + $availableSpace = PHP_INT_MAX; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + $allVersions = Storage::getVersions($uid, $filename); |
|
| 772 | + |
|
| 773 | + $time = time(); |
|
| 774 | + list($toDelete, $sizeOfDeletedVersions) = self::getExpireList($time, $allVersions, $availableSpace <= 0); |
|
| 775 | + |
|
| 776 | + $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
| 777 | + $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
| 778 | + |
|
| 779 | + // if still not enough free space we rearrange the versions from all files |
|
| 780 | + if ($availableSpace <= 0) { |
|
| 781 | + $result = Storage::getAllVersions($uid); |
|
| 782 | + $allVersions = $result['all']; |
|
| 783 | + |
|
| 784 | + foreach ($result['by_file'] as $versions) { |
|
| 785 | + list($toDeleteNew, $size) = self::getExpireList($time, $versions, $availableSpace <= 0); |
|
| 786 | + $toDelete = array_merge($toDelete, $toDeleteNew); |
|
| 787 | + $sizeOfDeletedVersions += $size; |
|
| 788 | + } |
|
| 789 | + $availableSpace = $availableSpace + $sizeOfDeletedVersions; |
|
| 790 | + $versionsSize = $versionsSize - $sizeOfDeletedVersions; |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + $logger = \OC::$server->getLogger(); |
|
| 794 | + foreach($toDelete as $key => $path) { |
|
| 795 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 796 | + self::deleteVersion($versionsFileview, $path); |
|
| 797 | + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 798 | + unset($allVersions[$key]); // update array with the versions we keep |
|
| 799 | + $logger->info('Expire: ' . $path, ['app' => 'files_versions']); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + // Check if enough space is available after versions are rearranged. |
|
| 803 | + // If not we delete the oldest versions until we meet the size limit for versions, |
|
| 804 | + // but always keep the two latest versions |
|
| 805 | + $numOfVersions = count($allVersions) -2 ; |
|
| 806 | + $i = 0; |
|
| 807 | + // sort oldest first and make sure that we start at the first element |
|
| 808 | + ksort($allVersions); |
|
| 809 | + reset($allVersions); |
|
| 810 | + while ($availableSpace < 0 && $i < $numOfVersions) { |
|
| 811 | + $version = current($allVersions); |
|
| 812 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 813 | + self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']); |
|
| 814 | + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
|
| 815 | + \OC::$server->getLogger()->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']); |
|
| 816 | + $versionsSize -= $version['size']; |
|
| 817 | + $availableSpace += $version['size']; |
|
| 818 | + next($allVersions); |
|
| 819 | + $i++; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + return $versionsSize; // finally return the new size of the version history |
|
| 823 | + } |
|
| 824 | + |
|
| 825 | + return false; |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + /** |
|
| 829 | + * Create recursively missing directories inside of files_versions |
|
| 830 | + * that match the given path to a file. |
|
| 831 | + * |
|
| 832 | + * @param string $filename $path to a file, relative to the user's |
|
| 833 | + * "files" folder |
|
| 834 | + * @param View $view view on data/user/ |
|
| 835 | + */ |
|
| 836 | + public static function createMissingDirectories($filename, $view) { |
|
| 837 | + $dirname = Filesystem::normalizePath(dirname($filename)); |
|
| 838 | + $dirParts = explode('/', $dirname); |
|
| 839 | + $dir = "/files_versions"; |
|
| 840 | + foreach ($dirParts as $part) { |
|
| 841 | + $dir = $dir . '/' . $part; |
|
| 842 | + if (!$view->file_exists($dir)) { |
|
| 843 | + $view->mkdir($dir); |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /** |
|
| 849 | + * Static workaround |
|
| 850 | + * @return Expiration |
|
| 851 | + */ |
|
| 852 | + protected static function getExpiration(){ |
|
| 853 | + if (is_null(self::$application)) { |
|
| 854 | + self::$application = new Application(); |
|
| 855 | + } |
|
| 856 | + return self::$application->getContainer()->query(Expiration::class); |
|
| 857 | + } |
|
| 858 | 858 | |
| 859 | 859 | } |
@@ -55,8 +55,8 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | class Storage { |
| 57 | 57 | |
| 58 | - const DEFAULTENABLED=true; |
|
| 59 | - const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota |
|
| 58 | + const DEFAULTENABLED = true; |
|
| 59 | + const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota |
|
| 60 | 60 | const VERSIONS_ROOT = 'files_versions/'; |
| 61 | 61 | |
| 62 | 62 | const DELETE_TRIGGER_MASTER_REMOVED = 0; |
@@ -70,17 +70,17 @@ discard block |
||
| 70 | 70 | |
| 71 | 71 | private static $max_versions_per_interval = array( |
| 72 | 72 | //first 10sec, one version every 2sec |
| 73 | - 1 => array('intervalEndsAfter' => 10, 'step' => 2), |
|
| 73 | + 1 => array('intervalEndsAfter' => 10, 'step' => 2), |
|
| 74 | 74 | //next minute, one version every 10sec |
| 75 | - 2 => array('intervalEndsAfter' => 60, 'step' => 10), |
|
| 75 | + 2 => array('intervalEndsAfter' => 60, 'step' => 10), |
|
| 76 | 76 | //next hour, one version every minute |
| 77 | - 3 => array('intervalEndsAfter' => 3600, 'step' => 60), |
|
| 77 | + 3 => array('intervalEndsAfter' => 3600, 'step' => 60), |
|
| 78 | 78 | //next 24h, one version every hour |
| 79 | - 4 => array('intervalEndsAfter' => 86400, 'step' => 3600), |
|
| 79 | + 4 => array('intervalEndsAfter' => 86400, 'step' => 3600), |
|
| 80 | 80 | //next 30days, one version per day |
| 81 | 81 | 5 => array('intervalEndsAfter' => 2592000, 'step' => 86400), |
| 82 | 82 | //until the end one version per week |
| 83 | - 6 => array('intervalEndsAfter' => -1, 'step' => 604800), |
|
| 83 | + 6 => array('intervalEndsAfter' => -1, 'step' => 604800), |
|
| 84 | 84 | ); |
| 85 | 85 | |
| 86 | 86 | /** @var \OCA\Files_Versions\AppInfo\Application */ |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $uid = User::getUser(); |
| 105 | 105 | } |
| 106 | 106 | Filesystem::initMountPoints($uid); |
| 107 | - if ( $uid !== User::getUser() ) { |
|
| 107 | + if ($uid !== User::getUser()) { |
|
| 108 | 108 | $info = Filesystem::getFileInfo($filename); |
| 109 | 109 | $ownerView = new View('/'.$uid.'/files'); |
| 110 | 110 | try { |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | * @return int versions size |
| 155 | 155 | */ |
| 156 | 156 | private static function getVersionsSize($user) { |
| 157 | - $view = new View('/' . $user); |
|
| 157 | + $view = new View('/'.$user); |
|
| 158 | 158 | $fileInfo = $view->getFileInfo('/files_versions'); |
| 159 | 159 | return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
| 160 | 160 | } |
@@ -172,13 +172,13 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // we only handle existing files |
| 175 | - if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
| 175 | + if (!Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) { |
|
| 176 | 176 | return false; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | list($uid, $filename) = self::getUidAndFilename($filename); |
| 180 | 180 | |
| 181 | - $files_view = new View('/'.$uid .'/files'); |
|
| 181 | + $files_view = new View('/'.$uid.'/files'); |
|
| 182 | 182 | |
| 183 | 183 | $eventDispatcher = \OC::$server->getEventDispatcher(); |
| 184 | 184 | $fileInfo = $files_view->getFileInfo($filename); |
@@ -245,14 +245,14 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | if (!Filesystem::file_exists($path)) { |
| 247 | 247 | |
| 248 | - $view = new View('/' . $uid . '/files_versions'); |
|
| 248 | + $view = new View('/'.$uid.'/files_versions'); |
|
| 249 | 249 | |
| 250 | 250 | $versions = self::getVersions($uid, $filename); |
| 251 | 251 | if (!empty($versions)) { |
| 252 | 252 | foreach ($versions as $v) { |
| 253 | - \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 254 | - self::deleteVersion($view, $filename . '.v' . $v['version']); |
|
| 255 | - \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 253 | + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path.$v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 254 | + self::deleteVersion($view, $filename.'.v'.$v['version']); |
|
| 255 | + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path.$v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED)); |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | } |
@@ -286,33 +286,33 @@ discard block |
||
| 286 | 286 | $rootView = new View(''); |
| 287 | 287 | |
| 288 | 288 | // did we move a directory ? |
| 289 | - if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 289 | + if ($rootView->is_dir('/'.$targetOwner.'/files/'.$targetPath)) { |
|
| 290 | 290 | // does the directory exists for versions too ? |
| 291 | - if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) { |
|
| 291 | + if ($rootView->is_dir('/'.$sourceOwner.'/files_versions/'.$sourcePath)) { |
|
| 292 | 292 | // create missing dirs if necessary |
| 293 | - self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 293 | + self::createMissingDirectories($targetPath, new View('/'.$targetOwner)); |
|
| 294 | 294 | |
| 295 | 295 | // move the directory containing the versions |
| 296 | 296 | $rootView->$operation( |
| 297 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath, |
|
| 298 | - '/' . $targetOwner . '/files_versions/' . $targetPath |
|
| 297 | + '/'.$sourceOwner.'/files_versions/'.$sourcePath, |
|
| 298 | + '/'.$targetOwner.'/files_versions/'.$targetPath |
|
| 299 | 299 | ); |
| 300 | 300 | } |
| 301 | - } else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) { |
|
| 301 | + } else if ($versions = Storage::getVersions($sourceOwner, '/'.$sourcePath)) { |
|
| 302 | 302 | // create missing dirs if necessary |
| 303 | - self::createMissingDirectories($targetPath, new View('/'. $targetOwner)); |
|
| 303 | + self::createMissingDirectories($targetPath, new View('/'.$targetOwner)); |
|
| 304 | 304 | |
| 305 | 305 | foreach ($versions as $v) { |
| 306 | 306 | // move each version one by one to the target directory |
| 307 | 307 | $rootView->$operation( |
| 308 | - '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'], |
|
| 309 | - '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version'] |
|
| 308 | + '/'.$sourceOwner.'/files_versions/'.$sourcePath.'.v'.$v['version'], |
|
| 309 | + '/'.$targetOwner.'/files_versions/'.$targetPath.'.v'.$v['version'] |
|
| 310 | 310 | ); |
| 311 | 311 | } |
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | // if we moved versions directly for a file, schedule expiration check for that file |
| 315 | - if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) { |
|
| 315 | + if (!$rootView->is_dir('/'.$targetOwner.'/files/'.$targetPath)) { |
|
| 316 | 316 | self::scheduleExpire($targetOwner, $targetPath); |
| 317 | 317 | } |
| 318 | 318 | |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | public static function rollback($file, $revision) { |
| 329 | 329 | |
| 330 | 330 | // add expected leading slash |
| 331 | - $file = '/' . ltrim($file, '/'); |
|
| 331 | + $file = '/'.ltrim($file, '/'); |
|
| 332 | 332 | list($uid, $filename) = self::getUidAndFilename($file); |
| 333 | 333 | if ($uid === null || trim($filename, '/') === '') { |
| 334 | 334 | return false; |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | $userFolder = \OC::$server->getUserFolder($uid); |
| 339 | 339 | |
| 340 | 340 | $users_view = new View('/'.$uid); |
| 341 | - $files_view = new View('/'. User::getUser().'/files'); |
|
| 341 | + $files_view = new View('/'.User::getUser().'/files'); |
|
| 342 | 342 | |
| 343 | 343 | $versionCreated = false; |
| 344 | 344 | |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | $versionCreated = true; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - $fileToRestore = 'files_versions' . $filename . '.v' . $revision; |
|
| 359 | + $fileToRestore = 'files_versions'.$filename.'.v'.$revision; |
|
| 360 | 360 | |
| 361 | 361 | // Restore encrypted version of the old file for the newly restored file |
| 362 | 362 | // This has to happen manually here since the file is manually copied below |
@@ -372,7 +372,7 @@ discard block |
||
| 372 | 372 | ); |
| 373 | 373 | |
| 374 | 374 | // rollback |
| 375 | - if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { |
|
| 375 | + if (self::copyFileContents($users_view, $fileToRestore, 'files'.$filename)) { |
|
| 376 | 376 | $files_view->touch($file, $revision); |
| 377 | 377 | Storage::scheduleExpire($uid, $file); |
| 378 | 378 | |
@@ -445,12 +445,12 @@ discard block |
||
| 445 | 445 | return $versions; |
| 446 | 446 | } |
| 447 | 447 | // fetch for old versions |
| 448 | - $view = new View('/' . $uid . '/'); |
|
| 448 | + $view = new View('/'.$uid.'/'); |
|
| 449 | 449 | |
| 450 | 450 | $pathinfo = pathinfo($filename); |
| 451 | 451 | $versionedFile = $pathinfo['basename']; |
| 452 | 452 | |
| 453 | - $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']); |
|
| 453 | + $dir = Filesystem::normalizePath(self::VERSIONS_ROOT.'/'.$pathinfo['dirname']); |
|
| 454 | 454 | |
| 455 | 455 | $dirContent = false; |
| 456 | 456 | if ($view->is_dir($dir)) { |
@@ -470,7 +470,7 @@ discard block |
||
| 470 | 470 | $pathparts = pathinfo($entryName); |
| 471 | 471 | $timestamp = substr($pathparts['extension'], 1); |
| 472 | 472 | $filename = $pathparts['filename']; |
| 473 | - $key = $timestamp . '#' . $filename; |
|
| 473 | + $key = $timestamp.'#'.$filename; |
|
| 474 | 474 | $versions[$key]['version'] = $timestamp; |
| 475 | 475 | $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp); |
| 476 | 476 | if (empty($userFullPath)) { |
@@ -478,9 +478,9 @@ discard block |
||
| 478 | 478 | } else { |
| 479 | 479 | $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]); |
| 480 | 480 | } |
| 481 | - $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename); |
|
| 481 | + $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'].'/'.$filename); |
|
| 482 | 482 | $versions[$key]['name'] = $versionedFile; |
| 483 | - $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName); |
|
| 483 | + $versions[$key]['size'] = $view->filesize($dir.'/'.$entryName); |
|
| 484 | 484 | $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile); |
| 485 | 485 | } |
| 486 | 486 | } |
@@ -498,7 +498,7 @@ discard block |
||
| 498 | 498 | * Expire versions that older than max version retention time |
| 499 | 499 | * @param string $uid |
| 500 | 500 | */ |
| 501 | - public static function expireOlderThanMaxForUser($uid){ |
|
| 501 | + public static function expireOlderThanMaxForUser($uid) { |
|
| 502 | 502 | $expiration = self::getExpiration(); |
| 503 | 503 | $threshold = $expiration->getMaxAgeAsTimestamp(); |
| 504 | 504 | $versions = self::getAllVersions($uid); |
@@ -508,7 +508,7 @@ discard block |
||
| 508 | 508 | |
| 509 | 509 | $toDelete = []; |
| 510 | 510 | foreach (array_reverse($versions['all']) as $key => $version) { |
| 511 | - if ((int)$version['version'] <$threshold) { |
|
| 511 | + if ((int) $version['version'] < $threshold) { |
|
| 512 | 512 | $toDelete[$key] = $version; |
| 513 | 513 | } else { |
| 514 | 514 | //Versions are sorted by time - nothing mo to iterate. |
@@ -516,11 +516,11 @@ discard block |
||
| 516 | 516 | } |
| 517 | 517 | } |
| 518 | 518 | |
| 519 | - $view = new View('/' . $uid . '/files_versions'); |
|
| 519 | + $view = new View('/'.$uid.'/files_versions'); |
|
| 520 | 520 | if (!empty($toDelete)) { |
| 521 | 521 | foreach ($toDelete as $version) { |
| 522 | 522 | \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
| 523 | - self::deleteVersion($view, $version['path'] . '.v' . $version['version']); |
|
| 523 | + self::deleteVersion($view, $version['path'].'.v'.$version['version']); |
|
| 524 | 524 | \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT)); |
| 525 | 525 | } |
| 526 | 526 | } |
@@ -536,19 +536,19 @@ discard block |
||
| 536 | 536 | $diff = time() - $timestamp; |
| 537 | 537 | |
| 538 | 538 | if ($diff < 60) { // first minute |
| 539 | - return $diff . " seconds ago"; |
|
| 539 | + return $diff." seconds ago"; |
|
| 540 | 540 | } elseif ($diff < 3600) { //first hour |
| 541 | - return round($diff / 60) . " minutes ago"; |
|
| 541 | + return round($diff / 60)." minutes ago"; |
|
| 542 | 542 | } elseif ($diff < 86400) { // first day |
| 543 | - return round($diff / 3600) . " hours ago"; |
|
| 543 | + return round($diff / 3600)." hours ago"; |
|
| 544 | 544 | } elseif ($diff < 604800) { //first week |
| 545 | - return round($diff / 86400) . " days ago"; |
|
| 545 | + return round($diff / 86400)." days ago"; |
|
| 546 | 546 | } elseif ($diff < 2419200) { //first month |
| 547 | - return round($diff / 604800) . " weeks ago"; |
|
| 547 | + return round($diff / 604800)." weeks ago"; |
|
| 548 | 548 | } elseif ($diff < 29030400) { // first year |
| 549 | - return round($diff / 2419200) . " months ago"; |
|
| 549 | + return round($diff / 2419200)." months ago"; |
|
| 550 | 550 | } else { |
| 551 | - return round($diff / 29030400) . " years ago"; |
|
| 551 | + return round($diff / 29030400)." years ago"; |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | } |
@@ -559,7 +559,7 @@ discard block |
||
| 559 | 559 | * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename |
| 560 | 560 | */ |
| 561 | 561 | private static function getAllVersions($uid) { |
| 562 | - $view = new View('/' . $uid . '/'); |
|
| 562 | + $view = new View('/'.$uid.'/'); |
|
| 563 | 563 | $dirs = array(self::VERSIONS_ROOT); |
| 564 | 564 | $versions = array(); |
| 565 | 565 | |
@@ -569,7 +569,7 @@ discard block |
||
| 569 | 569 | |
| 570 | 570 | foreach ($files as $file) { |
| 571 | 571 | $fileData = $file->getData(); |
| 572 | - $filePath = $dir . '/' . $fileData['name']; |
|
| 572 | + $filePath = $dir.'/'.$fileData['name']; |
|
| 573 | 573 | if ($file['type'] === 'dir') { |
| 574 | 574 | $dirs[] = $filePath; |
| 575 | 575 | } else { |
@@ -577,7 +577,7 @@ discard block |
||
| 577 | 577 | $relPathStart = strlen(self::VERSIONS_ROOT); |
| 578 | 578 | $version = substr($filePath, $versionsBegin + 2); |
| 579 | 579 | $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart); |
| 580 | - $key = $version . '#' . $relpath; |
|
| 580 | + $key = $version.'#'.$relpath; |
|
| 581 | 581 | $versions[$key] = array('path' => $relpath, 'timestamp' => $version); |
| 582 | 582 | } |
| 583 | 583 | } |
@@ -618,13 +618,13 @@ discard block |
||
| 618 | 618 | list($toDelete, $size) = self::getAutoExpireList($time, $versions); |
| 619 | 619 | } else { |
| 620 | 620 | $size = 0; |
| 621 | - $toDelete = []; // versions we want to delete |
|
| 621 | + $toDelete = []; // versions we want to delete |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | foreach ($versions as $key => $version) { |
| 625 | 625 | if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) { |
| 626 | 626 | $size += $version['size']; |
| 627 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 627 | + $toDelete[$key] = $version['path'].'.v'.$version['version']; |
|
| 628 | 628 | } |
| 629 | 629 | } |
| 630 | 630 | |
@@ -639,7 +639,7 @@ discard block |
||
| 639 | 639 | */ |
| 640 | 640 | protected static function getAutoExpireList($time, $versions) { |
| 641 | 641 | $size = 0; |
| 642 | - $toDelete = array(); // versions we want to delete |
|
| 642 | + $toDelete = array(); // versions we want to delete |
|
| 643 | 643 | |
| 644 | 644 | $interval = 1; |
| 645 | 645 | $step = Storage::$max_versions_per_interval[$interval]['step']; |
@@ -661,9 +661,9 @@ discard block |
||
| 661 | 661 | if ($nextInterval === -1 || $prevTimestamp > $nextInterval) { |
| 662 | 662 | if ($version['version'] > $nextVersion) { |
| 663 | 663 | //distance between two version too small, mark to delete |
| 664 | - $toDelete[$key] = $version['path'] . '.v' . $version['version']; |
|
| 664 | + $toDelete[$key] = $version['path'].'.v'.$version['version']; |
|
| 665 | 665 | $size += $version['size']; |
| 666 | - \OC::$server->getLogger()->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']); |
|
| 666 | + \OC::$server->getLogger()->info('Mark to expire '.$version['path'].' next version should be '.$nextVersion." or smaller. (prevTimestamp: ".$prevTimestamp."; step: ".$step, ['app' => 'files_versions']); |
|
| 667 | 667 | } else { |
| 668 | 668 | $nextVersion = $version['version'] - $step; |
| 669 | 669 | $prevTimestamp = $version['version']; |
@@ -718,8 +718,8 @@ discard block |
||
| 718 | 718 | // get available disk space for user |
| 719 | 719 | $user = \OC::$server->getUserManager()->get($uid); |
| 720 | 720 | if (is_null($user)) { |
| 721 | - \OC::$server->getLogger()->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']); |
|
| 722 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid); |
|
| 721 | + \OC::$server->getLogger()->error('Backends provided no user object for '.$uid, ['app' => 'files_versions']); |
|
| 722 | + throw new \OC\User\NoUserException('Backends provided no user object for '.$uid); |
|
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | \OC_Util::setupFS($uid); |
@@ -736,7 +736,7 @@ discard block |
||
| 736 | 736 | |
| 737 | 737 | $softQuota = true; |
| 738 | 738 | $quota = $user->getQuota(); |
| 739 | - if ( $quota === null || $quota === 'none' ) { |
|
| 739 | + if ($quota === null || $quota === 'none') { |
|
| 740 | 740 | $quota = Filesystem::free_space('/'); |
| 741 | 741 | $softQuota = false; |
| 742 | 742 | } else { |
@@ -751,7 +751,7 @@ discard block |
||
| 751 | 751 | if ($quota >= 0) { |
| 752 | 752 | if ($softQuota) { |
| 753 | 753 | $userFolder = \OC::$server->getUserFolder($uid); |
| 754 | - if(is_null($userFolder)) { |
|
| 754 | + if (is_null($userFolder)) { |
|
| 755 | 755 | $availableSpace = 0; |
| 756 | 756 | } else { |
| 757 | 757 | $free = $quota - $userFolder->getSize(false); // remaining free space for user |
@@ -791,18 +791,18 @@ discard block |
||
| 791 | 791 | } |
| 792 | 792 | |
| 793 | 793 | $logger = \OC::$server->getLogger(); |
| 794 | - foreach($toDelete as $key => $path) { |
|
| 794 | + foreach ($toDelete as $key => $path) { |
|
| 795 | 795 | \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
| 796 | 796 | self::deleteVersion($versionsFileview, $path); |
| 797 | 797 | \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
| 798 | 798 | unset($allVersions[$key]); // update array with the versions we keep |
| 799 | - $logger->info('Expire: ' . $path, ['app' => 'files_versions']); |
|
| 799 | + $logger->info('Expire: '.$path, ['app' => 'files_versions']); |
|
| 800 | 800 | } |
| 801 | 801 | |
| 802 | 802 | // Check if enough space is available after versions are rearranged. |
| 803 | 803 | // If not we delete the oldest versions until we meet the size limit for versions, |
| 804 | 804 | // but always keep the two latest versions |
| 805 | - $numOfVersions = count($allVersions) -2 ; |
|
| 805 | + $numOfVersions = count($allVersions) - 2; |
|
| 806 | 806 | $i = 0; |
| 807 | 807 | // sort oldest first and make sure that we start at the first element |
| 808 | 808 | ksort($allVersions); |
@@ -810,9 +810,9 @@ discard block |
||
| 810 | 810 | while ($availableSpace < 0 && $i < $numOfVersions) { |
| 811 | 811 | $version = current($allVersions); |
| 812 | 812 | \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
| 813 | - self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']); |
|
| 813 | + self::deleteVersion($versionsFileview, $version['path'].'.v'.$version['version']); |
|
| 814 | 814 | \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED)); |
| 815 | - \OC::$server->getLogger()->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']); |
|
| 815 | + \OC::$server->getLogger()->info('running out of space! Delete oldest version: '.$version['path'].'.v'.$version['version'], ['app' => 'files_versions']); |
|
| 816 | 816 | $versionsSize -= $version['size']; |
| 817 | 817 | $availableSpace += $version['size']; |
| 818 | 818 | next($allVersions); |
@@ -838,7 +838,7 @@ discard block |
||
| 838 | 838 | $dirParts = explode('/', $dirname); |
| 839 | 839 | $dir = "/files_versions"; |
| 840 | 840 | foreach ($dirParts as $part) { |
| 841 | - $dir = $dir . '/' . $part; |
|
| 841 | + $dir = $dir.'/'.$part; |
|
| 842 | 842 | if (!$view->file_exists($dir)) { |
| 843 | 843 | $view->mkdir($dir); |
| 844 | 844 | } |
@@ -849,7 +849,7 @@ discard block |
||
| 849 | 849 | * Static workaround |
| 850 | 850 | * @return Expiration |
| 851 | 851 | */ |
| 852 | - protected static function getExpiration(){ |
|
| 852 | + protected static function getExpiration() { |
|
| 853 | 853 | if (is_null(self::$application)) { |
| 854 | 854 | self::$application = new Application(); |
| 855 | 855 | } |