| 1 | package Dancer::SearchApp::Extractor::Image; | ||
| 2 | use strict; | ||
| 3 | use Carp 'croak'; | ||
| 4 | use Promises 'deferred'; | ||
| 5 | #no warnings 'experimental'; | ||
| 6 | #use feature 'signatures'; | ||
| 7 | use Image::ExifTool; | ||
| 8 | use POSIX 'strftime'; | ||
| 9 | use HTML::Entities; | ||
| 10 | |||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION = '0.06'; | ||
| 13 | |||
| 14 | =head1 File types | ||
| 15 | |||
| 16 | This uses L<Image::ExifTool> and thus likely handles most image files. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | sub examine { | ||
| 21 | my ( $class, %options ) = @_; | ||
| 22 | my $info = $options{info}; | ||
| 23 | |||
| 24 | my $result = deferred; | ||
| 25 | my $mime_type = $info->meta->{"Content-Type"}; | ||
| 26 | |||
| 27 | if( $mime_type =~ m!^image/.*$! ) { | ||
| 28 | my %res = ( | ||
| 29 | url => $options{ url }, | ||
| 30 | file => $options{ filename }, | ||
| 31 | folder => $options{ folder }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | my $info = Image::ExifTool->new; | ||
| 35 | my $file = $options{ filename }; | ||
| 36 | |||
| 37 | # If we have a filename, use that | ||
| 38 | if( $file ) { | ||
| 39 | |||
| 40 | $info->ExtractInfo("$file"); | ||
| 41 | |||
| 42 | my $ctime = (stat $file)[10]; | ||
| 43 | $res{ creation_date } = strftime('%Y-%m-%d %H:%M:%S', localtime($ctime)); | ||
| 44 | |||
| 45 | } elsif( $options{ content }) { | ||
| 46 | $info->ExtractInfo($options{ content }); | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | if( $info ) { | ||
| 51 | |||
| 52 | # go go go | ||
| 53 | $res{ title } = $info->GetValue( 'Title' ) || $file->basename; | ||
| 54 | $res{ author } = $info->GetValue( 'Author' ); | ||
| 55 | $res{ language } = 'en'; # ... | ||
| 56 | $res{ content } = encode_entities( join "\n", map { $_ => $info->GetValue($_) } $info->GetFoundTags('File'), $file->basename ); | ||
| 57 | # We should also generate/store a (tiny) thumbnail here | ||
| 58 | # to generate an "HTML" page for the file | ||
| 59 | |||
| 60 | $res{ mime_type } = $mime_type; | ||
| 61 | |||
| 62 | $result->resolve( \%res ); | ||
| 63 | } else { | ||
| 64 | # Nothing found | ||
| 65 | $result->resolve(); | ||
| 66 | } | ||
| 67 | } else { | ||
| 68 | $result->resolve(); | ||
| 69 | } | ||
| 70 | |||
| 71 | $result->promise | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |