<?php

namespace App\Controllers\GET;

use App\Controllers\Controller;

class StorageImageController extends Controller
{
    public function __construct($params)
    {
        parent::__construct($params);

        $this->check_auth();
    }

    public function init()
    {
        $path = get_path("/storage/image/" . $this->params["filename"]);

        if (!is_file_image($this->params["filename"]) && !is_file_media($this->params["filename"])) {
            http_response_code(404);
            exit;
        };

        if (file_exists($path)) {
            $mimeType = mime_content_type($path);

            if ($mimeType === false) {
                $mimeType = "image/jpeg";
            };

            if (is_file_media($this->params["filename"])) {
                $size = filesize($path);
                $fp = fopen($path, "rb");

                header("Content-Type: " . $mimeType);
                header("Accept-Ranges: bytes");

                $start = 0;
                $end = $size - 1;
                $length = $size;

                if (isset($_SERVER["HTTP_RANGE"])) {
                    preg_match("/bytes=(\d+)-(\d+)?/", $_SERVER["HTTP_RANGE"], $matches);
                    $start = intval($matches[1]);
                    $end = isset($matches[2]) ? intval($matches[2]) : $size - 1;
                    $length = $end - $start + 1;

                    header("HTTP/1.1 206 Partial Content");
                    header("Content-Range: bytes $start-$end/$size");
                }

                header("Content-Length: " . $length);
                fseek($fp, $start);
                echo fread($fp, $length);
                fclose($fp);
            } else {
                header("Content-Type: " . $mimeType);
                readfile($path);
            }
        } else {
            http_response_code(404);
            header("Content-Type: image/svg+xml");
            echo <<<SVG
            <svg width="128" height="128" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#prefix__clip0_110_302)"><path d="M128 0H0v128h128V0z" fill="#EFEFEF"/><path d="M52.699 59.21v-.87l3.835-6.067h.63v1.346h-.425l-2.898 4.585v.069h5.165v.937h-6.307zM56.807 61v-8.727h1.005V61h-1.005zm6.642.12c-.642 0-1.189-.175-1.64-.525-.452-.352-.797-.862-1.036-1.53-.239-.67-.358-1.48-.358-2.429 0-.943.12-1.748.358-2.416.242-.67.588-1.182 1.04-1.534.454-.355 1-.533 1.636-.533.637 0 1.18.178 1.632.533.455.352.801.864 1.04 1.534.242.668.362 1.473.362 2.416 0 .95-.119 1.759-.358 2.43-.238.667-.584 1.177-1.035 1.53-.452.349-.999.523-1.64.523zm0-.938c.637 0 1.13-.307 1.483-.92.352-.614.529-1.49.529-2.626 0-.755-.081-1.399-.243-1.93-.16-.531-.39-.936-.69-1.215a1.524 1.524 0 00-1.079-.417c-.63 0-1.123.31-1.478.933-.356.62-.533 1.496-.533 2.63 0 .755.08 1.397.238 1.925.16.529.388.93.687 1.206.3.276.663.414 1.086.414zm4.45-.972v-.87l3.834-6.067h.631v1.346h-.426l-2.898 4.585v.069h5.165v.937h-6.307zM72.005 61v-8.727h1.006V61h-1.006zM17.114 67.273V76h-1.057v-8.727h1.057zM19.096 76v-6.546h.972v1.023h.085c.136-.35.357-.62.66-.814.305-.196.67-.294 1.096-.294.432 0 .79.098 1.078.294.29.194.516.465.677.814h.069c.167-.338.419-.606.754-.805.335-.202.737-.303 1.206-.303.585 0 1.064.184 1.436.55.372.364.558.93.558 1.7V76h-1.006v-4.38c0-.484-.132-.829-.396-1.036a1.467 1.467 0 00-.933-.311c-.46 0-.817.139-1.07.417-.253.276-.379.625-.379 1.049V76H22.88v-4.483c0-.372-.12-.672-.362-.9-.241-.23-.552-.344-.933-.344-.262 0-.506.07-.733.209-.224.139-.406.332-.546.58a1.71 1.71 0 00-.204.847V76h-1.006zm12.356.153a2.66 2.66 0 01-1.13-.234 1.939 1.939 0 01-.805-.686c-.198-.301-.298-.665-.298-1.091 0-.375.074-.679.222-.912.147-.236.345-.42.592-.554.247-.133.52-.233.818-.298.301-.068.604-.122.908-.162.398-.051.72-.09.967-.115.25-.029.432-.075.546-.14.116-.066.174-.18.174-.342v-.034c0-.42-.115-.747-.345-.98-.227-.233-.572-.35-1.035-.35-.48 0-.857.106-1.13.316-.272.21-.464.435-.575.673l-.954-.34c.17-.398.397-.708.681-.93.287-.224.6-.38.938-.468.34-.091.676-.137 1.006-.137.21 0 .451.026.724.077.276.048.541.15.797.303.258.153.473.385.643.694.17.31.256.725.256 1.245V76h-1.006v-.886h-.05a1.767 1.767 0 01-.342.456c-.159.162-.37.3-.635.413-.264.114-.586.17-.967.17zm.153-.903c.398 0 .733-.078 1.006-.234a1.595 1.595 0 00.835-1.385v-.92c-.042.05-.136.097-.28.14-.143.04-.308.075-.495.106a22.303 22.303 0 01-.963.128c-.262.034-.506.09-.733.166a1.264 1.264 0 00-.546.337c-.136.148-.204.35-.204.605 0 .35.129.614.388.793.26.176.592.264.992.264zm7.325 3.34c-.486 0-.904-.062-1.253-.187a2.659 2.659 0 01-.874-.485 2.655 2.655 0 01-.55-.64l.802-.562c.09.12.206.256.345.409.14.156.33.291.57.405.245.116.565.174.96.174.528 0 .964-.127 1.308-.383.344-.256.516-.656.516-1.202v-1.33h-.086c-.074.12-.179.268-.315.444a1.788 1.788 0 01-.58.464c-.25.134-.588.2-1.014.2a2.731 2.731 0 01-1.423-.374 2.631 2.631 0 01-.993-1.091c-.241-.478-.362-1.057-.362-1.739 0-.67.118-1.254.353-1.751.236-.5.564-.887.985-1.16.42-.275.906-.413 1.457-.413.426 0 .764.071 1.014.213.253.14.447.299.58.478.136.176.242.32.315.434h.103v-1.04h.971v6.734c0 .562-.128 1.02-.383 1.372a2.184 2.184 0 01-1.023.78 3.86 3.86 0 01-1.423.25zm-.034-3.596c.403 0 .744-.092 1.022-.277.279-.184.49-.45.635-.796.145-.347.218-.762.218-1.245a3.39 3.39 0 00-.214-1.248 1.873 1.873 0 00-.63-.849c-.279-.204-.622-.306-1.031-.306-.427 0-.782.108-1.066.324a1.972 1.972 0 00-.635.869 3.36 3.36 0 00-.209 1.21c0 .455.072.857.214 1.206.144.347.357.62.639.818.284.196.636.294 1.057.294zm7.448 1.142c-.63 0-1.174-.139-1.632-.417a2.803 2.803 0 01-1.052-1.176c-.244-.506-.367-1.094-.367-1.765 0-.67.123-1.261.367-1.772a2.89 2.89 0 011.031-1.202c.443-.29.96-.435 1.551-.435.341 0 .678.057 1.01.17.332.114.635.3.908.555.273.253.49.588.652 1.005.162.418.243.932.243 1.543v.426h-5.046v-.87h4.023c0-.368-.074-.698-.222-.988a1.67 1.67 0 00-.622-.686 1.742 1.742 0 00-.946-.251c-.4 0-.747.1-1.04.298-.29.196-.512.452-.669.767a2.253 2.253 0 00-.234 1.014v.58c0 .494.085.913.256 1.257.173.34.413.6.72.78a2.11 2.11 0 001.07.264c.264 0 .502-.037.715-.11.216-.078.402-.191.559-.342.156-.153.277-.344.362-.57l.971.272c-.102.33-.274.62-.515.87-.242.246-.54.44-.895.579a3.318 3.318 0 01-1.198.204zm14.672-8.863V76h-1.022l-4.756-6.852h-.085V76h-1.057v-8.727h1.023l4.772 6.869h.086v-6.87h1.04zm4.638 8.863c-.591 0-1.11-.14-1.556-.421a2.866 2.866 0 01-1.04-1.18c-.247-.507-.37-1.097-.37-1.774 0-.681.123-1.277.37-1.785a2.86 2.86 0 011.04-1.185c.446-.281.965-.422 1.556-.422.59 0 1.108.14 1.55.422.447.281.794.676 1.04 1.185.25.508.376 1.103.376 1.785 0 .677-.125 1.267-.375 1.773-.248.506-.594.9-1.04 1.18-.443.282-.96.422-1.551.422zm0-.903c.449 0 .818-.115 1.108-.345.29-.23.504-.533.643-.908.14-.375.209-.781.209-1.219 0-.437-.07-.845-.209-1.223a2.06 2.06 0 00-.643-.916c-.29-.233-.66-.35-1.108-.35-.45 0-.818.117-1.108.35a2.06 2.06 0 00-.644.916 3.51 3.51 0 00-.209 1.223c0 .438.07.844.21 1.22.139.374.353.677.643.907.29.23.659.345 1.108.345zM73 69.454v.853H69.61v-.853h3.392zm-2.403-1.568h1.006v6.239c0 .284.04.497.123.64a.64.64 0 00.324.28c.134.046.274.069.422.069.11 0 .202-.006.273-.017l.17-.034.205.903a2.042 2.042 0 01-.286.077 2.102 2.102 0 01-.464.042c-.284 0-.563-.06-.835-.183a1.66 1.66 0 01-.674-.558c-.176-.25-.264-.566-.264-.946v-6.512zM78.026 76v-8.727h5.233v.937h-4.177v2.95h3.785v.937h-3.785V76h-1.056zm9.085.136c-.591 0-1.11-.14-1.556-.421a2.866 2.866 0 01-1.04-1.18c-.246-.507-.37-1.097-.37-1.774 0-.681.124-1.277.37-1.785a2.86 2.86 0 011.04-1.185c.446-.281.965-.422 1.556-.422.59 0 1.108.14 1.55.422a2.82 2.82 0 011.04 1.185c.25.508.376 1.103.376 1.785 0 .677-.125 1.267-.375 1.773-.248.506-.594.9-1.04 1.18-.443.282-.96.422-1.551.422zm0-.903c.449 0 .818-.115 1.108-.345.29-.23.504-.533.643-.908.14-.375.209-.781.209-1.219 0-.437-.07-.845-.209-1.223a2.061 2.061 0 00-.643-.916c-.29-.233-.66-.35-1.108-.35-.45 0-.818.117-1.108.35-.29.233-.504.538-.644.916a3.51 3.51 0 00-.208 1.223c0 .438.07.844.208 1.22.14.374.354.677.644.907.29.23.659.345 1.108.345zm8.626-1.91v-3.868h1.006V76h-1.006v-1.108h-.068a2.148 2.148 0 01-.716.848c-.324.23-.733.345-1.228.345-.409 0-.772-.09-1.09-.268a1.88 1.88 0 01-.75-.818c-.182-.367-.273-.828-.273-1.385v-4.16h1.006v4.092c0 .477.133.857.4 1.141.27.285.614.427 1.031.427.25 0 .505-.064.763-.192.261-.128.48-.324.656-.588.18-.264.269-.601.269-1.01zm3.853-1.26V76h-1.005v-6.546h.971v1.023h.085c.154-.332.387-.6.699-.8.313-.205.716-.308 1.21-.308.444 0 .831.091 1.164.273.332.179.591.452.775.818.185.364.277.824.277 1.38V76h-1.005v-4.09c0-.515-.134-.916-.401-1.203-.267-.29-.633-.434-1.099-.434-.321 0-.608.07-.861.209-.25.139-.448.342-.593.609-.144.267-.217.59-.217.972zm8.486 4.073a2.563 2.563 0 01-1.445-.413c-.418-.278-.744-.67-.98-1.176-.236-.509-.354-1.11-.354-1.803 0-.687.118-1.284.354-1.79.236-.505.564-.896.984-1.171.421-.276.907-.414 1.458-.414.426 0 .762.071 1.01.213.25.14.44.299.571.478.133.176.237.32.311.434h.085v-3.221h1.006V76h-.972v-1.006h-.119c-.074.12-.179.27-.316.452-.136.179-.331.34-.583.482-.253.139-.59.208-1.01.208zm.136-.903c.403 0 .744-.105 1.023-.315a1.98 1.98 0 00.635-.883c.145-.377.217-.813.217-1.308 0-.488-.071-.916-.213-1.282a1.863 1.863 0 00-.631-.861c-.278-.208-.622-.311-1.031-.311-.426 0-.781.11-1.065.328-.282.216-.493.51-.635.882a3.51 3.51 0 00-.209 1.244c0 .466.071.89.213 1.27.145.378.358.68.639.904.284.221.637.332 1.057.332z" fill="#AAA"/></g><defs><clipPath id="prefix__clip0_110_302"><path fill="#fff" d="M0 0h128v128H0z"/></clipPath></defs></svg>
            SVG;
        };
    }
}
