| 1 | package OpenFrame::AppKit::Segment::Images; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings::register; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use File::MMagic; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use File::Spec; | ||
| 9 | use Pipeline::Segment; | ||
| 10 | use OpenFrame::Response; | ||
| 11 | |||
| 12 | our $VERSION=3.03; | ||
| 13 | use base qw ( Pipeline::Segment ); | ||
| 14 | |||
| 15 | sub directory { | ||
| 16 | my $self = shift; | ||
| 17 | my $dir = shift; | ||
| 18 | if (defined($dir)) { | ||
| 19 | $self->{directory} = $dir; | ||
| 20 | return $self; | ||
| 21 | } else { | ||
| 22 | return $self->{directory}; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub dispatch { | ||
| 27 | my $self = shift; | ||
| 28 | my $pipe = shift; | ||
| 29 | |||
| 30 | # get the path from the request, via the store. | ||
| 31 | my $ofr = $pipe->store->get('OpenFrame::Request'); | ||
| 32 | if (!$ofr) { | ||
| 33 | $self->emit("no OpenFrame::Request available in store"); | ||
| 34 | return undef; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $path = $ofr->uri->path(); | ||
| 38 | |||
| 39 | # add the images directory | ||
| 40 | my ($volume, $dirs, $file) = File::Spec->splitpath( $path ); | ||
| 41 | my $realfile = File::Spec->catfile( $self->directory, $dirs, $file ); | ||
| 42 | |||
| 43 | $self->emit("trying to access $realfile"); | ||
| 44 | |||
| 45 | return unless -f $realfile; | ||
| 46 | return unless -r _; | ||
| 47 | |||
| 48 | my $mm = File::MMagic->new(); | ||
| 49 | my $type = $mm->checktype_filename($realfile); | ||
| 50 | $self->emit("found type: $type"); | ||
| 51 | |||
| 52 | return unless $type =~ m:^image/:; | ||
| 53 | |||
| 54 | $self->emit("serving image"); | ||
| 55 | |||
| 56 | my $fh = IO::File->new($realfile); | ||
| 57 | my $data = join('', <$fh>); | ||
| 58 | |||
| 59 | my $response = OpenFrame::Response->new(); | ||
| 60 | $response->code(ofOK()); | ||
| 61 | $response->message($data); | ||
| 62 | $response->mimetype($type); | ||
| 63 | return $response; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ | ||