old-RawContent.php

<?php

/*
 *
 *
 *
 *
 *
 *
 *   STOP
 *   This is being used by the Server component. At time of writing, required on line 71 of core/Server.php
 *
 *   DO NOT DELETE or MODIFY without testing
 *
 *
 *
 *
 *
 *
 */

namespace Lia\Compo;

/**
 * Routed files not otherwise handled will be sent, with the correct headers, including browser caching headers.  
 * Example: For file `public/funny/kitty.png`, url `/funny/kitty.png` will deliver the image file, as you would expect.  
 * Mimetypes (for headers) are derived from a PHP file internal to Liaison. It's not particularly efficient.  
 * 
 * @export(old.Route.rawFile)
 */
class oldRawContent { //extends \Lia\Compo {

    public $name;
    public function __construct($file){
        $this->file = $file;
        // $this->sendRawFile($file);
    }
    // public function onRoute_Display($event,$route){
    //     $file = $route->file;
    //     if (!file_exists($file))return;
    //     $ext = pathinfo($file,PATHINFO_EXTENSION);
    //     /**
    //      * RawContent sends event GetPreferredContentHandler, and if it returns null, then the raw file is sent.  
    //      * Your file handler must implement onGetPreferredContentHandler($event, $routeObject)
    //      * See [Router Docs](0-docs/Router.md)... or use [This link](0-docs/More.md) if the first one doesn't work. (not sure how I'm structuring my docs)
    //      * 
    //      * @export(old.Event.GetPreferredContentHandler)
    //      */
    //     $preferredHandler = $event->GetPreferredContentHandler($route);

    //     if ($preferredHandler==null)
    //         $this->sendRawFile($file);
        
    // }

    protected function sendRawFile($filePath){ 
        if (!is_file($filePath)){
            throw new \Exception("File '{$filePath}' does not exist");
        }

        $type = $this->getFileMimeType($filePath);

        if ($type===FALSE)throw new \Exception("Cannot determine file type from path '{$filePath}', so cannot send file headers or file.");
        $headers = apache_request_headers();

        $mtime = filemtime($filePath);
        if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $mtime) &&$mtime !==FALSE) {
            header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT', true, 304);
            header(('Content-type: '.$type));

        } else {
            header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT', true, 200);
            header(('Content-type: '.$type));
            header('Content-Length: '.filesize($filePath));
            header(('Cache-Control: max-age='.(60*60*24*30)));
            readfile($filePath);
        }
    }
    public function setHeaders($ext){
        $type = $this->getFileMimeType('f.'.$ext);
        header(('Content-type: '.$type));
    }
    protected function getFileMimeType($filePath){
        static $typeMap = NULL;
        $typeMap = $typeMap ?? require(__DIR__.'/../../code/file/mime_type_map.php');
        // var_dump($typeMap); 
        $ext = pathinfo($filePath,PATHINFO_EXTENSION);
        // if (!isset($typeMap['mimes'][$ext]))throw new \Exception("type not available for extension '{$ext}'");
        return $typeMap['mimes'][$ext][0] ?? false;
    }

    static public function extensionMimeType($extension){
        static $typeMap = NULL;
        $typeMap = $typeMap ?? require(__DIR__.'/../../code/file/mime_type_map.php');
        // var_dump($typeMap); 

        // if (!isset($typeMap['mimes'][$ext]))throw new \Exception("type not available for extension '{$ext}'");
        return $typeMap['mimes'][$extension][0] ?? false;
    }
}