/** * Saves a file to the default upload dir. * * @param String $file * @param String $filename * @param String $exr * @return String */ private function save_image($file, $filename = NULL, $ext = '.png') { // Generates a new filename if (!$filename) { $filename = Text::random(); // Generates a new file name if the file exists while (file_exists(realpath($this->config['ad']['upload_dir']) . DIRECTORY_SEPARATOR . $filename . $ext)) { $filename = Text::random(); } } // Saves image locally $saved_file = Upload::save($file, $filename . $ext, $this->config['ad']['upload_dir']); // Resize Image if ($saved_file) { $image = Image::factory(realpath($this->config['ad']['upload_dir']) . DIRECTORY_SEPARATOR . $filename . $ext); $image->resize($this->config['ad']['logo_max_size'], NULL, Image::INVERSE)->save(); $image->resize($this->config['ad']['logo_min_size'], NULL, Image::INVERSE)->save(realpath($this->config['ad']['upload_dir']) . DIRECTORY_SEPARATOR . $filename . '_thumb' . $ext); if ($this->config['global']['use_amazon'] == TRUE) { // Creates the S3 object $s3 = new Amazon_S3(); // Saves original image remotely $s3->upload(realpath($this->config['ad']['upload_dir']) . DIRECTORY_SEPARATOR . $filename . $ext, 'media/uploads/' . $filename . $ext, 'goworkat-static', array('Cache-Control' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Expires' => gmdate("D, d M Y H:i:s", strtotime("+10 years")))); // Saves thumbnail image remotely $s3->upload(realpath($this->config['ad']['upload_dir']) . DIRECTORY_SEPARATOR . $filename . '_thumb' . $ext, 'media/uploads/' . $filename . '_thumb' . $ext, 'goworkat-static', array('Cache-Control' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Expires' => gmdate("D, d M Y H:i:s", strtotime("+10 years")))); } // returns file without extension return $filename; } return false; }
/** * Get the S3 response * * @return object | false */ public function getResponse() { $query = ''; if (sizeof($this->parameters) > 0) { $query = substr($this->uri, -1) !== '?' ? '?' : '&'; foreach ($this->parameters as $var => $value) { if ($value == null || $value == '') { $query .= $var . '&'; } else { $query .= $var . '=' . $value . '&'; } } $query = substr($query, 0, -1); $this->uri .= $query; if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('logging', $this->parameters)) { $this->resource .= $query; } } $url = (Amazon_S3::$useSSL && extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri; //var_dump($this->bucket, $this->uri, $this->resource, $url); // Basic setup $curl = curl_init(); curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php'); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_URL, $url); // Headers $headers = array(); $amz = array(); foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } foreach ($this->headers as $header => $value) { if (strlen($value) > 0) { $headers[] = $header . ': ' . $value; } } // Collect AMZ headers for signature foreach ($this->amzHeaders as $header => $value) { if (strlen($value) > 0) { $amz[] = strToLower($header) . ':' . $value; } } // AMZ headers must be sorted (thanks Malone) if (sizeof($amz) > 0) { sort($amz); $amz = "\n" . implode("\n", $amz); } else { $amz = ''; } // Authorization string $headers[] = 'Authorization: ' . Amazon_S3::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback')); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Request types switch ($this->verb) { case 'GET': break; case 'PUT': if ($this->fp !== false) { curl_setopt($curl, CURLOPT_PUT, true); curl_setopt($curl, CURLOPT_INFILE, $this->fp); if ($this->size > 0) { curl_setopt($curl, CURLOPT_INFILESIZE, $this->size); } } elseif ($this->data !== false) { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); if ($this->size > 0) { curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size); } } else { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); } break; case 'HEAD': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); curl_setopt($curl, CURLOPT_NOBODY, true); break; case 'DELETE': curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; default: break; } // Execute, grab errors if (curl_exec($curl)) { $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); } else { $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource); } @curl_close($curl); // Parse body into XML if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) { $this->response->body = simplexml_load_string($this->response->body); // Grab S3 errors if (!in_array($this->response->code, array(200, 204)) && isset($this->response->body->Code, $this->response->body->Message)) { $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message); if (isset($this->response->body->Resource)) { $this->response->error['resource'] = (string) $this->response->body->Resource; } unset($this->response->body); } } // Clean up file resources if ($this->fp !== false && is_resource($this->fp)) { fclose($this->fp); } return $this->response; }
/** * This function will be responsible for deploying the static theme to * Amazon S3 Bucket */ public function action_index() { // Loads config $this->config = Kohana::$config->load('application'); // Creates the S3 object $s3 = new Amazon_S3(); // Get all files under media/ $files = $this->find_files($this->config['global']['base_path'] . '/media'); if (count($files) > 0) { echo "Uploading (" . count($files) . ") files...\n-----\n"; foreach ($files as $file) { // Keeps the original file name $original_file_name = $file; // Minify if (preg_match("/.*\\.(.*)\$/i", $file, $extension)) { if ($extension[1] == 'js' || $extension[1] == 'css') { echo "Compressing..."; /* Minify */ $file_minified = $this->minify($file, $extension[1]); if ($file_minified) { $file = $file_minified; } } } // Gzip exec("gzip -c -f " . $file . " > " . $file . ".gz"); $file .= ".gz"; // Send to Amazone echo "Sending {$file} ...\n"; $new_file = str_replace($this->config['global']['base_path'], "", $original_file_name); /** * Choose a mime/type */ $content_type = 'application/octet-stream'; if (preg_match("/\\.([^\\.]+)\$/i", $original_file_name, $matches)) { $extension = $matches[1]; switch ($extension) { case 'png': $content_type = 'image/png'; break; case 'jpg': $content_type = 'image/jpeg'; break; case 'jpeg': $content_type = 'image/jpeg'; break; case 'gif': $content_type = 'image/gif'; break; case 'css': $content_type = 'text/css'; break; case 'js': $content_type = 'application/x-javascript'; break; } } $s3->upload($file, trim($new_file, '/'), 'goworkat-static', array('Cache-Control' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Expires' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Content-Encoding' => 'gzip', 'Content-Type' => $content_type)); } echo "Completed.\n"; } else { echo "No files to upload under: " . $this->config['global']['base_path'] . '/media' . "\n"; } // Generates a merged file with the base JS files $js_merged_file = $this->config['global']['base_path'] . "/media/js/base.js"; if (file_exists($js_merged_file)) { unlink($js_merged_file); } foreach ($this->config['global']['js_files'] as $js_base_file) { exec("cat " . $this->config['global']['base_path'] . '/' . $js_base_file . ".temp-min >> " . $js_merged_file); } // Gzip exec("gzip -c -f " . $js_merged_file . " > " . $js_merged_file . ".gz"); $js_merged_file .= ".gz"; $s3->upload($js_merged_file, "media/js/base.js", 'goworkat-static', array('Cache-Control' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Expires' => gmdate("D, d M Y H:i:s", strtotime("+10 years")), 'Content-Encoding' => 'gzip', 'Content-Type' => 'application/x-javascript')); // Delete temp file exec("find " . $this->config['global']['base_path'] . "/media/ -name '*temp-min*' -exec rm -f {} \\;"); exec("find " . $this->config['global']['base_path'] . "/media/ -name '*.gz' -exec rm -f {} \\;"); die; }