Browse Source

if file is not in db, fall back to restore file to the users root

tags/v7.0.0alpha2
Bjoern Schiessle 10 years ago
parent
commit
eab844e226

+ 3
- 3
apps/files_trashbin/ajax/list.php View 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();
}

+ 6
- 7
apps/files_trashbin/index.php View 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;

+ 31
- 24
apps/files_trashbin/lib/helper.php View 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();

+ 22
- 38
apps/files_trashbin/lib/trashbin.php View 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

Loading…
Cancel
Save