| 1 | package File::Sticker::Scribe::Exif; | ||
| 2 | $File::Sticker::Scribe::Exif::VERSION = '4.9'; | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | File::Sticker::Scribe::Exif - read, write and standardize meta-data from EXIF file | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | version 4.9 | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use File::Sticker::Scribe::Exif; | ||
| 14 | |||
| 15 | my $obj = File::Sticker::Scribe::Exif->new(%args); | ||
| 16 | |||
| 17 | my %meta = $obj->read_meta($filename); | ||
| 18 | |||
| 19 | $obj->write_meta(%args); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This will write meta-data from EXIF 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 Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 1 | ▶ | use common::sense; |
|
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 Image::ExifTool::XMP; | ||
| 34 | use YAML::Any; | ||
| 35 | use File::Spec; | ||
| 36 | use List::MoreUtils qw(uniq); | ||
| 37 | |||
| 38 | use parent qw(File::Sticker::Scribe); | ||
| 39 | |||
| 40 | =head1 DEBUGGING | ||
| 41 | |||
| 42 | =head2 whoami | ||
| 43 | |||
| 44 | Used for debugging info | ||
| 45 | |||
| 46 | =cut | ||
| 47 | sub whoami { ( caller(1) )[3] } | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =head2 init | ||
| 52 | |||
| 53 | Initialize the object. | ||
| 54 | |||
| 55 | $scribe->init(wanted_fields=>{title=>'TEXT',count=>'NUMBER',tags=>'MULTI'}); | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub init { | ||
| 60 | my $self = shift; | ||
| 61 | my %parameters = @_; | ||
| 62 | |||
| 63 | $self->SUPER::init(%parameters); | ||
| 64 | |||
| 65 | } # init | ||
| 66 | |||
| 67 | =head2 priority | ||
| 68 | |||
| 69 | The priority of this writer. Scribes with higher priority get tried first. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub priority { | ||
| 74 | my $class = shift; | ||
| 75 | return 1; | ||
| 76 | } # priority | ||
| 77 | |||
| 78 | =head2 allowed_file | ||
| 79 | |||
| 80 | If this scribe can be used for the given file, then this returns true. | ||
| 81 | File must be one of: PDF or an image which is not a GIF. | ||
| 82 | (GIF files need to be treated separately) | ||
| 83 | (EPUB files need to be treated separately) | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub allowed_file { | ||
| 88 | my $self = shift; | ||
| 89 | my $file = shift; | ||
| 90 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 91 | |||
| 92 | $file = $self->_get_the_real_file(filename=>$file); | ||
| 93 | my $ft = $self->{file_magic}->info_from_filename($file); | ||
| 94 | if ($ft->{mime_type} =~ /(image|pdf)/ and $ft->{mime_type} !~ /gif/) | ||
| 95 | { | ||
| 96 | say STDERR 'Scribe ' . $self->name() . ' allows filetype ' . $ft->{mime_type} . ' of ' . $file if $self->{verbose} > 1; | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | return 0; | ||
| 100 | } # allowed_file | ||
| 101 | |||
| 102 | =head2 known_fields | ||
| 103 | |||
| 104 | Returns the fields which this scribe knows about. | ||
| 105 | |||
| 106 | my $known_fields = $scribe->known_fields(); | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub known_fields { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | return { | ||
| 114 | title=>'TEXT', | ||
| 115 | creator=>'TEXT', | ||
| 116 | description=>'TEXT', | ||
| 117 | location=>'TEXT', | ||
| 118 | tags=>'MULTI', | ||
| 119 | %{$self->{wanted_fields}}, | ||
| 120 | }; | ||
| 121 | } # known_fields | ||
| 122 | |||
| 123 | =head2 readonly_fields | ||
| 124 | |||
| 125 | Returns the fields which this scribe knows about, which can't be overwritten, | ||
| 126 | but are allowed to be "wanted" fields. Things like file-size etc. | ||
| 127 | |||
| 128 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub readonly_fields { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | return { | ||
| 136 | date=>'TEXT', | ||
| 137 | copyright=>'TEXT', | ||
| 138 | flash=>'TEXT', | ||
| 139 | filesize=>'NUMBER', | ||
| 140 | imagesize=>'TEXT', | ||
| 141 | imageheight=>'NUMBER', | ||
| 142 | imagewidth=>'NUMBER', | ||
| 143 | megapixels=>'NUMBER'}; | ||
| 144 | } # readonly_fields | ||
| 145 | |||
| 146 | =head2 read_meta | ||
| 147 | |||
| 148 | Read the meta-data from the given file. | ||
| 149 | |||
| 150 | my $meta = $obj->read_meta($filename); | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub read_meta { | ||
| 155 | my $self = shift; | ||
| 156 | my $filename = shift; | ||
| 157 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 158 | |||
| 159 | $filename = $self->_get_the_real_file(filename=>$filename); | ||
| 160 | my $exif_options = {DateFormat => "%Y-%m-%d %H:%M:%S"}; | ||
| 161 | my $info = ImageInfo($filename,$exif_options); | ||
| 162 | my %meta = (); | ||
| 163 | |||
| 164 | # Check if this is a Gutenberg book; they have quirks. | ||
| 165 | my $is_gutenberg_book = 0; | ||
| 166 | if (exists $info->{Identifier} | ||
| 167 | and $info->{'Identifier'} =~ m!http://www.gutenberg.org/ebooks/\d+!) | ||
| 168 | { | ||
| 169 | $is_gutenberg_book = 1; | ||
| 170 | # If this is a Gutenberg book, the Identifier holds the correct URL | ||
| 171 | $meta{'url'} = $info->{'Identifier'}; | ||
| 172 | } | ||
| 173 | # There are multiple fields which could be used as a file "description". | ||
| 174 | # Check through them until you find a non-empty one. | ||
| 175 | my $description = ''; | ||
| 176 | foreach my $field (qw(Caption-Abstract Comment UserComment ImageDescription Description MetadataDescription)) | ||
| 177 | { | ||
| 178 | if (exists $info->{$field} | ||
| 179 | and $info->{$field} | ||
| 180 | and $info->{$field} !~ /^---/ # YAML - not a description! | ||
| 181 | and !$description) | ||
| 182 | { | ||
| 183 | $description = $info->{$field}; | ||
| 184 | $description =~ s/\n$//; # remove trailing newlines | ||
| 185 | } | ||
| 186 | } | ||
| 187 | $meta{description} = $description if $description; | ||
| 188 | |||
| 189 | # There are multiple fields which could be used as a file content creator. | ||
| 190 | # Check through them until you find a non-empty one. | ||
| 191 | my $creator = ''; | ||
| 192 | foreach my $field (qw(Author Artist Creator MetadataCreator)) | ||
| 193 | { | ||
| 194 | if (exists $info->{$field} and $info->{$field} and !$creator) | ||
| 195 | { | ||
| 196 | $creator = $info->{$field}; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | $meta{creator} = $creator if $creator; | ||
| 200 | |||
| 201 | # There are multiple fields which could be used as a copyright notice. | ||
| 202 | # Check through them until you find a non-empty one. | ||
| 203 | my $copyright = ''; | ||
| 204 | foreach my $field (qw(License Rights MetadataRights)) | ||
| 205 | { | ||
| 206 | if (exists $info->{$field} and $info->{$field} and !$copyright) | ||
| 207 | { | ||
| 208 | $copyright = $info->{$field}; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | $meta{copyright} = $copyright if $copyright; | ||
| 212 | |||
| 213 | # Alt text! | ||
| 214 | my $alt_text = ''; | ||
| 215 | foreach my $field (qw(AltTextAccessibility)) | ||
| 216 | { | ||
| 217 | if (exists $info->{$field} and $info->{$field} and !$alt_text) | ||
| 218 | { | ||
| 219 | $alt_text = $info->{$field}; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | $meta{alt_text} = $alt_text if $alt_text; | ||
| 223 | |||
| 224 | # The URL could be from the Source or the Identifier | ||
| 225 | # Check through them until you find a non-empty one which contains an actual URL | ||
| 226 | foreach my $field (qw(Source Identifier MetadataIdentifier)) | ||
| 227 | { | ||
| 228 | if (exists $info->{$field} | ||
| 229 | and $info->{$field} | ||
| 230 | and $info->{$field} =~ /^http/ | ||
| 231 | and !exists $meta{url}) | ||
| 232 | { | ||
| 233 | $meta{url} = $info->{$field}; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | # CreateDate is going to be treated as a separate field | ||
| 238 | my $create_date = ''; | ||
| 239 | foreach my $field (qw(CreateDate MetadataCreateDate)) | ||
| 240 | { | ||
| 241 | if (exists $info->{$field} and $info->{$field} and !$create_date) | ||
| 242 | { | ||
| 243 | $create_date = $info->{$field}; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | $meta{create_date} = $create_date if $create_date; | ||
| 247 | |||
| 248 | # There are multiple fields which could be used as a file date. | ||
| 249 | # Check through them until you find a non-empty one. | ||
| 250 | my $date = ''; | ||
| 251 | foreach my $field (qw(DateTimeOriginal Date PublishedDate PublicationDate)) | ||
| 252 | { | ||
| 253 | if (exists $info->{$field} and $info->{$field} and !$date) | ||
| 254 | { | ||
| 255 | $date = $info->{$field}; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | $meta{date} = $date if $date; | ||
| 259 | |||
| 260 | # Use a consistent naming for tag fields. | ||
| 261 | # Combine the tag-like fields together. | ||
| 262 | # Preserve the order and check for dupicates later with uniq | ||
| 263 | # NOTE: a field may appear more than once with a number appended! | ||
| 264 | # So we need to check all the keys against a regex. | ||
| 265 | my @tags = (); | ||
| 266 | foreach my $field (sort keys %{$info}) | ||
| 267 | { | ||
| 268 | if ($field =~ /^(Keywords|Subject)$/ | ||
| 269 | and $info->{$field}) | ||
| 270 | { | ||
| 271 | my $val = $info->{$field}; | ||
| 272 | my @these_tags; | ||
| 273 | if ($is_gutenberg_book) | ||
| 274 | { | ||
| 275 | # gutenberg tags are multi-word, separated by comma-space or ' -- ' | ||
| 276 | # and can have parens in them | ||
| 277 | $val =~ s/\(//g; | ||
| 278 | $val =~ s/\)//g; | ||
| 279 | $val =~ s/\s--\s/,/g; | ||
| 280 | @these_tags = split(/,\s?/, $val); | ||
| 281 | } | ||
| 282 | else # comma-separated | ||
| 283 | { | ||
| 284 | @these_tags = split(/,\s*/, $val); | ||
| 285 | } | ||
| 286 | foreach my $t (@these_tags) | ||
| 287 | { | ||
| 288 | $t =~ s/ - / /g; # remove isolated dashes | ||
| 289 | $t =~ s/[^\w\s,-]//g; # remove non-word characters | ||
| 290 | push @tags, $t; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | # Are there any tags? | ||
| 295 | if (@tags) | ||
| 296 | { | ||
| 297 | # remove duplicates | ||
| 298 | $meta{tags} = [uniq @tags]; | ||
| 299 | } | ||
| 300 | else # remove empty tag-field | ||
| 301 | { | ||
| 302 | delete $meta{tags}; | ||
| 303 | } | ||
| 304 | |||
| 305 | # There are SOOOOOO many fields in EXIF data, just remember a subset of them | ||
| 306 | foreach my $field (qw( | ||
| 307 | Flash | ||
| 308 | ImageHeight | ||
| 309 | ImageSize | ||
| 310 | ImageWidth | ||
| 311 | Megapixels | ||
| 312 | PageCount | ||
| 313 | Location | ||
| 314 | Title | ||
| 315 | )) | ||
| 316 | { | ||
| 317 | if (exists $info->{$field} and $info->{$field}) | ||
| 318 | { | ||
| 319 | $meta{lc($field)} = $info->{$field}; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | # ------------------------------------------------- | ||
| 324 | # Freeform Fields | ||
| 325 | # These are stored as YAML data in the Instructions field. | ||
| 326 | # They used to be stored in the XMP:Description field, | ||
| 327 | # before then the ImageDescription field, before then the UserComment field | ||
| 328 | # so they need to be checked too. | ||
| 329 | # ------------------------------------------------- | ||
| 330 | if (exists $info->{Instructions} | ||
| 331 | and $info->{Instructions} | ||
| 332 | and $info->{Instructions} =~ /^---/) | ||
| 333 | { | ||
| 334 | say STDERR sprintf("Instructions='%s'", $info->{Instructions}) if $self->{verbose} > 2; | ||
| 335 | my $data; | ||
| 336 | eval {$data = Load($info->{Instructions});}; | ||
| 337 | if ($@) | ||
| 338 | { | ||
| 339 | warn __PACKAGE__, " Load of YAML data failed: $@"; | ||
| 340 | } | ||
| 341 | elsif (!$data) | ||
| 342 | { | ||
| 343 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 2; | ||
| 344 | } | ||
| 345 | else # okay | ||
| 346 | { | ||
| 347 | foreach my $field (sort keys %{$data}) | ||
| 348 | { | ||
| 349 | $meta{$field} = $data->{$field}; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | elsif (exists $info->{Description} | ||
| 354 | and $info->{Description} | ||
| 355 | and $info->{Description} =~ /^---/) | ||
| 356 | { | ||
| 357 | say STDERR sprintf("Description='%s'", $info->{Description}) if $self->{verbose} > 2; | ||
| 358 | my $data; | ||
| 359 | eval {$data = Load($info->{Description});}; | ||
| 360 | if ($@) | ||
| 361 | { | ||
| 362 | warn __PACKAGE__, " Load of YAML data failed: $@"; | ||
| 363 | } | ||
| 364 | elsif (!$data) | ||
| 365 | { | ||
| 366 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 2; | ||
| 367 | } | ||
| 368 | else # okay | ||
| 369 | { | ||
| 370 | foreach my $field (sort keys %{$data}) | ||
| 371 | { | ||
| 372 | $meta{$field} = $data->{$field}; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | elsif (exists $info->{ImageDescription} | ||
| 377 | and $info->{ImageDescription} | ||
| 378 | and $info->{ImageDescription} =~ /^---/) | ||
| 379 | { | ||
| 380 | say STDERR sprintf("ImageDescription='%s'", $info->{ImageDescription}) if $self->{verbose} > 2; | ||
| 381 | my $data; | ||
| 382 | eval {$data = Load($info->{ImageDescription});}; | ||
| 383 | if ($@) | ||
| 384 | { | ||
| 385 | warn __PACKAGE__, " Load of YAML data failed: $@"; | ||
| 386 | } | ||
| 387 | elsif (!$data) | ||
| 388 | { | ||
| 389 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 2; | ||
| 390 | } | ||
| 391 | else # okay | ||
| 392 | { | ||
| 393 | foreach my $field (sort keys %{$data}) | ||
| 394 | { | ||
| 395 | $meta{$field} = $data->{$field}; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 | elsif (exists $info->{UserComment} | ||
| 400 | and $info->{UserComment} | ||
| 401 | and $info->{UserComment} =~ /^---/) | ||
| 402 | { | ||
| 403 | say STDERR sprintf("UserComment='%s'", $info->{UserComment}) if $self->{verbose} > 2; | ||
| 404 | my $data; | ||
| 405 | eval {$data = Load($info->{UserComment});}; | ||
| 406 | if ($@) | ||
| 407 | { | ||
| 408 | warn __PACKAGE__, " Load of YAML data failed: $@"; | ||
| 409 | } | ||
| 410 | elsif (!$data) | ||
| 411 | { | ||
| 412 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 2; | ||
| 413 | } | ||
| 414 | else # okay | ||
| 415 | { | ||
| 416 | foreach my $field (sort keys %{$data}) | ||
| 417 | { | ||
| 418 | $meta{$field} = $data->{$field}; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | return \%meta; | ||
| 424 | } # read_meta | ||
| 425 | |||
| 426 | =head1 Helper Functions | ||
| 427 | |||
| 428 | Private interface. | ||
| 429 | |||
| 430 | =head2 replace_one_field | ||
| 431 | |||
| 432 | Overwrite the given field. This does no checking. | ||
| 433 | |||
| 434 | $writer->replace_one_field(filename=>$filename,field=>$field,value=>$value); | ||
| 435 | |||
| 436 | =cut | ||
| 437 | |||
| 438 | sub replace_one_field { | ||
| 439 | my $self = shift; | ||
| 440 | my %args = @_; | ||
| 441 | say STDERR whoami(), " field=$args{field},value=$args{value}" if $self->{verbose} > 2; | ||
| 442 | |||
| 443 | my $filename = $self->_get_the_real_file(filename=>$args{filename}); | ||
| 444 | my $field = $args{field}; | ||
| 445 | my $value = $args{value}; | ||
| 446 | |||
| 447 | my $ft = $self->{file_magic}->info_from_filename($filename); | ||
| 448 | my $et = new Image::ExifTool; | ||
| 449 | $et->Options(ListSep=>',',ListSplit=>','); | ||
| 450 | $et->ExtractInfo($filename); | ||
| 451 | |||
| 452 | my $success; | ||
| 453 | if ($field eq 'creator') | ||
| 454 | { | ||
| 455 | $success = $et->SetNewValue('Creator', $value); | ||
| 456 | } | ||
| 457 | elsif ($field eq 'copyright') | ||
| 458 | { | ||
| 459 | $success = $et->SetNewValue('License', $value); | ||
| 460 | } | ||
| 461 | elsif ($field eq 'title') | ||
| 462 | { | ||
| 463 | $success = $et->SetNewValue('Title', $value); | ||
| 464 | } | ||
| 465 | elsif ($field eq 'location') | ||
| 466 | { | ||
| 467 | $success = $et->SetNewValue('Location', $value); | ||
| 468 | } | ||
| 469 | elsif ($field eq 'create_date') | ||
| 470 | { | ||
| 471 | $success = $et->SetNewValue('CreateDate', $value); | ||
| 472 | } | ||
| 473 | elsif ($field eq 'alt_text') | ||
| 474 | { | ||
| 475 | $success = $et->SetNewValue('AltTextAccessibility', $value); | ||
| 476 | } | ||
| 477 | elsif ($field eq 'description') | ||
| 478 | { | ||
| 479 | # Okay, here's the messy relationship between the description, | ||
| 480 | # the freeform data, and GIMP. | ||
| 481 | # | ||
| 482 | # I originally wrote the freeform data in the UserComment field, and | ||
| 483 | # the description into the Description and Comment fields, then I found | ||
| 484 | # that GIMP uses the UserComment field as the description field at a | ||
| 485 | # higher priority than the Comment field. | ||
| 486 | # | ||
| 487 | # So then I wrote the freeform data in the ImageDescription field, | ||
| 488 | # then I found that GIMP ALSO uses THAT field as the description field | ||
| 489 | # at a higher priority than the Comment field. | ||
| 490 | # | ||
| 491 | # And that GIMP overwrites ALL THREE fields (Comment, UserComment, | ||
| 492 | # ImageDescription) with what it considers the description | ||
| 493 | # when saving a file. | ||
| 494 | # | ||
| 495 | # So then I used the XMP:Description (Description) field for the | ||
| 496 | # freeform data, because GIMP neither reads nor overwrites that. | ||
| 497 | # But other things do... | ||
| 498 | # | ||
| 499 | # So now I use the Instructions field. | ||
| 500 | |||
| 501 | # Before the decription is written, the freeform data | ||
| 502 | # needs to be converted to its new home. | ||
| 503 | $self->_convert_freeform_data(exif=>$et); | ||
| 504 | |||
| 505 | # GIMP reads and overwrites the Comment, UserComment | ||
| 506 | # and ImageDescription fields, so we need to do that too. | ||
| 507 | # Escpecially since GIMP does not look at the Comment field | ||
| 508 | # if one of the other two is not empty. | ||
| 509 | $success = $et->SetNewValue('UserComment', $value); | ||
| 510 | $success = $et->SetNewValue('ImageDescription', $value); | ||
| 511 | |||
| 512 | if ($ft->{mime_type} =~ /image\/jpeg/) | ||
| 513 | { | ||
| 514 | $success = $et->SetNewValue('Comment', $value); | ||
| 515 | } | ||
| 516 | } | ||
| 517 | elsif ($field eq 'tags') | ||
| 518 | { | ||
| 519 | if (ref $value eq 'ARRAY') | ||
| 520 | { | ||
| 521 | $success = $et->SetNewValue('Keywords', $value); | ||
| 522 | $success = $et->SetNewValue('Subject', $value); | ||
| 523 | } | ||
| 524 | else | ||
| 525 | { | ||
| 526 | my @tags = split(/,/,$value); | ||
| 527 | $success = $et->SetNewValue('Keywords', \@tags); | ||
| 528 | $success = $et->SetNewValue('Subject', \@tags); | ||
| 529 | } | ||
| 530 | } | ||
| 531 | else # freeform field | ||
| 532 | { | ||
| 533 | # Need to read all the YAML, change this field, and write it again | ||
| 534 | my $fdata = $self->_read_freeform_data(exif=>$et); | ||
| 535 | $fdata->{$field} = $value; | ||
| 536 | $success = $self->_write_freeform_data(newdata=>$fdata,exif=>$et); | ||
| 537 | } | ||
| 538 | |||
| 539 | if ($success) | ||
| 540 | { | ||
| 541 | $et->WriteInfo($filename); | ||
| 542 | } | ||
| 543 | return $success; | ||
| 544 | } # replace_one_field | ||
| 545 | |||
| 546 | =head2 delete_field_from_file | ||
| 547 | |||
| 548 | Completely remove the given field. This does no checking. | ||
| 549 | |||
| 550 | $writer->delete_field_from_file(filename=>$filename,field=>$field); | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | sub delete_field_from_file { | ||
| 555 | my $self = shift; | ||
| 556 | my %args = @_; | ||
| 557 | say STDERR whoami(), " field=$args{field}" if $self->{verbose} > 2; | ||
| 558 | |||
| 559 | my $filename = $self->_get_the_real_file(filename=>$args{filename}); | ||
| 560 | my $field = $args{field}; | ||
| 561 | |||
| 562 | my $ft = $self->{file_magic}->info_from_filename($filename); | ||
| 563 | my $et = new Image::ExifTool; | ||
| 564 | $et->Options(ListSep=>',',ListSplit=>','); | ||
| 565 | $et->ExtractInfo($filename); | ||
| 566 | |||
| 567 | my $success; | ||
| 568 | if ($field eq 'creator') | ||
| 569 | { | ||
| 570 | $success = $et->SetNewValue('Creator') | ||
| 571 | } | ||
| 572 | elsif ($field eq 'title') | ||
| 573 | { | ||
| 574 | $success = $et->SetNewValue('Title') | ||
| 575 | } | ||
| 576 | elsif ($field eq 'description') | ||
| 577 | { | ||
| 578 | # GIMP reads and overwrites the Comment, UserComment | ||
| 579 | # and ImageDescription fields, so we need to do that too. | ||
| 580 | if ($ft->{mime_type} =~ /image\/jpeg/) | ||
| 581 | { | ||
| 582 | $success = $et->SetNewValue('Comment'); | ||
| 583 | } | ||
| 584 | $success = $et->SetNewValue('ImageDescription'); | ||
| 585 | $success = $et->SetNewValue('UserComment'); | ||
| 586 | } | ||
| 587 | elsif ($field eq 'tags') | ||
| 588 | { | ||
| 589 | $success = $et->SetNewValue('Keywords'); | ||
| 590 | $success = $et->SetNewValue('Subject'); | ||
| 591 | } | ||
| 592 | else # freeform field | ||
| 593 | { | ||
| 594 | # Need to read all the YAML, change this field, and write it again | ||
| 595 | my $fdata = $self->_read_freeform_data(exif=>$et); | ||
| 596 | if (exists $fdata->{$field}) | ||
| 597 | { | ||
| 598 | delete $fdata->{$field}; | ||
| 599 | $success = $self->_write_freeform_data(newdata=>$fdata,exif=>$et); | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | if ($success) | ||
| 604 | { | ||
| 605 | $et->WriteInfo($filename); | ||
| 606 | } | ||
| 607 | return $success; | ||
| 608 | } # delete_field_from_file | ||
| 609 | |||
| 610 | =head2 _get_the_real_file | ||
| 611 | |||
| 612 | If the file is a directory, look for a cover file. | ||
| 613 | If the file is a soft link, look for the file it is pointing to | ||
| 614 | (because ExifTool behaves badly with soft links). | ||
| 615 | |||
| 616 | my $real_file = $scribe->_get_the_real_file(filename=>$filename); | ||
| 617 | |||
| 618 | =cut | ||
| 619 | |||
| 620 | sub _get_the_real_file { | ||
| 621 | my $self = shift; | ||
| 622 | my %args = @_; | ||
| 623 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 624 | |||
| 625 | my $filename = $args{filename}; | ||
| 626 | if (-d $filename) # is a directory, look for a cover file | ||
| 627 | { | ||
| 628 | my $cover_file = ($self->{cover_file} ? $self->{cover_file} : 'cover.jpg'); | ||
| 629 | $cover_file = File::Spec->catfile($filename, $cover_file); | ||
| 630 | if (-f $cover_file) | ||
| 631 | { | ||
| 632 | $filename = $cover_file; | ||
| 633 | } | ||
| 634 | else # give up and die | ||
| 635 | { | ||
| 636 | croak "$args{filename} is directory, cannot find $cover_file"; | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | # ExifTool has a wicked habit of replacing soft-linked files with the | ||
| 641 | # contents of the file rather than honouring the link. While using the | ||
| 642 | # exiftool script offers -overwrite_original_in_place to deal with this, | ||
| 643 | # the Perl module does not appear to have such an option available. | ||
| 644 | |||
| 645 | # So the way to get around this is to check if the file is a soft link, and | ||
| 646 | # if it is, find the real file, and write to that. And if *that* file is | ||
| 647 | # a soft link... go down the rabbit-hole as deep as it goes. | ||
| 648 | while (-l $filename) | ||
| 649 | { | ||
| 650 | my $realfile = readlink $filename; | ||
| 651 | if (-f $realfile) | ||
| 652 | { | ||
| 653 | $filename = $realfile; | ||
| 654 | } | ||
| 655 | else # give up and die | ||
| 656 | { | ||
| 657 | croak "$args{filename} is soft link, cannot find $realfile"; | ||
| 658 | } | ||
| 659 | } | ||
| 660 | |||
| 661 | return $filename; | ||
| 662 | } # _get_the_real_file | ||
| 663 | |||
| 664 | =head2 _read_freeform_data | ||
| 665 | |||
| 666 | Read the freeform data as YAML data from the Instructions field. | ||
| 667 | |||
| 668 | my $ydata = $self->_read_freeform_data(exif=>$exif); | ||
| 669 | |||
| 670 | =cut | ||
| 671 | |||
| 672 | sub _read_freeform_data { | ||
| 673 | my $self = shift; | ||
| 674 | my %args = @_; | ||
| 675 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 676 | |||
| 677 | # CONVERT FREEFORM DATA if needed BEFOREHAND | ||
| 678 | $self->_convert_freeform_data(%args); | ||
| 679 | |||
| 680 | my $ydata; | ||
| 681 | my $et = $args{exif}; | ||
| 682 | my $ystring = $et->GetValue('Instructions'); | ||
| 683 | $ystring = $et->GetNewValue('Instructions') if !$ystring; | ||
| 684 | say STDERR "ystring=$ystring" if $self->{verbose} > 2; | ||
| 685 | if ($ystring and $ystring =~ /^---/) # YAML data needs prefix | ||
| 686 | { | ||
| 687 | eval {$ydata = Load($ystring);}; | ||
| 688 | if ($@) | ||
| 689 | { | ||
| 690 | warn __PACKAGE__, " Load of YAML data failed: $@ // '$ystring'"; | ||
| 691 | } | ||
| 692 | elsif (!$ydata) | ||
| 693 | { | ||
| 694 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 1; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | say STDERR Dump($ydata) if $self->{verbose} > 2; | ||
| 698 | return $ydata; | ||
| 699 | } # _read_freeform_data | ||
| 700 | |||
| 701 | =head2 _write_freeform_data | ||
| 702 | |||
| 703 | Write the freeform data as YAML data into the Instructions field | ||
| 704 | This overwrites whatever is there, it does not check. | ||
| 705 | |||
| 706 | $self->_write_freeform_data(newdata=>\%newdata,exif=>$exif); | ||
| 707 | |||
| 708 | =cut | ||
| 709 | |||
| 710 | sub _write_freeform_data { | ||
| 711 | my $self = shift; | ||
| 712 | my %args = @_; | ||
| 713 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 714 | |||
| 715 | my $newdata = $args{newdata}; | ||
| 716 | my $et = $args{exif}; | ||
| 717 | # restore multi-value comma-separated fields to arrays | ||
| 718 | foreach my $fn (keys %{$self->{wanted_fields}}) | ||
| 719 | { | ||
| 720 | if ($self->{wanted_fields}->{$fn} eq 'MULTI' | ||
| 721 | and exists $newdata->{$fn} | ||
| 722 | and defined $newdata->{$fn} | ||
| 723 | and $newdata->{$fn} =~ /,/) | ||
| 724 | { | ||
| 725 | my @vals = split(/,/, $newdata->{$fn}); | ||
| 726 | $newdata->{$fn} = \@vals; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | my $ystring = Dump($newdata); | ||
| 730 | say STDERR "ystring=$ystring" if $self->{verbose} > 2; | ||
| 731 | my $success = $et->SetNewValue('Instructions', $ystring); | ||
| 732 | return $success; | ||
| 733 | } # _write_freeform_data | ||
| 734 | |||
| 735 | =head2 _convert_freeform_data | ||
| 736 | |||
| 737 | Convert the freeform data so that it is placed into the Instructions | ||
| 738 | field rather than the XMP:Description, UserComment or ImageDescription field. | ||
| 739 | |||
| 740 | $self->_convert_freeform_data(exif=>$exif); | ||
| 741 | |||
| 742 | =cut | ||
| 743 | |||
| 744 | sub _convert_freeform_data { | ||
| 745 | my $self = shift; | ||
| 746 | my %args = @_; | ||
| 747 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 748 | |||
| 749 | my $et = $args{exif}; | ||
| 750 | # Check if it needs conversion at all. | ||
| 751 | # If the Instructions field is not empty | ||
| 752 | # and contains YAML data, then nothing needs to be done. | ||
| 753 | my $ystring = $et->GetValue('Instructions'); | ||
| 754 | $ystring = $et->GetNewValue('Instructions') if !$ystring; | ||
| 755 | if ($ystring and $ystring =~ /^---/) # Assume YAML data | ||
| 756 | { | ||
| 757 | # no conversion needed | ||
| 758 | return 1; | ||
| 759 | } | ||
| 760 | |||
| 761 | # ------------------------------------ | ||
| 762 | # Conversion needed | ||
| 763 | # Read from XMP:Description, write into Instructions | ||
| 764 | # Otherwise read from ImageDescription. | ||
| 765 | # The YAML data might be in UserComment instead if old. | ||
| 766 | # ------------------------------------ | ||
| 767 | $ystring = $et->GetValue('Description'); | ||
| 768 | $ystring = $et->GetNewValue('Description') if !$ystring; | ||
| 769 | if (!$ystring or $ystring !~ /^---/) # Try ImageDescription | ||
| 770 | { | ||
| 771 | $ystring = $et->GetValue('ImageDescription'); | ||
| 772 | $ystring = $et->GetNewValue('ImageDescription') if !$ystring; | ||
| 773 | } | ||
| 774 | if (!$ystring or $ystring !~ /^---/) # Try UserComment | ||
| 775 | { | ||
| 776 | $ystring = $et->GetValue('UserComment'); | ||
| 777 | $ystring = $et->GetNewValue('UserComment') if !$ystring; | ||
| 778 | } | ||
| 779 | |||
| 780 | my $ydata; | ||
| 781 | my $success = 0; | ||
| 782 | if ($ystring and $ystring =~ /^---/) # Probably YAML data | ||
| 783 | { | ||
| 784 | # Check if the YAML data is valid | ||
| 785 | eval {$ydata = Load($ystring);}; | ||
| 786 | if ($@) | ||
| 787 | { | ||
| 788 | warn __PACKAGE__, " Load of YAML data failed: $@ ## '$ystring'"; | ||
| 789 | } | ||
| 790 | elsif (!$ydata) | ||
| 791 | { | ||
| 792 | warn __PACKAGE__, " no legal YAML" if $self->{verbose} > 1; | ||
| 793 | } | ||
| 794 | else # data is okay | ||
| 795 | { | ||
| 796 | $success = $et->SetNewValue('Instructions', $ystring); | ||
| 797 | if ($success) | ||
| 798 | { | ||
| 799 | # Clear out the XMP:Description field | ||
| 800 | $et->SetNewValue('XMP:Description'); | ||
| 801 | |||
| 802 | # Put the description, if there is one, | ||
| 803 | # into the UserComment, ImageDescription | ||
| 804 | my $desc = $et->GetValue('Comment'); | ||
| 805 | $desc = $et->GetNewValue('Comment') if !$desc; | ||
| 806 | $desc = $et->GetValue('Caption-Abstract') if !$desc; | ||
| 807 | if ($desc) | ||
| 808 | { | ||
| 809 | $et->SetNewValue('UserComment', $desc); | ||
| 810 | $et->SetNewValue('ImageDescription', $desc); | ||
| 811 | } | ||
| 812 | else | ||
| 813 | { | ||
| 814 | # Otherwise clear out no longer valid data | ||
| 815 | $et->SetNewValue('ImageDescription'); | ||
| 816 | $et->SetNewValue('UserComment'); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | } | ||
| 820 | } | ||
| 821 | else # No YAML data to convert | ||
| 822 | { | ||
| 823 | # Put some empty data in there. | ||
| 824 | my %newdata = (); | ||
| 825 | my $nystring = Dump(\%newdata); | ||
| 826 | $success = $et->SetNewValue('Instructions', $nystring); | ||
| 827 | } | ||
| 828 | |||
| 829 | return $success; | ||
| 830 | } # _convert_freeform_data | ||
| 831 | |||
| 832 | =head1 BUGS | ||
| 833 | |||
| 834 | Please report any bugs or feature requests to the author. | ||
| 835 | |||
| 836 | =cut | ||
| 837 | |||
| 838 | 1; # End of File::Sticker::Scribe | ||
| 839 | __END__ | ||