Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplifying the route by moving the UID to the beginning of the route

  • Loading branch information...
commit cd61683aa56b185a78fe86ff2a2e7d7596cb7522 1 parent 5c9ee74
@Zeelot Zeelot authored
Showing with 15 additions and 19 deletions.
  1. +9 −6 classes/controller/media.php
  2. +6 −13 config/media.php
View
15 classes/controller/media.php
@@ -13,23 +13,26 @@ public function before()
public function action_serve()
{
- $file = $this->request->param('file');
- $sep = $this->request->param('sep');
+ $filepath = $this->request->param('filepath');
$uid = $this->request->param('uid');
- $ext = $this->request->param('ext');
- $cfs_file = Kohana::find_file('media', $file, $ext);
+ $cfs_file = Kohana::find_file('media', $filepath, FALSE);
if ( ! $cfs_file)
throw new Kohana_Http_Exception_404;
+ $this->request->check_cache(sha1($this->request->uri()).filemtime($cfs_file));
+
// Send the file content as the response
$this->response->body(file_get_contents($cfs_file));
if ($this->config->cache)
{
// Save the contents to the public directory for future requests
- $public = $this->config->public_dir.'/'.$file.$sep.$uid.'.'.$ext;
+ $public = strtr($this->config->public_dir, array(
+ '<uid>/' => $uid ? $uid.'/' : '',
+ '<filename' => $filepath,
+ ));
$directory = dirname($public);
if ( ! is_dir($directory))
@@ -42,7 +45,7 @@ public function action_serve()
}
// Set the proper headers to allow caching
- $this->response->headers('Content-Type', (string) File::mime_by_ext($ext));
+ $this->response->headers('Content-Type', (string) File::mime_by_ext(pathinfo($cfs_file, PATHINFO_EXTENSION)));
$this->response->headers('Content-Length', (string) filesize($cfs_file));
$this->response->headers('Last-Modified', (string) date('r', filemtime($cfs_file)));
}
View
19 config/media.php
@@ -1,24 +1,17 @@
<?php defined('SYSPATH') or die('No direct script access.');
return array(
- 'route' => 'media/ko/<file>(<sep><uid>).<ext>',
+ 'route' => 'media/(<uid>/)kohana/<filepath>',
'regex' => array(
/**
- * Pattern to match the file path (without extension)
- * This pattern will match any file path until one of the following:
- * - an extension is found
- * - a forward slash is found, followed by a version number (#.#.#) where # is one or more digits
+ * Pattern to match the file path
*/
- 'file' => '(.*?)((?=(\.([a-zA-Z0-9]+)$))|(?=\/(?=([0-9]+\.){3})))',
- // Match the separator between file and hash
- 'sep' => '([\/])(?=([0-9]+\.){3})',
+ 'filepath' => '.*',
// Match the unique string that is not part of the media file
- 'uid' => '([a-zA-Z0-9\.])+(?=[\.][a-zA-Z0-9]+$)',
- // Match the file extension (without the dot)
- 'ext' => '([a-zA-Z0-9]+)$',
+ 'uid' => '.*?',
),
- // The public accessible directory
- 'public_dir' => DOCROOT.'media/ko',
+ // The public accessible directory where the file will be copied
+ 'public_dir' => DOCROOT.'media/<uid>/kohana/<filepath>',
// Write the files to the public directory when in production
'cache' => Kohana::$environment === Kohana::PRODUCTION,
);
Please sign in to comment.
Something went wrong with that request. Please try again.