| 1 | package AxKit::App::Gallery::Provider; | ||
| 2 | |||
| 3 | # Copyright (c) 2003 Nik Clayton | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions | ||
| 8 | # are met: | ||
| 9 | # 1. Redistributions of source code must retain the above copyright | ||
| 10 | # notice, this list of conditions and the following disclaimer. | ||
| 11 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | # notice, this list of conditions and the following disclaimer in the | ||
| 13 | # documentation and/or other materials provided with the distribution. | ||
| 14 | # | ||
| 15 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| 16 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 18 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 19 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 20 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 21 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 22 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 23 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 24 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 25 | # SUCH DAMAGE. | ||
| 26 | # | ||
| 27 | # $Id: Provider.pm,v 1.4 2003/11/09 12:33:44 nik Exp $ | ||
| 28 | |||
| 29 | use Apache::Constants qw(:common); | ||
| 30 | use Apache::Request; | ||
| 31 | use File::Path; | ||
| 32 | use URI; | ||
| 33 | use URI::Escape; # For unescaping URIs | ||
| 34 | use Apache::AxKit::Provider::File; | ||
| 35 | use Apache::AxKit::Exception; | ||
| 36 | #use Data::Dumper; | ||
| 37 | |||
| 38 | use base qw(Apache::AxKit::Provider::File); | ||
| 39 | |||
| 40 | sub get_fh { | ||
| 41 | my $self = shift; | ||
| 42 | my $r = $self->{apache}; | ||
| 43 | |||
| 44 | # $r->log_error("get_fh(), r is " . Dumper($r)); | ||
| 45 | |||
| 46 | return $self->SUPER::get_fh() | ||
| 47 | unless substr($r->content_type(), 0, 6) eq 'image/'; | ||
| 48 | |||
| 49 | my $format = $r->param('format'); | ||
| 50 | |||
| 51 | # $r->log_error("get_fh(), format is $format"); | ||
| 52 | |||
| 53 | $format = 'raw' unless $format; # Default format is raw | ||
| 54 | |||
| 55 | # If the format is not 'raw' then generate the XML info for the | ||
| 56 | # image. That's done by get_strref(), so bail out here | ||
| 57 | if($format ne 'raw') { | ||
| 58 | AxKit::Debug(5, "Format is not 'raw'"); | ||
| 59 | throw Apache::AxKit::Exception::IO( | ||
| 60 | -text => "Format is not 'raw'"); | ||
| 61 | } | ||
| 62 | |||
| 63 | return $self->SUPER::get_fh(); | ||
| 64 | } | ||
| 65 | |||
| 66 | # Given an image's filename, generate some XML with information about | ||
| 67 | # just that image. | ||
| 68 | sub get_strref { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | # XXX $self->{apache} seems to be an Apache object, not | ||
| 72 | # an Apache::Request object. So $r->param() doesn't work. Make | ||
| 73 | # sure it's a ::Request object. No idea why we need to do this | ||
| 74 | # here, but not in get_fh() -- maybe we do? | ||
| 75 | my $r = Apache::Request->new($self->{apache}); | ||
| 76 | |||
| 77 | # $r->log_error("get_strref() called: file: " . $r->filename() . " type: " . $r->content_type()); | ||
| 78 | |||
| 79 | # $r->log_error(Dumper($r)); | ||
| 80 | |||
| 81 | return $self->SUPER::get_strref() if ! -f $r->filename(); | ||
| 82 | |||
| 83 | $r->content_type('text/xml'); | ||
| 84 | |||
| 85 | # Get the filename, extract some stats | ||
| 86 | my $file = $r->filename(); | ||
| 87 | # $r->log_error("stat()ing $file"); | ||
| 88 | my $filesize = (stat($file))[7]; | ||
| 89 | my $mod = (stat(_))[9]; | ||
| 90 | |||
| 91 | # Use the filename to retrive the path, and separate out the filename | ||
| 92 | my $path; | ||
| 93 | ($path, $file) = $file =~ /(.*)\/(.*)/; # Extract the path/file info | ||
| 94 | |||
| 95 | my $uri = URI->new($r->uri()); | ||
| 96 | $uri =~ s/^\///; # Trim the leading '/' | ||
| 97 | $uri = join("\n", | ||
| 98 | map { "<component><e>$_</e><u>" . uri_unescape($_) . "</u></component>" } split(/\//, $uri)); | ||
| 99 | |||
| 100 | my $ct = $r->content_type(); | ||
| 101 | |||
| 102 | my $xml = <<EOXML; | ||
| 103 | <?xml version="1.0"?> | ||
| 104 | <imagesheet> | ||
| 105 | <config> | ||
| 106 | <perl-vars> | ||
| 107 | EOXML | ||
| 108 | |||
| 109 | foreach my $var (qw(ProofsheetColumns ImagesPerProofsheet | ||
| 110 | GalleryCache GalleryThumbQuality)) { | ||
| 111 | $xml .= "<var name='$var'>" . $r->dir_config($var) . "</var>\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $size = $r->param('size'); | ||
| 115 | |||
| 116 | # Make sure the specified size is one we're configured to | ||
| 117 | # support. If it isn't then use the default size | ||
| 118 | my $sizelist = $r->dir_config('GallerySizes'); | ||
| 119 | $sizelist = '133 640 800 1024' unless defined $sizelist; | ||
| 120 | my @sizes = split(/\s+/, $sizelist); | ||
| 121 | |||
| 122 | if($size eq 'thumb') { | ||
| 123 | $size = $sizes[0]; | ||
| 124 | } else { | ||
| 125 | $size = $sizes[1] unless grep { $_ eq $size } @sizes; | ||
| 126 | } | ||
| 127 | |||
| 128 | $xml .= <<EOXML; | ||
| 129 | <GallerySizes> | ||
| 130 | <size type="thumb">$sizes[0]</size> | ||
| 131 | EOXML | ||
| 132 | foreach (@sizes[1..$#sizes]) { | ||
| 133 | if($_ == $size) { | ||
| 134 | $xml .= "<size type=\"default\">$_</size>\n"; | ||
| 135 | } else { | ||
| 136 | $xml .= "<size>$_</size>\n"; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | $xml .= "</GallerySizes>"; | ||
| 140 | |||
| 141 | $xml .= <<EOXML; | ||
| 142 | </perl-vars> | ||
| 143 | </config> | ||
| 144 | |||
| 145 | <image> | ||
| 146 | <filename>$file</filename> | ||
| 147 | <filesize>$filesize</filesize> | ||
| 148 | <size>$size</size> | ||
| 149 | <modified>$mod</modified> | ||
| 150 | <uri>$uri</uri> | ||
| 151 | <dirpath>$path</dirpath> | ||
| 152 | <content-type>$ct</content-type> | ||
| 153 | </image> | ||
| 154 | </imagesheet> | ||
| 155 | EOXML | ||
| 156 | |||
| 157 | # $r->log_error("Provider returning $xml"); | ||
| 158 | return \$xml; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; |