Skip to content

Commit 8271966

Browse files
author
Ian Barber
committed
Add separate upload GZIP flag
Make config GZIP possible to enable separately to the main gzip. It still defaults to false, but at least in situations like #91 where it is showing benefit it can be easily enabled. In order to maintain the defaults though, the config names have become slightly confusing (one disable, one enable). I've added some constants to try and work around that, but I may be just setting us up for future problems!
1 parent 97e43f2 commit 8271966

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/Google/Config.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*/
2121
class Google_Config
2222
{
23+
const GZIP_DISABLED = true;
24+
const GZIP_ENABLED = false;
25+
const GZIP_UPLOADS_ENABLED = true;
26+
const GZIP_UPLOADS_DISABLED = false;
2327
const USE_AUTO_IO_SELECTION = "auto";
2428
private $configuration;
2529

@@ -50,8 +54,14 @@ public function __construct($ini_file_location = null)
5054
// If you want to pass in OAuth 2.0 settings, they will need to be
5155
// structured like this.
5256
'Google_Http_Request' => array(
53-
// Disable the use of gzip on calls if set to true.
54-
'disable_gzip' => false
57+
// Disable the use of gzip on calls if set to true. Defaults to false.
58+
'disable_gzip' => self::GZIP_ENABLED,
59+
60+
// We default gzip to disabled on uploads even if gzip is otherwise
61+
// enabled, due to some issues seen with small packet sizes for uploads.
62+
// Please test with this option before enabling gzip for uploads in
63+
// a production environment.
64+
'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,
5565
),
5666
'Google_Auth_OAuth2' => array(
5767
// Keys for OAuth 2.0 access, see the API console at
@@ -109,7 +119,7 @@ public function setClassConfig($class, $config, $value = null)
109119
$this->configuration['classes'][$class] = $config;
110120
}
111121
}
112-
122+
113123
public function getClassConfig($class, $key = null)
114124
{
115125
if (!isset($this->configuration['classes'][$class])) {
@@ -139,7 +149,7 @@ public function getAuthClass()
139149
{
140150
return $this->configuration['auth_class'];
141151
}
142-
152+
143153
/**
144154
* Set the auth class.
145155
*
@@ -155,7 +165,7 @@ public function setAuthClass($class)
155165
}
156166
$this->configuration['auth_class'] = $class;
157167
}
158-
168+
159169
/**
160170
* Set the IO class.
161171
*
@@ -268,7 +278,7 @@ public function setApprovalPrompt($approval)
268278
{
269279
$this->setAuthConfig('approval_prompt', $approval);
270280
}
271-
281+
272282
/**
273283
* Set the developer key for the auth class. Note that this is separate value
274284
* from the client ID - if it looks like a URL, its a client ID!
@@ -286,7 +296,7 @@ public function getBasePath()
286296
{
287297
return $this->configuration['base_path'];
288298
}
289-
299+
290300
/**
291301
* Set the auth configuration for the current auth class.
292302
* @param $key - the key to set

src/Google/Http/MediaFileUpload.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ class Google_Http_MediaFileUpload
5151

5252
/** @var int $progress */
5353
private $progress;
54-
54+
5555
/** @var Google_Client */
5656
private $client;
57-
57+
5858
/** @var Google_Http_Request */
5959
private $request;
60-
60+
6161
/** @var string */
6262
private $boundary;
6363

@@ -89,7 +89,7 @@ public function __construct(
8989
$this->chunkSize = $chunkSize;
9090
$this->progress = 0;
9191
$this->boundary = $boundary;
92-
92+
9393
// Process Media Request
9494
$this->process();
9595
}
@@ -102,16 +102,16 @@ public function setFileSize($size)
102102
{
103103
$this->size = $size;
104104
}
105-
106-
/**
105+
106+
/**
107107
* Return the progress on the upload
108108
* @return int progress in bytes uploaded.
109109
*/
110110
public function getProgress()
111111
{
112112
return $this->progress;
113113
}
114-
114+
115115
/**
116116
* Send the next part of the file to upload.
117117
* @param [$chunk] the next set of bytes to send. If false will used $data passed
@@ -141,7 +141,13 @@ public function nextChunk($chunk = false)
141141
$headers,
142142
$chunk
143143
);
144-
$httpRequest->disableGzip(); // Disable gzip for uploads.
144+
145+
if ($client->getClassConfig("Google_Http_Request", "enable_gzip_for_uploads") {
146+
$httpRequest->enableGzip();
147+
} else {
148+
$httpRequest->disableGzip();
149+
}
150+
145151
$response = $this->client->getIo()->makeRequest($httpRequest);
146152
$response->setExpectedClass($this->request->getExpectedClass());
147153
$code = $response->getResponseHttpCode();
@@ -150,13 +156,13 @@ public function nextChunk($chunk = false)
150156
// Track the amount uploaded.
151157
$range = explode('-', $response->getResponseHeader('range'));
152158
$this->progress = $range[1] + 1;
153-
159+
154160
// Allow for changing upload URLs.
155161
$location = $response->getResponseHeader('location');
156162
if ($location) {
157163
$this->resumeUri = $location;
158164
}
159-
165+
160166
// No problems, but upload not complete.
161167
return false;
162168
} else {
@@ -177,7 +183,7 @@ private function process()
177183

178184
$meta = $this->request->getPostBody();
179185
$meta = is_string($meta) ? json_decode($meta, true) : $meta;
180-
186+
181187
$uploadType = $this->getUploadType($meta);
182188
$this->request->setQueryParam('uploadType', $uploadType);
183189
$this->transformToUploadUrl();
@@ -214,7 +220,7 @@ private function process()
214220
$this->request->setRequestHeaders($contentTypeHeader);
215221
}
216222
}
217-
223+
218224
private function transformToUploadUrl()
219225
{
220226
$base = $this->request->getBaseComponent();

0 commit comments

Comments
 (0)