add redirect config option
authorChristian Weiske <cweiske@cweiske.de>
Tue, 8 Apr 2014 15:58:20 +0000 (17:58 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 8 Apr 2014 15:58:20 +0000 (17:58 +0200)
src/phancap/Config.php
src/phancap/Image.php
www/get.php

index 4d7c7a0f75f7e11272b5313eb97710757311b639..1bb43796dbc61a8db4ffff76fd2e00dd0da7904c 100644 (file)
@@ -29,6 +29,16 @@ class Config
      */
     public $access = true;
 
+    /**
+     * Redirect the browser to the cache URL.
+     * If disabled, the file is directly delivered.
+     *
+     * Helpful for debugging since it does not change the browser's URL.
+     *
+     * @var boolean
+     */
+    public $redirect = true;
+
     /**
      * How long requests with an old timestamp may be used.
      * 2 days default.
index 6750cfc9fba0b0fbe48bf58e536a33d75fe98010..ab3868e18f236b8d14c2c25331fe1690e3971b1c 100644 (file)
@@ -12,6 +12,19 @@ class Image
         $this->name = $name;
     }
 
+    public function getMimeType()
+    {
+        $ext = substr($this->name, -4);
+        if ($ext == '.jpg') {
+            return 'image/jpeg';
+        } else if ($ext == '.png') {
+            return 'image/png';
+        } else  if ($ext == '.png') {
+            return 'application/pdf';
+        }
+        return 'application/octet-stream';
+    }
+
     public function getPath()
     {
         return $this->config->cacheDir . $this->name;
index 1da3e8a9de4b77722101196ff4a6d719972205a5..15463e43359cdd3140343dcc49b9eaf758ea518a 100644 (file)
@@ -40,8 +40,13 @@ $rep = new Repository();
 $rep->setConfig($config);
 try {
     $img = $rep->getImage($options);
-    header('HTTP/1.0 302 Found');
-    header('Location: ' . $img->getUrl());
+    if ($config->redirect) {
+        header('HTTP/1.0 302 Found');
+        header('Location: ' . $img->getUrl());
+    } else {
+        header('Content-type: ' . $img->getMimeType());
+        readfile($img->getPath());
+    }
 } catch (\Exception $e) {
     //FIXME: handle 404s and so properly
     header('HTTP/1.0 500 Internal Server error');