From e3757ad55ac7f161d4de11e38fb57166a8ee85f7 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 26 Mar 2012 21:59:07 +0200 Subject: [PATCH] raw download support --- src/Phorkie/File.php | 18 ++++++++++++++++++ src/Phorkie/Repository.php | 13 +++++++++++++ www/.htaccess | 1 + www/raw.php | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 www/raw.php diff --git a/src/Phorkie/File.php b/src/Phorkie/File.php index f12d837..14a7bbf 100644 --- a/src/Phorkie/File.php +++ b/src/Phorkie/File.php @@ -17,6 +17,15 @@ class File */ public $repo; + public static $arMimeTypeMap = array( + 'css' => 'text/css', + 'htm' => 'text/html', + 'html' => 'text/html', + 'js' => 'application/javascript', + 'php' => 'text/x-php', + 'txt' => 'text/plain', + ); + public function __construct($path, Repository $repo) { $this->path = $path; @@ -48,6 +57,15 @@ class File return file_get_contents($this->path); } + public function getMimeType() + { + $type = $this->getType(); + if (!isset(static::$arMimeTypeMap[$type])) { + return null; + } + return static::$arMimeTypeMap[$type]; + } + /** * Get a link to the file * diff --git a/src/Phorkie/Repository.php b/src/Phorkie/Repository.php index ae01f6c..ebd420f 100644 --- a/src/Phorkie/Repository.php +++ b/src/Phorkie/Repository.php @@ -57,6 +57,19 @@ class Repository return $arFiles; } + public function getFileByName($name) + { + $base = basename($name); + if ($base != $name) { + throw new Exception('No directories supported for now'); + } + $path = $this->repoDir . '/' . $base; + if (!is_readable($path)) { + throw new Exception_Input('File does not exist'); + } + return new File($path, $this); + } + public function getDescription() { return file_get_contents($this->repoDir . '/.git/description'); diff --git a/www/.htaccess b/www/.htaccess index f41e1bf..a57b34d 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -3,3 +3,4 @@ RewriteBase / #RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^([0-9]+)$ /display.php?id=$1 +RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2 diff --git a/www/raw.php b/www/raw.php new file mode 100644 index 0000000..9128770 --- /dev/null +++ b/www/raw.php @@ -0,0 +1,21 @@ +loadFromRequest(); + +if (!isset($_GET['file']) || $_GET['file'] == '') { + throw new Exception_Input('File name missing'); +} + +$file = $repo->getFileByName($_GET['file']); +$mimetype = $file->getMimeType(); +if ($mimetype === null) { + $mimetype = 'text/plain'; +} +header('Content-Type: ' . $mimetype); +readfile($file->path); +?> -- 2.30.2