perlmojoliciousmojolicious-lite

In mojolicious, how to protect images from public view


Somebody can help me, please. I have app in mojolicious Lite

I wanna block images from all without session login

when i type http://m.y.i.p:3000/images/imageX.jpg

i wanna show images only with session login.

my html code is

<a href="images/imagex.jpg">shwo image 1 </a>

Solution

  • Same way as any other content. Set up a handler for requests, render (or don't render) the content.

    get '/images/*img' => sub {
        my $c = shift;
        if (!$c->session("is_authenticated")) {
            return $c->render( text => "Forbidden", status => 403 );
        }
        my $file = $c->param("img");
        if (!open(my $fh, '<', $IMAGE_DIR/$file)) {
            return $c->render( text => "Not found", status => 404 );
        }
        my $data = do { local $/; <$fh> };
        close $fh;
        $c->render( data => $data, format => 'jpg' );
    };
    

    Your request handler will take priority over the default handler that serves content from public folders, but once you have this handler in place, you don't need to store the files it serves in a public folder.