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