Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Rendering a file in any directory via Mojolicious::Lite

by clueless newbie (Curate)
on Feb 06, 2024 at 20:47 UTC ( [id://11157587]=perlquestion: print w/replies, xml ) Need Help??

clueless newbie has asked for the wisdom of the Perl Monks concerning the following question:

Ave

I'm trying to render an image (with its name) via a Mojolicious::Lite app. Note that a full path to the file is given and putting a link in "public" isn't very appealing. No doubt someone will say don't do that it's a security problem - but the app is only for my own use.

The app:

#!/usr/local/bin/perl use warnings; use Data::Dumper; use Mojolicious::Lite -signatures; use Mojolicious::Static; # Let's just say all we know about the image is my $directory = 'c:/xyzzy/test/'; # some arbitrary directory my $name = 'TEST'; my $extension = '.jpg'; # And we want to create the page as in "picture.html.ep" get '/picture' => sub($c) { #my $static = Mojolicious::Static->new; $c->stash(name=>$name,extension=>$extension); # The following generates #" Can't locate object method "static" via package "Mojolicious::C +ontroller"" warn Data::Dumper->new([\$c->static->paths],[qw(path)])->Dump(),q{ + }; push @{$c->static->paths}, $directory; warn Data::Dumper->new([\$c->static->paths],[qw(path)])->Dump(),q{ + }; }; app->start; __DATA__ @@ picture.html.ep <!DOCTYPE HTML> <html> <body> <h2><%== $directory.$name.$extension %></h2> <img src="<%== $name.$extension %>" alt="FAILED!" /> </body> </html>

So how does one supplement/modify/alter the path that Mojolicious::Lite uses to serve public files?

Replies are listed 'Best First'.
Re: Rendering a file in any directory via Mojolicious::Lite
by marto (Cardinal) on Feb 07, 2024 at 09:40 UTC

    Consider the following, assuming that /tmp/mojoexample/testing.jpg is a valid image.

    use Mojolicious::Lite -signatures; my $static = app->static; push @{$static->paths}, '/tmp/mojoexample/'; get '/' => sub($c){ $c->stash( name => 'testing' ); $c->stash( ext => '.jpg' ); } => 'index'; app->start; __DATA__ @@ index.html.ep <!DOCTYPE HTML> <html> <body> <img src="<%== $name.$ext %>" alt="it works!" /> </body> </html>

    Mojolicious::Static -> paths.

      Many thanks! I owe you a beer!

        Anytime (beers or perl help) :D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11157587]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-05-21 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found