diff options
Diffstat (limited to 'apps/files_external/lib/Lib/Storage/Google.php')
-rw-r--r-- | apps/files_external/lib/Lib/Storage/Google.php | 144 |
1 files changed, 70 insertions, 74 deletions
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); } } |