summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2016-03-17 12:36:47 +0100
committerVincent Petry <pvince81@owncloud.com>2016-03-17 12:36:47 +0100
commit5db41dfbed00c669a508f7173590060d033ddac9 (patch)
treeedc71a34e72f54e5257bce42c023717027e8b896 /apps
parentaa2674e7e24fc49ba57de067afce8ad97f0760f5 (diff)
downloadnextcloud-server-5db41dfbed00c669a508f7173590060d033ddac9.tar.gz
nextcloud-server-5db41dfbed00c669a508f7173590060d033ddac9.zip
Only use GDrive chunks when needed
Diffstat (limited to 'apps')
-rw-r--r--apps/files_external/lib/google.php72
1 files changed, 43 insertions, 29 deletions
diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php
index 112f7e9c57f..b79f42d1e00 100644
--- a/apps/files_external/lib/google.php
+++ b/apps/files_external/lib/google.php
@@ -493,11 +493,23 @@ class Google extends \OC\Files\Storage\Common {
$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);
+ }
+
if ($this->file_exists($path)) {
$file = $this->getDriveFile($path);
- $this->client->setDefer(true);
+ $this->client->setDefer($useChunking);
$request = $this->service->files->update($file->getId(), $file, $params);
} else {
$file = new \Google_Service_Drive_DriveFile();
@@ -506,41 +518,43 @@ class Google extends \OC\Files\Storage\Common {
$parent = new \Google_Service_Drive_ParentReference();
$parent->setId($parentFolder->getId());
$file->setParents(array($parent));
- $this->client->setDefer(true);
+ $this->client->setDefer($useChunking);
$request = $this->service->files->insert($file, $params);
}
- $chunkSizeBytes = 10 * 1024 * 1024;
+ 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);
+ }
- // 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(filesize($tmpFile));
-
- // 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;
+ }
- // 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);
-
// Reset to the client to execute requests immediately in the future.
$this->client->setDefer(false);