From 2f11f56d323cc7a559ecc42f521083301479920e Mon Sep 17 00:00:00 2001 From: kondou Date: Thu, 18 Apr 2013 18:28:03 +0200 Subject: Use !== and === in files_trashbin-app --- apps/files_trashbin/lib/trash.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index b9d900dfab4..a74d03ea4c5 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -262,14 +262,14 @@ class Trashbin { $query = \OC_DB::prepare('SELECT `location`,`type` FROM `*PREFIX*files_trash`' . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); $result = $query->execute(array($user, $filename, $timestamp))->fetchAll(); - if (count($result) != 1) { + if (count($result) !== 1) { \OC_Log::write('files_trashbin', 'trash bin database inconsistent!', \OC_Log::ERROR); return false; } // if location no longer exists, restore file in the root directory $location = $result[0]['location']; - if ($result[0]['location'] != '/' && + if ($result[0]['location'] !== '/' && (!$view->is_dir('files' . $result[0]['location']) || !$view->isUpdatable('files' . $result[0]['location']))) { $location = ''; -- cgit v1.2.3 From bf668eb14f460ca2dc8661236b533bc861116217 Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Thu, 25 Jul 2013 16:20:06 +0200 Subject: nicer format of unique names, use 'file (restored).txt' instead of 'files.txt.restored' --- apps/files_trashbin/lib/trash.php | 71 +++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 32 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index b9d900dfab4..cf6587ef549 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -283,11 +283,11 @@ class Trashbin { $location = ''; } - $source = \OC\Files\Filesystem::normalizePath('files_trashbin/files/' . $file); - $target = \OC\Files\Filesystem::normalizePath('files/' . $location . '/' . $filename); - // we need a extension in case a file/dir with the same name already exists - $ext = self::getUniqueExtension($location, $filename, $view); + $uniqueFilename = self::getUniqueFilename($location, $filename, $view); + + $source = \OC\Files\Filesystem::normalizePath('files_trashbin/files/' . $file); + $target = \OC\Files\Filesystem::normalizePath('files/' . $location . '/' . $uniqueFilename); $mtime = $view->filemtime($source); // disable proxy to prevent recursive calls @@ -295,24 +295,24 @@ class Trashbin { \OC_FileProxy::$enabled = false; // restore file - $restoreResult = $view->rename($source, $target . $ext); + $restoreResult = $view->rename($source, $target); // handle the restore result if ($restoreResult) { $fakeRoot = $view->getRoot(); $view->chroot('/' . $user . '/files'); - $view->touch('/' . $location . '/' . $filename . $ext, $mtime); + $view->touch('/' . $location . '/' . $uniqueFilename, $mtime); $view->chroot($fakeRoot); - \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', array('filePath' => \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $filename . $ext), + \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', array('filePath' => \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename), 'trashPath' => \OC\Files\Filesystem::normalizePath($file))); - if ($view->is_dir($target . $ext)) { - $trashbinSize -= self::calculateSize(new \OC\Files\View('/' . $user . '/' . $target . $ext)); + if ($view->is_dir($target)) { + $trashbinSize -= self::calculateSize(new \OC\Files\View('/' . $user . '/' . $target)); } else { - $trashbinSize -= $view->filesize($target . $ext); + $trashbinSize -= $view->filesize($target); } - $trashbinSize -= self::restoreVersions($view, $file, $filename, $ext, $location, $timestamp); - $trashbinSize -= self::restoreEncryptionKeys($view, $file, $filename, $ext, $location, $timestamp); + $trashbinSize -= self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); + $trashbinSize -= self::restoreEncryptionKeys($view, $file, $filename, $uniqueFilename, $location, $timestamp); if ($timestamp) { $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); @@ -338,15 +338,16 @@ class Trashbin { * * @param \OC\Files\View $view file view * @param $file complete path to file - * @param $filename name of file - * @param $ext file extension in case a file with the same $filename already exists + * @param $filename name of file once it was deleted + * @param $uniqueFilename new file name to restore the file without overwriting existing files * @param $location location if file * @param $timestamp deleteion time * * @return size of restored versions */ - private static function restoreVersions($view, $file, $filename, $ext, $location, $timestamp) { + private static function restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp) { $size = 0; + if (\OCP\App::isEnabled('files_versions')) { // disable proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -355,7 +356,7 @@ class Trashbin { $user = \OCP\User::getUser(); $rootView = new \OC\Files\View('/'); - $target = \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $filename . $ext); + $target = \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename); list($owner, $ownerPath) = self::getUidAndFilename($target); @@ -392,20 +393,20 @@ class Trashbin { * @param \OC\Files\View $view * @param $file complete path to file * @param $filename name of file - * @param $ext file extension in case a file with the same $filename already exists + * @param $uniqueFilename new file name to restore the file without overwriting existing files * @param $location location of file * @param $timestamp deleteion time * * @return size of restored encrypted file */ - private static function restoreEncryptionKeys($view, $file, $filename, $ext, $location, $timestamp) { + private static function restoreEncryptionKeys($view, $file, $filename, $uniqueFilename, $location, $timestamp) { // Take care of encryption keys TODO! Get '.key' in file between file name and delete date (also for permanent delete!) $size = 0; if (\OCP\App::isEnabled('files_encryption')) { $user = \OCP\User::getUser(); $rootView = new \OC\Files\View('/'); - $target = \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $filename . $ext); + $target = \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename); list($owner, $ownerPath) = self::getUidAndFilename($target); @@ -482,14 +483,11 @@ class Trashbin { // get current sharing state $sharingEnabled = \OCP\Share::isEnabled(); - // get the final filename - $target = \OC\Files\Filesystem::normalizePath($location . '/' . $filename); - // get users sharing this file - $usersSharing = $util->getSharingUsersArray($sharingEnabled, $target . $ext, $user); + $usersSharing = $util->getSharingUsersArray($sharingEnabled, $target, $user); // Attempt to set shareKey - $util->setSharedFileKeyfiles($session, $usersSharing, $target . $ext); + $util->setSharedFileKeyfiles($session, $usersSharing, $target); } } @@ -780,18 +778,27 @@ class Trashbin { * @param $view filesystem view relative to users root directory * @return string with unique extension */ - private static function getUniqueExtension($location, $filename, $view) { - $ext = ''; + private static function getUniqueFilename($location, $filename, $view) { + $ext = pathinfo($filename, PATHINFO_EXTENSION); + $name = pathinfo($filename, PATHINFO_FILENAME); + + // if extension is not empty we set a dot in front of it + if ($ext !== '') { + $ext = '.' . $ext; + } + if ($view->file_exists('files' . $location . '/' . $filename)) { - $tmpext = '.restored'; - $ext = $tmpext; - $i = 1; - while ($view->file_exists('files' . $location . '/' . $filename . $ext)) { - $ext = $tmpext . $i; + $i = 2; + $uniqueName = $name . " (restored)" . $ext; + while ($view->file_exists('files' . $location . '/' . $uniqueName)) { + $uniqueName = $name . " (restored " . $i . ")" . $ext; $i++; } + + return $uniqueName; } - return $ext; + + return $filename; } /** -- cgit v1.2.3 From 96e175ffbf6f209274395309e5b55ed724c5c2da Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Thu, 25 Jul 2013 16:27:40 +0200 Subject: don't move empty folders to the trash bin --- apps/files_trashbin/lib/trash.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index cf6587ef549..50fdaccfb1e 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -72,6 +72,11 @@ class Trashbin { $mime = $view->getMimeType('files' . $file_path); if ($view->is_dir('files' . $file_path)) { + $dirContent = $view->getDirectoryContent('files' . $file_path); + // no need to move empty folders to the trash bin + if (empty($dirContent)) { + return true; + } $type = 'dir'; } else { $type = 'file'; -- cgit v1.2.3 From a00cff7c0543a8860b839a0bf345478e99bc6c18 Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Fri, 26 Jul 2013 11:13:43 +0200 Subject: disable "deleted files" button if trash bin is empty --- apps/files/css/files.css | 2 +- apps/files/index.php | 9 ++++++++- apps/files/js/filelist.js | 1 + apps/files/js/files.js | 12 ++++++++++-- apps/files/templates/index.php | 4 +--- apps/files_trashbin/lib/trash.php | 15 +++++++++++++++ 6 files changed, 36 insertions(+), 7 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files/css/files.css b/apps/files/css/files.css index f2ca1065eca..117aac4c93e 100644 --- a/apps/files/css/files.css +++ b/apps/files/css/files.css @@ -24,7 +24,7 @@ #new>ul>li>p { cursor:pointer; } #new>ul>li>form>input { padding:0.3em; margin:-0.3em; } -#trash { height:17px; margin: 0 1em; z-index:1010; float: right; } +#trash { margin: 0 1em; z-index:1010; float: right; } #upload { height:27px; padding:0; margin-left:0.2em; overflow:hidden; diff --git a/apps/files/index.php b/apps/files/index.php index 2f005391509..4f9e881eb2d 100644 --- a/apps/files/index.php +++ b/apps/files/index.php @@ -126,6 +126,12 @@ if ($needUpgrade) { $publicUploadEnabled = 'no'; } + $trashEnabled = \OCP\App::isEnabled('files_trashbin'); + $trashEmpty = true; + if ($trashEnabled) { + $trashEmpty = \OCA\Files_Trashbin\Trashbin::isEmpty($user); + } + OCP\Util::addscript('files', 'fileactions'); OCP\Util::addscript('files', 'files'); OCP\Util::addscript('files', 'keyboardshortcuts'); @@ -136,7 +142,8 @@ if ($needUpgrade) { $tmpl->assign('isCreatable', \OC\Files\Filesystem::isCreatable($dir . '/')); $tmpl->assign('permissions', $permissions); $tmpl->assign('files', $files); - $tmpl->assign('trash', \OCP\App::isEnabled('files_trashbin')); + $tmpl->assign('trash', $trashEnabled); + $tmpl->assign('trashEmpty', $trashEmpty); $tmpl->assign('uploadMaxFilesize', $maxUploadFilesize); $tmpl->assign('uploadMaxHumanFilesize', OCP\Util::humanFileSize($maxUploadFilesize)); $tmpl->assign('allowZipDownload', intval(OCP\Config::getSystemValue('allowZipDownload', true))); diff --git a/apps/files/js/filelist.js b/apps/files/js/filelist.js index 04a9fb91649..b858e2580ee 100644 --- a/apps/files/js/filelist.js +++ b/apps/files/js/filelist.js @@ -392,6 +392,7 @@ var FileList={ files.removeClass('selected'); }); procesSelection(); + checkTrashStatus(); } else { $.each(files,function(index,file) { var deleteAction = $('tr').filterAttr('data-file',file).children("td.date").children(".move2trash"); diff --git a/apps/files/js/files.js b/apps/files/js/files.js index 98fc53b71a9..55280edde42 100644 --- a/apps/files/js/files.js +++ b/apps/files/js/files.js @@ -121,10 +121,10 @@ $(document).ready(function() { }); // Show trash bin - $('#trash a').live('click', function() { + $('#trash').on('click', function() { window.location=OC.filePath('files_trashbin', '', 'index.php'); }); - + var lastChecked; // Sets the file link behaviour : @@ -845,3 +845,11 @@ function getUniqueName(name){ } return name; } + +function checkTrashStatus() { + $.post(OC.filePath('files_trashbin', 'ajax', 'isEmpty.php'), function(result){ + if (result.data.isEmpty === false) { + $("input[type=button][id=trash]").removeAttr("disabled"); + } + }); +} \ No newline at end of file diff --git a/apps/files/templates/index.php b/apps/files/templates/index.php index e0731609368..a70c0eaa117 100644 --- a/apps/files/templates/index.php +++ b/apps/files/templates/index.php @@ -38,9 +38,7 @@ - + >
diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index 50fdaccfb1e..cff97418fe4 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -868,5 +868,20 @@ class Trashbin { //Listen to delete user signal \OCP\Util::connectHook('OC_User', 'pre_deleteUser', "OCA\Files_Trashbin\Hooks", "deleteUser_hook"); } + + /** + * @brief check if trash bin is empty for a given user + * @param string $user + */ + public static function isEmpty($user) { + + $trashSize = self::getTrashbinSize($user); + + if ($trashSize !== false && $trashSize > 0) { + return false; + } + + return true; + } } -- cgit v1.2.3 From 0182a503a6876a37493c29055a21cc22e1164404 Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Fri, 26 Jul 2013 11:45:38 +0200 Subject: expire trash bin if trash bin exceeds max size after a new file was added to ownCloud --- apps/files_trashbin/lib/hooks.php | 4 ++++ apps/files_trashbin/lib/trash.php | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/hooks.php b/apps/files_trashbin/lib/hooks.php index f1df1d7ec77..b2c6bc1df50 100644 --- a/apps/files_trashbin/lib/hooks.php +++ b/apps/files_trashbin/lib/hooks.php @@ -56,4 +56,8 @@ class Hooks { Trashbin::deleteUser($uid); } } + + public static function post_write_hook($params) { + Trashbin::resizeTrash(\OCP\User::getUser()); + } } diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index cff97418fe4..b2bd562e37d 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -105,8 +105,8 @@ class Trashbin { \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', array('filePath' => \OC\Files\Filesystem::normalizePath($file_path), 'trashPath' => \OC\Files\Filesystem::normalizePath($filename . '.d' . $timestamp))); - $trashbinSize += self::retainVersions($view, $file_path, $filename, $timestamp); - $trashbinSize += self::retainEncryptionKeys($view, $file_path, $filename, $timestamp); + $trashbinSize += self::retainVersions($file_path, $filename, $timestamp); + $trashbinSize += self::retainEncryptionKeys($file_path, $filename, $timestamp); } else { \OC_Log::write('files_trashbin', 'Couldn\'t move ' . $file_path . ' to the trash bin', \OC_log::ERROR); } @@ -119,14 +119,13 @@ class Trashbin { /** * Move file versions to trash so that they can be restored later * - * @param \OC\Files\View $view * @param $file_path path to original file * @param $filename of deleted file * @param $timestamp when the file was deleted * * @return size of stored versions */ - private static function retainVersions($view, $file_path, $filename, $timestamp) { + private static function retainVersions($file_path, $filename, $timestamp) { $size = 0; if (\OCP\App::isEnabled('files_versions')) { @@ -159,14 +158,13 @@ class Trashbin { /** * Move encryption keys to trash so that they can be restored later * - * @param \OC\Files\View $view * @param $file_path path to original file * @param $filename of deleted file * @param $timestamp when the file was deleted * * @return size of encryption keys */ - private static function retainEncryptionKeys($view, $file_path, $filename, $timestamp) { + private static function retainEncryptionKeys($file_path, $filename, $timestamp) { $size = 0; if (\OCP\App::isEnabled('files_encryption')) { @@ -670,9 +668,32 @@ class Trashbin { return $availableSpace; } + /** + * @brief resize trash bin if necessary after a new file was added to ownCloud + * @param string $user user id + */ + public static function resizeTrash($user) { + + $size = self::getTrashbinSize($user); + + if ($size === false || $size < 0) { + $size = self::calculateSize(new \OC\Files\View('/' . $user . '/files_trashbin')); + } + + $freeSpace = self::calculateFreeSpace($size); + + if ($freeSpace < 0) { + $newSize = $size - self::expire($size); + if ($newSize !== $size) { + self::setTrashbinSize($user, $newSize); + } + } + } + /** * clean up the trash bin * @param current size of the trash bin + * @return size of expired files */ private static function expire($trashbinSize) { @@ -839,7 +860,7 @@ class Trashbin { $result = $query->execute(array($user))->fetchAll(); if ($result) { - return $result[0]['size']; + return (int)$result[0]['size']; } return false; } @@ -867,6 +888,8 @@ class Trashbin { \OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Files_Trashbin\Hooks", "remove_hook"); //Listen to delete user signal \OCP\Util::connectHook('OC_User', 'pre_deleteUser', "OCA\Files_Trashbin\Hooks", "deleteUser_hook"); + //Listen to post write hook + \OCP\Util::connectHook('OC_Filesystem', 'post_write', "OCA\Files_Trashbin\Hooks", "post_write_hook"); } /** -- cgit v1.2.3 From a5d9b985c3d8a412b831cb1ceb9ba723ecd7a150 Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Fri, 26 Jul 2013 12:01:45 +0200 Subject: make 'restored' extension translatable --- apps/files_trashbin/lib/trash.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index b2bd562e37d..d09c4d5f7b1 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -807,6 +807,7 @@ class Trashbin { private static function getUniqueFilename($location, $filename, $view) { $ext = pathinfo($filename, PATHINFO_EXTENSION); $name = pathinfo($filename, PATHINFO_FILENAME); + $l = \OC_L10N::get('files_trashbin'); // if extension is not empty we set a dot in front of it if ($ext !== '') { @@ -815,9 +816,9 @@ class Trashbin { if ($view->file_exists('files' . $location . '/' . $filename)) { $i = 2; - $uniqueName = $name . " (restored)" . $ext; + $uniqueName = $name . " (".$l->t("restored").")". $ext; while ($view->file_exists('files' . $location . '/' . $uniqueName)) { - $uniqueName = $name . " (restored " . $i . ")" . $ext; + $uniqueName = $name . " (".$l->t("restored") . " " . $i . ")" . $ext; $i++; } -- cgit v1.2.3 From 0a9bb8ba73f80fd63592d994c837345698131e9b Mon Sep 17 00:00:00 2001 From: Björn Schießle Date: Fri, 26 Jul 2013 15:15:47 +0200 Subject: reduce time for auto expire to 30 days by default --- apps/files_trashbin/lib/trash.php | 2 +- config/config.sample.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trash.php b/apps/files_trashbin/lib/trash.php index d09c4d5f7b1..ba940084a87 100644 --- a/apps/files_trashbin/lib/trash.php +++ b/apps/files_trashbin/lib/trash.php @@ -25,7 +25,7 @@ namespace OCA\Files_Trashbin; class Trashbin { // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) - const DEFAULT_RETENTION_OBLIGATION = 180; + const DEFAULT_RETENTION_OBLIGATION = 30; // unit: percentage; 50% of available disk space/quota const DEFAULTMAXSIZE = 50; diff --git a/config/config.sample.php b/config/config.sample.php index 4d1950d60ee..8d978ea6ee7 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -114,8 +114,8 @@ $CONFIG = array( /* Password to use for sendmail mail, depends on mail_smtpauth if this is used */ "mail_smtppassword" => "", -/* How long should ownCloud keep deleted files in the trash bin, default value: 180 days */ -'trashbin_retention_obligation' => 180, +/* How long should ownCloud keep deleted files in the trash bin, default value: 30 days */ +'trashbin_retention_obligation' => 30, /* allow user to change his display name, if it is supported by the back-end */ 'allow_user_to_change_display_name' => true, -- cgit v1.2.3