Implemented deleteAll() method in OC_FilesystemView (interface) and OC_Filestorage_Common (logic)
Made OC_Filestorage_Local and OC_Filestorage_Shared extend OC_Filestorage_Common Set searchInDir() to protected instead of private in OC_Filestorage_Local and OC_Filestorage_Shared Added class documentation to OC_Filestorage_Common Cleaned up OCA_Versions::expireAll()
This commit is contained in:
parent
49033ff8e0
commit
a9a913c273
5 changed files with 97 additions and 85 deletions
|
@ -25,7 +25,7 @@ require_once( 'lib_share.php' );
|
|||
/**
|
||||
* Convert target path to source path and pass the function call to the correct storage provider
|
||||
*/
|
||||
class OC_Filestorage_Shared extends OC_Filestorage {
|
||||
class OC_Filestorage_Shared extends OC_Filestorage_Common {
|
||||
|
||||
private $datadir;
|
||||
private $sourcePaths = array();
|
||||
|
@ -492,7 +492,7 @@ class OC_Filestorage_Shared extends OC_Filestorage {
|
|||
return $this->searchInDir($query);
|
||||
}
|
||||
|
||||
private function searchInDir($query, $path = "") {
|
||||
protected function searchInDir($query, $path = "") {
|
||||
$files = array();
|
||||
if ($dh = $this->opendir($path)) {
|
||||
while (($filename = readdir($dh)) !== false) {
|
||||
|
|
|
@ -261,10 +261,8 @@ class Storage {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* expire old versions of a file.
|
||||
* @brief Erase a file's versions which exceed the set quota
|
||||
*/
|
||||
public static function expire($filename) {
|
||||
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
|
||||
|
@ -298,90 +296,16 @@ class Storage {
|
|||
}
|
||||
|
||||
/**
|
||||
* @brief erase all old versions of all user files
|
||||
* @return
|
||||
* @brief Erase all old versions of all user files
|
||||
* @return true/false
|
||||
*/
|
||||
public static function expireAll() {
|
||||
|
||||
function deleteAll( $directory, $empty = false ) {
|
||||
|
||||
// strip leading slash
|
||||
if( substr( $directory, 0, 1 ) == "/" ) {
|
||||
|
||||
$directory = substr( $directory, 1 );
|
||||
|
||||
}
|
||||
|
||||
// strip trailing slash
|
||||
if( substr( $directory, -1) == "/" ) {
|
||||
|
||||
$directory = substr( $directory, 0, -1 );
|
||||
|
||||
}
|
||||
|
||||
$view = new \OC_FilesystemView('');
|
||||
|
||||
if ( !$view->file_exists( $directory ) || !$view->is_dir( $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
} elseif( !$view->is_readable( $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
} else {
|
||||
|
||||
$foldername = \OCP\Config::getSystemValue('datadirectory') .'/' . \OCP\USER::getUser() .'/' . $directory; // have to set an absolute path for use with PHP's opendir as OC version doesn't work
|
||||
|
||||
$directoryHandle = $view->opendir( \OCP\USER::getUser() . '/' . $directory );
|
||||
|
||||
while ( $contents = readdir( $directoryHandle ) ) {
|
||||
|
||||
if ( $contents != '.' && $contents != '..') {
|
||||
|
||||
$path = $directory . "/" . $contents;
|
||||
|
||||
if ( $view->is_dir( $path ) ) {
|
||||
|
||||
deleteAll( $path );
|
||||
|
||||
} else {
|
||||
|
||||
$view->unlink( \OCP\USER::getUser() .'/' . $path ); // TODO: make unlink use same system path as is_dir
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//$view->closedir( $directoryHandle ); // TODO: implement closedir in OC_FSV
|
||||
|
||||
if ( $empty == false ) {
|
||||
|
||||
if ( !$view->rmdir( $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
$view = new \OC_FilesystemView('');
|
||||
|
||||
$dir = \OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
|
||||
|
||||
if ( deleteAll( $dir, true ) ) {
|
||||
|
||||
return true;
|
||||
|
||||
} else {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
return $view->deleteAll( $dir, true );
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,18 @@
|
|||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Storage backend class for providing common filesystem operation methods
|
||||
* which are not storage-backend specific.
|
||||
*
|
||||
* OC_Filestorage_Common is never used directly; it is extended by all other
|
||||
* storage backends, where its methods may be overridden, and additional
|
||||
* (backend-specific) methods are defined.
|
||||
*
|
||||
* Some OC_Filestorage_Common methods call functions which are first defined
|
||||
* in classes which extend it, e.g. $this->stat() .
|
||||
*/
|
||||
|
||||
abstract class OC_Filestorage_Common extends OC_Filestorage {
|
||||
|
||||
public function __construct($parameters){}
|
||||
|
@ -87,6 +99,79 @@ abstract class OC_Filestorage_Common extends OC_Filestorage {
|
|||
return $count>0;
|
||||
}
|
||||
// abstract public function fopen($path,$mode);
|
||||
|
||||
/**
|
||||
* @brief Deletes all files and folders recursively within a directory
|
||||
* @param $directory The directory whose contents will be deleted
|
||||
* @param $empty Flag indicating whether directory will be emptied
|
||||
* @returns true/false
|
||||
*
|
||||
* @note By default the directory specified by $directory will be
|
||||
* deleted together with its contents. To avoid this set $empty to true
|
||||
*/
|
||||
public function deleteAll( $directory, $empty = false ) {
|
||||
|
||||
// strip leading slash
|
||||
if( substr( $directory, 0, 1 ) == "/" ) {
|
||||
|
||||
$directory = substr( $directory, 1 );
|
||||
|
||||
}
|
||||
|
||||
// strip trailing slash
|
||||
if( substr( $directory, -1) == "/" ) {
|
||||
|
||||
$directory = substr( $directory, 0, -1 );
|
||||
|
||||
}
|
||||
|
||||
if ( !$this->file_exists( \OCP\USER::getUser() . '/' . $directory ) || !$this->is_dir( \OCP\USER::getUser() . '/' . $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
} elseif( !$this->is_readable( \OCP\USER::getUser() . '/' . $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
} else {
|
||||
|
||||
$directoryHandle = $this->opendir( \OCP\USER::getUser() . '/' . $directory );
|
||||
|
||||
while ( $contents = readdir( $directoryHandle ) ) {
|
||||
|
||||
if ( $contents != '.' && $contents != '..') {
|
||||
|
||||
$path = $directory . "/" . $contents;
|
||||
|
||||
if ( $this->is_dir( $path ) ) {
|
||||
|
||||
deleteAll( $path );
|
||||
|
||||
} else {
|
||||
|
||||
$this->unlink( \OCP\USER::getUser() .'/' . $path ); // TODO: make unlink use same system path as is_dir
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//$this->closedir( $directoryHandle ); // TODO: implement closedir in OC_FSV
|
||||
|
||||
if ( $empty == false ) {
|
||||
|
||||
if ( !$this->rmdir( $directory ) ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
public function getMimeType($path){
|
||||
if(!$this->file_exists($path)){
|
||||
return false;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/**
|
||||
* for local filestore, we only have to map the paths
|
||||
*/
|
||||
class OC_Filestorage_Local extends OC_Filestorage{
|
||||
class OC_Filestorage_Local extends OC_Filestorage_Common{
|
||||
protected $datadir;
|
||||
private static $mimetypes=null;
|
||||
public function __construct($arguments){
|
||||
|
@ -172,7 +172,7 @@ class OC_Filestorage_Local extends OC_Filestorage{
|
|||
return $this->datadir.$path;
|
||||
}
|
||||
|
||||
private function searchInDir($query,$dir=''){
|
||||
protected function searchInDir($query,$dir=''){
|
||||
$files=array();
|
||||
foreach (scandir($this->datadir.$dir) as $item) {
|
||||
if ($item == '.' || $item == '..') continue;
|
||||
|
|
|
@ -252,6 +252,9 @@ class OC_FilesystemView {
|
|||
public function unlink($path){
|
||||
return $this->basicOperation('unlink',$path,array('delete'));
|
||||
}
|
||||
public function deleteAll( $directory, $empty = false ) {
|
||||
return $this->basicOperation( 'deleteAll', $directory, array('delete'), $empty );
|
||||
}
|
||||
public function rename($path1,$path2){
|
||||
$absolutePath1=$this->getAbsolutePath($path1);
|
||||
$absolutePath2=$this->getAbsolutePath($path2);
|
||||
|
|
Loading…
Reference in a new issue