Example #1
0
 /**
  * Uploads gzipped file
  *
  * @param string $local_path
  * @param string $remote_path
  * @param bool $force_rewrite
  * @return array
  */
 function _upload_gzip($local_path, $remote_path, $force_rewrite = false)
 {
     if (!function_exists('gzencode')) {
         return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, "GZIP library doesn't exists.");
     }
     if (!file_exists($local_path)) {
         return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Source file not found.');
     }
     $contents = @file_get_contents($local_path);
     if ($contents === false) {
         return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Unable to read file.');
     }
     $data = gzencode($contents);
     $md5 = md5($data);
     $content_md5 = $this->_get_content_md5($md5);
     if (!$force_rewrite) {
         try {
             $properties = $this->_client->getBlobProperties($this->_config['container'], $remote_path);
             $size = @filesize($local_path);
             if ($size === (int) $properties->Size && $content_md5 === $properties->ContentMd5) {
                 return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'File up-to-date.');
             }
         } catch (Exception $exception) {
         }
     }
     $headers = $this->_get_headers($local_path);
     $headers = array_merge($headers, array('Content-MD5' => $content_md5, 'Content-Encoding' => 'gzip'));
     try {
         $this->_client->putBlobData($this->_config['container'], $remote_path, $data, array(), null, $headers);
     } catch (Exception $exception) {
         return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf('Unable to put blob (%s).', $exception->getMessage()));
     }
     return $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
 }