2011-11-09 17:41:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robin Appelman
|
|
|
|
* @copyright 2011 Robin Appelman icewind1991@gmail.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* provide caching for filesystem info in the database
|
|
|
|
*
|
|
|
|
* not used by OC_Filesystem for reading filesystem info,
|
|
|
|
* instread apps should use OC_FileCache::get where possible
|
|
|
|
*
|
|
|
|
* It will try to keep the data up to date but changes from outside ownCloud can invalidate the cache
|
|
|
|
*/
|
|
|
|
class OC_FileCache{
|
|
|
|
/**
|
|
|
|
* get the filesystem info from the cache
|
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2012-06-15 15:05:27 +00:00
|
|
|
* returns an associative array with the following keys:
|
2011-11-09 17:41:57 +00:00
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 00:00:04 +00:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function get($path,$root=false){
|
|
|
|
if(OC_FileCache_Update::hasUpdated($path,$root)){
|
|
|
|
if($root===false){//filesystem hooks are only valid for the default root
|
2012-02-05 00:25:36 +00:00
|
|
|
OC_Hook::emit('OC_Filesystem','post_write',array('path'=>$path));
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
OC_FileCache_Update::update($path,$root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
return OC_FileCache_Cached::get($path,$root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* put filesystem info in the cache
|
|
|
|
* @param string $path
|
|
|
|
* @param array data
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*
|
|
|
|
* $data is an assiciative array in the same format as returned by get
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function put($path,$data,$root=false){
|
|
|
|
if($root===false){
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
|
|
|
$path=$root.$path;
|
2012-06-15 17:56:15 +00:00
|
|
|
$parent=self::getParentId($path);
|
|
|
|
$id=self::getId($path,'');
|
2011-11-12 00:00:04 +00:00
|
|
|
if($id!=-1){
|
|
|
|
self::update($id,$data);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
if(isset(OC_FileCache_Cached::$savedData[$path])){
|
|
|
|
$data=array_merge($data,OC_FileCache_Cached::$savedData[$path]);
|
|
|
|
unset(OC_FileCache_Cached::$savedData[$path]);
|
2012-02-21 19:48:14 +00:00
|
|
|
}
|
|
|
|
if(!isset($data['size']) or !isset($data['mtime'])){//save incomplete data for the next time we write it
|
2012-06-15 17:56:15 +00:00
|
|
|
OC_FileCache_Cached::$savedData[$path]=$data;
|
2012-02-21 19:48:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-11-12 00:00:04 +00:00
|
|
|
if(!isset($data['encrypted'])){
|
|
|
|
$data['encrypted']=false;
|
|
|
|
}
|
|
|
|
if(!isset($data['versioned'])){
|
|
|
|
$data['versioned']=false;
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
$mimePart=dirname($data['mimetype']);
|
2012-05-03 21:16:18 +00:00
|
|
|
$data['size']=(int)$data['size'];
|
|
|
|
$data['ctime']=(int)$data['mtime'];
|
|
|
|
$data['writable']=(int)$data['writable'];
|
|
|
|
$data['encrypted']=(int)$data['encrypted'];
|
|
|
|
$data['versioned']=(int)$data['versioned'];
|
2012-01-31 15:12:49 +00:00
|
|
|
$user=OC_User::getUser();
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('INSERT INTO *PREFIX*fscache(parent, name, path, path_hash, size, mtime, ctime, mimetype, mimepart,`user`,writable,encrypted,versioned) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)');
|
2012-03-30 16:12:33 +00:00
|
|
|
$result=$query->execute(array($parent,basename($path),$path,md5($path),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned']));
|
2012-02-25 19:27:16 +00:00
|
|
|
if(OC_DB::isError($result)){
|
|
|
|
OC_Log::write('files','error while writing file('.$path.') to cache',OC_Log::ERROR);
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update filesystem info of a file
|
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
private static function update($id,$data){
|
2011-11-12 00:00:04 +00:00
|
|
|
$arguments=array();
|
|
|
|
$queryParts=array();
|
2012-02-05 13:04:10 +00:00
|
|
|
foreach(array('size','mtime','ctime','mimetype','encrypted','versioned','writable') as $attribute){
|
2011-11-12 00:00:04 +00:00
|
|
|
if(isset($data[$attribute])){
|
|
|
|
$arguments[]=$data[$attribute];
|
|
|
|
$queryParts[]=$attribute.'=?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isset($data['mimetype'])){
|
|
|
|
$arguments[]=dirname($data['mimetype']);
|
|
|
|
$queryParts[]='mimepart=?';
|
|
|
|
}
|
|
|
|
$arguments[]=$id;
|
2012-02-10 10:30:38 +00:00
|
|
|
|
|
|
|
$sql = 'UPDATE *PREFIX*fscache SET '.implode(' , ',$queryParts).' WHERE id=?';
|
|
|
|
$query=OC_DB::prepare($sql);
|
2012-02-25 19:27:16 +00:00
|
|
|
$result=$query->execute($arguments);
|
|
|
|
if(OC_DB::isError($result)){
|
|
|
|
OC_Log::write('files','error while updating file('.$path.') in cache',OC_Log::ERROR);
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* register a file move in the cache
|
|
|
|
* @param string oldPath
|
|
|
|
* @param string newPath
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function move($oldPath,$newPath,$root=false){
|
|
|
|
if($root===false){
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
|
|
|
$oldPath=$root.$oldPath;
|
|
|
|
$newPath=$root.$newPath;
|
2011-11-09 17:41:57 +00:00
|
|
|
$newParent=self::getParentId($newPath);
|
2012-03-30 16:12:33 +00:00
|
|
|
$query=OC_DB::prepare('UPDATE *PREFIX*fscache SET parent=? ,name=?, path=?, path_hash=? WHERE path_hash=?');
|
|
|
|
$query->execute(array($newParent,basename($newPath),$newPath,md5($newPath),md5($oldPath)));
|
2012-06-04 15:59:21 +00:00
|
|
|
|
|
|
|
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE path LIKE ?');
|
|
|
|
$oldLength=strlen($oldPath);
|
|
|
|
$updateQuery=OC_DB::prepare('UPDATE *PREFIX*fscache SET path=?, path_hash=? WHERE path_hash=?');
|
|
|
|
while($row= $query->execute(array($oldPath.'/%'))->fetchRow()){
|
|
|
|
$old=$row['path'];
|
|
|
|
$new=$newPath.substr($old,$oldLength);
|
|
|
|
$updateQuery->execute(array($new,md5($new),md5($old)));
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete info from the cache
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function delete($path,$root=false){
|
|
|
|
if($root===false){
|
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
$query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE path_hash=?');
|
|
|
|
$query->execute(array(md5($root.$path)));
|
|
|
|
|
|
|
|
//delete everything inside the folder
|
|
|
|
$query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE path LIKE ?');
|
|
|
|
$query->execute(array($root.$path.'/%'));
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return array of filenames matching the querty
|
|
|
|
* @param string $query
|
2012-01-31 15:12:49 +00:00
|
|
|
* @param boolean $returnData
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array of filepaths
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function search($search,$returnData=false,$root=false){
|
|
|
|
if($root===false){
|
2012-02-02 18:04:13 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-01-31 15:12:49 +00:00
|
|
|
$rootLen=strlen($root);
|
|
|
|
if(!$returnData){
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE name LIKE ? AND `user`=?');
|
2012-01-31 15:12:49 +00:00
|
|
|
}else{
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('SELECT * FROM *PREFIX*fscache WHERE name LIKE ? AND `user`=?');
|
2012-01-31 15:12:49 +00:00
|
|
|
}
|
|
|
|
$result=$query->execute(array("%$search%",OC_User::getUser()));
|
2011-11-09 17:41:57 +00:00
|
|
|
$names=array();
|
|
|
|
while($row=$result->fetchRow()){
|
2012-01-31 15:12:49 +00:00
|
|
|
if(!$returnData){
|
|
|
|
$names[]=substr($row['path'],$rootLen);
|
|
|
|
}else{
|
|
|
|
$row['path']=substr($row['path'],$rootLen);
|
|
|
|
$names[]=$row;
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all files and folders in a folder
|
|
|
|
* @param string path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* returns an array of assiciative arrays with the following keys:
|
|
|
|
* - name
|
|
|
|
* - size
|
|
|
|
* - mtime
|
|
|
|
* - ctime
|
|
|
|
* - mimetype
|
2011-11-12 00:00:04 +00:00
|
|
|
* - encrypted
|
|
|
|
* - versioned
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function getFolderContent($path,$root=false,$mimetype_filter=''){
|
2012-06-16 23:26:30 +00:00
|
|
|
if(OC_FileCache_Update::hasUpdated($path,$root,true)){
|
2012-06-15 17:56:15 +00:00
|
|
|
OC_FileCache_Update::updateFolder($path,$root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
return OC_FileCache_Cached::getFolderContent($path,$root,$mimetype_filter);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if a file or folder is in the cache
|
|
|
|
* @param string $path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function inCache($path,$root=false){
|
|
|
|
return self::getId($path,$root)!=-1;
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id as used in the cache
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function getId($path,$root=false){
|
|
|
|
if($root===false){
|
|
|
|
$root=OC_Filesystem::getRoot();
|
2012-02-25 19:27:16 +00:00
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
|
|
|
|
$query=OC_DB::prepare('SELECT id FROM *PREFIX*fscache WHERE path_hash=?');
|
|
|
|
$result=$query->execute(array(md5($root.$path)));
|
2012-02-25 19:27:16 +00:00
|
|
|
if(OC_DB::isError($result)){
|
|
|
|
OC_Log::write('files','error while getting file id of '.$path,OC_Log::ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
|
2012-02-25 19:27:16 +00:00
|
|
|
$result=$result->fetchRow();
|
2011-11-09 17:41:57 +00:00
|
|
|
if(is_array($result)){
|
|
|
|
return $result['id'];
|
|
|
|
}else{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 09:44:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file path from the id, relative to the home folder of the user
|
|
|
|
* @param int id
|
|
|
|
* @param string user (optional)
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPath($id,$user=''){
|
|
|
|
if(!$user){
|
|
|
|
$user=OC_User::getUser();
|
|
|
|
}
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE id=? AND `user`=?');
|
2012-04-15 09:44:04 +00:00
|
|
|
$result=$query->execute(array($id,$user));
|
|
|
|
$row=$result->fetchRow();
|
2012-04-16 08:06:52 +00:00
|
|
|
$path=$row['path'];
|
|
|
|
$root='/'.$user.'/files';
|
|
|
|
if(substr($path,0,strlen($root))!=$root){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return substr($path,strlen($root));
|
2012-04-15 09:44:04 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get the file id of the parent folder, taking into account '/' has no parent
|
|
|
|
* @param string $path
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private static function getParentId($path){
|
|
|
|
if($path=='/'){
|
|
|
|
return -1;
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
return self::getId(dirname($path),'');
|
2012-02-21 19:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 17:41:57 +00:00
|
|
|
/**
|
|
|
|
* adjust the size of the parent folders
|
|
|
|
* @param string $path
|
|
|
|
* @param int $sizeDiff
|
2012-06-15 17:56:15 +00:00
|
|
|
* @param string root (optinal)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function increaseSize($path,$sizeDiff, $root=false){
|
2012-02-05 00:25:36 +00:00
|
|
|
if($sizeDiff==0) return;
|
2012-06-15 17:56:15 +00:00
|
|
|
$id=self::getId($path,'');
|
|
|
|
while($id!=-1){//walk up the filetree increasing the size of all parent folders
|
2011-11-09 17:41:57 +00:00
|
|
|
$query=OC_DB::prepare('UPDATE *PREFIX*fscache SET size=size+? WHERE id=?');
|
|
|
|
$query->execute(array($sizeDiff,$id));
|
2012-06-15 17:56:15 +00:00
|
|
|
$id=self::getParentId($path);
|
2011-11-09 17:41:57 +00:00
|
|
|
$path=dirname($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* recursively scan the filesystem and fill the cache
|
|
|
|
* @param string $path
|
2012-02-02 18:04:13 +00:00
|
|
|
* @param OC_EventSource $enventSource (optional)
|
|
|
|
* @param int count (optional)
|
2012-02-05 00:25:36 +00:00
|
|
|
* @param string root (optionak)
|
2011-11-09 17:41:57 +00:00
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function scan($path,$eventSource=false,&$count=0,$root=false){
|
2012-03-31 14:24:53 +00:00
|
|
|
if($eventSource){
|
|
|
|
$eventSource->send('scanning',array('file'=>$path,'count'=>$count));
|
|
|
|
}
|
|
|
|
$lastSend=$count;
|
2012-06-15 17:56:15 +00:00
|
|
|
if($root===false){
|
2012-02-05 00:25:36 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=new OC_FilesystemView($root);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
2012-02-05 00:25:36 +00:00
|
|
|
self::scanFile($path,$root);
|
2012-04-29 14:11:17 +00:00
|
|
|
$dh=$view->opendir($path.'/');
|
|
|
|
$totalSize=0;
|
|
|
|
if($dh){
|
|
|
|
while (($filename = readdir($dh)) !== false) {
|
|
|
|
if($filename != '.' and $filename != '..'){
|
|
|
|
$file=$path.'/'.$filename;
|
|
|
|
if($view->is_dir($file.'/')){
|
|
|
|
self::scan($file,$eventSource,$count,$root);
|
|
|
|
}else{
|
|
|
|
$totalSize+=self::scanFile($file,$root);
|
|
|
|
$count++;
|
|
|
|
if($count>$lastSend+25 and $eventSource){
|
|
|
|
$lastSend=$count;
|
|
|
|
$eventSource->send('scanning',array('file'=>$path,'count'=>$count));
|
2012-03-31 14:24:53 +00:00
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-15 17:56:15 +00:00
|
|
|
|
|
|
|
OC_FileCache_Update::cleanFolder($path,$root);
|
|
|
|
self::increaseSize($path,$totalSize,$root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scan a single file
|
|
|
|
* @param string path
|
|
|
|
* @param string root (optional)
|
|
|
|
* @return int size of the scanned file
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function scanFile($path,$root=false){
|
|
|
|
if($root===false){
|
2012-02-05 00:25:36 +00:00
|
|
|
$view=OC_Filesystem::getView();
|
|
|
|
}else{
|
2012-06-15 17:56:15 +00:00
|
|
|
$view=new OC_FilesystemView($root);
|
2012-02-05 00:25:36 +00:00
|
|
|
}
|
|
|
|
if(!$view->is_readable($path)) return; //cant read, nothing we can do
|
2012-04-24 22:10:02 +00:00
|
|
|
clearstatcache();
|
2012-02-05 00:25:36 +00:00
|
|
|
$mimetype=$view->getMimeType($path);
|
2012-05-16 23:47:58 +00:00
|
|
|
$stat=$view->stat($path);
|
|
|
|
if($mimetype=='httpd/unix-directory'){
|
|
|
|
$writable=$view->is_writable($path.'/');
|
|
|
|
}else{
|
|
|
|
$writable=$view->is_writable($path);
|
|
|
|
}
|
2012-02-05 00:25:36 +00:00
|
|
|
$stat['mimetype']=$mimetype;
|
2012-02-05 13:04:10 +00:00
|
|
|
$stat['writable']=$writable;
|
2012-02-05 00:25:36 +00:00
|
|
|
if($path=='/'){
|
|
|
|
$path='';
|
|
|
|
}
|
|
|
|
self::put($path,$stat,$root);
|
|
|
|
return $stat['size'];
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-14 23:09:06 +00:00
|
|
|
* find files by mimetype
|
2011-11-09 17:41:57 +00:00
|
|
|
* @param string $part1
|
|
|
|
* @param string $part2 (optional)
|
2012-02-08 16:29:54 +00:00
|
|
|
* @param string root (optional)
|
2011-11-09 17:41:57 +00:00
|
|
|
* @return array of file paths
|
|
|
|
*
|
|
|
|
* $part1 and $part2 together form the complete mimetype.
|
|
|
|
* e.g. searchByMime('text','plain')
|
|
|
|
*
|
|
|
|
* seccond mimetype part can be ommited
|
|
|
|
* e.g. searchByMime('audio')
|
|
|
|
*/
|
2012-06-15 17:56:15 +00:00
|
|
|
public static function searchByMime($part1,$part2=null,$root=false){
|
|
|
|
if($root===false){
|
2012-02-08 16:29:54 +00:00
|
|
|
$root=OC_Filesystem::getRoot();
|
|
|
|
}
|
2012-03-09 15:47:53 +00:00
|
|
|
$rootLen=strlen($root);
|
|
|
|
$root .= '%';
|
2012-02-08 16:29:54 +00:00
|
|
|
$user=OC_User::getUser();
|
|
|
|
if(!$part2){
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE mimepart=? AND `user`=? AND path LIKE ?');
|
2012-03-09 15:47:53 +00:00
|
|
|
$result=$query->execute(array($part1,$user, $root));
|
2011-11-09 17:41:57 +00:00
|
|
|
}else{
|
2012-05-03 21:16:18 +00:00
|
|
|
$query=OC_DB::prepare('SELECT path FROM *PREFIX*fscache WHERE mimetype=? AND `user`=? AND path LIKE ? ');
|
2012-03-09 15:45:57 +00:00
|
|
|
$result=$query->execute(array($part1.'/'.$part2,$user, $root));
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
$names=array();
|
|
|
|
while($row=$result->fetchRow()){
|
2012-02-08 16:29:54 +00:00
|
|
|
$names[]=substr($row['path'],$rootLen);
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
return $names;
|
|
|
|
}
|
2012-02-05 00:25:36 +00:00
|
|
|
|
2012-03-30 17:44:38 +00:00
|
|
|
/**
|
|
|
|
* clean old pre-path_hash entries
|
|
|
|
*/
|
|
|
|
public static function clean(){
|
|
|
|
$query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE LENGTH(path_hash)<30');
|
|
|
|
$query->execute();
|
|
|
|
}
|
2011-11-09 17:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//watch for changes and try to keep the cache up to date
|
2012-06-15 17:56:15 +00:00
|
|
|
OC_Hook::connect('OC_Filesystem','post_write','OC_FileCache_Update','fileSystemWatcherWrite');
|
|
|
|
OC_Hook::connect('OC_Filesystem','post_delete','OC_FileCache_Update','fileSystemWatcherDelete');
|
|
|
|
OC_Hook::connect('OC_Filesystem','post_rename','OC_FileCache_Update','fileSystemWatcherRename');
|