Merge pull request #5805 from owncloud/shared-storage-copy-folder

Use View::copy and View::rename when copying or renaming files in the shared storage
This commit is contained in:
Frank Karlitschek 2013-11-13 10:17:37 -08:00
commit 58ff8a8673
1 changed files with 47 additions and 59 deletions

View File

@ -34,7 +34,7 @@ class Shared extends \OC\Files\Storage\Common {
$this->sharedFolder = $arguments['sharedFolder']; $this->sharedFolder = $arguments['sharedFolder'];
} }
public function getId(){ public function getId() {
return 'shared::' . $this->sharedFolder; return 'shared::' . $this->sharedFolder;
} }
@ -73,7 +73,7 @@ class Shared extends \OC\Files\Storage\Common {
\OC\Files\Filesystem::initMountPoints($source['fileOwner']); \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
$mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']); $mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
if (is_array($mount)) { if (is_array($mount)) {
$this->files[$target]['fullPath'] = $mount[key($mount)]->getMountPoint().$source['path']; $this->files[$target]['fullPath'] = $mount[key($mount)]->getMountPoint() . $source['path'];
} else { } else {
$this->files[$target]['fullPath'] = false; $this->files[$target]['fullPath'] = false;
} }
@ -222,7 +222,7 @@ class Shared extends \OC\Files\Storage\Common {
if ($path == '' || $path == '/') { if ($path == '' || $path == '/') {
$mtime = 0; $mtime = 0;
$dh = $this->opendir($path); $dh = $this->opendir($path);
if(is_resource($dh)) { if (is_resource($dh)) {
while (($filename = readdir($dh)) !== false) { while (($filename = readdir($dh)) !== false) {
$tempmtime = $this->filemtime($filename); $tempmtime = $this->filemtime($filename);
if ($tempmtime > $mtime) { if ($tempmtime > $mtime) {
@ -244,7 +244,7 @@ class Shared extends \OC\Files\Storage\Common {
$source = $this->getSourcePath($path); $source = $this->getSourcePath($path);
if ($source) { if ($source) {
$info = array( $info = array(
'target' => $this->sharedFolder.$path, 'target' => $this->sharedFolder . $path,
'source' => $source, 'source' => $source,
); );
\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
@ -257,11 +257,12 @@ class Shared extends \OC\Files\Storage\Common {
if ($source = $this->getSourcePath($path)) { if ($source = $this->getSourcePath($path)) {
// Check if permission is granted // Check if permission is granted
if (($this->file_exists($path) && !$this->isUpdatable($path)) if (($this->file_exists($path) && !$this->isUpdatable($path))
|| ($this->is_dir($path) && !$this->isCreatable($path))) { || ($this->is_dir($path) && !$this->isCreatable($path))
) {
return false; return false;
} }
$info = array( $info = array(
'target' => $this->sharedFolder.$path, 'target' => $this->sharedFolder . $path,
'source' => $source, 'source' => $source,
); );
\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
@ -304,31 +305,16 @@ class Shared extends \OC\Files\Storage\Common {
$pos1 = strpos($path1, '/', 1); $pos1 = strpos($path1, '/', 1);
$pos2 = strpos($path2, '/', 1); $pos2 = strpos($path2, '/', 1);
if ($pos1 !== false && $pos2 !== false && ($oldSource = $this->getSourcePath($path1))) { if ($pos1 !== false && $pos2 !== false && ($oldSource = $this->getSourcePath($path1))) {
$newSource = $this->getSourcePath(dirname($path2)).'/'.basename($path2); $newSource = $this->getSourcePath(dirname($path2)) . '/' . basename($path2);
if (dirname($path1) == dirname($path2)) { // Within the same folder, we only need UPDATE permissions
// Rename the file if UPDATE permission is granted if (dirname($path1) == dirname($path2) and $this->isUpdatable($path1)) {
if ($this->isUpdatable($path1)) {
list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource); list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
list( , $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource); list(, $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource);
return $storage->rename($oldInternalPath, $newInternalPath); return $storage->rename($oldInternalPath, $newInternalPath);
} // otherwise DELETE and CREATE permissions required
} else { } elseif ($this->isDeletable($path1) && $this->isCreatable(dirname($path2))) {
// Move the file if DELETE and CREATE permissions are granted $rootView = new \OC\Files\View('');
if ($this->isDeletable($path1) && $this->isCreatable(dirname($path2))) { return $rootView->rename($oldSource, $newSource);
// Get the root shared folder
$folder1 = substr($path1, 0, $pos1);
$folder2 = substr($path2, 0, $pos2);
// Copy and unlink the file if it exists in a different shared folder
if ($folder1 != $folder2) {
if ($this->copy($path1, $path2)) {
return $this->unlink($path1);
}
} else {
list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
list( , $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource);
return $storage->rename($oldInternalPath, $newInternalPath);
}
}
} }
} }
} }
@ -338,10 +324,10 @@ class Shared extends \OC\Files\Storage\Common {
public function copy($path1, $path2) { public function copy($path1, $path2) {
// Copy the file if CREATE permission is granted // Copy the file if CREATE permission is granted
if ($this->isCreatable(dirname($path2))) { if ($this->isCreatable(dirname($path2))) {
$source = $this->fopen($path1, 'r'); $oldSource = $this->getSourcePath($path1);
$target = $this->fopen($path2, 'w'); $newSource = $this->getSourcePath(dirname($path2)) . '/' . basename($path2);
list ($count, $result) = \OC_Helper::streamCopy($source, $target); $rootView = new \OC\Files\View('');
return $result; return $rootView->copy($oldSource, $newSource);
} }
return false; return false;
} }
@ -372,7 +358,7 @@ class Shared extends \OC\Files\Storage\Common {
} }
} }
$info = array( $info = array(
'target' => $this->sharedFolder.$path, 'target' => $this->sharedFolder . $path,
'source' => $source, 'source' => $source,
'mode' => $mode, 'mode' => $mode,
); );
@ -412,6 +398,7 @@ class Shared extends \OC\Files\Storage\Common {
} }
return false; return false;
} }
public function touch($path, $mtime = null) { public function touch($path, $mtime = null) {
if ($source = $this->getSourcePath($path)) { if ($source = $this->getSourcePath($path)) {
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source); list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
@ -422,11 +409,12 @@ class Shared extends \OC\Files\Storage\Common {
public static function setup($options) { public static function setup($options) {
if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user'] if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user']
|| \OCP\Share::getItemsSharedWith('file')) { || \OCP\Share::getItemsSharedWith('file')
) {
$user_dir = $options['user_dir']; $user_dir = $options['user_dir'];
\OC\Files\Filesystem::mount('\OC\Files\Storage\Shared', \OC\Files\Filesystem::mount('\OC\Files\Storage\Shared',
array('sharedFolder' => '/Shared'), array('sharedFolder' => '/Shared'),
$user_dir.'/Shared/'); $user_dir . '/Shared/');
} }
} }