Move streamwrappers to seperate files and put them in a namespace

This commit is contained in:
Robin Appelman 2013-01-28 15:34:15 +01:00
parent bca5ce724e
commit c9c919da57
14 changed files with 242 additions and 204 deletions

View File

@ -1,24 +1,24 @@
<?php
/**
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@owncloud.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/>.
*/
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@owncloud.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/>.
*/
namespace OC\Files\Storage;
@ -36,7 +36,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
// TODO options: storage class, encryption server side, encrypt before upload?
public function __construct($params) {
$this->id = 'amazon::'.$params['key'] . md5($params['secret']);
$this->id = 'amazon::' . $params['key'] . md5($params['secret']);
$this->s3 = new \AmazonS3(array('key' => $params['key'], 'secret' => $params['secret']));
$this->bucket = $params['bucket'];
}
@ -51,7 +51,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return $response;
// This object could be a folder, a '/' must be at the end of the path
} else if (substr($path, -1) != '/') {
$response = $this->s3->get_object_metadata($this->bucket, $path.'/');
$response = $this->s3->get_object_metadata($this->bucket, $path . '/');
if ($response) {
$this->objects[$path] = $response;
return $response;
@ -61,7 +61,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return false;
}
public function getId(){
public function getId() {
return $this->id;
}
@ -104,8 +104,8 @@ class AmazonS3 extends \OC\Files\Storage\Common {
foreach ($response->body->CommonPrefixes as $object) {
$files[] = basename($object->Prefix);
}
\OC_FakeDirStream::$dirs['amazons3'.$path] = $files;
return opendir('fakedir://amazons3'.$path);
\OC\Files\Stream\Dir::register('amazons3' . $path, $files);
return opendir('fakedir://amazons3' . $path);
}
return false;
}
@ -194,13 +194,13 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);
}
self::$tempFiles[$tmpFile] = $path;
return fopen('close://'.$tmpFile, $mode);
return fopen('close://' . $tmpFile, $mode);
}
return false;
}
@ -209,8 +209,8 @@ class AmazonS3 extends \OC\Files\Storage\Common {
if (isset(self::$tempFiles[$tmpFile])) {
$handle = fopen($tmpFile, 'r');
$response = $this->s3->create_object($this->bucket,
self::$tempFiles[$tmpFile],
array('fileUpload' => $handle));
self::$tempFiles[$tmpFile],
array('fileUpload' => $handle));
if ($response->isOK()) {
unlink($tmpFile);
}

View File

@ -114,7 +114,7 @@ class Dropbox extends \OC\Files\Storage\Common {
foreach ($contents as $file) {
$files[] = basename($file['path']);
}
\OC_FakeDirStream::$dirs['dropbox'.$path] = $files;
\OC\Files\Stream\Dir::register('dropbox'.$path, $files);
return opendir('fakedir://dropbox'.$path);
}
return false;
@ -232,7 +232,7 @@ class Dropbox extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);

View File

@ -84,7 +84,7 @@ class FTP extends \OC\Files\Storage\StreamWrapper{
$ext='';
}
$tmpFile=OCP\Files::tmpFile($ext);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$this->getFile($path, $tmpFile);
}

View File

@ -273,7 +273,7 @@ class Google extends \OC\Files\Storage\Common {
$this->entries[$name] = $entry;
}
}
\OC_FakeDirStream::$dirs['google'.$path] = $files;
\OC\Files\Stream\Dir::register('google'.$path, $files);
return opendir('fakedir://google'.$path);
}
@ -450,7 +450,7 @@ class Google extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OC_Helper::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'r');
file_put_contents($tmpFile, $source);

View File

@ -371,7 +371,7 @@ class SWIFT extends \OC\Files\Storage\Common{
$subContainers=$this->getSubContainers($container);
$files=array_merge($files, $subContainers);
$id=$this->getContainerName($path);
\OC_FakeDirStream::$dirs[$id]=$files;
\OC\Files\Stream\Dir::register($id, $files);
return opendir('fakedir://'.$id);
}
@ -465,7 +465,7 @@ class SWIFT extends \OC\Files\Storage\Common{
case 'c':
case 'c+':
$tmpFile=$this->getTmpFile($path);
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
self::$tempFiles[$tmpFile]=$path;
return fopen('close://'.$tmpFile, $mode);
}

View File

