| 1 | #!/usr/bin/perl | ||
| 2 | # Apache::UpnpImgBrowser | ||
| 3 | |||
| 4 | # | ||
| 5 | # James Pavlick, 2007 | ||
| 6 | # mod_perl module for browsing directories of images stored on a Upnp device | ||
| 7 | # | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Apache::UpnpImgBrowser | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | Add the following to your httpd.conf and restart. Then point your browser to | ||
| 16 | http://yoursite/photos | ||
| 17 | |||
| 18 | |||
| 19 | <Location /photos> | ||
| 20 | AllowOverride None | ||
| 21 | #Options -Indexes -Includes -FollowSymLinks | ||
| 22 | Order allow,deny | ||
| 23 | Allow from all | ||
| 24 | |||
| 25 | SetHandler perl-script | ||
| 26 | PerlHandler Apache::UpnpImgBrowser | ||
| 27 | PerlSetVar Basedir Photos/Folder | ||
| 28 | PerlSetVar Rows 10 | ||
| 29 | PerlSetVar Cols 5 | ||
| 30 | PerlSetVar Thumb-size 50x20 | ||
| 31 | PerlSetVar Show-names 0 | ||
| 32 | PerlSetVar Hide-dirs 1 | ||
| 33 | PerlSetVar Filter vacation | ||
| 34 | </Location> | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | I<Apache::UpnpImgBroswer> is a mod_perl application for displaying photos | ||
| 39 | hosted on a UPnPAV compliant Media Server. I<Apache::UpnpImgBroswer> will | ||
| 40 | automatically discover all these types of devices on your network. | ||
| 41 | |||
| 42 | =head1 CAVEATS | ||
| 43 | |||
| 44 | =over 2 | ||
| 45 | |||
| 46 | =item B<*> Thumbnails only work with TwonkyVision | ||
| 47 | |||
| 48 | =item B<*> Lots of work still to do | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | |||
| 53 | package Apache::UpnpImgBrowser; | ||
| 54 | use strict; | ||
| 55 | use Apache2; | ||
| 56 | use Apache::RequestRec; | ||
| 57 | use Apache::RequestIO; | ||
| 58 | use Apache::Const qw(:common HTTP_OK); | ||
| 59 | use Apache::Log; | ||
| 60 | use APR::Const qw(:filetype); | ||
| 61 | use APR::Finfo; | ||
| 62 | use Image::Magick (); | ||
| 63 | use DirHandle (); | ||
| 64 | use FileHandle (); | ||
| 65 | use File::Basename qw(fileparse); | ||
| 66 | use Net::UPnP::ControlPoint; | ||
| 67 | use Net::UPnP::AV::MediaServer; | ||
| 68 | use SOAP::Lite maptype => {}; | ||
| 69 | use XML::Simple; | ||
| 70 | use Data::Dumper; | ||
| 71 | use POSIX qw(strftime); | ||
| 72 | use Cache::FileCache; | ||
| 73 | use URI::Escape; | ||
| 74 | use LWP::Simple; | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | use vars qw(%gOptions @gOutput $gOutputStarted $gCp @gDeviceList $gLastDir $cache $VERSION); | ||
| 79 | |||
| 80 | $VERSION = 0.01; | ||
| 81 | |||
| 82 | %gOptions = ( | ||
| 83 | 'thumb-size' => '100x75', # set thumbnail size | ||
| 84 | 'force' => 0, # always rebuild thumbnails | ||
| 85 | 'rows' => 5, # rows to display | ||
| 86 | 'cols' => 4, # columns to display | ||
| 87 | 'show-names' => 0, # show thumbnail names | ||
| 88 | 'hide-dirs' => 0, # hide the photo directories list | ||
| 89 | 'filter' => '.*', # display only directories that match | ||
| 90 | 'basedir' => 'Photos', # Base directory of the UPNP server | ||
| 91 | ); | ||
| 92 | |||
| 93 | $cache = new Cache::FileCache( { 'namespace' => 'UpnpImgBrowser', | ||
| 94 | 'default_expires_in' => 600 } ); | ||
| 95 | |||
| 96 | |||
| 97 | sub handler { | ||
| 98 | my($r) = shift; | ||
| 99 | |||
| 100 | $gCp = $cache->get('cp'); | ||
| 101 | @gDeviceList = $cache->get('device'); | ||
| 102 | |||
| 103 | if (! defined $gCp) { | ||
| 104 | $gCp = Net::UPnP::ControlPoint->new(); | ||
| 105 | $cache->set('cp', $gCp); | ||
| 106 | } | ||
| 107 | |||
| 108 | unless (grep ref $_ eq 'Net::UPnP::Device', @gDeviceList) { | ||
| 109 | @gDeviceList = (upnpInitDevice($r)); | ||
| 110 | $cache->set('device', @gDeviceList); | ||
| 111 | } | ||
| 112 | |||
| 113 | # store output in this array | ||
| 114 | @gOutput = (); | ||
| 115 | |||
| 116 | $gOutputStarted = 0; | ||
| 117 | |||
| 118 | # set config values | ||
| 119 | my $val = ''; | ||
| 120 | $gOptions{'thumb-size'} = $val if($val = $r->dir_config('Thumb-size')); | ||
| 121 | $gOptions{'force'} = $val if($val = $r->dir_config('Force')); | ||
| 122 | $gOptions{'rows'} = $val if($val = $r->dir_config('Rows')); | ||
| 123 | $gOptions{'cols'} = $val if($val = $r->dir_config('Cols')); | ||
| 124 | $gOptions{'show-names'} = $val if($val = $r->dir_config('Show-names')); | ||
| 125 | $gOptions{'hide-dirs'} = $val if($val = $r->dir_config('Hide-dirs')); | ||
| 126 | $gOptions{'basedir'} = $val if($val = $r->dir_config('Basedir')); | ||
| 127 | $gOptions{'filter'} = $val if($val = $r->dir_config('Filter')); | ||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | if($r->args =~ /image=/) { | ||
| 132 | # show image detail html page | ||
| 133 | showImgDetail($r); | ||
| 134 | |||
| 135 | } elsif ($r->args =~ /target=/) { | ||
| 136 | # proxy the image request | ||
| 137 | proxyRequest($r); | ||
| 138 | |||
| 139 | } else { | ||
| 140 | # show the thumb nail page | ||
| 141 | showImgThumbs($r); | ||
| 142 | } | ||
| 143 | |||
| 144 | $r->print(@gOutput); | ||
| 145 | |||
| 146 | return OK; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | # upnpInitDevice | ||
| 152 | # | ||
| 153 | # | ||
| 154 | sub upnpInitDevice { | ||
| 155 | my $r = shift; | ||
| 156 | |||
| 157 | my @retval = (); | ||
| 158 | my @device_list = $gCp->search(st => "urn:schemas-upnp-org:device:MediaServer:1", mx => 5); | ||
| 159 | |||
| 160 | foreach my $device (@device_list) { | ||
| 161 | my $udn = $device->getudn; | ||
| 162 | next if grep($_->getudn eq $udn, @retval); | ||
| 163 | push(@retval, $device); | ||
| 164 | |||
| 165 | $r->log_error("Added name: " . $device->getfriendlyname() . " " . | ||
| 166 | $device->getudn . " type: " . $device->getdevicetype() | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | |||
| 170 | @retval; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | # upnpGetMetadata | ||
| 176 | # | ||
| 177 | # | ||
| 178 | sub upnpGetMetadata { | ||
| 179 | my ($r, $device, $objid) = @_; | ||
| 180 | |||
| 181 | $objid ||= 0; | ||
| 182 | |||
| 183 | my(%retval) = (); | ||
| 184 | |||
| 185 | my($basedir) = $gOptions{'basedir'} || ''; | ||
| 186 | my(%action_in_arg) = ( | ||
| 187 | 'ObjectID' => $objid, | ||
| 188 | ); | ||
| 189 | |||
| 190 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 191 | |||
| 192 | my $mediaServer = Net::UPnP::AV::MediaServer->new(); | ||
| 193 | $mediaServer->setdevice($device); | ||
| 194 | |||
| 195 | my $action = $mediaServer->browsemetadata(%action_in_arg); | ||
| 196 | my $action_out_arg = $action->getargumentlist(); | ||
| 197 | my $result = $action_out_arg->{'Result'}; | ||
| 198 | |||
| 199 | return undef unless $result; | ||
| 200 | |||
| 201 | my $tree = XMLin($result, forcearray => ["container"]); | ||
| 202 | |||
| 203 | foreach my $objectid (keys %{$tree->{container}}) { | ||
| 204 | my $entry = $tree->{container}{$objectid}; | ||
| 205 | |||
| 206 | $retval{title} = $entry->{"dc:title"}; | ||
| 207 | $retval{date} = $entry->{"dc:date"}; | ||
| 208 | $retval{parent} = $entry->{"parentID"}; | ||
| 209 | $retval{childcount} = $entry->{"childCount"}; | ||
| 210 | $retval{id} = $objectid; | ||
| 211 | } | ||
| 212 | |||
| 213 | \%retval; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | # upnpGetFileLis, | ||
| 218 | sub upnpGetFileList { | ||
| 219 | my ($r, $device, $objid) = @_; | ||
| 220 | |||
| 221 | $objid ||= 0; | ||
| 222 | |||
| 223 | my($basedir) = $gOptions{'basedir'} || ''; | ||
| 224 | my(@retval) = (); | ||
| 225 | my(%action_in_arg) = ( | ||
| 226 | 'ObjectID' => $objid, | ||
| 227 | 'BrowseFlag' => 'BrowseDirectChildren', | ||
| 228 | 'Filter' => '*', | ||
| 229 | 'StartingIndex' => 0, | ||
| 230 | 'RequestedCount' => 0, | ||
| 231 | ); | ||
| 232 | |||
| 233 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 234 | |||
| 235 | |||
| 236 | my $service = $device->getservicebyname('urn:schemas-upnp-org:service:ContentDirectory:1'); | ||
| 237 | return undef unless $service; | ||
| 238 | my $action = $service->postcontrol('Browse', \%action_in_arg); | ||
| 239 | |||
| 240 | my $action_out_arg = $action->getargumentlist(); | ||
| 241 | |||
| 242 | my $result = $action_out_arg->{'Result'}; | ||
| 243 | return undef unless $result; | ||
| 244 | |||
| 245 | my $tree = XMLin($result, forcearray => ["item"]); | ||
| 246 | |||
| 247 | foreach my $objectid (keys %{$tree->{item}}) { | ||
| 248 | my $entry = $tree->{item}{$objectid}; | ||
| 249 | my %item; | ||
| 250 | |||
| 251 | # Only allow photo items | ||
| 252 | next unless $entry->{'upnp:class'} eq 'object.item.imageItem.photo'; | ||
| 253 | |||
| 254 | $item{title} = $entry->{"dc:title"}; | ||
| 255 | $item{date} = $entry->{"dc:date"}; | ||
| 256 | $item{parent} = $entry->{"parentID"}; | ||
| 257 | $item{id} = $objectid; | ||
| 258 | $item{res} = []; | ||
| 259 | |||
| 260 | my @tmp = (ref $entry->{"res"} eq 'ARRAY') ? | ||
| 261 | @{$entry->{"res"}} : ($entry->{"res"}); | ||
| 262 | |||
| 263 | foreach my $res (@tmp) { | ||
| 264 | my($w, $h) = split('x', $res->{'resolution'}); | ||
| 265 | my($size) = $res->{'size'}; #$w * $h; | ||
| 266 | |||
| 267 | my($url) = $res->{'content'}; | ||
| 268 | my($contenttype) = $res->{'protocolInfo'}; | ||
| 269 | |||
| 270 | $contenttype =~ s/http-get:[^:]*:([^:]*):.*/$1/; | ||
| 271 | next unless $contenttype =~ /^image/; # only allow images | ||
| 272 | |||
| 273 | $url =~ s/^http:\/\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?\///; | ||
| 274 | |||
| 275 | push(@{$item{res}}, | ||
| 276 | { | ||
| 277 | height => $h, | ||
| 278 | width => $w, | ||
| 279 | size => $size, | ||
| 280 | url => $url, | ||
| 281 | contenttype => $contenttype | ||
| 282 | }); | ||
| 283 | } | ||
| 284 | |||
| 285 | push(@retval, \%item); | ||
| 286 | } | ||
| 287 | |||
| 288 | @retval; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | # upnpGetDirectory | ||
| 293 | # | ||
| 294 | # | ||
| 295 | sub upnpGetDirectory{ | ||
| 296 | my ($r, $device, $objid) = @_; | ||
| 297 | |||
| 298 | $objid ||= 0; | ||
| 299 | |||
| 300 | my($basedir) = $gOptions{'basedir'} || ''; | ||
| 301 | my(@retval) = (); | ||
| 302 | my(%action_in_arg) = ( | ||
| 303 | 'ObjectID' => $objid, | ||
| 304 | 'BrowseFlag' => 'BrowseDirectChildren', | ||
| 305 | 'Filter' => '*', | ||
| 306 | 'StartingIndex' => 0, | ||
| 307 | 'RequestedCount' => 0, | ||
| 308 | 'SortCriteria' => '', | ||
| 309 | ); | ||
| 310 | |||
| 311 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 312 | |||
| 313 | my $service = $device->getservicebyname('urn:schemas-upnp-org:service:ContentDirectory:1'); | ||
| 314 | return undef unless $service; | ||
| 315 | |||
| 316 | my $action = $service->postcontrol('Browse', \%action_in_arg); | ||
| 317 | |||
| 318 | my $action_out_arg = $action->getargumentlist(); | ||
| 319 | my $result = $action_out_arg->{'Result'}; | ||
| 320 | return undef unless $result; | ||
| 321 | |||
| 322 | my $tree = XMLin($result, forcearray => ["container"]); | ||
| 323 | |||
| 324 | foreach my $objectid (keys %{$tree->{container}}) { | ||
| 325 | my $entry = $tree->{container}{$objectid}; | ||
| 326 | my %dir; | ||
| 327 | |||
| 328 | $dir{title} = $entry->{"dc:title"}; | ||
| 329 | $dir{date} = $entry->{"dc:date"}; | ||
| 330 | $dir{parent} = $entry->{"parentID"}; | ||
| 331 | $dir{id} = $objectid; | ||
| 332 | |||
| 333 | push(@retval, \%dir); | ||
| 334 | } | ||
| 335 | |||
| 336 | @retval; | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | # showImgThumbs | ||
| 341 | # | ||
| 342 | # list the contents of a directory as an HTML thumbnail index | ||
| 343 | # | ||
| 344 | sub showImgThumbs { | ||
| 345 | my($r) = shift; | ||
| 346 | |||
| 347 | my(%in) = map { split('=', $_) } (split('&', $r->args)); | ||
| 348 | |||
| 349 | my($perpage) = $gOptions{'rows'} * $gOptions{'cols'}; | ||
| 350 | my($page) = $in{'page'} || 1; | ||
| 351 | my($dir) = $in{dir} || 0; | ||
| 352 | |||
| 353 | my($dev) = $in{'dev'} || $gDeviceList[0]->getudn; | ||
| 354 | |||
| 355 | my(@filelist) = getFileList($r, $dir, $dev); | ||
| 356 | my(@dirlist) = getDirectoryList($r, $dir, $dev); | ||
| 357 | |||
| 358 | |||
| 359 | my $parentdir = getParentId($r, $dir); | ||
| 360 | my $parentname = getMetadata($r, $parentdir, $dev)->{title}; | ||
| 361 | # my $dirname = (grep $_->{id} eq $dir, @dirlist)[0]->{title}; | ||
| 362 | my $dirname = ''; | ||
| 363 | |||
| 364 | |||
| 365 | # Output page to client | ||
| 366 | $r->content_type('text/html'); | ||
| 367 | output( | ||
| 368 | "<html>\n", | ||
| 369 | "<head>\n", | ||
| 370 | " <title>Image Browser</title>\n", | ||
| 371 | qq| <style type="text/css">\n|, | ||
| 372 | " <!--\n", | ||
| 373 | " a { text-decoration: none; }\n", | ||
| 374 | " .menu { color: orange; text-decoration: none; }\n", | ||
| 375 | " #searchbox\n", | ||
| 376 | " {\n", | ||
| 377 | " text-align: left;\n", | ||
| 378 | " border: #DDDDDD 1px solid;\n", | ||
| 379 | " border-top: none;\n", | ||
| 380 | " padding-top: 6px;\n", | ||
| 381 | " padding-right: 2px;\n", | ||
| 382 | " border-left: none;\n", | ||
| 383 | " position: absolute;\n", | ||
| 384 | " left: 0px;\n", | ||
| 385 | " top: 0px;\n", | ||
| 386 | " width: 180px;\n", | ||
| 387 | " }\n", | ||
| 388 | " #leftcol\n", | ||
| 389 | " {\n", | ||
| 390 | " text-align: left;\n", | ||
| 391 | " border: #DDDDDD 1px solid;\n", | ||
| 392 | " border-top: none;\n", | ||
| 393 | " border-bottom: none;\n", | ||
| 394 | " padding-top: 6px;\n", | ||
| 395 | " padding-right: 2px;\n", | ||
| 396 | " padding-bottom: 10px;\n", | ||
| 397 | " border-left: none;\n", | ||
| 398 | " position: absolute;\n", | ||
| 399 | " left: 0px;\n", | ||
| 400 | " width: 180px;\n", | ||
| 401 | " height: 600px;\n", | ||
| 402 | " }\n", | ||
| 403 | " -->\n", | ||
| 404 | " </style>\n", | ||
| 405 | "</head>\n", | ||
| 406 | qq|<body bgcolor="#FFFFFF">\n\n|, | ||
| 407 | qq|<center>\n|, | ||
| 408 | ); | ||
| 409 | |||
| 410 | output( | ||
| 411 | qq|<div id="searchbox">\n|, | ||
| 412 | qq|<table width="170" align="left" border="0">\n|, | ||
| 413 | qq| <tr><td>\n|, | ||
| 414 | qq| <form method="get" action="./">\n|, | ||
| 415 | qq| <select name="dev" onchange="this.form.submit()">\n|, | ||
| 416 | ); | ||
| 417 | |||
| 418 | |||
| 419 | # UPnP MediaServer List | ||
| 420 | foreach my $device | ||
| 421 | (sort {$a->getfriendlyname cmp $b->getfriendlyname} @gDeviceList) { | ||
| 422 | my $name = $device->getfriendlyname; | ||
| 423 | my $udn = $device->getudn; | ||
| 424 | my $selected = ($udn eq $dev) ? "selected" : ""; | ||
| 425 | |||
| 426 | output(qq| <option value="$udn" $selected>$name\n|); | ||
| 427 | } | ||
| 428 | |||
| 429 | |||
| 430 | output( | ||
| 431 | qq| </select>\n|, | ||
| 432 | qq| </form></td></tr>\n|, | ||
| 433 | qq|</table>\n|, | ||
| 434 | qq|</div>\n\n|, | ||
| 435 | qq|<br clear="all">\n\n|, | ||
| 436 | qq|<div id="leftcol">\n|, | ||
| 437 | qq|<table width="170" align="left" border="0">\n|, | ||
| 438 | qq| <tr><td> </td></tr>|, | ||
| 439 | qq| <tr><td><a href="./?dev=$dev"><span class="menu">Home</span></a>|, | ||
| 440 | ); | ||
| 441 | |||
| 442 | |||
| 443 | # Back Button | ||
| 444 | if ($dir == 0) { | ||
| 445 | output(qq|</td></tr>\n|); | ||
| 446 | |||
| 447 | } else { | ||
| 448 | output( | ||
| 449 | qq| \| <a href="./?dir=$parentdir&dev=$dev"><span class="menu">Back ($parentname)</span></a></td></tr>\n|, | ||
| 450 | ); | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | # Directory List | ||
| 456 | if(@dirlist && ! $gOptions{'hide-dirs'}) { | ||
| 457 | |||
| 458 | output( | ||
| 459 | qq| <tr><td> </td></tr>|, | ||
| 460 | qq| <tr><td><b>Image Directories:</b></td></tr>\n|, | ||
| 461 | ); | ||
| 462 | |||
| 463 | foreach my $dir (sort {$a->{title} cmp $b->{title}} @dirlist) { | ||
| 464 | my($title) = $dir->{'title'}; | ||
| 465 | my($id) = $dir->{'id'}; | ||
| 466 | |||
| 467 | output(qq| <tr><td> <a href="./?dir=$id&dev=$dev">$title</a><td></tr>\n|); | ||
| 468 | } | ||
| 469 | } | ||
| 470 | |||
| 471 | |||
| 472 | |||
| 473 | output( | ||
| 474 | qq|</table>\n|, | ||
| 475 | qq|</div>\n\n|, | ||
| 476 | qq|<br clear="all"\n\n|, | ||
| 477 | qq|<table border=0 bgcolor="#CCCCCC">\n|, | ||
| 478 | qq| <tr><td>\n|, | ||
| 479 | qq| <table border=0 cellpadding=0 cellspacing=0 width="100%">\n|, | ||
| 480 | qq| <tr><td align="center" bgcolor="#AAAAAA"><font size="+1"><b>$dirname</font></b></td></tr>\n|, | ||
| 481 | qq| </table>\n\n|, | ||
| 482 | ); | ||
| 483 | |||
| 484 | |||
| 485 | my($w, $h) = split('x', $gOptions{'thumb-size'}); | ||
| 486 | |||
| 487 | ### Thumbnail image table | ||
| 488 | output(qq| <table align="center" bgcolor="#CCCCCC" border="1" width="|, ($w + 25 )* $gOptions{'cols'}, qq|">\n|); | ||
| 489 | |||
| 490 | |||
| 491 | # Thumbs List | ||
| 492 | if(@filelist) { | ||
| 493 | my($counter) = 0; | ||
| 494 | my(@sorted) = (sort {$a->{title} cmp $b->{title}} @filelist); | ||
| 495 | my($startIdx) = ($page - 1) * $perpage; | ||
| 496 | my($endIdx) = ($page * $perpage < @filelist)? | ||
| 497 | $page * $perpage: scalar @filelist; | ||
| 498 | |||
| 499 | for(my $x = $startIdx; $x < $endIdx; $x++) { | ||
| 500 | my $thumburl = getThumbImgResource($sorted[$x])->{url}; | ||
| 501 | # my $thumburl = $sorted[$x]->{thumburl}; | ||
| 502 | my $url = getImgResource($sorted[$x])->{url}; | ||
| 503 | # my $url = $sorted[$x]->{url}; | ||
| 504 | my $title = $sorted[$x]->{title}; | ||
| 505 | my $contenttype = $sorted[$x]->{contenttype}; | ||
| 506 | |||
| 507 | $title = (length $title >= 12)? | ||
| 508 | substr($title, 0, 12) . "...": $title; | ||
| 509 | |||
| 510 | output(" <tr>\n") if( ($counter % $gOptions{'cols'}) == 0 ); | ||
| 511 | |||
| 512 | output( | ||
| 513 | " ", | ||
| 514 | qq| <td align="center">|, | ||
| 515 | qq|<a href="./?dir=$dir&dev=$dev&page=$page&image=$url">|, | ||
| 516 | qq|<img src="./?target=$thumburl&dev=$dev" hspace="15" height="$h" width="$w"></A>|, | ||
| 517 | qq|<br>|, | ||
| 518 | ); | ||
| 519 | |||
| 520 | output($title) if($gOptions{'show-names'}); | ||
| 521 | |||
| 522 | output( | ||
| 523 | qq|</td>\n|, | ||
| 524 | ); | ||
| 525 | |||
| 526 | output(" </tr>\n") if( (($counter + 1) % $gOptions{'cols'}) == 0 ); | ||
| 527 | |||
| 528 | $counter++; | ||
| 529 | } | ||
| 530 | |||
| 531 | } else { | ||
| 532 | output( qq|<tr><td align="center"><font size="+2"><b>No Images Available</b></font</td></tr>\n|); | ||
| 533 | } | ||
| 534 | |||
| 535 | output(qq| </table>\n\n|,); | ||
| 536 | |||
| 537 | |||
| 538 | |||
| 539 | ### Navigation bar table | ||
| 540 | output( | ||
| 541 | qq| <table bgcolor="#AAAAAA" cellpadding=0 cellspacing=0 border=0 width="100%">\n|, | ||
| 542 | qq| <tr>\n| | ||
| 543 | ); | ||
| 544 | |||
| 545 | # previous page | ||
| 546 | if($page > 1) { | ||
| 547 | my $pagenum = $page - 1; | ||
| 548 | output(qq| <td width=100 align="center"><a href="./?dir=$dir&dev=$dev&page=$pagenum">[previous]</A></td>\n|); | ||
| 549 | |||
| 550 | } else { | ||
| 551 | output(qq| <td width=100> </td>\n|); | ||
| 552 | } | ||
| 553 | |||
| 554 | |||
| 555 | |||
| 556 | # page indexes | ||
| 557 | if(@filelist) { | ||
| 558 | output( | ||
| 559 | qq| <td align="center" valign="bottom">\n|, | ||
| 560 | qq| <form>\n|, | ||
| 561 | qq| <select onChange="document.location='./?dir=$dir&dev=$dev&page=' + this.options[this.selectedIndex].value">\n| | ||
| 562 | ); | ||
| 563 | |||
| 564 | for(my $y = 0; $y <= int((@filelist - 1)/$perpage); $y++) { | ||
| 565 | my $pagenum = $y + 1; | ||
| 566 | |||
| 567 | ($pagenum == $page) ? | ||
| 568 | output(qq| <option value="$pagenum" selected>page $pagenum\n|): | ||
| 569 | output(qq| <option value="$pagenum">page $pagenum\n|); | ||
| 570 | } | ||
| 571 | |||
| 572 | output( | ||
| 573 | qq| </select><br>\n|, | ||
| 574 | qq| <font size="-1"><i>Total: </i>|, scalar @filelist, qq| images</font>\n|, | ||
| 575 | qq| </form>\n|, | ||
| 576 | qq| </td>\n| | ||
| 577 | ); | ||
| 578 | } | ||
| 579 | |||
| 580 | # next page | ||
| 581 | if( $page * $perpage < @filelist) { | ||
| 582 | my $pagenum = $page + 1; | ||
| 583 | output(qq| <td width=100 align="center"><a href="./?dir=$dir&dev=$dev&page=$pagenum">[next]</A></TD>\n|); | ||
| 584 | |||
| 585 | } else { | ||
| 586 | output(qq| <td width=100> </td>\n|); | ||
| 587 | } | ||
| 588 | |||
| 589 | |||
| 590 | output( | ||
| 591 | " </tr>\n", | ||
| 592 | " </table>\n", | ||
| 593 | " </td></tr>\n", | ||
| 594 | "</table>\n", | ||
| 595 | "</center>\n", | ||
| 596 | "</body>\n", | ||
| 597 | "</html>\n" | ||
| 598 | ); | ||
| 599 | |||
| 600 | } | ||
| 601 | |||
| 602 | |||
| 603 | |||
| 604 | # showThumbFile | ||
| 605 | # | ||
| 606 | # | ||
| 607 | sub showThumbFile { | ||
| 608 | my $r = shift || return; | ||
| 609 | |||
| 610 | my %mimeType = | ||
| 611 | (jpg => 'image/jpeg', gif => 'image/gif', png => 'image/png'); | ||
| 612 | my ($name, $path, $ext) = fileparse($r->filename, qr{\.\w*$}); | ||
| 613 | my($w, $h) = split('x', $gOptions{'thumb-size'}); | ||
| 614 | |||
| 615 | $name =~ s/^\.//g; | ||
| 616 | $path =~ s/\/$//g; | ||
| 617 | $ext =~ s/^\.//g; | ||
| 618 | |||
| 619 | (my $imgName = $name) =~ s/-thumb$//i; | ||
| 620 | my($thumbName) = "$name.$ext"; | ||
| 621 | my($Img) = Image::Magick->new(); | ||
| 622 | my($tw, $th) = (0, 0); | ||
| 623 | |||
| 624 | if(-f "$path/$thumbName") { | ||
| 625 | ($tw, $th) = $Img->Ping("$path/$thumbName"); | ||
| 626 | } | ||
| 627 | |||
| 628 | # build the thumbnail if it doesn't exist | ||
| 629 | if( ($w != $tw && $h != $th) || $gOptions{'force'} ) { | ||
| 630 | $Img->Read("$path/../$imgName.$ext"); | ||
| 631 | $Img->Resize(geometry=>"$gOptions{'thumb-size'}"); | ||
| 632 | $Img->Write("$path/$thumbName"); | ||
| 633 | } | ||
| 634 | |||
| 635 | my $Fh = FileHandle->new(); | ||
| 636 | $Fh->open("$path/$thumbName") || die("Can't open image file $path/$thumbName"); | ||
| 637 | |||
| 638 | $r->content_type($mimeType{lc $ext}); | ||
| 639 | output(<$Fh>); | ||
| 640 | |||
| 641 | undef $Img; | ||
| 642 | $Fh->close(); | ||
| 643 | } | ||
| 644 | |||
| 645 | |||
| 646 | |||
| 647 | # showImgDetail | ||
| 648 | # | ||
| 649 | # | ||
| 650 | # | ||
| 651 | sub showImgDetail { | ||
| 652 | my($r) = shift; | ||
| 653 | |||
| 654 | my(%in) = map { my($key, $val) = split('=', $_); $key => $val } | ||
| 655 | (split('&', $r->args)); | ||
| 656 | |||
| 657 | my $image = $in{image}; | ||
| 658 | my $page = $in{page} || 1; | ||
| 659 | my $dir = $in{dir}; | ||
| 660 | my $dev = uri_unescape($in{dev}) || $gDeviceList[0]->getudn; | ||
| 661 | $dev =~ s/\+/ /g; | ||
| 662 | |||
| 663 | |||
| 664 | # only rotate up to 360 degrees | ||
| 665 | if($in{'rot'} >= 360) { $in{'rot'} -= 360 }; | ||
| 666 | |||
| 667 | # only allow scaling to 25% and 200% of image size | ||
| 668 | if($in{'scale'} < -75) { $in{'scale'} = -75 }; | ||
| 669 | if($in{'scale'} > 100) { $in{'scale'} = 100 }; | ||
| 670 | |||
| 671 | my $scale = $in{'scale'} + 100; | ||
| 672 | |||
| 673 | my $rotText = ($in{'rot'}) ? "(Rotated: $in{'rot'}°)" : ''; | ||
| 674 | my $scaleText = ($in{'scale'}) ? "(Scaled: $scale%)" : ''; | ||
| 675 | |||
| 676 | $r->content_type("text/html"); | ||
| 677 | output( | ||
| 678 | "<html>\n", | ||
| 679 | "<head>\n", | ||
| 680 | " <title>Image</title>\n", | ||
| 681 | "</head>\n", | ||
| 682 | qq|<body bgcolor="#ffffff">\n\n|, | ||
| 683 | "<center>\n", | ||
| 684 | qq|<table border=1>\n|, | ||
| 685 | qq| <tr><td> </td><td align="center"><a href="./?dir=$dir&dev=$dev&page=$page"><b>Image Index</b></a></td><td> </tr>\n|, | ||
| 686 | # qq| <TR><TD align="center"><A href="$baseUri?page=$in{'page'}&name=$in{'name'}&rot=|, | ||
| 687 | # $in{'rot'} + 270, qq|&scale=$in{'scale'}"><B>270</B></A></TD>|, | ||
| 688 | # qq|<TD align="center"><A href="$baseUri?page=$in{'page'}&name=$in{'name'}&rot=$in{'rot'}&scale=|, $in{'scale'} - 25, qq|">-</A> Zoom <A href="$baseUri?page=$in{'page'}&name=$in{'name'}&rot=$in{'rot'}&scale=|, $in{'scale'} + 25, qq|">+</A></TD>|, | ||
| 689 | # qq|<TD align="center"><A href="$baseUri?page=$in{'page'}&name=$in{'name'}&rot=|, | ||
| 690 | # $in{'rot'} + 90, qq|&scale=$in{'scale'}"><B>90</B></A></TD></TR>\n|, | ||
| 691 | # qq| <TR><TD> </TD><TD align="center"><A href="$baseUri?page=$in{'page'}&name=$in{'name'}&rot=|, | ||
| 692 | # $in{'rot'} + 180, qq|&scale=$in{'scale'}"><B>180</B></A></TD><TD> </TD></TR>\n|, | ||
| 693 | "</table>\n", | ||
| 694 | qq|<table border=0 width="100%">\n|, | ||
| 695 | ); | ||
| 696 | |||
| 697 | |||
| 698 | output(qq| <tr><td align="center"><img src="./?target=$image&dev=$dev|); | ||
| 699 | # output(qq|rot=$in{'rot'}|) if($in{'rot'}); | ||
| 700 | # output(qq|&scale=$in{'scale'}|) if($in{'scale'}); | ||
| 701 | output(qq|" border=0></td></tr>\n|); | ||
| 702 | |||
| 703 | output( | ||
| 704 | "</table>\n", | ||
| 705 | "</center>\n", | ||
| 706 | "</body>\n", | ||
| 707 | "</html>\n", | ||
| 708 | ); | ||
| 709 | } | ||
| 710 | |||
| 711 | |||
| 712 | |||
| 713 | |||
| 714 | # getThumbImgResource | ||
| 715 | # | ||
| 716 | # | ||
| 717 | sub getThumbImgResource { | ||
| 718 | my $item = shift; | ||
| 719 | |||
| 720 | return unless $item; | ||
| 721 | |||
| 722 | my @res = sort {$a->{size} <=> $b->{size}} @{$item->{'res'}}; | ||
| 723 | |||
| 724 | my %rv = (); | ||
| 725 | foreach my $x (keys %{$res[0]}) { | ||
| 726 | $rv{$x} = $res[0]->{$x}; | ||
| 727 | } | ||
| 728 | |||
| 729 | $rv{url} =~ s/disk/albumart/; | ||
| 730 | \%rv; | ||
| 731 | } | ||
| 732 | |||
| 733 | |||
| 734 | # getImgResource | ||
| 735 | # | ||
| 736 | # | ||
| 737 | sub getImgResource { | ||
| 738 | my $item = shift; | ||
| 739 | |||
| 740 | return unless $item; | ||
| 741 | |||
| 742 | my @res = sort {$a->{size} <=> $b->{size}} @{$item->{'res'}}; | ||
| 743 | $res[0]; | ||
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | |||
| 748 | # proxyRequest | ||
| 749 | # | ||
| 750 | # | ||
| 751 | sub proxyRequest { | ||
| 752 | my($r) = shift; | ||
| 753 | |||
| 754 | my(%in) = map { my($key, $val) = split('=', $_); $key => $val } (split('&', $r->args)); | ||
| 755 | my $target = $in{target}; | ||
| 756 | my $dev = $in{dev}; | ||
| 757 | |||
| 758 | my($device) = (grep $_->getudn eq $dev, @gDeviceList)[0]; | ||
| 759 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 760 | |||
| 761 | return undef unless $target; | ||
| 762 | |||
| 763 | $device->getlocation =~ m|(http://[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\:[0-9]+)|; | ||
| 764 | my $baseurl = $1; | ||
| 765 | my $content = get(join('/', $baseurl,$target)); | ||
| 766 | |||
| 767 | output($content); | ||
| 768 | } | ||
| 769 | |||
| 770 | |||
| 771 | # processImg | ||
| 772 | # | ||
| 773 | # | ||
| 774 | # | ||
| 775 | sub processImg { | ||
| 776 | my($r) = shift; | ||
| 777 | |||
| 778 | my(%in) = map { my($key, $val) = split('=', $_); $key => $val } (split('&', $r->args)); | ||
| 779 | |||
| 780 | my($imgfile) = $r->filename; | ||
| 781 | my ($name, $path, $ext) = fileparse($imgfile, qr{\..*}); | ||
| 782 | |||
| 783 | $path =~ s/\/$//g; | ||
| 784 | $ext =~ s/^\.//g; | ||
| 785 | |||
| 786 | # only rotate up to 360 degrees | ||
| 787 | if($in{'rot'} >= 360) { $in{'rot'} -= 360 }; | ||
| 788 | |||
| 789 | |||
| 790 | # only allow scaling to 25% and 200% of image size | ||
| 791 | if($in{'scale'} < -75) { $in{'scale'} = -75 }; | ||
| 792 | if($in{'scale'} > 100) { $in{'scale'} = 100 }; | ||
| 793 | |||
| 794 | if(%in) { | ||
| 795 | my($tmpfile) = "/tmp/$name." . time . ".$$.$ext"; | ||
| 796 | |||
| 797 | my $scale = $in{'scale'} + 100; | ||
| 798 | |||
| 799 | my($Img) = Image::Magick->new; | ||
| 800 | $Img->Read($imgfile); | ||
| 801 | $Img->Rotate(degrees=>$in{'rot'}) if($in{'rot'}); | ||
| 802 | $Img->Scale(geometry=>"${scale}%x${scale}%") if($in{'scale'}); | ||
| 803 | $Img->Write("$tmpfile"); | ||
| 804 | |||
| 805 | my($fh) = FileHandle->new("$tmpfile"); | ||
| 806 | |||
| 807 | unless($fh) { | ||
| 808 | $r->log_error("Couldn't open file '$tmpfile'"); | ||
| 809 | return SERVER_ERROR; | ||
| 810 | } | ||
| 811 | |||
| 812 | local $/; | ||
| 813 | output(<$fh>); | ||
| 814 | $fh->close; | ||
| 815 | |||
| 816 | unlink $tmpfile; | ||
| 817 | |||
| 818 | } else { | ||
| 819 | return DECLINED; | ||
| 820 | } | ||
| 821 | |||
| 822 | } | ||
| 823 | |||
| 824 | |||
| 825 | |||
| 826 | |||
| 827 | # getParentId | ||
| 828 | # | ||
| 829 | # | ||
| 830 | sub getParentId { | ||
| 831 | my($r, $dir) = @_; | ||
| 832 | |||
| 833 | my $retval; | ||
| 834 | |||
| 835 | if($dir =~ /\$/) { | ||
| 836 | my @tmp = split('\$', $dir); | ||
| 837 | pop @tmp; | ||
| 838 | $retval = join('$', @tmp); | ||
| 839 | } else { | ||
| 840 | $retval = 0; | ||
| 841 | } | ||
| 842 | |||
| 843 | $retval; | ||
| 844 | } | ||
| 845 | |||
| 846 | |||
| 847 | |||
| 848 | # getMetadata | ||
| 849 | # | ||
| 850 | # | ||
| 851 | sub getMetadata { | ||
| 852 | my($r, $id, $dev) = @_; | ||
| 853 | |||
| 854 | my($retval); | ||
| 855 | |||
| 856 | my($device) = (grep $_->getudn eq $dev, @gDeviceList)[0]; | ||
| 857 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 858 | |||
| 859 | $retval = upnpGetMetadata($r, $device, $id); | ||
| 860 | |||
| 861 | $retval; | ||
| 862 | } | ||
| 863 | |||
| 864 | |||
| 865 | |||
| 866 | # getFileList | ||
| 867 | # | ||
| 868 | # | ||
| 869 | sub getFileList { | ||
| 870 | my($r, $dir, $dev) = @_; | ||
| 871 | |||
| 872 | my(@retval) = (); | ||
| 873 | |||
| 874 | my($device) = (grep $_->getudn eq $dev, @gDeviceList)[0]; | ||
| 875 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 876 | |||
| 877 | @retval = upnpGetFileList($r, $device, $dir); | ||
| 878 | |||
| 879 | @retval; | ||
| 880 | } | ||
| 881 | |||
| 882 | |||
| 883 | # getDirectoryList | ||
| 884 | # | ||
| 885 | # | ||
| 886 | sub getDirectoryList { | ||
| 887 | my($r, $dir, $dev) = @_; | ||
| 888 | |||
| 889 | my(@retval) = (); | ||
| 890 | |||
| 891 | my $device = (grep $_->getudn eq $dev, @gDeviceList)[0]; | ||
| 892 | return undef unless ref $device eq 'Net::UPnP::Device'; | ||
| 893 | |||
| 894 | @retval = upnpGetDirectory($r, $device, $dir); | ||
| 895 | |||
| 896 | @retval; | ||
| 897 | } | ||
| 898 | |||
| 899 | |||
| 900 | |||
| 901 | sub output { push(@gOutput, @_); } | ||
| 902 | |||
| 903 | |||
| 904 | |||
| 905 | 1; |