From ef69abd4dc696305474e45d7cd2661dcb51bdb8a Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Sat, 14 Jan 2012 23:12:32 +0100 Subject: [PATCH] constans for signals handling --- apps/gallery/lib/hooks_handlers.php | 34 ++++---- lib/filesystem.php | 125 +++++++++++++++++++++++----- 2 files changed, 124 insertions(+), 35 deletions(-) diff --git a/apps/gallery/lib/hooks_handlers.php b/apps/gallery/lib/hooks_handlers.php index 534f76896d..7248e67f0a 100644 --- a/apps/gallery/lib/hooks_handlers.php +++ b/apps/gallery/lib/hooks_handlers.php @@ -21,9 +21,9 @@ * */ -OC_Hook::connect("OC_Filesystem", "post_write", "OC_Gallery_Hooks_Handlers", "addPhotoFromPath"); -OC_Hook::connect("OC_Filesystem", "delete", "OC_Gallery_Hooks_Handlers", "removePhoto"); -OC_Hook::connect("OC_Filesystem", "post_rename", "OC_Gallery_Hooks_Handlers", "renamePhoto"); +OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_write, "OC_Gallery_Hooks_Handlers", "addPhotoFromPath"); +OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_delete, "OC_Gallery_Hooks_Handlers", "removePhoto"); +OC_Hook::connect(OC_Filesystem::CLASSNAME, OC_Filesystem::signal_post_rename, "OC_Gallery_Hooks_Handlers", "renamePhoto"); require_once(OC::$CLASSPATH['OC_Gallery_Album']); require_once(OC::$CLASSPATH['OC_Gallery_Photo']); @@ -61,8 +61,10 @@ class OC_Gallery_Hooks_Handlers { } public static function addPhotoFromPath($params) { - if (!self::isPhoto($params['path'])) return; - $fullpath = $params['path']; + $fullpath = $params[OC_Filesystem::signal_param_path]; + + if (!self::isPhoto($fullpath)) return; + OC_Log::write(self::$APP_TAG, 'Adding file with path '. $fullpath, OC_Log::DEBUG); $path = substr($fullpath, 0, strrpos($fullpath, '/')); if ($path == '') $path = '/'; @@ -82,7 +84,7 @@ class OC_Gallery_Hooks_Handlers { } public static function removePhoto($params) { - $path = $params['path']; + $path = $params[OC_Filesystem::signal_param_path]; if (OC_Filesystem::is_dir($path) && self::directoryContainsPhotos($path)) { OC_Gallery_Album::removeByPath($path, OC_User::getUser()); } elseif (self::isPhoto($path)) { @@ -91,15 +93,17 @@ class OC_Gallery_Hooks_Handlers { } public static function renamePhoto($params) { - if (OC_Filesystem::is_dir($params['newpath']) && self::directoryContainsPhotos($params['newpath'])) { - OC_Gallery_Album::changePath($params['oldpath'], $params['newpath'], OC_User::getUser()); - } elseif (!self::isPhoto($params['newpath'])) { - $olddir = substr($params['oldpath'], 0, strrpos($params['oldpath'], '/')); - $newdir = substr($params['newpath'], 0, strrpos($params['newpath'], '/')); + $oldpath = $params[OC_Filesystem::signal_param_oldpath]; + $newpath = $params[OC_Filesystem::signal_param_newpath]; + if (OC_Filesystem::is_dir($newpath) && self::directoryContainsPhotos($newpath)) { + OC_Gallery_Album::changePath($oldpath, $newpath, OC_User::getUser()); + } elseif (!self::isPhoto($newpath)) { + $olddir = substr($oldpath, 0, strrpos($oldpath, '/')); + $newdir = substr($newpath, 0, strrpos($newpath, '/')); if ($olddir == '') $olddir = '/'; if ($newdir == '') $newdir = '/'; - if (!self::isPhoto($params['newpath'])) return; - OC_Log::write(self::$APP_TAG, 'Moving photo from '.$params['oldpath'].' to '.$params['newpath'], OC_Log::DEBUG); + if (!self::isPhoto($newpath)) return; + OC_Log::write(self::$APP_TAG, 'Moving photo from '.$oldpath.' to '.$newpath, OC_Log::DEBUG); $album; $newAlbumId; $oldAlbumId; @@ -120,7 +124,7 @@ class OC_Gallery_Hooks_Handlers { } $newalbum = $newalbum->fetchRow(); if ($oldalbum->numRows() == 0) { - OC_Gallery_Photo::create($newalbum['album_id'], $params['newpath']); + OC_Gallery_Photo::create($newalbum['album_id'], $newpath); return; } $oldalbum = $oldalbum->fetchRow(); @@ -128,7 +132,7 @@ class OC_Gallery_Hooks_Handlers { $oldAlbumId = $oldalbum['album_id']; } - OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $params['oldpath'], $params['newpath']); + OC_Gallery_Photo::changePath($oldAlbumId, $newAlbumId, $oldpath, $newpath); } } } diff --git a/lib/filesystem.php b/lib/filesystem.php index 44401260c5..fd0ac3b621 100644 --- a/lib/filesystem.php +++ b/lib/filesystem.php @@ -47,8 +47,93 @@ class OC_Filesystem{ static private $mounts=array(); static private $fakeRoot=''; static private $storageTypes=array(); + + /** + * classname which used for hooks handling + * used as signalclass in OC_Hooks::emit() + */ + const CLASSNAME = 'OC_Filesystem'; + + /** + * signalname emited before file renaming + * @param oldpath + * @param newpath + */ + const signal_rename = 'rename'; + + /** + * signal emited after file renaming + * @param oldpath + * @param newpath + */ + const signal_post_rename = 'post_rename'; + /** + * signal emited before file/dir creation + * @param path + * @param run changing this flag to false in hook handler will cancel event + */ + const signal_create = 'create'; + + /** + * signal emited after file/dir creation + * @param path + * @param run changing this flag to false in hook handler will cancel event + */ + const signal_post_create = 'post_create'; + + /** + * signal emits before file/dir copy + * @param oldpath + * @param newpath + * @param run changing this flag to false in hook handler will cancel event + */ + const signal_copy = 'copy'; + + /** + * signal emits after file/dir copy + * @param oldpath + * @param newpath + */ + const signal_post_copy = 'post_copy'; + + /** + * signal emits before file/dir save + * @param path + * @param run changing this flag to false in hook handler will cancel event + */ + const signal_write = 'write'; + + /** + * signal emits after file/dir save + * @param path + */ + const signal_post_write = 'post_write'; + /** + * signal emits when reading file/dir + * @param path + */ + const signal_read = 'read'; + + /** + * signal emits when removing file/dir + * @param path + */ + const signal_delete = 'delete'; + + /** + * parameters definitions for signals + */ + const signal_param_path = 'path'; + const signal_param_oldpath = 'oldpath'; + const signal_param_newpath = 'newpath'; + + /** + * run - changing this flag to false in hook handler will cancel event + */ + const signal_param_run = 'run'; + /** * register a storage type * @param string type @@ -285,7 +370,7 @@ class OC_Filesystem{ static public function rename($path1,$path2){ if(OC_FileProxy::runPreProxies('rename',$path1,$path2) and self::is_writeable($path1) and self::isValidPath($path2)){ $run=true; - OC_Hook::emit( 'OC_Filesystem', 'rename', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_rename, array( self::signal_param_oldpath => $path1 , self::signal_param_newpath=>$path2, self::signal_param_run => &$run)); if($run){ $mp1=self::getMountPoint($path1); $mp2=self::getMountPoint($path2); @@ -298,7 +383,7 @@ class OC_Filesystem{ $result=$storage2->fromTmpFile($tmpFile,self::getInternalPath($path2)); $storage1->unlink(self::getInternalPath($path1)); } - OC_Hook::emit( 'OC_Filesystem', 'post_rename', array( 'oldpath' => $path1, 'newpath'=>$path2)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_rename, array( self::signal_param_oldpath => $path1, self::signal_param_newpath=>$path2)); return $result; } } @@ -306,13 +391,13 @@ class OC_Filesystem{ static public function copy($path1,$path2){ if(OC_FileProxy::runPreProxies('copy',$path1,$path2) and self::is_readable($path1) and self::isValidPath($path2)){ $run=true; - OC_Hook::emit( 'OC_Filesystem', 'copy', array( 'oldpath' => $path1 ,'newpath'=>$path2, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_copy, array( self::signal_param_oldpath => $path1 , self::signal_param_newpath=>$path2, self::signal_param_run => &$run)); $exists=self::file_exists($path2); if($run and !$exists){ - OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path2, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_create, array( self::signal_param_path => $path2, self::signal_param_run => &$run)); } if($run){ - OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path2, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_write, array( self::signal_param_path => $path2, self::signal_param_run => &$run)); } if($run){ $mp1=self::getMountPoint($path1); @@ -325,11 +410,11 @@ class OC_Filesystem{ $tmpFile=$storage1->toTmpFile(self::getInternalPath($path1)); $result=$storage2->fromTmpFile($tmpFile,self::getInternalPath($path2)); } - OC_Hook::emit( 'OC_Filesystem', 'post_copy', array( 'oldpath' => $path1 ,'newpath'=>$path2)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_copy, array( self::signal_param_oldpath => $path1 , self::signal_param_newpath=>$path2)); if(!$exists){ - OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path2)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_create, array( self::signal_param_path => $path2)); } - OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path2)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_write, array( self::signal_param_path => $path2)); return $result; } } @@ -360,7 +445,7 @@ class OC_Filesystem{ } static public function toTmpFile($path){ if(OC_FileProxy::runPreProxies('toTmpFile',$path) and self::isValidPath($path) and $storage=self::getStorage($path)){ - OC_Hook::emit( 'OC_Filesystem', 'read', array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, self::signal_read, array( self::signal_param_path => $path)); return $storage->toTmpFile(self::getInternalPath($path)); } } @@ -369,17 +454,17 @@ class OC_Filesystem{ $run=true; $exists=self::file_exists($path); if(!$exists){ - OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_create, array( self::signal_param_path => $path, self::signal_param_run => &$run)); } if($run){ - OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_write, array( self::signal_param_path => $path, self::signal_param_run => &$run)); } if($run){ $result=$storage->fromTmpFile($tmpFile,self::getInternalPath($path)); if(!$exists){ - OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_create, array( self::signal_param_path => $path)); } - OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_write, array( self::signal_param_path => $path)); return $result; } } @@ -389,17 +474,17 @@ class OC_Filesystem{ $run=true; $exists=self::file_exists($path); if(!$exists){ - OC_Hook::emit( 'OC_Filesystem', 'create', array( 'path' => $path, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_create, array( self::signal_param_path => $path, self::signal_param_run => &$run)); } if($run){ - OC_Hook::emit( 'OC_Filesystem', 'write', array( 'path' => $path, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, self::signal_write, array( self::signal_param_path => $path, self::signal_param_run => &$run)); } if($run){ $result=$storage->fromUploadedFile($tmpFile,self::getInternalPath($path)); if(!$exists){ - OC_Hook::emit( 'OC_Filesystem', 'post_create', array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_create, array( self::signal_param_path => $path)); } - OC_Hook::emit( 'OC_Filesystem', 'post_write', array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, self::signal_post_write, array( self::signal_param_path => $path)); return $result; } } @@ -453,9 +538,9 @@ class OC_Filesystem{ $run=true; foreach($hooks as $hook){ if($hook!='read'){ - OC_Hook::emit( 'OC_Filesystem', $hook, array( 'path' => $path, 'run' => &$run)); + OC_Hook::emit( self::CLASSNAME, $hook, array( self::signal_param_path => $path, self::signal_param_run => &$run)); }else{ - OC_Hook::emit( 'OC_Filesystem', $hook, array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, $hook, array( self::signal_param_path => $path)); } } if($run){ @@ -467,7 +552,7 @@ class OC_Filesystem{ $result=OC_FileProxy::runPostProxies($operation,$path,$result); foreach($hooks as $hook){ if($hook!='read'){ - OC_Hook::emit( 'OC_Filesystem', 'post_'.$hook, array( 'path' => $path)); + OC_Hook::emit( self::CLASSNAME, 'post_'.$hook, array( self::signal_param_path => $path)); } } return $result;