/** * 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; }
/** * 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; }