| 1 | package File::Sticker::Scribe::Gif; | ||
| 2 | $File::Sticker::Scribe::Gif::VERSION = '4.9'; | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | File::Sticker::Scribe::Gif - read, write and standardize meta-data from GIF file | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | version 4.9 | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use File::Sticker::Scribe::Gif; | ||
| 14 | |||
| 15 | my $obj = File::Sticker::Scribe::Gif->new(%args); | ||
| 16 | |||
| 17 | my %meta = $obj->read_meta($filename); | ||
| 18 | |||
| 19 | $obj->write_meta(%args); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This will read and write meta-data from GIF files, and standardize it to a common | ||
| 24 | nomenclature, such as "tags" for things called tags, or Keywords or Subject etc. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use common::sense; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 0 | ▶ | use File::LibMagic; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 32 | use Image::ExifTool qw(:Public); | ||
| 33 | use YAML::Any; | ||
| 34 | use File::Spec; | ||
| 35 | |||
| 36 | use parent qw(File::Sticker::Scribe); | ||
| 37 | |||
| 38 | # FOR DEBUGGING | ||
| 39 | =head1 DEBUGGING | ||
| 40 | |||
| 41 | =head2 whoami | ||
| 42 | |||
| 43 | Used for debugging info | ||
| 44 | |||
| 45 | =cut | ||
| 46 | sub whoami { ( caller(1) )[3] } | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 priority | ||
| 51 | |||
| 52 | The priority of this scribe. Scribes with higher priority get tried first. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub priority { | ||
| 57 | my $class = shift; | ||
| 58 | return 2; | ||
| 59 | } # priority | ||
| 60 | |||
| 61 | =head2 allowed_file | ||
| 62 | |||
| 63 | If this scribe can be used for the given file, then this returns true. | ||
| 64 | File must be a GIF image. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub allowed_file { | ||
| 69 | my $self = shift; | ||
| 70 | my $file = shift; | ||
| 71 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 72 | |||
| 73 | $file = $self->_get_the_real_file(filename=>$file); | ||
| 74 | my $ft = $self->{file_magic}->info_from_filename($file); | ||
| 75 | if ($ft->{mime_type} eq 'image/gif') | ||
| 76 | { | ||
| 77 | say STDERR 'Scribe ' . $self->name() . ' allows filetype ' . $ft->{mime_type} . ' of ' . $file if $self->{verbose} > 1; | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | return 0; | ||
| 81 | } # allowed_file | ||
| 82 | |||
| 83 | =head2 known_fields | ||
| 84 | |||
| 85 | Returns the fields which this scribe knows about. | ||
| 86 | This scribe has no limitations, because all the fields are freeform fields. | ||
| 87 | |||
| 88 | my $known_fields = $scribe->known_fields(); | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | sub known_fields { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | if ($self->{wanted_fields}) | ||
| 96 | { | ||
| 97 | return $self->{wanted_fields}; | ||
| 98 | } | ||
| 99 | return {}; | ||
| 100 | } # known_fields | ||
| 101 | |||
| 102 | =head2 readonly_fields | ||
| 103 | |||
| 104 | Returns the fields which this scribe knows about, which can't be overwritten, | ||
| 105 | but are allowed to be "wanted" fields. Things like file-size etc. | ||
| 106 | |||
| 107 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub readonly_fields { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | return { | ||
| 115 | date=>'TEXT', | ||
| 116 | filesize=>'NUMBER', | ||
| 117 | imagesize=>'TEXT', | ||
| 118 | imageheight=>'NUMBER', | ||
| 119 | imagewidth=>'NUMBER', | ||
| 120 | megapixels=>'NUMBER'}; | ||
| 121 | } # readonly_fields | ||
| 122 | |||
| 123 | =head2 read_meta | ||
| 124 | |||
| 125 | Read the meta-data from the given file. | ||
| 126 | |||
| 127 | my $meta = $obj->read_meta($filename); | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub read_meta { | ||
| 132 | my $self = shift; | ||
| 133 | my $filename = shift; | ||
| 134 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 135 | |||
| 136 | my $filename = $self->_get_the_real_file($filename); | ||
| 137 | my $info = ImageInfo($filename); | ||
| 138 | my %meta = (); | ||
| 139 | |||
| 140 | # There are multiple fields which could be used as a file date. | ||
| 141 | # Check through them until you find a non-empty one. | ||
| 142 | my $date = ''; | ||
| 143 | foreach my $field (qw(CreateDate DateTimeOriginal Date PublishedDate PublicationDate)) | ||
| 144 | { | ||
| 145 | if (exists $info->{$field} and $info->{$field} and !$date) | ||
| 146 | { | ||
| 147 | $date = $info->{$field}; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | $meta{date} = $date if $date; | ||
| 151 | |||
| 152 | # There are SOOOOOO many fields in image data, just remember a subset of them | ||
| 153 | foreach my $field (qw( | ||
| 154 | FileSize | ||
| 155 | ImageHeight | ||
| 156 | ImageSize | ||
| 157 | ImageWidth | ||
| 158 | Megapixels | ||
| 159 | )) | ||
| 160 | { | ||
| 161 | if (exists $info->{$field} and $info->{$field}) | ||
| 162 | { | ||
| 163 | $meta{lc($field)} = $info->{$field}; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | # ------------------------------------------------- | ||
| 168 | # Freeform Fields | ||
| 169 | # These are stored as YAML data in the Comment field. | ||
| 170 | # ------------------------------------------------- | ||
| 171 | if (exists $info->{Comment} and $info->{Comment}) | ||
| 172 | { | ||
| 173 | say STDERR "Comment=", $info->{Comment} if $self->{verbose} > 2; | ||
| 174 | my $data; | ||
| 175 | eval {$data = Load($info->{Comment});}; | ||
| 176 | if ($@) | ||
| 177 | { | ||
| 178 | warn __PACKAGE__, " Load of YAML data failed: $@"; | ||
| 179 | } | ||
| 180 | elsif (!$data) | ||
| 181 | { | ||
| 182 | warn __PACKAGE__, " no legal YAML"; | ||
| 183 | } | ||
| 184 | else # okay | ||
| 185 | { | ||
| 186 | foreach my $field (sort keys %{$data}) | ||
| 187 | { | ||
| 188 | $meta{$field} = $data->{$field}; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | return \%meta; | ||
| 194 | } # read_meta | ||
| 195 | |||
| 196 | =head2 delete_field_from_file | ||
| 197 | |||
| 198 | Completely remove the given field. This does no checking. | ||
| 199 | |||
| 200 | $scribe->delete_field_from_file(filename=>$filename,field=>$field); | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub delete_field_from_file { | ||
| 205 | my $self = shift; | ||
| 206 | my %args = @_; | ||
| 207 | say STDERR whoami(), " field=$args{field}" if $self->{verbose} > 2; | ||
| 208 | |||
| 209 | my $filename = $args{filename}; | ||
| 210 | my $field = $args{field}; | ||
| 211 | |||
| 212 | my $fdata = $self->_load_meta($filename); | ||
| 213 | if (exists $fdata->{$field}) | ||
| 214 | { | ||
| 215 | delete $fdata->{$field}; | ||
| 216 | $self->_write_meta(filename=>$filename,meta=>$fdata); | ||
| 217 | } | ||
| 218 | |||
| 219 | } # delete_field_from_file | ||
| 220 | |||
| 221 | =head2 replace_all_meta | ||
| 222 | |||
| 223 | Overwrite the existing meta-data with that given. | ||
| 224 | THIS DOES NOT CHECK that readonly fields are not being overwritten. | ||
| 225 | |||
| 226 | (This supercedes the parent method because we can do it more efficiently this way) | ||
| 227 | |||
| 228 | $scribe->replace_all_meta(filename=>$filename,meta=>\%meta); | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub replace_all_meta { | ||
| 233 | my $self = shift; | ||
| 234 | my %args = @_; | ||
| 235 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 236 | |||
| 237 | $self->_write_meta(%args); | ||
| 238 | |||
| 239 | } # replace_all_meta | ||
| 240 | |||
| 241 | =head2 replace_one_field | ||
| 242 | |||
| 243 | Overwrite the given field. This does no checking. | ||
| 244 | |||
| 245 | $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value); | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub replace_one_field { | ||
| 250 | my $self = shift; | ||
| 251 | my %args = @_; | ||
| 252 | say STDERR whoami(), " field=$args{field},value=$args{value}" if $self->{verbose} > 2; | ||
| 253 | |||
| 254 | my $filename = $args{filename}; | ||
| 255 | my $field = $args{field}; | ||
| 256 | my $value = $args{value}; | ||
| 257 | |||
| 258 | my $info = $self->_load_meta($filename); | ||
| 259 | $info->{$field} = $value; | ||
| 260 | $self->_write_meta(filename=>$filename,meta=>$info); | ||
| 261 | |||
| 262 | } # replace_one_field | ||
| 263 | |||
| 264 | =head1 Helper Functions | ||
| 265 | |||
| 266 | Private interface. | ||
| 267 | |||
| 268 | =head2 _load_meta | ||
| 269 | |||
| 270 | Quick non-checking loading of the meta-data. Does not standardize any fields. | ||
| 271 | |||
| 272 | my $meta = $self->_load_meta($filename); | ||
| 273 | |||
| 274 | =cut | ||
| 275 | sub _load_meta { | ||
| 276 | my $self = shift; | ||
| 277 | my $filename = shift; | ||
| 278 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 279 | |||
| 280 | $filename = $self->_get_the_real_file(filename=>$filename); | ||
| 281 | my $et = new Image::ExifTool; | ||
| 282 | $et->Options(ListSep=>',',ListSplit=>','); | ||
| 283 | $et->ExtractInfo($filename); | ||
| 284 | my $yaml_str = $et->GetValue('Comment'); | ||
| 285 | my $meta; | ||
| 286 | eval {$meta = Load($yaml_str);}; | ||
| 287 | if ($@) | ||
| 288 | { | ||
| 289 | warn __PACKAGE__, " Load of data failed: $@"; | ||
| 290 | return {}; | ||
| 291 | } | ||
| 292 | if (!$meta) | ||
| 293 | { | ||
| 294 | warn __PACKAGE__, " no legal YAML"; | ||
| 295 | return {}; | ||
| 296 | } | ||
| 297 | return $meta; | ||
| 298 | } # _load_meta | ||
| 299 | |||
| 300 | =head2 _write_meta | ||
| 301 | |||
| 302 | Write the meta-data as YAML data in the Comment field. | ||
| 303 | This overwrites whatever is there, it does not check. | ||
| 304 | This saves multi-value comma-separated fields as arrays. | ||
| 305 | |||
| 306 | $self->_write_meta(meta=>\%meta,filename=>$filename); | ||
| 307 | |||
| 308 | =cut | ||
| 309 | sub _write_meta { | ||
| 310 | my $self = shift; | ||
| 311 | my %args = @_; | ||
| 312 | |||
| 313 | my $filename = $self->_get_the_real_file(filename=>$args{filename}); | ||
| 314 | my $meta = $args{meta}; | ||
| 315 | my $et = new Image::ExifTool; | ||
| 316 | $et->Options(ListSep=>',',ListSplit=>','); | ||
| 317 | $et->ExtractInfo($filename); | ||
| 318 | |||
| 319 | # restore multi-value comma-separated fields to arrays | ||
| 320 | foreach my $fn (keys %{$self->{wanted_fields}}) | ||
| 321 | { | ||
| 322 | if ($self->{wanted_fields}->{$fn} eq 'MULTI' | ||
| 323 | and exists $meta->{$fn} | ||
| 324 | and defined $meta->{$fn} | ||
| 325 | and $meta->{$fn} =~ /,/) | ||
| 326 | { | ||
| 327 | my @vals = split(/,/, $meta->{$fn}); | ||
| 328 | $meta->{$fn} = \@vals; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | my $yaml_str = Dump($meta); | ||
| 332 | say STDERR "yaml_str=$yaml_str" if $self->{verbose} > 2; | ||
| 333 | my $success = $et->SetNewValue('Comment', $yaml_str); | ||
| 334 | if ($success) | ||
| 335 | { | ||
| 336 | $et->WriteInfo($filename); | ||
| 337 | } | ||
| 338 | |||
| 339 | } # _write_meta | ||
| 340 | |||
| 341 | =head2 _get_the_real_file | ||
| 342 | |||
| 343 | If the file is a soft link, look for the file it is pointing to | ||
| 344 | (because ExifTool behaves badly with soft links). | ||
| 345 | |||
| 346 | my $real_file = $scribe->_get_the_real_file(filename=>$filename); | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub _get_the_real_file { | ||
| 351 | my $self = shift; | ||
| 352 | my %args = @_; | ||
| 353 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 354 | |||
| 355 | my $filename = $args{filename}; | ||
| 356 | # ExifTool has a wicked habit of replacing soft-linked files with the | ||
| 357 | # contents of the file rather than honouring the link. While using the | ||
| 358 | # exiftool script offers -overwrite_original_in_place to deal with this, | ||
| 359 | # the Perl module does not appear to have such an option available. | ||
| 360 | |||
| 361 | # So the way to get around this is to check if the file is a soft link, and | ||
| 362 | # if it is, find the real file, and write to that. And if *that* file is | ||
| 363 | # a soft link... go down the rabbit-hole as deep as it goes. | ||
| 364 | |||
| 365 | while (-l $filename) | ||
| 366 | { | ||
| 367 | my $realfile = readlink $filename; | ||
| 368 | if (-f $realfile) | ||
| 369 | { | ||
| 370 | $filename = $realfile; | ||
| 371 | } | ||
| 372 | else # give up and die | ||
| 373 | { | ||
| 374 | croak "$args{filename} is soft link, cannot find $realfile"; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | return $filename; | ||
| 379 | } # _get_the_real_file | ||
| 380 | |||
| 381 | =head1 BUGS | ||
| 382 | |||
| 383 | Please report any bugs or feature requests to the author. | ||
| 384 | |||
| 385 | =cut | ||
| 386 | |||
| 387 | 1; # End of File::Sticker::Scribe::Gif | ||
| 388 | __END__ | ||