divineniiquaye/flight-routing

View on GitHub
src/Handlers/FileHandler.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

/*
 * This file is part of Flight Routing.
 *
 * PHP version 8.0 and above required
 *
 * @author    Divine Niiquaye Ibok <divineibok@gmail.com>
 * @copyright 2019 Divine Niiquaye Ibok (https://divinenii.com/)
 * @license   https://opensource.org/licenses/BSD-3-Clause License
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Flight\Routing\Handlers;

use Flight\Routing\Exceptions\InvalidControllerException;
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface};

/**
 * Returns the contents from a file.
 *
 * @author Divine Niiquaye Ibok <divineibok@gmail.com>
 */
final class FileHandler
{
    public const MIME_TYPE = [
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'text/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',
        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',
        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',
        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',
        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',
        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',
        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    ];

    public function __construct(private string $filename, private ?string $mimeType = null)
    {
    }

    public function __invoke(ResponseFactoryInterface $factory): ResponseInterface
    {
        if (!\file_exists($view = $this->filename) || !$contents = \file_get_contents($view)) {
            throw new InvalidControllerException(\sprintf('Failed to fetch contents from file "%s"', $view));
        }

        if (empty($mime = $this->mimeType ?? self::MIME_TYPE[\pathinfo($view, \PATHINFO_EXTENSION)] ?? null)) {
            $mime = (new \finfo(\FILEINFO_MIME_TYPE))->file($view); // @codeCoverageIgnoreStart

            if (false === $mime) {
                throw new InvalidControllerException(\sprintf('Failed to detect mime type of file "%s"', $view));
            } // @codeCoverageIgnoreEnd
        }

        $response = $factory->createResponse()->withHeader('Content-Type', $mime);
        $response->getBody()->write($contents);

        return $response;
    }
}