allow to restore single files/folder from a deleted folder

This commit is contained in:
Björn Schießle 2013-01-22 12:00:04 +01:00
parent 3fd28632c4
commit 2c3216266c
5 changed files with 87 additions and 41 deletions

View file

@ -5,17 +5,24 @@ if(!OC_User::isLoggedIn()) {
}
$files = $_REQUEST['files'];
$dirlisting = $_REQUEST['dirlisting'];
$list = explode(';', $files);
$error = array();
$i = 0;
foreach ($list as $file) {
$delimiter = strrpos($file, '.d');
$filename = substr($file, 0, $delimiter);
$timestamp = substr($file, $delimiter+2);
if ( $dirlisting=='0') {
$delimiter = strrpos($file, '.d');
$filename = substr($file, 0, $delimiter);
$timestamp = substr($file, $delimiter+2);
} else {
$path_parts = pathinfo($file);
$filename = $path_parts['basename'];
$timestamp = null;
}
if ( !OCA_Trash\Trashbin::restore($filename, $timestamp) ) {
if ( !OCA_Trash\Trashbin::restore($file, $filename, $timestamp) ) {
$error[] = $filename;
} else {
$success[$i]['filename'] = $filename;

View file

@ -28,7 +28,6 @@ if ($dir) {
$tmp = substr($dir, 0, $pos);
$pos = strrpos($tmp, '.d');
$timestamp = substr($tmp,$pos+2);
error_log("timestamp: $timestamp");
$result[] = array(
'id' => $entryName,
'timestamp' => $timestamp,
@ -68,17 +67,23 @@ foreach ($result as $r) {
}
// Make breadcrumb
$breadcrumb = array('dir' => '', 'name' => 'Trash');
$breadcrumb = array(array('dir' => '', 'name' => 'Trash'));
$pathtohere = '';
foreach (explode('/', $dir) as $i) {
if ($i != '') {
if ($i != '') {
if ( preg_match('/^(.+)\.d[0-9]+$/', $i, $match) ) {
error_log("match!");
$name = $match[1];
} else {
$name = $i;
}
$pathtohere .= '/' . $i;
$breadcrumb[] = array('dir' => $pathtohere, 'name' => $i);
$breadcrumb[] = array('dir' => $pathtohere, 'name' => $name);
}
}
$breadcrumbNav = new OCP\Template('files', 'part.breadcrumb', '');
$breadcrumbNav->assign('breadcrumb', array(array('dir' => '', 'name' => 'Trash')), false);
$breadcrumbNav->assign('breadcrumb', $breadcrumb, false);
$breadcrumbNav->assign('baseURL', OCP\Util::linkTo('files_trashbin', 'index.php') . '?dir=', false);
$list = new OCP\Template('files_trashbin', 'part.list', '');

View file

@ -5,7 +5,7 @@ $(document).ready(function() {
FileActions.register('all', 'Undelete', OC.PERMISSION_READ, '', function(filename) {
var tr=$('tr').filterAttr('data-file', filename);
$.post(OC.filePath('files_trashbin','ajax','undelete.php'),
{files:tr.attr('data-filename')+'.d'+tr.attr('data-timestamp')},
{files:tr.attr('data-file'), dirlisting:tr.attr('data-dirlisting') },
function(result){
for (var i = 0; i < result.data.success.length; i++) {
var row = document.getElementById(result.data.success[i].filename+'.d'+result.data.success[i].timestamp);
@ -64,8 +64,9 @@ $(document).ready(function() {
$('.undelete').click('click',function(event) {
var fileslist=getSelectedFiles('file').join(';');
var dirlisting=getSelectedFiles('dirlisting')[0];
$.post(OC.filePath('files_trashbin','ajax','undelete.php'),
{files:fileslist},
{files:fileslist, dirlisting:dirlisting},
function(result){
for (var i = 0; i < result.data.success.length; i++) {
var row = document.getElementById(result.data.success[i].filename+'.d'+result.data.success[i].timestamp);
@ -133,7 +134,8 @@ function getSelectedFiles(property){
name:$(element).attr('data-filename'),
file:$(element).attr('data-file'),
timestamp:$(element).attr('data-timestamp'),
type:$(element).attr('data-type')
type:$(element).attr('data-type'),
dirlisting:$(element).attr('data-dirlisting')
};
if(property){
files.push(file[property]);

View file

@ -73,49 +73,63 @@ 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
*/
public static function restore($filename, $timestamp) {
public static function restore($file, $filename, $timestamp) {
$user = \OCP\User::getUser();
$view = new \OC_FilesystemView('/'.$user);
$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 ) {
\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']) ) {
$location = '/';
if ( $timestamp ) {
$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 ) {
\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']) ) {
$location = '';
}
} else {
$path_parts = pathinfo($filename);
$result[] = array(
'location' => $path_parts['dirname'],
'type' => $view->is_dir('/files_trashbin/'.$file) ? 'dir' : 'files',
);
$location = '';
}
$source = 'files_trashbin/'.$filename.'.d'.$timestamp;
$source = \OC_Filesystem::normalizePath('files_trashbin/'.$file);
$target = \OC_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);
if( $view->rename($source, $target.$ext) ) {
// if versioning app is enabled, copy versions from the trash bin back to the original location
if ( $return && \OCP\App::isEnabled('files_versions') ) {
if ( $result[0][type] == 'dir' ) {
$view->rename('versions_trashbin/'. $filename.'.d'.$timestamp, 'files_versions/'.$location.'/'.$filename.$ext);
} else if ( $versions = self::getVersionsFromTrash($filename, $timestamp) ) {
if ( \OCP\App::isEnabled('files_versions') ) {
if ( $result[0]['type'] == 'dir' ) {
$view->rename(\OC_Filesystem::normalizePath('versions_trashbin/'. $file), \OC_Filesystem::normalizePath('files_versions/'.$location.'/'.$filename.$ext));
} else if ( $versions = self::getVersionsFromTrash($file, $timestamp) ) {
foreach ($versions as $v) {
$view->rename('versions_trashbin/'.$filename.'.v'.$v.'.d'.$timestamp, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
if ($timestamp ) {
$view->rename('versions_trashbin/'.$filename.'.v'.$v.'.d'.$timestamp, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
} else {
$view->rename('versions_trashbin/'.$file.'.v'.$v, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
}
}
}
}
$query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
$query->execute(array($user,$filename,$timestamp));
if ( $timestamp ) {
$query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
$query->execute(array($user,$filename,$timestamp));
}
return true;
}
@ -189,12 +203,22 @@ class Trashbin {
$versionsName = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($filename);
$versions = array();
if ($timestamp ) {
// fetch for old versions
$matches = glob( $versionsName.'.v*.d'.$timestamp );
$matches = glob( $versionsName.'.v*.d'.$timestamp );
$offset = -strlen($timestamp)-2;
} else {
$matches = glob( $versionsName.'.v*' );
}
foreach( $matches as $ma ) {
$parts = explode( '.v', substr($ma, 0, -strlen($timestamp)-2) );
$versions[] = ( end( $parts ) );
if ( $timestamp ) {
$parts = explode( '.v', substr($ma, 0, $offset) );
$versions[] = ( end( $parts ) );
} else {
$parts = explode( '.v', $ma );
$versions[] = ( end( $parts ) );
}
}
return $versions;
}

View file

@ -24,13 +24,21 @@
$name = str_replace('%2F', '/', $name);
$directory = str_replace('+', '%20', urlencode($file['directory']));
$directory = str_replace('%2F', '/', $directory); ?>
<tr id="<?php echo $file['name'].'.d'.$file['timestamp'];?>"
data-file="<?php echo $file['name'].'.d'.$file['timestamp'];?>"
data-filename="<?php echo $file['name'];?>"
<tr data-filename="<?php echo $file['name'];?>"
data-type="<?php echo ($file['type'] == 'dir')?'dir':'file'?>"
data-mime="<?php echo $file['mimetype']?>"
data-permissions='<?php echo $file['permissions']; ?>'
<?php if ( $_['dirlisting'] ): ?>
id="<?php echo $file['directory'].'/'.$file['name'];?>"
data-file="<?php echo $file['directory'].'/'.$file['name'];?>"
data-timestamp=''
data-dirlisting=1
<?php else: ?>
id="<?php echo $file['name'].'.d'.$file['timestamp'];?>"
data-file="<?php echo $file['name'].'.d'.$file['timestamp'];?>"
data-timestamp='<?php echo $file['timestamp'];?>'
data-permissions='<?php echo $file['permissions']; ?>'>
data-dirlisting=0
<?php endif; ?>>
<td class="filename svg"
<?php if($file['type'] == 'dir'): ?>
style="background-image:url(<?php echo OCP\mimetype_icon('dir'); ?>)"