From eab844e2267a2955711f9d426c47eedeeea2d479 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 25 Feb 2014 20:46:41 +0100 Subject: if file is not in db, fall back to restore file to the users root --- apps/files_trashbin/lib/helper.php | 55 ++++++++++++++++++--------------- apps/files_trashbin/lib/trashbin.php | 60 +++++++++++++----------------------- 2 files changed, 53 insertions(+), 62 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/helper.php b/apps/files_trashbin/lib/helper.php index c454b35a5f2..f1cec6cc94e 100644 --- a/apps/files_trashbin/lib/helper.php +++ b/apps/files_trashbin/lib/helper.php @@ -12,35 +12,42 @@ class Helper */ public static function getTrashFiles($dir){ $result = array(); + $timestamp = null; $user = \OCP\User::getUser(); - if ($dir && $dir !== '/') { - $view = new \OC_Filesystemview('/'.$user.'/files_trashbin/files'); - $dirContent = $view->opendir($dir); - if ($dirContent === false){ - return null; - } - if(is_resource($dirContent)){ - while(($entryName = readdir($dirContent)) !== false) { - if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) { - $pos = strpos($dir.'/', '/', 1); - $tmp = substr($dir, 0, $pos); - $pos = strrpos($tmp, '.d'); - $timestamp = substr($tmp, $pos+2); - $result[] = array( - 'id' => $entryName, - 'timestamp' => $timestamp, - 'mime' => $view->getMimeType($dir.'/'.$entryName), - 'type' => $view->is_dir($dir.'/'.$entryName) ? 'dir' : 'file', - 'location' => $dir, - ); + $view = new \OC_Filesystemview('/' . $user . '/files_trashbin/files'); + + if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { + throw new \Exception('Directory does not exists'); + } + + $dirContent = $view->opendir($dir); + if ($dirContent === false) { + return $result; + } + if (is_resource($dirContent)) { + while (($entryName = readdir($dirContent)) !== false) { + if (!\OC\Files\Filesystem::isIgnoredDir($entryName)) { + $id = $entryName; + if ($dir === '' || $dir === '/') { + $pathparts = pathinfo($entryName); + $timestamp = substr($pathparts['extension'], 1); + $id = $pathparts['filename']; + } else if ($timestamp === null) { + // for subfolders we need to calculate the timestamp only once + $parts = explode('/', ltrim($dir, '/')); + $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); } + $result[] = array( + 'id' => $id, + 'timestamp' => $timestamp, + 'mime' => $view->getMimeType($dir . '/' . $entryName), + 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file', + 'location' => $dir, + ); } - closedir($dirContent); } - } else { - $query = \OC_DB::prepare('SELECT `id`,`location`,`timestamp`,`type`,`mime` FROM `*PREFIX*files_trash` WHERE `user` = ?'); - $result = $query->execute(array($user))->fetchAll(); + closedir($dirContent); } $files = array(); diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index bc77e9c1543..3933395c298 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -62,11 +62,13 @@ class Trashbin { /** + * @brief copy file to owners trash + * @param string $sourcePath * @param string $owner + * @param string $ownerPath * @param integer $timestamp - * @param string $type */ - private static function copyFilesToOwner($sourcePath, $owner, $ownerPath, $timestamp, $type, $mime) { + private static function copyFilesToOwner($sourcePath, $owner, $ownerPath, $timestamp) { self::setUpTrash($owner); $ownerFilename = basename($ownerPath); @@ -82,12 +84,10 @@ class Trashbin { if ($view->file_exists($target)) { - $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`type`,`mime`,`user`) VALUES (?,?,?,?,?,?)"); - $result = $query->execute(array($ownerFilename, $timestamp, $ownerLocation, $type, $mime, $owner)); - if (!$result) { // if file couldn't be added to the database than also don't store it in the trash bin. - $view->deleteAll($owner.'/files_trashbin/files/' . $ownerFilename . '.d' . $timestamp); + $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); + $result = $query->execute(array($ownerFilename, $timestamp, $ownerLocation, $owner)); + if (!$result) { \OC_Log::write('files_trashbin', 'trash bin database couldn\'t be updated for the files owner', \OC_log::ERROR); - return; } } } @@ -110,13 +110,6 @@ class Trashbin { $filename = $path_parts['basename']; $location = $path_parts['dirname']; $timestamp = time(); - $mime = $view->getMimeType('files' . $file_path); - - if ($view->is_dir('files' . $file_path)) { - $type = 'dir'; - } else { - $type = 'file'; - } $userTrashSize = self::getTrashbinSize($user); if ($userTrashSize === false || $userTrashSize < 0) { @@ -132,12 +125,10 @@ class Trashbin { if ($view->file_exists('files_trashbin/files/' . $filename . '.d' . $timestamp)) { $size = $sizeOfAddedFiles; - $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`type`,`mime`,`user`) VALUES (?,?,?,?,?,?)"); - $result = $query->execute(array($filename, $timestamp, $location, $type, $mime, $user)); - if (!$result) { // if file couldn't be added to the database than also don't store it in the trash bin. - $view->deleteAll('files_trashbin/files/' . $filename . '.d' . $timestamp); + $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); + $result = $query->execute(array($filename, $timestamp, $location, $user)); + if (!$result) { \OC_Log::write('files_trashbin', 'trash bin database couldn\'t be updated', \OC_log::ERROR); - return; } \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))); @@ -147,7 +138,7 @@ class Trashbin { // if owner !== user we need to also add a copy to the owners trash if ($user !== $owner) { - self::copyFilesToOwner($file_path, $owner, $ownerPath, $timestamp, $type, $mime); + self::copyFilesToOwner($file_path, $owner, $ownerPath, $timestamp); } } else { \OC_Log::write('files_trashbin', 'Couldn\'t move ' . $file_path . ' to the trash bin', \OC_log::ERROR); @@ -330,29 +321,22 @@ class Trashbin { if ($trashbinSize === false || $trashbinSize < 0) { $trashbinSize = self::calculateSize(new \OC\Files\View('/' . $user . '/files_trashbin')); } + $location = ''; if ($timestamp) { - $query = \OC_DB::prepare('SELECT `location`,`type` FROM `*PREFIX*files_trash`' - . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); + $query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`' + . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); $result = $query->execute(array($user, $filename, $timestamp))->fetchAll(); 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'] !== '/' && - (!$view->is_dir('files' . $result[0]['location']) || - !$view->isUpdatable('files' . $result[0]['location']))) { - $location = ''; + } else { + $location = $result[0]['location']; + // if location no longer exists, restore file in the root directory + if ($location !== '/' && + (!$view->is_dir('files' . $location) || + !$view->isUpdatable('files' . $location))) { + $location = ''; + } } - } else { - $path_parts = pathinfo($file); - $result[] = array( - 'location' => $path_parts['dirname'], - 'type' => $view->is_dir('/files_trashbin/files/' . $file) ? 'dir' : 'files', - ); - $location = ''; } // we need a extension in case a file/dir with the same name already exists -- cgit v1.2.3 From abad625cac2ef0417996f196f522ef006836940d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 25 Feb 2014 21:28:39 +0100 Subject: only enable previews if the encryption is disabled --- apps/files_trashbin/lib/helper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/helper.php b/apps/files_trashbin/lib/helper.php index f1cec6cc94e..4bb51860485 100644 --- a/apps/files_trashbin/lib/helper.php +++ b/apps/files_trashbin/lib/helper.php @@ -70,7 +70,11 @@ class Helper $i['directory'] = ''; } $i['permissions'] = \OCP\PERMISSION_READ; - $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($r['mime']); + if (\OCP\App::isEnabled('files_encryption')) { + $i['isPreviewAvailable'] = false; + } else { + $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($r['mime']); + } $i['icon'] = \OCA\Files\Helper::determineIcon($i); $files[] = $i; } -- cgit v1.2.3 From 4562909a2021be795b89cb7fcf6f244d7f6a8204 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 25 Feb 2014 21:35:54 +0100 Subject: get trash size from file cache --- apps/files_trashbin/appinfo/database.xml | 4 +- apps/files_trashbin/appinfo/update.php | 2 +- apps/files_trashbin/appinfo/version | 2 +- apps/files_trashbin/lib/trashbin.php | 79 +++----------------------------- lib/private/files/cache/homecache.php | 2 +- 5 files changed, 11 insertions(+), 78 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/appinfo/database.xml b/apps/files_trashbin/appinfo/database.xml index d08c3469b02..db104ee9298 100644 --- a/apps/files_trashbin/appinfo/database.xml +++ b/apps/files_trashbin/appinfo/database.xml @@ -49,7 +49,7 @@ type text - true + false 4 @@ -57,7 +57,7 @@ mime text - true + false 255 diff --git a/apps/files_trashbin/appinfo/update.php b/apps/files_trashbin/appinfo/update.php index 0ca232668d7..ca7b87a8681 100644 --- a/apps/files_trashbin/appinfo/update.php +++ b/apps/files_trashbin/appinfo/update.php @@ -2,7 +2,7 @@ $installedVersion=OCP\Config::getAppValue('files_trashbin', 'installed_version'); -if (version_compare($installedVersion, '0.4', '<')) { +if (version_compare($installedVersion, '0.6', '<')) { //size of the trash bin could be incorrect, remove it for all users to //enforce a recalculation during next usage. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trashsize`'); diff --git a/apps/files_trashbin/appinfo/version b/apps/files_trashbin/appinfo/version index 2eb3c4fe4ee..5a2a5806df6 100644 --- a/apps/files_trashbin/appinfo/version +++ b/apps/files_trashbin/appinfo/version @@ -1 +1 @@ -0.5 +0.6 diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 3933395c298..f6816b2b4c2 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -112,9 +112,6 @@ class Trashbin { $timestamp = time(); $userTrashSize = self::getTrashbinSize($user); - if ($userTrashSize === false || $userTrashSize < 0) { - $userTrashSize = self::calculateSize(new \OC\Files\View('/' . $user . '/files_trashbin')); - } // disable proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; @@ -146,17 +143,12 @@ class Trashbin { $userTrashSize += $size; $userTrashSize -= self::expire($userTrashSize, $user); - self::setTrashbinSize($user, $userTrashSize); // if owner !== user we also need to update the owners trash size if($owner !== $user) { $ownerTrashSize = self::getTrashbinSize($owner); - if ($ownerTrashSize === false || $ownerTrashSize < 0) { - $ownerTrashSize = self::calculateSize(new \OC\Files\View('/' . $owner . '/files_trashbin')); - } $ownerTrashSize += $size; $ownerTrashSize -= self::expire($ownerTrashSize, $owner); - self::setTrashbinSize($owner, $ownerTrashSize); } } @@ -317,10 +309,6 @@ class Trashbin { $user = \OCP\User::getUser(); $view = new \OC\Files\View('/' . $user); - $trashbinSize = self::getTrashbinSize($user); - if ($trashbinSize === false || $trashbinSize < 0) { - $trashbinSize = self::calculateSize(new \OC\Files\View('/' . $user . '/files_trashbin')); - } $location = ''; if ($timestamp) { $query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`' @@ -361,22 +349,15 @@ class Trashbin { $view->chroot($fakeRoot); \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)) { - $trashbinSize -= self::calculateSize(new \OC\Files\View('/' . $user . '/' . $target)); - } else { - $trashbinSize -= $view->filesize($target); - } - $trashbinSize -= self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); - $trashbinSize -= self::restoreEncryptionKeys($view, $file, $filename, $uniqueFilename, $location, $timestamp); + self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); + 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`=?'); $query->execute(array($user, $filename, $timestamp)); } - self::setTrashbinSize($user, $trashbinSize); - // enable proxy \OC_FileProxy::$enabled = $proxyStatus; @@ -399,10 +380,8 @@ class Trashbin { * @param $location location if file * @param $timestamp deleteion time * - * @return size of restored versions */ private static function restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp) { - $size = 0; if (\OCP\App::isEnabled('files_versions')) { // disable proxy to prevent recursive calls @@ -423,15 +402,12 @@ class Trashbin { } if ($view->is_dir('/files_trashbin/versions/' . $file)) { - $size += self::calculateSize(new \OC\Files\View('/' . $user . '/' . 'files_trashbin/versions/' . $file)); $rootView->rename(\OC\Files\Filesystem::normalizePath($user . '/files_trashbin/versions/' . $file), \OC\Files\Filesystem::normalizePath($owner . '/files_versions/' . $ownerPath)); } else if ($versions = self::getVersionsFromTrash($versionedFile, $timestamp)) { foreach ($versions as $v) { if ($timestamp) { - $size += $view->filesize('files_trashbin/versions/' . $versionedFile . '.v' . $v . '.d' . $timestamp); $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v . '.d' . $timestamp, $owner . '/files_versions/' . $ownerPath . '.v' . $v); } else { - $size += $view->filesize('files_trashbin/versions/' . $versionedFile . '.v' . $v); $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v, $owner . '/files_versions/' . $ownerPath . '.v' . $v); } } @@ -440,7 +416,6 @@ class Trashbin { // enable proxy \OC_FileProxy::$enabled = $proxyStatus; } - return $size; } /** @@ -453,11 +428,9 @@ class Trashbin { * @param $location location of file * @param $timestamp deleteion time * - * @return size of restored encrypted file */ 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('/'); @@ -502,18 +475,15 @@ class Trashbin { if ($rootView->is_dir($keyfile)) { // handle keyfiles - $size += self::calculateSize(new \OC\Files\View($keyfile)); $rootView->rename($keyfile, $baseDir . '/keyfiles/' . $ownerPath); // handle share-keys if ($timestamp) { $sharekey .= '.d' . $timestamp; } - $size += self::calculateSize(new \OC\Files\View($sharekey)); $rootView->rename($sharekey, $baseDir . '/share-keys/' . $ownerPath); } else { // handle keyfiles - $size += $rootView->filesize($keyfile); $rootView->rename($keyfile, $baseDir . '/keyfiles/' . $ownerPath . '.key'); // handle share-keys @@ -522,8 +492,6 @@ class Trashbin { $ownerShareKey .= '.d' . $timestamp; } - $size += $rootView->filesize($ownerShareKey); - // move only owners key $rootView->rename($ownerShareKey, $baseDir . '/share-keys/' . $ownerPath . '.' . $user . '.shareKey'); @@ -550,7 +518,6 @@ class Trashbin { // enable proxy \OC_FileProxy::$enabled = $proxyStatus; } - return $size; } /** @@ -560,7 +527,6 @@ class Trashbin { $user = \OCP\User::getUser(); $view = new \OC\Files\View('/' . $user); $view->deleteAll('files_trashbin'); - self::setTrashbinSize($user, 0); $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?'); $query->execute(array($user)); @@ -581,11 +547,6 @@ class Trashbin { $view = new \OC\Files\View('/' . $user); $size = 0; - $trashbinSize = self::getTrashbinSize($user); - if ($trashbinSize === false || $trashbinSize < 0) { - $trashbinSize = self::calculateSize(new \OC\Files\View('/' . $user . '/files_trashbin')); - } - if ($timestamp) { $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); $query->execute(array($user, $filename, $timestamp)); @@ -604,8 +565,6 @@ class Trashbin { } $view->unlink('/files_trashbin/files/' . $file); \OC_Hook::emit('\OCP\Trashbin', 'delete', array('path' => '/files_trashbin/files/' . $file)); - $trashbinSize -= $size; - self::setTrashbinSize($user, $trashbinSize); return $size; } @@ -751,17 +710,10 @@ class Trashbin { $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, $user); - if ($newSize !== $size) { - self::setTrashbinSize($user, $newSize); - } + self::expire($size, $user); } } @@ -938,28 +890,9 @@ class Trashbin { * @return mixed trash bin size or false if no trash bin size is stored */ private static function getTrashbinSize($user) { - $query = \OC_DB::prepare('SELECT `size` FROM `*PREFIX*files_trashsize` WHERE `user`=?'); - $result = $query->execute(array($user))->fetchAll(); - - if ($result) { - return (int)$result[0]['size']; - } - return false; - } - - /** - * write to the database how much space is in use for the trash bin - * - * @param $user owner of the trash bin - * @param $size size of the trash bin - */ - private static function setTrashbinSize($user, $size) { - if (self::getTrashbinSize($user) === false) { - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*files_trashsize` (`size`, `user`) VALUES (?, ?)'); - } else { - $query = \OC_DB::prepare('UPDATE `*PREFIX*files_trashsize` SET `size`=? WHERE `user`=?'); - } - $query->execute(array($size, $user)); + $view = new \OC\Files\View('/' . $user); + $fileInfo = $view->getFileInfo('/files_trashbin'); + return $fileInfo['size']; } /** diff --git a/lib/private/files/cache/homecache.php b/lib/private/files/cache/homecache.php index a7c310a3782..a35e4d5e1b9 100644 --- a/lib/private/files/cache/homecache.php +++ b/lib/private/files/cache/homecache.php @@ -16,7 +16,7 @@ class HomeCache extends Cache { * @return int */ public function calculateFolderSize($path) { - if ($path !== '/' and $path !== '' and $path !== 'files') { + if ($path !== '/' and $path !== '' and $path !== 'files' and $path !== 'files_trashbin') { return parent::calculateFolderSize($path); } -- cgit v1.2.3 From 5ddabd7cd02108718b286b36de5927d90afc150a Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 3 Mar 2014 12:23:21 +0100 Subject: fix mimetype detection, introduce fake etag to identify preview images --- apps/files_trashbin/lib/helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/helper.php b/apps/files_trashbin/lib/helper.php index 4bb51860485..fe0d1d30a72 100644 --- a/apps/files_trashbin/lib/helper.php +++ b/apps/files_trashbin/lib/helper.php @@ -41,7 +41,7 @@ class Helper $result[] = array( 'id' => $id, 'timestamp' => $timestamp, - 'mime' => $view->getMimeType($dir . '/' . $entryName), + 'mime' => \OC_Helper::getFileNameMimeType($id), 'type' => $view->is_dir($dir . '/' . $entryName) ? 'dir' : 'file', 'location' => $dir, ); @@ -58,6 +58,7 @@ class Helper $i['name'] = $r['id']; $i['date'] = \OCP\Util::formatDate($r['timestamp']); $i['timestamp'] = $r['timestamp']; + $i['etag'] = $r['timestamp']; // add fake etag, it is only needed to identify the preview image $i['mimetype'] = $r['mime']; $i['type'] = $r['type']; if ($i['type'] === 'file') { -- cgit v1.2.3 From 0f864c38bba9e3ad352064db76a3d46be46eba2d Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Tue, 11 Mar 2014 14:10:12 +0100 Subject: add preDelete hook for files_trashbin app --- apps/files_trashbin/lib/trashbin.php | 1 + 1 file changed, 1 insertion(+) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index f6816b2b4c2..7e91f8a59ff 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -563,6 +563,7 @@ class Trashbin { } else { $size += $view->filesize('/files_trashbin/files/' . $file); } + \OC_Hook::emit('\OCP\Trashbin', 'preDelete', array('path' => '/files_trashbin/files/' . $file)); $view->unlink('/files_trashbin/files/' . $file); \OC_Hook::emit('\OCP\Trashbin', 'delete', array('path' => '/files_trashbin/files/' . $file)); -- cgit v1.2.3 From 332c14a878e438cac22ca7da9a65af1647471452 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 17 Mar 2014 16:35:08 +0100 Subject: Use a more efficient way to determine that the trashbin is empty that doesn't rely on the cache --- apps/files_trashbin/lib/trashbin.php | 43 +++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 7e91f8a59ff..dc38c339ecb 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -78,8 +78,8 @@ class Trashbin { $view = new \OC\Files\View('/'); - $source = \OCP\User::getUser().'/files_trashbin/files/' . $sourceFilename . '.d' . $timestamp; - $target = $owner.'/files_trashbin/files/' . $ownerFilename . '.d' . $timestamp; + $source = \OCP\User::getUser() . '/files_trashbin/files/' . $sourceFilename . '.d' . $timestamp; + $target = $owner . '/files_trashbin/files/' . $ownerFilename . '.d' . $timestamp; self::copy_recursive($source, $target, $view); @@ -117,7 +117,7 @@ class Trashbin { $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; - $sizeOfAddedFiles = self::copy_recursive('/files/'.$file_path, $trashPath, $view); + $sizeOfAddedFiles = self::copy_recursive('/files/' . $file_path, $trashPath, $view); \OC_FileProxy::$enabled = $proxyStatus; if ($view->file_exists('files_trashbin/files/' . $filename . '.d' . $timestamp)) { @@ -145,7 +145,7 @@ class Trashbin { $userTrashSize -= self::expire($userTrashSize, $user); // if owner !== user we also need to update the owners trash size - if($owner !== $user) { + if ($owner !== $user) { $ownerTrashSize = self::getTrashbinSize($owner); $ownerTrashSize += $size; $ownerTrashSize -= self::expire($ownerTrashSize, $owner); @@ -298,6 +298,7 @@ class Trashbin { /** * restore files from trash bin + * * @param $file path to the deleted file * @param $filename name of the file * @param $timestamp time when the file was deleted @@ -312,7 +313,7 @@ class Trashbin { $location = ''; if ($timestamp) { $query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`' - . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); + . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); $result = $query->execute(array($user, $filename, $timestamp))->fetchAll(); if (count($result) !== 1) { \OC_Log::write('files_trashbin', 'trash bin database inconsistent!', \OC_Log::ERROR); @@ -320,8 +321,9 @@ class Trashbin { $location = $result[0]['location']; // if location no longer exists, restore file in the root directory if ($location !== '/' && - (!$view->is_dir('files' . $location) || - !$view->isUpdatable('files' . $location))) { + (!$view->is_dir('files' . $location) || + !$view->isUpdatable('files' . $location)) + ) { $location = ''; } } @@ -631,6 +633,7 @@ class Trashbin { /** * check to see whether a file exists in trashbin + * * @param $filename path to the file * @param $timestamp of deletion time * @return true if file exists, otherwise false @@ -720,6 +723,7 @@ class Trashbin { /** * clean up the trash bin + * * @param int $trashbinSize current size of the trash bin * @param string $user * @return int size of expired files @@ -755,7 +759,7 @@ class Trashbin { // if size limit for trash bin reached, delete oldest files in trash bin if ($availableSpace < 0) { $query = \OC_DB::prepare('SELECT `location`,`type`,`id`,`timestamp` FROM `*PREFIX*files_trash`' - . ' WHERE `user`=? ORDER BY `timestamp` ASC'); + . ' WHERE `user`=? ORDER BY `timestamp` ASC'); $result = $query->execute(array($user))->fetchAll(); $length = count($result); $i = 0; @@ -803,6 +807,7 @@ class Trashbin { /** * find all versions which belong to the file we want to restore + * * @param $filename name of the file which should be restored * @param $timestamp timestamp when the file was deleted */ @@ -822,10 +827,10 @@ class Trashbin { foreach ($matches as $ma) { if ($timestamp) { $parts = explode('.v', substr($ma, 0, $offset)); - $versions[] = ( end($parts) ); + $versions[] = (end($parts)); } else { $parts = explode('.v', $ma); - $versions[] = ( end($parts) ); + $versions[] = (end($parts)); } } return $versions; @@ -833,6 +838,7 @@ class Trashbin { /** * find unique extension for restored file if a file with the same name already exists + * * @param $location where the file should be restored * @param $filename name of the file * @param \OC\Files\View $view filesystem view relative to users root directory @@ -850,9 +856,9 @@ class Trashbin { if ($view->file_exists('files' . $location . '/' . $filename)) { $i = 2; - $uniqueName = $name . " (".$l->t("restored").")". $ext; + $uniqueName = $name . " (" . $l->t("restored") . ")" . $ext; while ($view->file_exists('files' . $location . '/' . $uniqueName)) { - $uniqueName = $name . " (".$l->t("restored") . " " . $i . ")" . $ext; + $uniqueName = $name . " (" . $l->t("restored") . " " . $i . ")" . $ext; $i++; } @@ -915,16 +921,13 @@ class Trashbin { public static function isEmpty($user) { $view = new \OC\Files\View('/' . $user . '/files_trashbin'); - $content = $view->getDirectoryContent('/files'); - - if ($content) { - return false; - } - - return true; + $dh = $view->opendir('/files'); + readdir($dh); //'..' + readdir($dh); //'.' + return readdir($dh) === false; } public static function preview_icon($path) { - return \OC_Helper::linkToRoute( 'core_ajax_trashbin_preview', array('x' => 36, 'y' => 36, 'file' => $path )); + return \OC_Helper::linkToRoute('core_ajax_trashbin_preview', array('x' => 36, 'y' => 36, 'file' => $path)); } } -- cgit v1.2.3 From 38b1529c2a24a0fc58ce78a444f918750442a8aa Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 17 Mar 2014 17:03:32 +0100 Subject: More robust way to check if the folder is empty --- apps/files_trashbin/lib/trashbin.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index dc38c339ecb..62879b0ca85 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -922,9 +922,12 @@ class Trashbin { $view = new \OC\Files\View('/' . $user . '/files_trashbin'); $dh = $view->opendir('/files'); - readdir($dh); //'..' - readdir($dh); //'.' - return readdir($dh) === false; + while ($file = readdir($dh)) { + if($file !== '.' and $file !== '..'){ + return false; + } + } + return true; } public static function preview_icon($path) { -- cgit v1.2.3 From 214357ca68ebd80cc59cd1ee34a5a292feaebbe0 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 19 Mar 2014 13:53:59 +0100 Subject: Improve sorting performance of large lists of files --- apps/files/ajax/rawlist.php | 5 ++--- apps/files/lib/helper.php | 15 +++++++++------ apps/files_trashbin/lib/helper.php | 8 ++++++-- 3 files changed, 17 insertions(+), 11 deletions(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files/ajax/rawlist.php b/apps/files/ajax/rawlist.php index 89c21a172fc..6433ddefd69 100644 --- a/apps/files/ajax/rawlist.php +++ b/apps/files/ajax/rawlist.php @@ -33,6 +33,8 @@ if (is_array($mimetypes) && count($mimetypes)) { } else { $files = array_merge($files, \OC\Files\Filesystem::getDirectoryContent($dir)); } +// Sort by name +usort($files, array('\OCA\Files\Helper', 'fileCmp')); $result = array(); foreach ($files as $file) { @@ -51,7 +53,4 @@ foreach ($files as $file) { $result[] = $fileData; } -// Sort by name -usort($result, array('\OCA\Files\Helper', 'fileCmp')); - OC_JSON::success(array('data' => $result)); diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php index b9e41a352bc..c41e2d15581 100644 --- a/apps/files/lib/helper.php +++ b/apps/files/lib/helper.php @@ -51,17 +51,20 @@ class Helper /** * Comparator function to sort files alphabetically and have * the directories appear first - * @param array $a file - * @param array $b file - * @return -1 if $a must come before $b, 1 otherwise + * + * @param \OCP\Files\FileInfo $a file + * @param \OCP\Files\FileInfo $b file + * @return int -1 if $a must come before $b, 1 otherwise */ public static function fileCmp($a, $b) { - if ($a['type'] === 'dir' and $b['type'] !== 'dir') { + $aType = $a->getType(); + $bType = $b->getType(); + if ($aType === 'dir' and $bType !== 'dir') { return -1; - } elseif ($a['type'] !== 'dir' and $b['type'] === 'dir') { + } elseif ($aType !== 'dir' and $bType === 'dir') { return 1; } else { - return strnatcasecmp($a['name'], $b['name']); + return strnatcasecmp($a->getName(), $b->getName()); } } diff --git a/apps/files_trashbin/lib/helper.php b/apps/files_trashbin/lib/helper.php index fe0d1d30a72..9c24332a964 100644 --- a/apps/files_trashbin/lib/helper.php +++ b/apps/files_trashbin/lib/helper.php @@ -2,13 +2,15 @@ namespace OCA\Files_Trashbin; +use OC\Files\FileInfo; + class Helper { /** * Retrieves the contents of a trash bin directory. * @param string $dir path to the directory inside the trashbin * or empty to retrieve the root of the trashbin - * @return array of files + * @return \OCP\Files\FileInfo[] */ public static function getTrashFiles($dir){ $result = array(); @@ -52,6 +54,8 @@ class Helper $files = array(); $id = 0; + list($storage, $internalPath) = $view->resolvePath($dir); + $absoluteDir = $view->getAbsolutePath($dir); foreach ($result as $r) { $i = array(); $i['id'] = $id++; @@ -77,7 +81,7 @@ class Helper $i['isPreviewAvailable'] = \OC::$server->getPreviewManager()->isMimeSupported($r['mime']); } $i['icon'] = \OCA\Files\Helper::determineIcon($i); - $files[] = $i; + $files[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i); } usort($files, array('\OCA\Files\Helper', 'fileCmp')); -- cgit v1.2.3 From 6b66b42f6e4399676677bd6f6534ad3c33c8404f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 24 Mar 2014 12:33:44 +0100 Subject: catch the case where files_trashbin/files doesn't exist --- apps/files_trashbin/lib/trashbin.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'apps/files_trashbin/lib') diff --git a/apps/files_trashbin/lib/trashbin.php b/apps/files_trashbin/lib/trashbin.php index 62879b0ca85..7b14a4ec081 100644 --- a/apps/files_trashbin/lib/trashbin.php +++ b/apps/files_trashbin/lib/trashbin.php @@ -922,8 +922,11 @@ class Trashbin { $view = new \OC\Files\View('/' . $user . '/files_trashbin'); $dh = $view->opendir('/files'); + if (!$dh) { + return false; + } while ($file = readdir($dh)) { - if($file !== '.' and $file !== '..'){ + if ($file !== '.' and $file !== '..') { return false; } } -- cgit v1.2.3