Google_MediaFileUpload

The Google Drive WP Media Google MediaFileUpload class.

Defined (1)

The class is defined in the following location(s).

/gdwpm-api/service/Google_MediaFileUpload.php  
  1. class Google_MediaFileUpload { 
  2. const UPLOAD_MEDIA_TYPE = 'media'; 
  3. const UPLOAD_MULTIPART_TYPE = 'multipart'; 
  4. const UPLOAD_RESUMABLE_TYPE = 'resumable'; 
  5.  
  6. /** @var string $mimeType */ 
  7. public $mimeType; 
  8.  
  9. /** @var string $data */ 
  10. public $data; 
  11.  
  12. /** @var bool $resumable */ 
  13. public $resumable; 
  14.  
  15. /** @var int $chunkSize */ 
  16. public $chunkSize; 
  17.  
  18. /** @var int $size */ 
  19. public $size; 
  20.  
  21. /** @var string $resumeUri */ 
  22. public $resumeUri; 
  23.  
  24. /** @var int $progress */ 
  25. public $progress; 
  26.  
  27. /** 
  28. * @param $mimeType string 
  29. * @param $data string The bytes you want to upload. 
  30. * @param $resumable bool 
  31. * @param bool $chunkSize File will be uploaded in chunks of this many bytes. 
  32. * only used if resumable=True 
  33. */ 
  34. public function __construct($mimeType, $data, $resumable=false, $chunkSize=false) { 
  35. $this->mimeType = $mimeType; 
  36. $this->data = $data; 
  37. $this->size = strlen($this->data); 
  38. $this->resumable = $resumable; 
  39. if(!$chunkSize) { 
  40. $chunkSize = 256 * 1024; 
  41. $this->chunkSize = $chunkSize; 
  42. $this->progress = 0; 
  43.  
  44. public function setFileSize($size) { 
  45. $this->size = $size; 
  46.  
  47. /** 
  48. * @static 
  49. * @param $meta 
  50. * @param $params 
  51. * @return array|bool 
  52. */ 
  53. public static function process($meta, &$params) { 
  54. $payload = array(); 
  55. $meta = is_string($meta) ? json_decode($meta, true) : $meta; 
  56. $uploadType = self::getUploadType($meta, $payload, $params); 
  57. if (!$uploadType) { 
  58. // Process as a normal API request. 
  59. return false; 
  60.  
  61. // Process as a media upload request. 
  62. $params['uploadType'] = array( 
  63. 'type' => 'string',  
  64. 'location' => 'query',  
  65. 'value' => $uploadType,  
  66. ); 
  67.  
  68. $mimeType = isset($params['mimeType']) 
  69. ? $params['mimeType']['value'] 
  70. : false; 
  71. unset($params['mimeType']); 
  72.  
  73. if (!$mimeType) { 
  74. $mimeType = $payload['content-type']; 
  75.  
  76. if (isset($params['file'])) { 
  77. // This is a standard file upload with curl. 
  78. $file = $params['file']['value']; 
  79. unset($params['file']); 
  80. return self::processFileUpload($file, $mimeType); 
  81.  
  82. $data = isset($params['data']) 
  83. ? $params['data']['value'] 
  84. : false; 
  85. unset($params['data']); 
  86.  
  87. if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { 
  88. $payload['content-type'] = $mimeType; 
  89. $payload['postBody'] = is_string($meta) ? $meta : json_encode($meta); 
  90.  
  91. } elseif (self::UPLOAD_MEDIA_TYPE == $uploadType) { 
  92. // This is a simple media upload. 
  93. $payload['content-type'] = $mimeType; 
  94. $payload['postBody'] = $data; 
  95.  
  96. elseif (self::UPLOAD_MULTIPART_TYPE == $uploadType) { 
  97. // This is a multipart/related upload. 
  98. $boundary = isset($params['boundary']['value']) ? $params['boundary']['value'] : mt_rand(); 
  99. $boundary = str_replace('"', '', $boundary); 
  100. $payload['content-type'] = 'multipart/related; boundary=' . $boundary; 
  101. $related = "--$boundary\r\n"; 
  102. $related .= "Content-Type: application/json; charset=UTF-8\r\n"; 
  103. $related .= "\r\n" . json_encode($meta) . "\r\n"; 
  104. $related .= "--$boundary\r\n"; 
  105. $related .= "Content-Type: $mimeType\r\n"; 
  106. $related .= "Content-Transfer-Encoding: base64\r\n"; 
  107. $related .= "\r\n" . base64_encode($data) . "\r\n"; 
  108. $related .= "--$boundary--"; 
  109. $payload['postBody'] = $related; 
  110.  
  111. return $payload; 
  112.  
  113. /** 
  114. * Prepares a standard file upload via cURL. 
  115. * @param $file 
  116. * @param $mime 
  117. * @return array Includes the processed file name. 
  118. * @visible For testing. 
  119. */ 
  120. public static function processFileUpload($file, $mime) { 
  121. if (!$file) return array(); 
  122. if (substr($file, 0, 1) != '@') { 
  123. $file = '@' . $file; 
  124.  
  125. // This is a standard file upload with curl. 
  126. $params = array('postBody' => array('file' => $file)); 
  127. if ($mime) { 
  128. $params['content-type'] = $mime; 
  129.  
  130. return $params; 
  131.  
  132. /** 
  133. * Valid upload types: 
  134. * - resumable (UPLOAD_RESUMABLE_TYPE) 
  135. * - media (UPLOAD_MEDIA_TYPE) 
  136. * - multipart (UPLOAD_MULTIPART_TYPE) 
  137. * - none (false) 
  138. * @param $meta 
  139. * @param $payload 
  140. * @param $params 
  141. * @return bool|string 
  142. */ 
  143. public static function getUploadType($meta, &$payload, &$params) { 
  144. if (isset($params['mediaUpload']) 
  145. && get_class($params['mediaUpload']['value']) == 'Google_MediaFileUpload') { 
  146. $upload = $params['mediaUpload']['value']; 
  147. unset($params['mediaUpload']); 
  148. $payload['content-type'] = $upload->mimeType; 
  149. if (isset($upload->resumable) && $upload->resumable) { 
  150. return self::UPLOAD_RESUMABLE_TYPE; 
  151.  
  152. // Allow the developer to override the upload type. 
  153. if (isset($params['uploadType'])) { 
  154. return $params['uploadType']['value']; 
  155.  
  156. $data = isset($params['data']['value']) 
  157. ? $params['data']['value'] : false; 
  158.  
  159. if (false == $data && false == isset($params['file'])) { 
  160. // No upload data available. 
  161. return false; 
  162.  
  163. if (isset($params['file'])) { 
  164. return self::UPLOAD_MEDIA_TYPE; 
  165.  
  166. if (false == $meta) { 
  167. return self::UPLOAD_MEDIA_TYPE; 
  168.  
  169. return self::UPLOAD_MULTIPART_TYPE; 
  170.  
  171.  
  172. public function nextChunk(Google_HttpRequest $req, $chunk=false) { 
  173. if (false == $this->resumeUri) { 
  174. $this->resumeUri = $this->getResumeUri($req); 
  175.  
  176. if (false == $chunk) { 
  177. $chunk = substr($this->data, $this->progress, $this->chunkSize); 
  178.  
  179. $lastBytePos = $this->progress + strlen($chunk) - 1; 
  180. $headers = array( 
  181. 'content-range' => "bytes $this->progress-$lastBytePos/$this->size",  
  182. 'content-type' => $req->getRequestHeader('content-type'),  
  183. 'content-length' => $this->chunkSize,  
  184. 'expect' => '',  
  185. ); 
  186.  
  187. $httpRequest = new Google_HttpRequest($this->resumeUri, 'PUT', $headers, $chunk); 
  188. $response = Google_Client::$io->authenticatedRequest($httpRequest); 
  189. $code = $response->getResponseHttpCode(); 
  190. if (308 == $code) { 
  191. $range = explode('-', $response->getResponseHeader('range')); 
  192. $this->progress = $range[1] + 1; 
  193. return false; 
  194. } else { 
  195. return Google_REST::decodeHttpResponse($response); 
  196.  
  197. private function getResumeUri(Google_HttpRequest $httpRequest) { 
  198. $result = null; 
  199. $body = $httpRequest->getPostBody(); 
  200. if ($body) { 
  201. $httpRequest->setRequestHeaders(array( 
  202. 'content-type' => 'application/json; charset=UTF-8',  
  203. 'content-length' => Google_Utils::getStrLen($body),  
  204. 'x-upload-content-type' => $this->mimeType,  
  205. 'x-upload-content-length' => $this->size,  
  206. 'expect' => '',  
  207. )); 
  208.  
  209. $response = Google_Client::$io->makeRequest($httpRequest); 
  210. $location = $response->getResponseHeader('location'); 
  211. $code = $response->getResponseHttpCode(); 
  212. if (200 == $code && true == $location) { 
  213. return $location; 
  214. throw new Google_Exception("Failed to start the resumable upload");