在Mojolicious中,如何保护图像不公开显示

时间:2018-07-20 20:34:23

标签: perl mojolicious mojolicious-lite

请有人可以帮助我。我在mojolicious Lite中有应用程序

我想在没有会话登录的情况下阻止所有图像

当我输入http://m.y.i.p:3000/images/imageX.jpg

我只想通过会话登录显示图像。

我的html代码是

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

2 个答案:

答案 0 :(得分:3)

与其他任何内容相同。为请求设置处理程序,呈现(或不呈现)内容。

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' );
};

您的请求处理程序将优先于提供公共文件夹中内容的默认处理程序,但是一旦安装了该处理程序,就无需将其提供的文件存储在公共文件夹中。

答案 1 :(得分:-1)

另一个解决方案是

get '/pays/*img' => sub {
my $self = shift;

my $img = $self->param('img');
plugin 'RenderFile';

my @imgext = split(/\./, $img);
my $ext = $imgext[-1];


$self->render_file(
'filepath' => "/directiry/$img",
'format'   => "$ext",                 # will change Content-Type "application/x-download" to "application/pdf"
'content_disposition' => 'inline',   # will change Content-Disposition from "attachment" to "inline"
                     # delete file after completed

);

它是使用插件'RenderFile';

相关问题