Created
December 2, 2010 10:15
-
-
Save birkir/725079 to your computer and use it in GitHub Desktop.
Media Controller for my lovely Media system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Handles all media the website will ever have to need. It minifies all | |
* javascript and stylesheets. Then it compresses every file to gzip and outputs | |
* it to the browser, if he accepts it. | |
* | |
* @package OpenTorrent | |
* @category Controllers | |
* @author Birkir R Gudjonsson <[email protected]> | |
* @copyright Copyright (c) 2010, Birkir R Gudjonsson | |
*/ | |
class Controller_Media extends Controller { | |
// Add cache headers to client | |
private $cache = TRUE; | |
// Directory to keep media | |
protected $_directory = 'media/'; | |
/** | |
* | |
*/ | |
public function action_index() | |
{ | |
$this->request->response = '404 File not found'; | |
} | |
/** | |
* | |
* @param string $file Filename | |
*/ | |
public function action_img($file = NULL) | |
{ | |
$file = APPPATH.$this->_directory.$this->request->action.'/'.$file; | |
$this->passthru($file); | |
} | |
public function action_css($file = NULL) | |
{ | |
$file = APPPATH.$this->_directory.$this->request->action.'/'.$file; | |
$file = $this->minify($file, 'css'); | |
$file = $this->gzip($file); | |
$this->passthru($file, true); | |
} | |
public function action_js($file = NULL) | |
{ | |
$file = APPPATH.$this->_directory.$this->request->action.'/'.$file; | |
if (substr($file,-7) == '.min.js') | |
{ | |
$file = $this->gzip($file); | |
$this->passthru($file, true); | |
return false; | |
} | |
$file = $this->minify($file, 'js'); | |
$file = $this->gzip($file); | |
$this->passthru($file, true); | |
} | |
public function minify($file = NULL, $type = 'css') | |
{ | |
if ( ! is_file($file)) | |
{ | |
throw new Kohana_Exception('File does not exist'); | |
} | |
$this->minified = APPPATH.'cache/minify/'.md5_file($file); | |
$file_content = file_get_contents($file); | |
$min_content = Minify::factory($type)->set($file_content)->min(); | |
$fh = fopen($this->minified, 'w+'); | |
fwrite($fh, $min_content); | |
fclose($fh); | |
return $this->minified; | |
} | |
public function passthru($file = NULL, $gzip = FALSE) | |
{ | |
if ( ! is_file($file)) | |
{ | |
throw new Kohana_Exception('File does not exist'); | |
} | |
$headers = array(); | |
if ($this->request->accept_encoding('gzip') AND $gzip == TRUE) | |
{ | |
$headers['Content-Encoding'] = 'gzip'; | |
} | |
$headers['Content-Type'] = File::mime_by_ext($file); | |
$headers['Content-length'] = filesize($file); | |
$headers['Last-Modified'] = date('r', filemtime($file)); | |
if ($this->cache == TRUE) | |
{ | |
$headers['Cache-Control'] = 'must-revalidate'; | |
$headers['Expires'] = gmdate("D, d M Y H:i:s", time() + (86400 * 3)).' GMT'; | |
} | |
$this->request->headers += $headers; | |
$this->request->send_headers(); | |
$image = @fopen($file, 'rb'); | |
if ($image) | |
{ | |
fpassthru($image); | |
exit; | |
} | |
} | |
/** | |
* GZip the file and return cached version | |
* | |
* @param string File path | |
* @return string Cached version | |
*/ | |
public function gzip($file = NULL) | |
{ | |
$this->cached = APPPATH.'cache/media/'.md5_file($file); | |
if ( ! is_file($file)) | |
{ | |
throw new Kohana_Exception('File does not exist'); | |
} | |
$fh = fopen($this->cached, 'w+'); | |
fwrite($fh, gzencode(file_get_contents($file))); | |
fclose($fh); | |
return $this->cached; | |
} | |
} // End Controller_Media |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment