Merge pull request #7704 from owncloud/preview_use_pre_instead_of_post_delete_hook_2

use preDelete instead of postDelete hook
This commit is contained in:
Vincent Petry 2014-03-17 12:59:35 +01:00
commit bd98538a36
2 changed files with 58 additions and 12 deletions

View file

@ -661,7 +661,10 @@ class OC {
*/
public static function registerPreviewHooks() {
OC_Hook::connect('OC_Filesystem', 'post_write', 'OC\Preview', 'post_write');
OC_Hook::connect('OC_Filesystem', 'delete', 'OC\Preview', 'post_delete');
OC_Hook::connect('OC_Filesystem', 'preDelete', 'OC\Preview', 'prepare_delete_files');
OC_Hook::connect('\OCP\Versions', 'preDelete', 'OC\Preview', 'prepare_delete');
OC_Hook::connect('\OCP\Trashbin', 'preDelete', 'OC\Preview', 'prepare_delete');
OC_Hook::connect('OC_Filesystem', 'delete', 'OC\Preview', 'post_delete_files');
OC_Hook::connect('\OCP\Versions', 'delete', 'OC\Preview', 'post_delete');
OC_Hook::connect('\OCP\Trashbin', 'delete', 'OC\Preview', 'post_delete');
}

View file

@ -42,6 +42,10 @@ class Preview {
private $scalingup;
private $mimetype;
//filemapper used for deleting previews
// index is path, value is fileinfo
static public $deleteFileMapper = array();
//preview images object
/**
* @var \OC_Image
@ -166,7 +170,11 @@ class Preview {
}
protected function getFileInfo() {
if (!$this->info) {
$absPath = $this->fileView->getAbsolutePath($this->file);
$absPath = Files\Filesystem::normalizePath($absPath);
if(array_key_exists($absPath, self::$deleteFileMapper)) {
$this->info = self::$deleteFileMapper[$absPath];
} else if (!$this->info) {
$this->info = $this->fileView->getFileInfo($this->file);
}
return $this->info;
@ -181,7 +189,10 @@ class Preview {
$this->file = $file;
$this->info = null;
if ($file !== '') {
$this->mimetype = $this->getFileInfo()->getMimetype();
$this->getFileInfo();
if($this->info !== null && $this->info !== false) {
$this->mimetype = $this->info->getMimetype();
}
}
return $this;
}
@ -274,10 +285,13 @@ class Preview {
$file = $this->getFile();
$fileInfo = $this->getFileInfo($file);
$fileId = $fileInfo->getId();
if($fileInfo !== null && $fileInfo !== false) {
$fileId = $fileInfo->getId();
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/' . $this->getMaxX() . '-' . $this->getMaxY() . '.png';
return $this->userView->unlink($previewPath);
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/' . $this->getMaxX() . '-' . $this->getMaxY() . '.png';
return $this->userView->unlink($previewPath);
}
return false;
}
/**
@ -288,11 +302,14 @@ class Preview {
$file = $this->getFile();
$fileInfo = $this->getFileInfo($file);
$fileId = $fileInfo->getId();
if($fileInfo !== null && $fileInfo !== false) {
$fileId = $fileInfo->getId();
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
$this->userView->deleteAll($previewPath);
return $this->userView->rmdir($previewPath);
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
$this->userView->deleteAll($previewPath);
return $this->userView->rmdir($previewPath);
}
return false;
}
/**
@ -398,6 +415,9 @@ class Preview {
$scalingUp = $this->getScalingUp();
$fileInfo = $this->getFileInfo($file);
if($fileInfo === null || $fileInfo === false) {
return new \OC_Image();
}
$fileId = $fileInfo->getId();
$cached = $this->isCached();
@ -623,12 +643,35 @@ class Preview {
self::post_delete($args);
}
public static function post_delete($args) {
public static function prepare_delete_files($args) {
self::prepare_delete($args, 'files/');
}
public static function prepare_delete($args, $prefix='') {
$path = $args['path'];
if (substr($path, 0, 1) === '/') {
$path = substr($path, 1);
}
$preview = new Preview(\OC_User::getUser(), 'files/', $path);
$view = new \OC\Files\View('/' . \OC_User::getUser() . '/' . $prefix);
$info = $view->getFileInfo($path);
\OC\Preview::$deleteFileMapper = array_merge(
\OC\Preview::$deleteFileMapper,
array(
Files\Filesystem::normalizePath($view->getAbsolutePath($path)) => $info,
)
);
}
public static function post_delete_files($args) {
self::post_delete($args, 'files/');
}
public static function post_delete($args, $prefix='') {
$path = Files\Filesystem::normalizePath($args['path']);
$preview = new Preview(\OC_User::getUser(), $prefix, $path);
$preview->deleteAllPreviews();
}