raw download support
authorChristian Weiske <cweiske@cweiske.de>
Mon, 26 Mar 2012 19:59:07 +0000 (21:59 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 26 Mar 2012 19:59:07 +0000 (21:59 +0200)
src/Phorkie/File.php
src/Phorkie/Repository.php
www/.htaccess
www/raw.php [new file with mode: 0644]

index f12d837a9b111eeb771893af3d60f86b0042de27..14a7bbf335fbbeb907a99941b1a29eee1ba1df33 100644 (file)
@@ -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
      *
index ae01f6c6868479c074b9c5d049a7012adb4138c2..ebd420f431374abed39c96ffa1332078665ac82e 100644 (file)
@@ -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');
index f41e1bf393ed8ff59f2b9ecda0f58d61235a3eee..a57b34df48be7e7ae654a08b5767bc77d6a8682b 100644 (file)
@@ -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 (file)
index 0000000..9128770
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Displays a file
+ */
+namespace Phorkie;
+require_once 'www-header.php';
+$repo = new Repository();
+$repo->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);
+?>