Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-03-22 23:27:14 +0300
committerThomas Müller <thomas.mueller@tmit.eu>2016-03-22 23:27:14 +0300
commita9c13dc58a445e27bdab8ed939bfce28db7eb591 (patch)
treec8015cab61316b0f3c1c0f5266c463b6aee760e3
parent656c7f38b66ac0048b83f0a016fdfdc7ac2d27c9 (diff)
parent1e1c0885c63c3568e74966b1f8ea33fc3cd3ccb6 (diff)
Merge pull request #23361 from owncloud/stable9-gdrive-chunkupload
[stable9] Chunk upload for GDrive
-rw-r--r--apps/files_external/lib/google.php57
1 files changed, 52 insertions, 5 deletions
diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php
index 5e5716cf438..b79f42d1e00 100644
--- a/apps/files_external/lib/google.php
+++ b/apps/files_external/lib/google.php
@@ -490,18 +490,27 @@ class Google extends \OC\Files\Storage\Common {
$path = self::$tempFiles[$tmpFile];
$parentFolder = $this->getDriveFile(dirname($path));
if ($parentFolder) {
- // TODO Research resumable upload
$mimetype = \OC::$server->getMimeTypeDetector()->detect($tmpFile);
- $data = file_get_contents($tmpFile);
$params = array(
- 'data' => $data,
'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);
+ }
+
if ($this->file_exists($path)) {
$file = $this->getDriveFile($path);
- $result = $this->service->files->update($file->getId(), $file, $params);
+ $this->client->setDefer($useChunking);
+ $request = $this->service->files->update($file->getId(), $file, $params);
} else {
$file = new \Google_Service_Drive_DriveFile();
$file->setTitle(basename($path));
@@ -509,8 +518,46 @@ class Google extends \OC\Files\Storage\Common {
$parent = new \Google_Service_Drive_ParentReference();
$parent->setId($parentFolder->getId());
$file->setParents(array($parent));
- $result = $this->service->files->insert($file, $params);
+ $this->client->setDefer($useChunking);
+ $request = $this->service->files->insert($file, $params);
+ }
+
+ 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;
}
+
+ // Reset to the client to execute requests immediately in the future.
+ $this->client->setDefer(false);
+
if ($result) {
$this->setDriveFile($path, $result);
}