Skip to content

Commit

Permalink
Allow Horde_Vfs::writeData to accept a stream as input.
Browse files Browse the repository at this point in the history
For every driver except Mongo, the stream is maintained. Mongo
must read it into a string since there is no way to stream the
bytes to the backend.

Related to Bug: 10775.
  • Loading branch information
mrubinsk committed Feb 2, 2017
1 parent a595e30 commit 9198c3c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 31 deletions.
31 changes: 26 additions & 5 deletions framework/Vfs/lib/Horde/Vfs/Base.php
Expand Up @@ -285,10 +285,11 @@ public function write($path, $name, $tmpFile, $autocreate = false)
*
* @abstract
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
Expand Down Expand Up @@ -751,7 +752,7 @@ protected function _checkQuotaWrite($mode, $data, $path = null, $name = null)
throw new Horde_Vfs_Exception('Unable to read VFS file (filesize() failed).');
}
} else {
$filesize = strlen($data);
$filesize = $this->_getDataSize($data);
}
$oldsize = 0;
if ($name) {
Expand All @@ -772,6 +773,26 @@ protected function _checkQuotaWrite($mode, $data, $path = null, $name = null)
}
}

/**
* Return the size of $data.
*
* @param string|resource $data The data.
*
* @return integer The data length.
*/
protected function _getDataSize($data)
{
if (is_resource($data)) {
$pos = fseek($data, 0, SEEK_END);
$size = ftell($data);
fseek($data, $pos);

return $size;
}

return strlen($data);
}

/**
* Checks the quota when preparing to delete data.
*
Expand Down
15 changes: 10 additions & 5 deletions framework/Vfs/lib/Horde/Vfs/File.php
Expand Up @@ -291,10 +291,11 @@ public function copy($path, $name, $dest, $autocreate = false)
/**
* Store a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
Expand All @@ -310,7 +311,7 @@ public function writeData($path, $name, $data, $autocreate = false)

// Treat an attempt to write an empty file as a touch() call,
// since otherwise the file will not be created at all.
if (!strlen($data)) {
if (!$this->_getDataSize($data)) {
if (@touch($this->_getNativePath($path, $name))) {
return;
}
Expand All @@ -320,6 +321,10 @@ public function writeData($path, $name, $data, $autocreate = false)
$this->_checkQuotaWrite('string', $data, $path, $name);

// Otherwise we go ahead and try to write out the file.
if (is_resource($data)) {
rewind($data);
}

if (!@file_put_contents($this->_getNativePath($path, $name), $data)) {
$this->_throwException(new Horde_Vfs_Exception(sprintf('Unable to write data to VFS file %s/%s.', $path, $name)));
}
Expand Down
12 changes: 8 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/Ftp.php
Expand Up @@ -240,16 +240,20 @@ public function write($path, $name, $tmpFile, $autocreate = false)
/**
* Stores a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
public function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = Horde_Util::getTempFile('vfs');
if (is_resource($data)) {
rewind($data);
}
file_put_contents($tmpFile, $data);
try {
$this->write($path, $name, $tmpFile, $autocreate);
Expand Down
7 changes: 7 additions & 0 deletions framework/Vfs/lib/Horde/Vfs/Mongo.php
Expand Up @@ -200,6 +200,13 @@ protected function _write($type, $path, $name, $data, $autocreate)
break;

case 'string':
// MONGO currently has no ability to stream data TO the
// server. I.e., there is no opposite version of
// MongoGridFSFile::getResource().
if (is_resource($data)) {
rewind($data);
$data = stream_get_contents($data);
}
$this->_files->storeBytes($data, $mdata);
break;
}
Expand Down
9 changes: 5 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/Musql.php
Expand Up @@ -61,10 +61,11 @@ class Horde_Vfs_Musql extends Horde_Vfs_Sql
/**
* Stores a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
Expand Down
12 changes: 8 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/Smb.php
Expand Up @@ -196,16 +196,20 @@ public function write($path, $name, $tmpFile, $autocreate = false)
/**
* Stores a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
public function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = Horde_Util::getTempFile('vfs');
if (is_resource($data)) {
rewind($data);
}
file_put_contents($tmpFile, $data);
try {
$this->write($path, $name, $tmpFile, $autocreate);
Expand Down
9 changes: 5 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/Sql.php
Expand Up @@ -216,10 +216,11 @@ public function write($path, $name, $tmpFile, $autocreate = false)
/**
* Store a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
Expand Down
3 changes: 3 additions & 0 deletions framework/Vfs/lib/Horde/Vfs/SqlFile.php
Expand Up @@ -89,6 +89,9 @@ public function writeData($path, $name, $data, $autocreate = false)
throw new Horde_Vfs_Exception('Unable to open VFS file for writing.');
}
}
if (is_resource($data)) {
rewind($data);
}

if (!@fwrite($fp, $data)) {
throw new Horde_Vfs_Exception('Unable to write VFS file data.');
Expand Down
12 changes: 8 additions & 4 deletions framework/Vfs/lib/Horde/Vfs/Ssh2.php
Expand Up @@ -206,16 +206,20 @@ public function write($path, $name, $tmpFile, $autocreate = false)
/**
* Stores a file in the VFS from raw data.
*
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string $data The file data.
* @param boolean $autocreate Automatically create directories?
* @param string $path The path to store the file in.
* @param string $name The filename to use.
* @param string|resource $data The data as a string or stream resource.
* Resources allowed @since 2.4.0
* @param boolean $autocreate Automatically create directories?
*
* @throws Horde_Vfs_Exception
*/
public function writeData($path, $name, $data, $autocreate = false)
{
$tmpFile = Horde_Util::getTempFile('vfs');
if (is_resource($data)) {
rewind($data);
}
file_put_contents($tmpFile, $data);
clearstatcache();
$this->write($path, $name, $tmpFile, $autocreate);
Expand Down
2 changes: 1 addition & 1 deletion framework/Vfs/package.xml
Expand Up @@ -437,7 +437,7 @@ Reading, writing and listing of files are all supported.</description>
<package>
<name>Horde_Db</name>
<channel>pear.horde.org</channel>
<min>2.2.0</min>
<min>2.4.0</min>
<max>3.0.0alpha1</max>
<exclude>3.0.0alpha1</exclude>
</package>
Expand Down

0 comments on commit 9198c3c

Please sign in to comment.