Merge pull request #7529 from owncloud/getlocalfile-cache
Add caching for getLocalFile on remote storages
This commit is contained in:
commit
3eb58d9973
|
@ -208,11 +208,12 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
} else {
|
} else {
|
||||||
$ext = '';
|
$ext = '';
|
||||||
}
|
}
|
||||||
$tmpFile = \OCP\Files::tmpFile($ext);
|
|
||||||
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
|
||||||
if ($this->file_exists($path)) {
|
if ($this->file_exists($path)) {
|
||||||
$this->getFile($path, $tmpFile);
|
$tmpFile = $this->getCachedFile($path);
|
||||||
|
} else {
|
||||||
|
$tmpFile = \OCP\Files::tmpFile($ext);
|
||||||
}
|
}
|
||||||
|
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
|
||||||
self::$tempFiles[$tmpFile] = $path;
|
self::$tempFiles[$tmpFile] = $path;
|
||||||
return fopen('close://' . $tmpFile, $mode);
|
return fopen('close://' . $tmpFile, $mode);
|
||||||
}
|
}
|
||||||
|
@ -251,8 +252,7 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
if ($this->file_exists($path)) {
|
if ($this->file_exists($path)) {
|
||||||
try {
|
try {
|
||||||
$this->client->proppatch($this->encodePath($path), array('{DAV:}lastmodified' => $mtime));
|
$this->client->proppatch($this->encodePath($path), array('{DAV:}lastmodified' => $mtime));
|
||||||
}
|
} catch (\Sabre_DAV_Exception_NotImplemented $e) {
|
||||||
catch (\Sabre_DAV_Exception_NotImplemented $e) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -261,17 +261,7 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function uploadFile($path, $target) {
|
||||||
* @param string $path
|
|
||||||
* @param string $target
|
|
||||||
*/
|
|
||||||
public function getFile($path, $target) {
|
|
||||||
$this->init();
|
|
||||||
$source=$this->fopen($path, 'r');
|
|
||||||
file_put_contents($target, $source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function uploadFile($path, $target) {
|
|
||||||
$this->init();
|
$this->init();
|
||||||
$source = fopen($path, 'r');
|
$source = fopen($path, 'r');
|
||||||
|
|
||||||
|
@ -295,6 +285,7 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
\OCP\Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, \OCP\Util::ERROR);
|
\OCP\Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, \OCP\Util::ERROR);
|
||||||
}
|
}
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
|
$this->removeCachedFile($target);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rename($path1, $path2) {
|
public function rename($path1, $path2) {
|
||||||
|
@ -303,6 +294,8 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
|
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
|
||||||
try {
|
try {
|
||||||
$this->client->request('MOVE', $path1, null, array('Destination' => $path2));
|
$this->client->request('MOVE', $path1, null, array('Destination' => $path2));
|
||||||
|
$this->removeCachedFile($path1);
|
||||||
|
$this->removeCachedFile($path2);
|
||||||
return true;
|
return true;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -315,6 +308,7 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
|
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
|
||||||
try {
|
try {
|
||||||
$this->client->request('COPY', $path1, null, array('Destination' => $path2));
|
$this->client->request('COPY', $path1, null, array('Destination' => $path2));
|
||||||
|
$this->removeCachedFile($path2);
|
||||||
return true;
|
return true;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -368,6 +362,7 @@ class DAV extends \OC\Files\Storage\Common{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* URL encodes the given path but keeps the slashes
|
* URL encodes the given path but keeps the slashes
|
||||||
|
*
|
||||||
* @param string $path to encode
|
* @param string $path to encode
|
||||||
* @return string encoded path
|
* @return string encoded path
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -27,6 +27,11 @@ abstract class Common implements \OC\Files\Storage\Storage {
|
||||||
protected $watcher;
|
protected $watcher;
|
||||||
protected $storageCache;
|
protected $storageCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $cachedFiles = array();
|
||||||
|
|
||||||
public function __construct($parameters) {
|
public function __construct($parameters) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,11 +127,13 @@ abstract class Common implements \OC\Files\Storage\Storage {
|
||||||
|
|
||||||
public function file_put_contents($path, $data) {
|
public function file_put_contents($path, $data) {
|
||||||
$handle = $this->fopen($path, "w");
|
$handle = $this->fopen($path, "w");
|
||||||
|
$this->removeCachedFile($path);
|
||||||
return fwrite($handle, $data);
|
return fwrite($handle, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rename($path1, $path2) {
|
public function rename($path1, $path2) {
|
||||||
if ($this->copy($path1, $path2)) {
|
if ($this->copy($path1, $path2)) {
|
||||||
|
$this->removeCachedFile($path1);
|
||||||
return $this->unlink($path1);
|
return $this->unlink($path1);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -137,6 +144,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
|
||||||
$source = $this->fopen($path1, 'r');
|
$source = $this->fopen($path1, 'r');
|
||||||
$target = $this->fopen($path2, 'w');
|
$target = $this->fopen($path2, 'w');
|
||||||
list($count, $result) = \OC_Helper::streamCopy($source, $target);
|
list($count, $result) = \OC_Helper::streamCopy($source, $target);
|
||||||
|
$this->removeCachedFile($path2);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,13 +170,14 @@ abstract class Common implements \OC\Files\Storage\Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLocalFile($path) {
|
public function getLocalFile($path) {
|
||||||
return $this->toTmpFile($path);
|
return $this->getCachedFile($path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $path
|
* @param string $path
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function toTmpFile($path) { //no longer in the storage api, still useful here
|
protected function toTmpFile($path) { //no longer in the storage api, still useful here
|
||||||
$source = $this->fopen($path, 'r');
|
$source = $this->fopen($path, 'r');
|
||||||
if (!$source) {
|
if (!$source) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -352,4 +361,15 @@ abstract class Common implements \OC\Files\Storage\Storage {
|
||||||
// default, which is not local
|
// default, which is not local
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getCachedFile($path) {
|
||||||
|
if (!isset($this->cachedFiles[$path])) {
|
||||||
|
$this->cachedFiles[$path] = $this->toTmpFile($path);
|
||||||
|
}
|
||||||
|
return $this->cachedFiles[$path];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function removeCachedFile($path) {
|
||||||
|
unset($this->cachedFiles[$path]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue