Skip to content

Add getMimeType($ext) function in class Response#1153

Merged
walkor merged 5 commits intowalkor:masterfrom
joanhey:patch-2
Apr 29, 2026
Merged

Add getMimeType($ext) function in class Response#1153
walkor merged 5 commits intowalkor:masterfrom
joanhey:patch-2

Conversation

@joanhey
Copy link
Copy Markdown
Contributor

@joanhey joanhey commented Apr 20, 2026

I'm creating a static file class for Adapterman.
First I tried some code creating a StaticPreCompressed middleware for Webman (later I'll send it).
That check the Accept-Encoding to try to serve pre-compressed .br and .gz files first, and serve correct headers.

But always download the file, even with the Content-Type header, because the middleware add it, but we change the file to serve adding .br or .gz. And this code search for .br or .gz extension:
if (!isset($headers['Content-Disposition']) && !isset(self::$mimeTypeMap[$extension]))

So I refactored the code, and added the method for not repeat the code in the middleware again.

For easier use the function. Especially in middlewares.
@joanhey joanhey marked this pull request as draft April 20, 2026 22:29
@joanhey joanhey marked this pull request as ready for review April 20, 2026 22:42
@joanhey
Copy link
Copy Markdown
Contributor Author

joanhey commented Apr 27, 2026

Code example for compressed static files, than now always download without this change:

    #[Get('/static/{path}')]
    public function files($request, $path)
    {
        //return new Response()->withFile('/data/static/' . $path);

        //$response = $handler($request);

        $response = new Response();
        //$path = $request->path();
        $path = "/data/static/$path";
        // Access to files beginning with. Is prohibited
        if (str_contains($path, '/.')) {
            return response('<h1>403 forbidden</h1>', 403);
        }
        if (!is_file($path)) { // in middleware public_path().$path
            return response('<h1>404 Not Found</h1>', 404);
        }

        $headers = [];
        $ext = pathinfo($path, PATHINFO_EXTENSION);
        
        if($encoding = $request->header('accept-encoding', '')) {
            if(str_contains($encoding, 'br') && is_file("$path.br")) {
                $path .= '.br';
                $headers = ['Content-Encoding' => 'br',
                            'Vary' => 'Accept-Encoding'];

            } elseif (str_contains($encoding, 'gzip') && is_file("$path.gz")) {
                $path .= '.gz';
                $headers = ['Content-Encoding' => 'gzip',
                            'Vary' => 'Accept-Encoding'];
            }
            
            //$headers['Content-Type'] = $response::$mimeTypeMap[$ext];// ?? 'application/octet-stream';
            $headers['Content-Type'] = $response->getMime($ext);
        }

        //return response(var_dump($headers));
        // Add HTTP headers
        $response->withHeaders($headers
            //+ [
            //'Cache-Control'                    => 'max-age=31536000, immutable',
            //'Access-Control-Allow-Origin'      => '*',
            //'Access-Control-Allow-Credentials' => 'true',
            //]
            )
            ->file($path);
        
        return $response;

Test it in the controller, but we have ready a StaticPreCompressed.php midleware for Webman.

@walkor walkor merged commit 5ed2f9b into walkor:master Apr 29, 2026
24 checks passed
@joanhey joanhey deleted the patch-2 branch April 30, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants