summaryrefslogtreecommitdiffstats
path: root/apps/files_external/lib/Lib
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2017-01-03 17:26:44 +0100
committerMorris Jobke <hey@morrisjobke.de>2017-01-06 15:33:32 +0100
commit5774d3e82c093166e21ebb1d469e85acd240dab8 (patch)
tree909001d4ad898f1c4ccab05333f4dd52e8d71b37 /apps/files_external/lib/Lib
parent3ab22c2df53c8b86cbd72037ab407327b18cce11 (diff)
downloadnextcloud-server-5774d3e82c093166e21ebb1d469e85acd240dab8.tar.gz
nextcloud-server-5774d3e82c093166e21ebb1d469e85acd240dab8.zip
replace close:// streamwrapper with CallBackWrapper
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/files_external/lib/Lib')
-rw-r--r--apps/files_external/lib/Lib/Storage/AmazonS3.php16
-rw-r--r--apps/files_external/lib/Lib/Storage/Dropbox.php28
-rw-r--r--apps/files_external/lib/Lib/Storage/FTP.php16
-rw-r--r--apps/files_external/lib/Lib/Storage/Google.php144
-rw-r--r--apps/files_external/lib/Lib/Storage/Swift.php16
5 files changed, 105 insertions, 115 deletions
diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php
index e6e26e3547a..9dab25f7197 100644
--- a/apps/files_external/lib/Lib/Storage/AmazonS3.php
+++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php
@@ -42,6 +42,7 @@ require_once 'aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
+use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\ObjectStore\S3ConnectionTrait;
@@ -366,14 +367,15 @@ class AmazonS3 extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OCP\Files::tmpFile($ext);
- \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::$tmpFiles[$tmpFile] = $path;
- return fopen('close://' . $tmpFile, $mode);
+ $handle = fopen($tmpFile, $mode);
+ return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) {
+ $this->writeBack($tmpFile, $path);
+ });
}
return false;
}
@@ -514,15 +516,11 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return $this->id;
}
- public function writeBack($tmpFile) {
- if (!isset(self::$tmpFiles[$tmpFile])) {
- return false;
- }
-
+ public function writeBack($tmpFile, $path) {
try {
$this->getConnection()->putObject(array(
'Bucket' => $this->bucket,
- 'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]),
+ 'Key' => $this->cleanKey($path),
'SourceFile' => $tmpFile,
'ContentType' => \OC::$server->getMimeTypeDetector()->detect($tmpFile),
'ContentLength' => filesize($tmpFile)
diff --git a/apps/files_external/lib/Lib/Storage/Dropbox.php b/apps/files_external/lib/Lib/Storage/Dropbox.php
index 45bc6cd0e98..d2ba1cca751 100644
--- a/apps/files_external/lib/Lib/Storage/Dropbox.php
+++ b/apps/files_external/lib/Lib/Storage/Dropbox.php
@@ -31,6 +31,7 @@
namespace OCA\Files_External\Lib\Storage;
use GuzzleHttp\Exception\RequestException;
+use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use Icewind\Streams\RetryWrapper;
use OCP\Files\StorageNotAvailableException;
@@ -45,8 +46,6 @@ class Dropbox extends \OC\Files\Storage\Common {
private $metaData = array();
private $oauth;
- private static $tempFiles = array();
-
public function __construct($params) {
if (isset($params['configured']) && $params['configured'] == 'true'
&& isset($params['app_key'])
@@ -305,27 +304,26 @@ class Dropbox extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OCP\Files::tmpFile($ext);
- \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);
+ $handle = fopen($tmpFile, $mode);
+ return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
+ $this->writeBack($tmpFile, $path);
+ });
}
return false;
}
- public function writeBack($tmpFile) {
- if (isset(self::$tempFiles[$tmpFile])) {
- $handle = fopen($tmpFile, 'r');
- try {
- $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle);
- unlink($tmpFile);
- $this->deleteMetaData(self::$tempFiles[$tmpFile]);
- } catch (\Exception $exception) {
- \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
- }
+ public function writeBack($tmpFile, $path) {
+ $handle = fopen($tmpFile, 'r');
+ try {
+ $this->dropbox->putFile($path, $handle);
+ unlink($tmpFile);
+ $this->deleteMetaData($path);
+ } catch (\Exception $exception) {
+ \OCP\Util::writeLog('files_external', $exception->getMessage(), \OCP\Util::ERROR);
}
}
diff --git a/apps/files_external/lib/Lib/Storage/FTP.php b/apps/files_external/lib/Lib/Storage/FTP.php
index 6f34416d111..22fe2090f30 100644
--- a/apps/files_external/lib/Lib/Storage/FTP.php
+++ b/apps/files_external/lib/Lib/Storage/FTP.php
@@ -33,6 +33,7 @@
namespace OCA\Files_External\Lib\Storage;
+use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\RetryWrapper;
class FTP extends StreamWrapper{
@@ -127,21 +128,20 @@ class FTP extends StreamWrapper{
$ext='';
}
$tmpFile=\OCP\Files::tmpFile($ext);
- \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$this->getFile($path, $tmpFile);
}
- self::$tempFiles[$tmpFile]=$path;
- return fopen('close://'.$tmpFile, $mode);
+ $handle = fopen($tmpFile, $mode);
+ return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
+ $this->writeBack($tmpFile, $path);
+ });
}
return false;
}
- public function writeBack($tmpFile) {
- if (isset(self::$tempFiles[$tmpFile])) {
- $this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
- unlink($tmpFile);
- }
+ public function writeBack($tmpFile, $path) {
+ $this->uploadFile($tmpFile, $path);
+ unlink($tmpFile);
}
/**
diff --git a/apps/files_external/lib/Lib/Storage/Google.php b/apps/files_external/lib/Lib/Storage/Google.php
index a3133cb4743..b22b0c29263 100644
--- a/apps/files_external/lib/Lib/Storage/Google.php
+++ b/apps/files_external/lib/Lib/Storage/Google.php
@@ -36,6 +36,7 @@
namespace OCA\Files_External\Lib\Storage;
use GuzzleHttp\Exception\RequestException;
+use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use Icewind\Streams\RetryWrapper;
@@ -50,8 +51,6 @@ class Google extends \OC\Files\Storage\Common {
private $service;
private $driveFiles;
- private static $tempFiles = array();
-
// Google Doc mimetypes
const FOLDER = 'application/vnd.google-apps.folder';
const DOCUMENT = 'application/vnd.google-apps.document';
@@ -495,94 +494,91 @@ class Google extends \OC\Files\Storage\Common {
case 'c':
case 'c+':
$tmpFile = \OCP\Files::tmpFile($ext);
- \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$source = $this->fopen($path, 'rb');
file_put_contents($tmpFile, $source);
}
- self::$tempFiles[$tmpFile] = $path;
- return fopen('close://'.$tmpFile, $mode);
+ $handle = fopen($tmpFile, $mode);
+ return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
+ $this->writeBack($tmpFile, $path);
+ });
}
}
- public function writeBack($tmpFile) {
- if (isset(self::$tempFiles[$tmpFile])) {
- $path = self::$tempFiles[$tmpFile];
- $parentFolder = $this->getDriveFile(dirname($path));
- if ($parentFolder) {
- $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile);
- $params = array(
- 'mimeType' => $mimetype,
- 'uploadType' => 'media'
- );
- $result = false;
+ public function writeBack($tmpFile, $path) {
+ $parentFolder = $this->getDriveFile(dirname($path));
+ if ($parentFolder) {
+ $mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile);
+ $params = array(
+ 'mimeType' => $mimetype,
+ 'uploadType' => 'media'
+ );
+ $result = false;
+
+ $chunkSizeBytes = 10 * 1024 * 1024;
+
+ $useChunking = false;
+ $size = filesize($tmpFile);
+ if ($size > $chunkSizeBytes) {
+ $useChunking = true;
+ } else {
+ $params['data'] = file_get_contents($tmpFile);
+ }
- $chunkSizeBytes = 10 * 1024 * 1024;
+ if ($this->file_exists($path)) {
+ $file = $this->getDriveFile($path);
+ $this->client->setDefer($useChunking);
+ $request = $this->service->files->update($file->getId(), $file, $params);
+ } else {
+ $file = new \Google_Service_Drive_DriveFile();
+ $file->setTitle(basename($path));
+ $file->setMimeType($mimetype);
+ $parent = new \Google_Service_Drive_ParentReference();
+ $parent->setId($parentFolder->getId());
+ $file->setParents(array($parent));
+ $this->client->setDefer($useChunking);
+ $request = $this->service->files->insert($file, $params);
+ }
- $useChunking = false;
- $size = filesize($tmpFile);
- if ($size > $chunkSizeBytes) {
- $useChunking = true;
- } else {
- $params['data'] = file_get_contents($tmpFile);
+ if ($useChunking) {
+ // Create a media file upload to represent our upload process.
+ $media = new \Google_Http_MediaFileUpload(
+ $this->client,
+ $request,
+ 'text/plain',
+ null,
+ true,
+ $chunkSizeBytes
+ );
+ $media->setFileSize($size);
+
+ // Upload the various chunks. $status will be false until the process is
+ // complete.
+ $status = false;
+ $handle = fopen($tmpFile, 'rb');
+ while (!$status && !feof($handle)) {
+ $chunk = fread($handle, $chunkSizeBytes);
+ $status = $media->nextChunk($chunk);
}
- if ($this->file_exists($path)) {
- $file = $this->getDriveFile($path);
- $this->client->setDefer($useChunking);
- $request = $this->service->files->update($file->getId(), $file, $params);
- } else {
- $file = new \Google_Service_Drive_DriveFile();
- $file->setTitle(basename($path));
- $file->setMimeType($mimetype);
- $parent = new \Google_Service_Drive_ParentReference();
- $parent->setId($parentFolder->getId());
- $file->setParents(array($parent));
- $this->client->setDefer($useChunking);
- $request = $this->service->files->insert($file, $params);
+ // The final value of $status will be the data from the API for the object
+ // that has been uploaded.
+ $result = false;
+ if ($status !== false) {
+ $result = $status;
}
- if ($useChunking) {
- // Create a media file upload to represent our upload process.
- $media = new \Google_Http_MediaFileUpload(
- $this->client,
- $request,
- 'text/plain',
- null,
- true,
- $chunkSizeBytes
- );
- $media->setFileSize($size);
-
- // Upload the various chunks. $status will be false until the process is
- // complete.
- $status = false;
- $handle = fopen($tmpFile, 'rb');
- while (!$status && !feof($handle)) {
- $chunk = fread($handle, $chunkSizeBytes);
- $status = $media->nextChunk($chunk);
- }
-
- // The final value of $status will be the data from the API for the object
- // that has been uploaded.
- $result = false;
- if ($status !== false) {
- $result = $status;
- }
-
- fclose($handle);
- } else {
- $result = $request;
- }
+ fclose($handle);
+ } else {
+ $result = $request;
+ }
- // Reset to the client to execute requests immediately in the future.
- $this->client->setDefer(false);
+ // Reset to the client to execute requests immediately in the future.
+ $this->client->setDefer(false);
- if ($result) {
- $this->setDriveFile($path, $result);
- }
+ if ($result) {
+ $this->setDriveFile($path, $result);
}
- unlink($tmpFile);
}
}
diff --git a/apps/files_external/lib/Lib/Storage/Swift.php b/apps/files_external/lib/Lib/Storage/Swift.php
index ba0b4898e2e..5fec278ef3d 100644
--- a/apps/files_external/lib/Lib/Storage/Swift.php
+++ b/apps/files_external/lib/Lib/Storage/Swift.php
@@ -37,6 +37,7 @@ namespace OCA\Files_External\Lib\Storage;
use Guzzle\Http\Url;
use Guzzle\Http\Exception\ClientErrorResponseException;
+use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\IteratorDirectory;
use OpenCloud;
use OpenCloud\Common\Exceptions;
@@ -410,7 +411,6 @@ class Swift extends \OC\Files\Storage\Common {
$ext = '';
}
$tmpFile = \OCP\Files::tmpFile($ext);
- \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
// Fetch existing file if required
if ($mode[0] !== 'w' && $this->file_exists($path)) {
if ($mode[0] === 'x') {
@@ -424,9 +424,10 @@ class Swift extends \OC\Files\Storage\Common {
fseek($tmpFile, 0, SEEK_END);
}
}
- self::$tmpFiles[$tmpFile] = $path;
-
- return fopen('close://' . $tmpFile, $mode);
+ $handle = fopen($tmpFile, $mode);
+ return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
+ $this->writeBack($tmpFile, $path);
+ });
}
}
@@ -615,12 +616,9 @@ class Swift extends \OC\Files\Storage\Common {
return $this->container;
}
- public function writeBack($tmpFile) {
- if (!isset(self::$tmpFiles[$tmpFile])) {
- return false;
- }
+ public function writeBack($tmpFile, $path) {
$fileData = fopen($tmpFile, 'r');
- $this->getContainer()->uploadObject(self::$tmpFiles[$tmpFile], $fileData);
+ $this->getContainer()->uploadObject($path, $fileData);
// invalidate target object to force repopulation on fetch
$this->objectCache->remove(self::$tmpFiles[$tmpFile]);
unlink($tmpFile);