move all the files stuff into a files class

This commit is contained in:
Frank Karlitschek 2012-05-02 12:54:31 +02:00
parent f1830866a3
commit 8c7f854671
18 changed files with 144 additions and 104 deletions

View File

@ -46,7 +46,7 @@ $result=array();
if(strpos($dir,'..') === false){ if(strpos($dir,'..') === false){
$fileCount=count($files['name']); $fileCount=count($files['name']);
for($i=0;$i<$fileCount;$i++){ for($i=0;$i<$fileCount;$i++){
$target = OCP\Util::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]); $target = OCP\Files::buildNotExistingFileName(stripslashes($dir), $files['name'][$i]);
if(is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i],$target)){ if(is_uploaded_file($files['tmp_name'][$i]) and OC_Filesystem::fromTmpFile($files['tmp_name'][$i],$target)){
$meta=OC_FileCache::getCached($target); $meta=OC_FileCache::getCached($target);
$result[]=array( "status" => "success", 'mime'=>$meta['mimetype'],'size'=>$meta['size'],'name'=>basename($target)); $result[]=array( "status" => "success", 'mime'=>$meta['mimetype'],'size'=>$meta['size'],'name'=>basename($target));

View File

@ -104,7 +104,7 @@ class OC_Filestorage_Archive extends OC_Filestorage_Common{
} }
public function touch($path, $mtime=null){ public function touch($path, $mtime=null){
if(is_null($mtime)){ if(is_null($mtime)){
$tmpFile=OCP\Util::tmpFile(); $tmpFile=OCP\Files::tmpFile();
$this->archive->extractFile($path,$tmpFile); $this->archive->extractFile($path,$tmpFile);
$this->archive->addfile($path,$tmpFile); $this->archive->addfile($path,$tmpFile);
}else{ }else{

View File

@ -93,7 +93,7 @@ class OC_Archive_TAR extends OC_Archive{
*/ */
function rename($source,$dest){ function rename($source,$dest){
//no proper way to delete, rename entire archive, rename file and remake archive //no proper way to delete, rename entire archive, rename file and remake archive
$tmp=OCP\Util::tmpFolder(); $tmp=OCP\Files::tmpFolder();
$this->tar->extract($tmp); $this->tar->extract($tmp);
rename($tmp.$source,$tmp.$dest); rename($tmp.$source,$tmp.$dest);
$this->tar=null; $this->tar=null;
@ -177,7 +177,7 @@ class OC_Archive_TAR extends OC_Archive{
* @return bool * @return bool
*/ */
function extractFile($path,$dest){ function extractFile($path,$dest){
$tmp=OCP\Util::tmpFolder(); $tmp=OCP\Files::tmpFolder();
if(!$this->fileExists($path)){ if(!$this->fileExists($path)){
return false; return false;
} }
@ -185,7 +185,7 @@ class OC_Archive_TAR extends OC_Archive{
if($success){ if($success){
rename($tmp.$path,$dest); rename($tmp.$path,$dest);
} }
OCP\Util::rmdirr($tmp); OCP\Files::rmdirr($tmp);
return $success; return $success;
} }
/** /**
@ -216,9 +216,9 @@ class OC_Archive_TAR extends OC_Archive{
return false; return false;
} }
//no proper way to delete, extract entire archive, delete file and remake archive //no proper way to delete, extract entire archive, delete file and remake archive
$tmp=OCP\Util::tmpFolder(); $tmp=OCP\Files::tmpFolder();
$this->tar->extract($tmp); $this->tar->extract($tmp);
OCP\Util::rmdirr($tmp.$path); OCP\Files::rmdirr($tmp.$path);
$this->tar=null; $this->tar=null;
unlink($this->path); unlink($this->path);
$this->reopen(); $this->reopen();
@ -237,7 +237,7 @@ class OC_Archive_TAR extends OC_Archive{
}else{ }else{
$ext=''; $ext='';
} }
$tmpFile=OCP\Util::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
if($this->fileExists($path)){ if($this->fileExists($path)){
$this->extractFile($path,$tmpFile); $this->extractFile($path,$tmpFile);
}elseif($mode=='r' or $mode=='rb'){ }elseif($mode=='r' or $mode=='rb'){

View File

@ -169,7 +169,7 @@ class OC_Archive_ZIP extends OC_Archive{
}else{ }else{
$ext=''; $ext='';
} }
$tmpFile=OCP\Util::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack'); OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
if($this->fileExists($path)){ if($this->fileExists($path)){
$this->extractFile($path,$tmpFile); $this->extractFile($path,$tmpFile);

View File

@ -55,7 +55,7 @@ abstract class Test_Archive extends UnitTestCase {
$textFile=$dir.'/lorem.txt'; $textFile=$dir.'/lorem.txt';
$this->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt')); $this->assertEqual(file_get_contents($textFile),$this->instance->getFile('lorem.txt'));
$tmpFile=OCP\Util::tmpFile('.txt'); $tmpFile=OCP\Files::tmpFile('.txt');
$this->instance->extractFile('lorem.txt',$tmpFile); $this->instance->extractFile('lorem.txt',$tmpFile);
$this->assertEqual(file_get_contents($textFile),file_get_contents($tmpFile)); $this->assertEqual(file_get_contents($textFile),file_get_contents($tmpFile));
} }
@ -89,7 +89,7 @@ abstract class Test_Archive extends UnitTestCase {
$this->instance=$this->getNew(); $this->instance=$this->getNew();
$fh=$this->instance->getStream('lorem.txt','w'); $fh=$this->instance->getStream('lorem.txt','w');
$source=fopen($dir.'/lorem.txt','r'); $source=fopen($dir.'/lorem.txt','r');
OCP\Util::streamCopy($source,$fh); OCP\Files::streamCopy($source,$fh);
fclose($source); fclose($source);
fclose($fh); fclose($fh);
$this->assertTrue($this->instance->fileExists('lorem.txt')); $this->assertTrue($this->instance->fileExists('lorem.txt'));
@ -109,13 +109,13 @@ abstract class Test_Archive extends UnitTestCase {
public function testExtract(){ public function testExtract(){
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data'; $dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';
$this->instance=$this->getExisting(); $this->instance=$this->getExisting();
$tmpDir=OCP\Util::tmpFolder(); $tmpDir=OCP\Files::tmpFolder();
$this->instance->extract($tmpDir); $this->instance->extract($tmpDir);
$this->assertEqual(true,file_exists($tmpDir.'lorem.txt')); $this->assertEqual(true,file_exists($tmpDir.'lorem.txt'));
$this->assertEqual(true,file_exists($tmpDir.'dir/lorem.txt')); $this->assertEqual(true,file_exists($tmpDir.'dir/lorem.txt'));
$this->assertEqual(true,file_exists($tmpDir.'logo-wide.png')); $this->assertEqual(true,file_exists($tmpDir.'logo-wide.png'));
$this->assertEqual(file_get_contents($dir.'/lorem.txt'),file_get_contents($tmpDir.'lorem.txt')); $this->assertEqual(file_get_contents($dir.'/lorem.txt'),file_get_contents($tmpDir.'lorem.txt'));
OCP\Util::rmdirr($tmpDir); OCP\Files::rmdirr($tmpDir);
} }
public function testMoveRemove(){ public function testMoveRemove(){
$dir=OC::$SERVERROOT.'/apps/files_archive/tests/data'; $dir=OC::$SERVERROOT.'/apps/files_archive/tests/data';

View File

@ -13,7 +13,7 @@ class Test_Filestorage_Archive_Zip extends Test_FileStorage {
private $tmpFile; private $tmpFile;
public function setUp(){ public function setUp(){
$this->tmpFile=OCP\Util::tmpFile('.zip'); $this->tmpFile=OCP\Files::tmpFile('.zip');
$this->instance=new OC_Filestorage_Archive(array('archive'=>$this->tmpFile)); $this->instance=new OC_Filestorage_Archive(array('archive'=>$this->tmpFile));
} }

View File

@ -16,7 +16,7 @@ if(is_dir(OC::$SERVERROOT.'/apps/files_archive/tests/data')){
} }
protected function getNew(){ protected function getNew(){
return new OC_Archive_TAR(OCP\Util::tmpFile('.tar.gz')); return new OC_Archive_TAR(OCP\Files::tmpFile('.tar.gz'));
} }
} }
}else{ }else{

View File

@ -16,7 +16,7 @@ if(is_dir(OC::$SERVERROOT.'/apps/files_archive/tests/data')){
} }
protected function getNew(){ protected function getNew(){
return new OC_Archive_ZIP(OCP\Util::tmpFile('.zip')); return new OC_Archive_ZIP(OCP\Files::tmpFile('.zip'));
} }
} }
}else{ }else{

View File

@ -92,7 +92,7 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
//first encrypt the target file so we don't end up with a half encrypted file //first encrypt the target file so we don't end up with a half encrypted file
OCP\Util::writeLog('files_encryption','Decrypting '.$path.' before writing',OCP\Util::DEBUG); OCP\Util::writeLog('files_encryption','Decrypting '.$path.' before writing',OCP\Util::DEBUG);
$tmp=fopen('php://temp'); $tmp=fopen('php://temp');
OCP\Util::streamCopy($result,$tmp); OCP\Files::streamCopy($result,$tmp);
fclose($result); fclose($result);
OC_Filesystem::file_put_contents($path,$tmp); OC_Filesystem::file_put_contents($path,$tmp);
fclose($tmp); fclose($tmp);
@ -104,7 +104,7 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
public function postGetMimeType($path,$mime){ public function postGetMimeType($path,$mime){
if(self::isEncrypted($path)){ if(self::isEncrypted($path)){
$mime=OCP\Util::getMimeType('crypt://'.$path,'w'); $mime=OCP\Files::getMimeType('crypt://'.$path,'w');
} }
return $mime; return $mime;
} }

View File

@ -27,14 +27,14 @@ class Test_Encryption extends UnitTestCase {
$this->assertNotEqual($encrypted,$source); $this->assertNotEqual($encrypted,$source);
$this->assertEqual($decrypted,$source); $this->assertEqual($decrypted,$source);
$tmpFileEncrypted=OCP\Util::tmpFile(); $tmpFileEncrypted=OCP\Files::tmpFile();
OC_Crypt::encryptfile($file,$tmpFileEncrypted,$key); OC_Crypt::encryptfile($file,$tmpFileEncrypted,$key);
$encrypted=file_get_contents($tmpFileEncrypted); $encrypted=file_get_contents($tmpFileEncrypted);
$decrypted=OC_Crypt::blockDecrypt($encrypted,$key); $decrypted=OC_Crypt::blockDecrypt($encrypted,$key);
$this->assertNotEqual($encrypted,$source); $this->assertNotEqual($encrypted,$source);
$this->assertEqual($decrypted,$source); $this->assertEqual($decrypted,$source);
$tmpFileDecrypted=OCP\Util::tmpFile(); $tmpFileDecrypted=OCP\Files::tmpFile();
OC_Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key); OC_Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
$decrypted=file_get_contents($tmpFileDecrypted); $decrypted=file_get_contents($tmpFileDecrypted);
$this->assertEqual($decrypted,$source); $this->assertEqual($decrypted,$source);

View File

@ -22,7 +22,7 @@ class Test_CryptStream extends UnitTestCase {
$file=OC::$SERVERROOT.'/3rdparty/MDB2.php'; $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
$source=fopen($file,'r'); $source=fopen($file,'r');
$target=$this->getStream('test2','w'); $target=$this->getStream('test2','w');
OCP\Util::streamCopy($source,$target); OCP\Files::streamCopy($source,$target);
fclose($target); fclose($target);
fclose($source); fclose($source);
@ -44,7 +44,7 @@ class Test_CryptStream extends UnitTestCase {
$id=uniqid(); $id=uniqid();
} }
if(!isset($this->tmpFiles[$id])){ if(!isset($this->tmpFiles[$id])){
$file=OCP\Util::tmpFile(); $file=OCP\Files::tmpFile();
$this->tmpFiles[$id]=$file; $this->tmpFiles[$id]=$file;
}else{ }else{
$file=$this->tmpFiles[$id]; $file=$this->tmpFiles[$id];

View File

@ -108,7 +108,7 @@ class OC_FileStorage_FTP extends OC_Filestorage_Common{
}else{ }else{
$ext=''; $ext='';
} }
$tmpFile=OCP\Util::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack'); OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
if($this->file_exists($path)){ if($this->file_exists($path)){
$this->getFile($path,$tmpFile); $this->getFile($path,$tmpFile);

View File

@ -84,7 +84,7 @@ class OC_Filestorage_Google extends OC_Filestorage_Common {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
} }
if ($isDownload) { if ($isDownload) {
$tmpFile = OCP\Util::tmpFile(); $tmpFile = OCP\Files::tmpFile();
$fp = fopen($tmpFile, 'w'); $fp = fopen($tmpFile, 'w');
curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec($curl); curl_exec($curl);

View File

@ -164,7 +164,7 @@ class OC_FileStorage_SWIFT extends OC_Filestorage_Common{
* @return array * @return array
*/ */
private function getSubContainers($container){ private function getSubContainers($container){
$tmpFile=OCP\Util::tmpFile(); $tmpFile=OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container); $obj=$this->getSubContainerFile($container);
try{ try{
$obj->save_to_filename($tmpFile); $obj->save_to_filename($tmpFile);
@ -190,7 +190,7 @@ class OC_FileStorage_SWIFT extends OC_Filestorage_Common{
if(!$name){ if(!$name){
return false; return false;
} }
$tmpFile=OCP\Util::tmpFile(); $tmpFile=OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container); $obj=$this->getSubContainerFile($container);
try{ try{
$obj->save_to_filename($tmpFile); $obj->save_to_filename($tmpFile);
@ -225,7 +225,7 @@ class OC_FileStorage_SWIFT extends OC_Filestorage_Common{
if(!$name){ if(!$name){
return false; return false;
} }
$tmpFile=OCP\Util::tmpFile(); $tmpFile=OCP\Files::tmpFile();
$obj=$this->getSubContainerFile($container); $obj=$this->getSubContainerFile($container);
try{ try{
$obj->save_to_filename($tmpFile); $obj->save_to_filename($tmpFile);
@ -501,7 +501,7 @@ class OC_FileStorage_SWIFT extends OC_Filestorage_Common{
private function getTmpFile($path){ private function getTmpFile($path){
$obj=$this->getObject($path); $obj=$this->getObject($path);
if(!is_null($obj)){ if(!is_null($obj)){
$tmpFile=OCP\Util::tmpFile(); $tmpFile=OCP\Files::tmpFile();
$obj->save_to_filename($tmpFile); $obj->save_to_filename($tmpFile);
return $tmpFile; return $tmpFile;
}else{ }else{

View File

@ -150,7 +150,7 @@ class OC_FileStorage_DAV extends OC_Filestorage_Common{
}else{ }else{
$ext=''; $ext='';
} }
$tmpFile=OCP\Util::tmpFile($ext); $tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack'); OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
if($this->file_exists($path)){ if($this->file_exists($path)){
$this->getFile($path,$tmpFile); $this->getFile($path,$tmpFile);

View File

@ -23,7 +23,7 @@ if(!is_array($config) or !isset($config['ftp']) or !$config['ftp']['run']){
} }
public function tearDown(){ public function tearDown(){
OCP\Util::rmdirr($this->instance->constructUrl('')); OCP\Files::rmdirr($this->instance->constructUrl(''));
} }
} }
} }

114
lib/public/files.php Normal file
View File

@ -0,0 +1,114 @@
<?php
/**
* ownCloud
*
* @author Frank Karlitschek
* @copyright 2010 Frank Karlitschek karlitschek@kde.org
*
* 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/>.
*
*/
/**
* Public interface of ownCloud for apps to use.
* Files Class
*
*/
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP;
class Files {
/**
* @brief Recusive deletion of folders
* @param string $dir path to the folder
*
*/
static function rmdirr($dir) {
\OC_Helper::rmdirr( $dir );
}
/**
* get the mimetype form a local file
* @param string path
* @return string
* does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
*/
static function getMimeType($path){
return(\OC_Helper::getMimeType( $path ));
}
/**
* copy the contents of one stream to another
* @param resource source
* @param resource target
* @return int the number of bytes copied
*/
public static function streamCopy($source,$target){
return(\OC_Helper::streamCopy($source,$target));
}
/**
* create a temporary file with an unique filename
* @param string postfix
* @return string
*
* temporary files are automatically cleaned up after the script is finished
*/
public static function tmpFile($postfix=''){
return(\OC_Helper::tmpFile($postfix));
}
/**
* create a temporary folder with an unique filename
* @return string
*
* temporary files are automatically cleaned up after the script is finished
*/
public static function tmpFolder(){
return(\OC_Helper::tmpFolder());
}
/**
* Adds a suffix to the name in case the file exists
*
* @param $path
* @param $filename
* @return string
*/
public static function buildNotExistingFileName($path, $filename){
return(\OC_Helper::buildNotExistingFileName($path, $filename));
}
}
?>

View File

@ -193,80 +193,6 @@ class Util {
/**
* @brief Recusive deletion of folders
* @param string $dir path to the folder
*
*/
static function rmdirr($dir) {
\OC_Helper::rmdirr( $dir );
}
/**
* get the mimetype form a local file
* @param string path
* @return string
* does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
*/
static function getMimeType($path){
return(\OC_Helper::getMimeType( $path ));
}
/**
* copy the contents of one stream to another
* @param resource source
* @param resource target
* @return int the number of bytes copied
*/
public static function streamCopy($source,$target){
return(\OC_Helper::streamCopy($source,$target));
}
/**
* create a temporary file with an unique filename
* @param string postfix
* @return string
*
* temporary files are automatically cleaned up after the script is finished
*/
public static function tmpFile($postfix=''){
return(\OC_Helper::tmpFile($postfix));
}
/**
* create a temporary folder with an unique filename
* @return string
*
* temporary files are automatically cleaned up after the script is finished
*/
public static function tmpFolder(){
return(\OC_Helper::tmpFolder());
}
/**
* Adds a suffix to the name in case the file exists
*
* @param $path
* @param $filename
* @return string
*/
public static function buildNotExistingFileName($path, $filename){
return(\OC_Helper::buildNotExistingFileName($path, $filename));
}
} }