]> source.dussan.org Git - nextcloud-server.git/commitdiff
if file is not in db, fall back to restore file to the users root
authorBjoern Schiessle <schiessle@owncloud.com>
Tue, 25 Feb 2014 19:46:41 +0000 (20:46 +0100)
committerBjoern Schiessle <schiessle@owncloud.com>
Mon, 10 Mar 2014 14:13:28 +0000 (15:13 +0100)
apps/files_trashbin/ajax/list.php
apps/files_trashbin/index.php
apps/files_trashbin/lib/helper.php
apps/files_trashbin/lib/trashbin.php

index c9dc13b7840ba650d32cddf8f6afa330e6dabcf9..124a236bcbd505ea904181e4f2ce909766bb28b2 100644 (file)
@@ -26,9 +26,9 @@ if($doBreadcrumb) {
 }
 
 // make filelist
-$files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir);
-
-if ($files === null){
+try {
+       $files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir);
+} catch (Exception $e) {
        header("HTTP/1.0 404 Not Found");
        exit();
 }
index 93f385dd30b57054767f9fd448a85642bf881f14..f0c5b0508b8440285ccca7e89ed0e1fedf98e882 100644 (file)
@@ -37,19 +37,18 @@ if ($isIE8 && isset($_GET['dir'])){
 $ajaxLoad = false;
 
 if (!$isIE8){
-       $files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir);
+       try {
+               $files = \OCA\Files_Trashbin\Helper::getTrashFiles($dir);
+       } catch (Exception $e) {
+               header('Location: ' . OCP\Util::linkTo('files_trashbin', 'index.php'));
+               exit();
+       }
 }
 else{
        $files = array();
        $ajaxLoad = true;
 }
 
-// Redirect if directory does not exist
-if ($files === null){
-       header('Location: ' . OCP\Util::linkTo('files_trashbin', 'index.php'));
-       exit();
-}
-
 $dirlisting = false;
 if ($dir && $dir !== '/') {
     $dirlisting = true;
index c454b35a5f2f231bb216836f33be665481ebe549..f1cec6cc94eae2691c08f96a66cd43a9e71243bc 100644 (file)
@@ -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();
index bc77e9c1543b1b24b6d977b0fb7f0b544a37e9b1..3933395c29861c43291becdf4db2613e8eab4c09 100644 (file)
@@ -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