@ -104,13 +104,15 @@ class DAV extends \OC\Files\Storage\Common{
try {
$response=$this->client->propfind($path, array(), 1);
$id=md5('webdav'.$this->root.$path);
$content = array();
\OC_FakeDirStream::$dirs[$id]=array();
$files=array_keys($response);
array_shift($files);//the first entry is the current directory
foreach ($files as $file) {
$file = urldecode(basename($file));
\OC_FakeDirStream::$dirs[$id][]=$file;
$content[]=$file;
}
\OC\Files\Stream\Dir::register($id, $content);
return opendir('fakedir://'.$id);
} catch(\Exception $e) {
return false;
@ -194,7 +196,7 @@ class DAV extends \OC\Files\Storage\Common{
$ext='';
}
$tmpFile = \OCP\Files::tmpFile($ext);
\OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if($this->file_exists($path)) {
$this->getFile($path, $tmpFile);
}

View File

@ -102,7 +102,7 @@ class Shared extends \OC\Files\Storage\Common {
public function opendir($path) {
if ($path == '' || $path == '/') {
$files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_Folder::FORMAT_OPENDIR);
\OC_FakeDirStream::$dirs['shared'] = $files;
\OC\Files\Stream\Dir::register('shared', $files);
return opendir('fakedir://shared');
} else if ($source = $this->getSourcePath($path)) {
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);

View File

@ -308,7 +308,7 @@ class OC_Archive_TAR extends OC_Archive{
if($mode=='r' or $mode=='rb') {
return fopen($tmpFile, $mode);
}else{
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
self::$tempFiles[$tmpFile]=$path;
return fopen('close://'.$tmpFile, $mode);
}

View File

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

View File

@ -421,10 +421,9 @@ class OC
}
// register the stream wrappers
require_once 'streamwrappers.php';
stream_wrapper_register("fakedir", "OC_FakeDirStream");
stream_wrapper_register('static', 'OC_StaticStreamWrapper');
stream_wrapper_register('close', 'OC_CloseStreamWrapper');
stream_wrapper_register('fakedir', 'OC\Files\Stream\Dir');
stream_wrapper_register('static', 'OC\Files\Stream\StaticStream');
stream_wrapper_register('close', 'OC\Files\Stream\Close');
self::checkConfig();
self::checkInstalled();

100
lib/files/stream/close.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Stream;
/**
* stream wrapper that provides a callback on stream close
*/
class Close {
private static $callBacks = array();
private $path = '';
private $source;
private static $open = array();
public function stream_open($path, $mode, $options, &$opened_path) {
$path = substr($path, strlen('close://'));
$this->path = $path;
$this->source = fopen($path, $mode);
if (is_resource($this->source)) {
$this->meta = stream_get_meta_data($this->source);
}
self::$open[] = $path;
return is_resource($this->source);
}
public function stream_seek($offset, $whence = SEEK_SET) {
fseek($this->source, $offset, $whence);
}
public function stream_tell() {
return ftell($this->source);
}
public function stream_read($count) {
return fread($this->source, $count);
}
public function stream_write($data) {
return fwrite($this->source, $data);
}
public function stream_set_option($option, $arg1, $arg2) {
switch ($option) {
case STREAM_OPTION_BLOCKING:
stream_set_blocking($this->source, $arg1);
break;
case STREAM_OPTION_READ_TIMEOUT:
stream_set_timeout($this->source, $arg1, $arg2);
break;
case STREAM_OPTION_WRITE_BUFFER:
stream_set_write_buffer($this->source, $arg1, $arg2);
}
}
public function stream_stat() {
return fstat($this->source);
}
public function stream_lock($mode) {
flock($this->source, $mode);
}
public function stream_flush() {
return fflush($this->source);
}
public function stream_eof() {
return feof($this->source);
}
public function url_stat($path) {
$path = substr($path, strlen('close://'));
if (file_exists($path)) {
return stat($path);
} else {
return false;
}
}
public function stream_close() {
fclose($this->source);
if (isset(self::$callBacks[$this->path])) {
call_user_func(self::$callBacks[$this->path], $this->path);
}
}
public function unlink($path) {
$path = substr($path, strlen('close://'));
return unlink($path);
}
public static function registerCallback($path, $callback) {
self::$callBacks[$path] = $callback;
}
}

47
lib/files/stream/dir.php Normal file
View File

@ -0,0 +1,47 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Stream;
class Dir {
private static $dirs = array();
private $name;
private $index;
public function dir_opendir($path, $options) {
$this->name = substr($path, strlen('fakedir://'));
$this->index = 0;
if (!isset(self::$dirs[$this->name])) {
self::$dirs[$this->name] = array();
}
return true;
}
public function dir_readdir() {
if ($this->index >= count(self::$dirs[$this->name])) {
return false;
}
$filename = self::$dirs[$this->name][$this->index];
$this->index++;
return $filename;
}
public function dir_closedir() {
$this->name = '';
return true;
}
public function dir_rewinddir() {
$this->index = 0;
return true;
}
public static function register($path, $content) {
self::$dirs[$path] = $content;
}
}

View File

@ -1,54 +1,30 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class OC_FakeDirStream{
public static $dirs=array();
private $name;
private $index;
namespace OC\Files\Stream;
public function dir_opendir($path, $options) {
$this->name=substr($path, strlen('fakedir://'));
$this->index=0;
if(!isset(self::$dirs[$this->name])) {
self::$dirs[$this->name]=array();
}
return true;
}
public function dir_readdir() {
if($this->index>=count(self::$dirs[$this->name])) {
return false;
}
$filename=self::$dirs[$this->name][$this->index];
$this->index++;
return $filename;
}
public function dir_closedir() {
$this->name='';
return true;
}
public function dir_rewinddir() {
$this->index=0;
return true;
}
}
class OC_StaticStreamWrapper {
class StaticStream {
public $context;
protected static $data = array();
protected $path = '';
protected $path = '';
protected $pointer = 0;
protected $writable = false;
public function stream_close() {}
public function stream_close() {
}
public function stream_eof() {
return $this->pointer >= strlen(self::$data[$this->path]);
}
public function stream_flush() {}
public function stream_flush() {
}
public function stream_open($path, $mode, $options, &$opened_path) {
switch ($mode[0]) {
@ -213,89 +189,3 @@ class OC_StaticStreamWrapper {
return false;
}
}
/**
* stream wrapper that provides a callback on stream close
*/
class OC_CloseStreamWrapper{
public static $callBacks=array();
private $path='';
private $source;
private static $open=array();
public function stream_open($path, $mode, $options, &$opened_path) {
$path=substr($path, strlen('close://'));
$this->path=$path;
$this->source=fopen($path, $mode);
if(is_resource($this->source)) {
$this->meta=stream_get_meta_data($this->source);
}
self::$open[]=$path;
return is_resource($this->source);
}
public function stream_seek($offset, $whence=SEEK_SET) {
fseek($this->source, $offset, $whence);
}
public function stream_tell() {
return ftell($this->source);
}
public function stream_read($count) {
return fread($this->source, $count);
}
public function stream_write($data) {
return fwrite($this->source, $data);
}
public function stream_set_option($option, $arg1, $arg2) {
switch($option) {
case STREAM_OPTION_BLOCKING:
stream_set_blocking($this->source, $arg1);
break;
case STREAM_OPTION_READ_TIMEOUT:
stream_set_timeout($this->source, $arg1, $arg2);
break;
case STREAM_OPTION_WRITE_BUFFER:
stream_set_write_buffer($this->source, $arg1, $arg2);
}
}
public function stream_stat() {
return fstat($this->source);
}
public function stream_lock($mode) {
flock($this->source, $mode);
}
public function stream_flush() {
return fflush($this->source);
}
public function stream_eof() {
return feof($this->source);
}
public function url_stat($path) {
$path=substr($path, strlen('close://'));
if(file_exists($path)) {
return stat($path);
}else{
return false;
}
}
public function stream_close() {
fclose($this->source);
if(isset(self::$callBacks[$this->path])) {
call_user_func(self::$callBacks[$this->path], $this->path);
}
}
public function unlink($path) {
$path=substr($path, strlen('close://'));
return unlink($path);
}
}

View File

@ -1,41 +1,41 @@
<?php
/**
* ownCloud
*
* @author Robin Appelman
* @copyright 2012 Robin Appelman icewind@owncloud.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/>.
*
*/
* ownCloud
*
* @author Robin Appelman
* @copyright 2012 Robin Appelman icewind@owncloud.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/>.
*
*/
class Test_StreamWrappers extends PHPUnit_Framework_TestCase {
public function testFakeDir() {
$items=array('foo', 'bar');
OC_FakeDirStream::$dirs['test']=$items;
$dh=opendir('fakedir://test');
$result=array();
while($file=readdir($dh)) {
$result[]=$file;
$items = array('foo', 'bar');
\OC\Files\Stream\Dir::register('test', $items);
$dh = opendir('fakedir://test');
$result = array();
while ($file = readdir($dh)) {
$result[] = $file;
$this->assertContains($file, $items);
}
$this->assertEquals(count($items), count($result));
}
public function testStaticStream() {
$sourceFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
$staticFile='static://test';
$sourceFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
$staticFile = 'static://test';
$this->assertFalse(file_exists($staticFile));
file_put_contents($staticFile, file_get_contents($sourceFile));
$this->assertTrue(file_exists($staticFile));
@ -47,27 +47,27 @@ class Test_StreamWrappers extends PHPUnit_Framework_TestCase {
public function testCloseStream() {
//ensure all basic stream stuff works
$sourceFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
$tmpFile=OC_Helper::TmpFile('.txt');
$file='close://'.$tmpFile;
$sourceFile = OC::$SERVERROOT . '/tests/data/lorem.txt';
$tmpFile = OC_Helper::TmpFile('.txt');
$file = 'close://' . $tmpFile;
$this->assertTrue(file_exists($file));
file_put_contents($file, file_get_contents($sourceFile));
$this->assertEquals(file_get_contents($sourceFile), file_get_contents($file));
unlink($file);
clearstatcache();
$this->assertFalse(file_exists($file));
//test callback
$tmpFile=OC_Helper::TmpFile('.txt');
$file='close://'.$tmpFile;
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array('Test_StreamWrappers', 'closeCallBack');
$fh=fopen($file, 'w');
$tmpFile = OC_Helper::TmpFile('.txt');
$file = 'close://' . $tmpFile;
\OC\Files\Stream\Close::registerCallback($tmpFile, array('Test_StreamWrappers', 'closeCallBack'));
$fh = fopen($file, 'w');
fwrite($fh, 'asd');
try{
try {
fclose($fh);
$this->fail('Expected exception');
}catch(Exception $e) {
$path=$e->getMessage();
} catch (Exception $e) {
$path = $e->getMessage();
$this->assertEquals($path, $tmpFile);
}
}