rendering images located outside of public directory

35 views
Skip to first unread message

Caveman Pl

unread,
Apr 5, 2018, 2:26:01 PM4/5/18
to Mojolicious
Hi group,

I have no idea how to export images located outside of public directory.
Forgive me to disturbing groups such a lame questions but cant find  working example with google... 

I have a route

$r->get('/pdataexport_qc')->to('Pdataexport#showQC');
$r->post('/showqcshowimg')->to('Pdataexport#showQCshowIMG');

in my Pdataexport.pm

sub showQC{
    my $self = shift;
    my $pDataPath = $self->config('phenotypeDataFromResoraDbFilesPath');
    my $dir = $self->param('dir');
    $self->stash( dirToQC => $dir);
    $self->render('phenotypeExport/showDataQC');
}

my template phenotypeExport/showDataQC.html.ep


% layout 'all';
<table border="0" style="border-collapse: collapse;">
<tr><td><img src="https://192.168.11.11:1777/showqcshowimg?dir=<%=$dirToQC%>&img=AH1vsAGE.png"></td></tr>
</table>
</body>
</html>

which generates me nice url
<table border="0" style="border-collapse: collapse;">
<tr><td><img src="https://192.168.11.11:1777/showqcshowimg?dir=2018-04-03-18-32-46_G&img=AH1vsAGE.png">
</table>
</table>


but in log output I can see that was not routed to showqcshowimg
sub showQCshowIMG {
    my $self = shift;
    my $img = $self->param('img');
    my $dir = $self->param('dir');
    my $pDataPath = $self->config('phenotypeDataFromResoraDbFilesPath');
    $self->render_file(filepath => $pDataPath.$dir."/".$img, 'filename' => $img );
}

Can you give a simpe example how it should be done?
or maybe you know page or open project which can play as example?

Thank you,
tj




Dan Book

unread,
Apr 5, 2018, 2:32:12 PM4/5/18
to mojol...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Dan Book

unread,
Apr 5, 2018, 2:34:07 PM4/5/18
to mojol...@googlegroups.com
Sorry, your issue is in routing: your showimg route is POST-only, but browsers will always use GET to request an image.

Caveman Pl

unread,
Apr 5, 2018, 3:18:57 PM4/5/18
to Mojolicious
Hi Dan,

Changing 

$r->post('/showqcshowimg')->to('Pdataexport#showQCshowIMG');

to

$r->get('/showqcshowimg')->to('Pdataexport#showQCshowIMG');

doesn't help.

Thank you,
tj
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.

Stefan Adams

unread,
Apr 5, 2018, 4:02:57 PM4/5/18
to mojolicious
I might be off base, but I'm wondering if what you're looking for is how to serve static files from a non default location?

Caveman Pl

unread,
Apr 5, 2018, 4:25:37 PM4/5/18
to Mojolicious
Hi Stefan,

Yes they are static files outside dir tree generated by command "mojo generate app" which is located in my home dir /home/tomasz/work/mojo/my_app/. 

I have tryed also inlib/MyApp.pm

use Mojolicious::Static;
my $static = Mojolicious::Static->new;
push @{$static->paths}, '/data/common/mojo/';

and than in phenotypeExport/showDataQC.html.ep

<img src="<%=$dirToQC%>/AH1vsAGE.png">

which create nice url in generated page 
<img src="/data/common/mojo/2018-04-03-18-32-46_G/AH1vsAGE.png">

but it doesnt work also...

Thank you,

tj

Stefan Adams

unread,
Apr 5, 2018, 4:33:56 PM4/5/18
to mojolicious
I don't think you want to create a new object, you want to modify the current app.

Instead, $app->static->paths

Sent from my phone...

Caveman Pl

unread,
Apr 5, 2018, 5:06:51 PM4/5/18
to Mojolicious
didnt know what an $app object is...

when I add to startup subroutine in lib/MyApp.pm
push @{$self->static->paths}, '/data/common/mojo/';
nothing is happen - image doesnt appear on page

when try to add
use Mojolicious::Lite;
my $app = app;
my $static = $app->static;
all hangs...

tj

Tekki

unread,
Apr 6, 2018, 2:29:11 AM4/6/18
to Mojolicious
<img src="..."> is the browser URL, not the absolute path of the file on the server. So if you add '/data/common/mojo' to the static paths, the tag for '/data/common/mojo/2018-04-03-18-32-46_G/AH1vsAGE.png' is

<img src="
/2018-04-03-18-32-46_G/AH1vsAGE.png">

Am Donnerstag, 5. April 2018 22:25:37 UTC+2 schrieb Caveman Pl:
<img src="/data/common/mojo/2018-04-03-18-32-46_G/AH1vsAGE.png">

Caveman Pl

unread,
Apr 6, 2018, 9:44:37 AM4/6/18
to Mojolicious
It works!

Thank you All,

tj
Reply all
Reply to author
Forward
0 new messages