summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2014-02-25 20:46:41 +0100
committerBjoern Schiessle <schiessle@owncloud.com>2014-03-10 15:13:28 +0100
commiteab844e2267a2955711f9d426c47eedeeea2d479 (patch)
tree31efab64fb3a949ba4c57ba7c04e75349f7c9d2f /apps
parent9b4af31bac977cb788a6f4a013d32ba0a21437f0 (diff)
downloadnextcloud-server-eab844e2267a2955711f9d426c47eedeeea2d479.tar.gz
nextcloud-server-eab844e2267a2955711f9d426c47eedeeea2d479.zip
if file is not in db, fall back to restore file to the users root
Diffstat (limited to 'apps')
-rw-r--r--apps/files_trashbin/ajax/list.php6
-rw-r--r--apps/files_trashbin/index.php13
-rw-r--r--apps/files_trashbin/lib/helper.php55
-rw-r--r--apps/files_trashbin/lib/trashbin.php60
4 files changed, 62 insertions, 72 deletions
diff --git a/apps/files_trashbin/ajax/list.php b/apps/files_trashbin/ajax/list.php
index c9dc13b7840..124a236bcbd 100644
--- a/apps/files_trashbin/ajax/list.php
+++ b/apps/files_trashbin/ajax/list.php
@@ -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();
}
diff --git a/apps/files_trashbin/index.php b/apps/files_trashbin/index.php
index 93f385dd30b..f0c5b0508b8 100644
--- a/apps/files_trashbin/index.php
+++ b/apps/files_trashbin/index.php
@@ -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;
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