| 1 | package File::Sticker; | ||
| 2 | $File::Sticker::VERSION = '4.9'; | ||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | File::Sticker - Read, Write file meta-data | ||
| 6 | |||
| 7 | =head1 VERSION | ||
| 8 | |||
| 9 | version 4.9 | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use File::Sticker; | ||
| 14 | |||
| 15 | my $obj = File::Sticker->new(%args); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This will read and write meta-data from files, in a standardized manner. | ||
| 20 | And update a database with that information. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | 1 | ▶ | use common::sense; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 0 | ▶ | use File::Sticker::Scribe; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 26 | use File::Sticker::Database; | ||
| 27 | use POSIX qw(strftime); | ||
| 28 | use String::CamelCase qw(wordsplit); | ||
| 29 | use YAML::Any; | ||
| 30 | use Path::Tiny; | ||
| 31 | use Hash::Merge; | ||
| 32 | use Clone 'clone'; | ||
| 33 | use File::Basename; | ||
| 34 | use File::Spec; | ||
| 35 | use File::Copy; | ||
| 36 | |||
| 37 | use Module::Pluggable instantiate => 'new', | ||
| 38 | search_path => ['File::Sticker::Scribe'], | ||
| 39 | sub_name => 'all_scribes'; | ||
| 40 | use Module::Pluggable instantiate => 'new', | ||
| 41 | search_path => ['File::Sticker::Derive'], | ||
| 42 | sub_name => 'derivers'; | ||
| 43 | |||
| 44 | # FOR DEBUGGING | ||
| 45 | =head1 DEBUGGING | ||
| 46 | |||
| 47 | =head2 whoami | ||
| 48 | |||
| 49 | Used for debugging info | ||
| 50 | |||
| 51 | =cut | ||
| 52 | sub whoami { ( caller(1) )[3] } | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 new | ||
| 57 | |||
| 58 | Create a new object, setting global values for the object. | ||
| 59 | |||
| 60 | my $obj = File::Sticker->new( | ||
| 61 | wanted_fields=>\%wanted_fields, | ||
| 62 | verbose=>$verbose, | ||
| 63 | dbname=>$dbname, | ||
| 64 | field_order=>\@fields, | ||
| 65 | primary_table=>$primary_table, | ||
| 66 | tagfield=>$tagfield, | ||
| 67 | ); | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub new { | ||
| 72 | my $class = shift; | ||
| 73 | my %parameters = (@_); | ||
| 74 | my $self = bless ({%parameters}, ref ($class) || $class); | ||
| 75 | |||
| 76 | my %new_args = (); | ||
| 77 | foreach my $key (qw(wanted_fields verbose topdir read_all)) | ||
| 78 | { | ||
| 79 | if (exists $self->{$key}) | ||
| 80 | { | ||
| 81 | $new_args{$key} = $self->{$key}; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | # ------------------------------------- | ||
| 85 | # Modules | ||
| 86 | my %to_disable = (); | ||
| 87 | foreach my $mod (@{$self->{disable}}) | ||
| 88 | { | ||
| 89 | $to_disable{$mod} = 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | # ------------------------------------- | ||
| 93 | # Scribes | ||
| 94 | # Find out what scribes are available, and group them by priority | ||
| 95 | my @scribes = $self->all_scribes(); | ||
| 96 | $self->{_scribe_pri} = {}; | ||
| 97 | foreach my $rd (@scribes) | ||
| 98 | { | ||
| 99 | my $nm = $rd->name(); | ||
| 100 | my $priority = $rd->priority(); | ||
| 101 | if ($to_disable{$nm}) | ||
| 102 | { | ||
| 103 | print STDERR "DISABLE SCRIBE: ${nm}\n" if $self->{verbose} > 1; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | print STDERR "SCRIBE: ${nm}\n" if $self->{verbose} > 1; | ||
| 108 | $rd->init(%new_args); | ||
| 109 | if (!exists $self->{_scribe_pri}->{$priority}) | ||
| 110 | { | ||
| 111 | $self->{_scribe_pri}->{$priority} = []; | ||
| 112 | } | ||
| 113 | push @{$self->{_scribe_pri}->{$priority}}, $rd; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | # ------------------------------------- | ||
| 117 | # Derivers | ||
| 118 | # Find out what derivers are available and enabled. | ||
| 119 | my @derivers = $self->derivers(); | ||
| 120 | # Sort by order, numerically | ||
| 121 | @derivers = sort {$a->order() <=> $b->order()} @derivers; | ||
| 122 | $self->{_derivers} = []; | ||
| 123 | foreach my $dd (@derivers) | ||
| 124 | { | ||
| 125 | my $nm = $dd->name(); | ||
| 126 | if ($to_disable{$nm}) | ||
| 127 | { | ||
| 128 | print STDERR "DISABLE DERIVER: ${nm}\n" if $self->{verbose} > 1; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | print STDERR "DERIVER: ${nm}\n" if $self->{verbose} > 1; | ||
| 133 | $dd->init(%new_args); | ||
| 134 | push @{$self->{_derivers}}, $dd; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | # ------------------------------------- | ||
| 139 | # Database (optional) | ||
| 140 | # ------------------------------------- | ||
| 141 | if (exists $self->{dbname} | ||
| 142 | and defined $self->{dbname} | ||
| 143 | and exists $self->{wanted_fields} | ||
| 144 | and defined $self->{wanted_fields} | ||
| 145 | and exists $self->{field_order} | ||
| 146 | and defined $self->{field_order} | ||
| 147 | and exists $self->{primary_table} | ||
| 148 | and defined $self->{primary_table}) | ||
| 149 | { | ||
| 150 | # we have enough to instantiate a database object | ||
| 151 | $self->{db} = File::Sticker::Database->new( | ||
| 152 | dbname=>$self->{dbname}, | ||
| 153 | wanted_fields=>$self->{wanted_fields}, | ||
| 154 | field_order=>$self->{field_order}, | ||
| 155 | primary_table=>$self->{primary_table}, | ||
| 156 | taggable_fields=>$self->{taggable_fields}, | ||
| 157 | topdir=>$self->{topdir}, | ||
| 158 | tagfield=>$self->{tagfield}, | ||
| 159 | space_sep=>$self->{space_sep}, | ||
| 160 | readonly=>$self->{readonly}, | ||
| 161 | verbose=>$self->{verbose}, | ||
| 162 | ); | ||
| 163 | $self->{db}->do_connect(); | ||
| 164 | $self->{db}->create_tables(); | ||
| 165 | } | ||
| 166 | |||
| 167 | return ($self); | ||
| 168 | } # new | ||
| 169 | |||
| 170 | =head2 read_meta | ||
| 171 | |||
| 172 | This will read the meta-data from the file, using all possible ways. | ||
| 173 | |||
| 174 | my $info = $fs->read_meta(filename=>$filename,read_all=>0); | ||
| 175 | |||
| 176 | =cut | ||
| 177 | sub read_meta ($%) { | ||
| 178 | my $self = shift; | ||
| 179 | my %args = @_; | ||
| 180 | my $filename = $args{filename}; | ||
| 181 | say STDERR whoami(), if $self->{verbose} > 2; | ||
| 182 | |||
| 183 | if (!-r $filename) | ||
| 184 | { | ||
| 185 | # the file may not exist yet, so don't die | ||
| 186 | return {}; | ||
| 187 | } | ||
| 188 | |||
| 189 | # Don't limit this to only one scribe, due to data being | ||
| 190 | # held in multiple forms, we need a fallback. | ||
| 191 | my @possible_scribes = (); | ||
| 192 | # Look for the high-priority scribes first | ||
| 193 | foreach my $pri (reverse sort keys %{$self->{_scribe_pri}}) | ||
| 194 | { | ||
| 195 | foreach my $rd (@{$self->{_scribe_pri}->{$pri}}) | ||
| 196 | { | ||
| 197 | if ($rd->allow($filename)) | ||
| 198 | { | ||
| 199 | push @possible_scribes, $rd; | ||
| 200 | say STDERR "Scribe($pri) ", $rd->name() if $self->{verbose} > 1; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | # Merge in ALL found data; LEFT_PRECEDENT because the | ||
| 206 | # earlier scribes have higher priority. | ||
| 207 | my $merge = Hash::Merge->new('LEFT_PRECEDENT'); | ||
| 208 | my $meta = {}; | ||
| 209 | foreach my $scribe (@possible_scribes) | ||
| 210 | { | ||
| 211 | say STDERR "Reading ", $scribe->name() if $self->{verbose} > 1; | ||
| 212 | my $info = $scribe->read_meta($filename); | ||
| 213 | my $newmeta = $merge->merge($meta, $info); | ||
| 214 | $meta = $newmeta; | ||
| 215 | print STDERR "META: ", Dump($meta), "\n" if $self->{verbose} > 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | # Now that we've read the data from the file, it's time to derive values | ||
| 219 | # WITHOUT overwrites. | ||
| 220 | $meta = $self->derive_values(filename=>$filename, meta=>$meta); | ||
| 221 | |||
| 222 | if (!$args{read_all}) | ||
| 223 | { | ||
| 224 | # If we only want the wanted fields, remove anything that isn't wanted | ||
| 225 | if (exists $self->{field_order} | ||
| 226 | and defined $self->{field_order}) | ||
| 227 | { | ||
| 228 | # The 'field_order' information includes all the derived fields | ||
| 229 | # that we are interested in, so check that first if it exists. | ||
| 230 | my %ofields = (); | ||
| 231 | foreach my $ofn (@{$self->{field_order}}) | ||
| 232 | { | ||
| 233 | $ofields{$ofn} = 1; | ||
| 234 | } | ||
| 235 | |||
| 236 | my @fields = sort keys %{$meta}; | ||
| 237 | foreach my $fn (@fields) | ||
| 238 | { | ||
| 239 | if (! exists $ofields{$fn}) | ||
| 240 | { | ||
| 241 | delete $meta->{$fn}; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | elsif (exists $self->{wanted_fields} | ||
| 246 | and defined $self->{wanted_fields}) | ||
| 247 | { | ||
| 248 | # However, it's possible for wanted_fields to exist without field_order | ||
| 249 | # for things which don't have a database, so check these instead. | ||
| 250 | my @fields = sort keys %{$meta}; | ||
| 251 | foreach my $fn (@fields) | ||
| 252 | { | ||
| 253 | if (! exists $self->{wanted_fields}->{$fn}) | ||
| 254 | { | ||
| 255 | delete $meta->{$fn}; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | return $meta; | ||
| 262 | } # read_meta | ||
| 263 | |||
| 264 | =head2 add_field_to_file | ||
| 265 | |||
| 266 | Add the contents of the given field to the file, taking into account multi-value fields. | ||
| 267 | |||
| 268 | $sticker->add_field_to_file( | ||
| 269 | filename=>$filename, | ||
| 270 | field=>$field, | ||
| 271 | value=>$value); | ||
| 272 | |||
| 273 | =cut | ||
| 274 | sub add_field_to_file { | ||
| 275 | my $self = shift; | ||
| 276 | my %args = @_; | ||
| 277 | say STDERR whoami(), if $self->{verbose} > 2; | ||
| 278 | |||
| 279 | my $filename = $args{filename}; | ||
| 280 | my $field = $args{field}; | ||
| 281 | my $value = $args{value}; | ||
| 282 | |||
| 283 | if (!-w $filename) | ||
| 284 | { | ||
| 285 | return undef; | ||
| 286 | } | ||
| 287 | my $scribe = $self->_get_scribe($filename); | ||
| 288 | if (defined $scribe) | ||
| 289 | { | ||
| 290 | # Never over-write readonly fields. | ||
| 291 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 292 | if (exists $readonly_fields->{$field} | ||
| 293 | and defined $readonly_fields->{$field}) | ||
| 294 | { | ||
| 295 | return undef; | ||
| 296 | } | ||
| 297 | |||
| 298 | my $old_meta = $self->read_meta(filename=>$filename,read_all=>0); | ||
| 299 | |||
| 300 | $scribe->add_field_to_file( | ||
| 301 | filename=>$filename, | ||
| 302 | field=>$field, | ||
| 303 | value=>$value, | ||
| 304 | old_meta=>$old_meta); | ||
| 305 | } | ||
| 306 | } # add_field_to_file | ||
| 307 | |||
| 308 | =head2 refill_fields_for_file | ||
| 309 | |||
| 310 | Derive new values for the given fields, refilling the existing values. | ||
| 311 | For multi-value fields, it replaces ALL the values. | ||
| 312 | |||
| 313 | $sticker->refill_fields_for_file(filename=>$filename,refill=>\@refill); | ||
| 314 | |||
| 315 | =cut | ||
| 316 | |||
| 317 | sub refill_fields_for_file { | ||
| 318 | my $self = shift; | ||
| 319 | my %args = @_; | ||
| 320 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 321 | |||
| 322 | my $filename = $args{filename}; | ||
| 323 | my @refill_fields = @{$args{refill}}; | ||
| 324 | |||
| 325 | if (!-w $filename) | ||
| 326 | { | ||
| 327 | return undef; | ||
| 328 | } | ||
| 329 | if (!@refill_fields) | ||
| 330 | { | ||
| 331 | return undef; | ||
| 332 | } | ||
| 333 | my $scribe = $self->_get_scribe($filename); | ||
| 334 | if (defined $scribe) | ||
| 335 | { | ||
| 336 | my $old_meta = $self->read_meta(filename=>$filename,read_all=>0); | ||
| 337 | my $derived_meta = $self->derive_values(%args,refill=>\@refill_fields, | ||
| 338 | filename=>$filename,meta=>$old_meta); | ||
| 339 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 340 | |||
| 341 | # For each wanted derivable field (which is not a readonly field) | ||
| 342 | foreach my $field (@refill_fields) | ||
| 343 | { | ||
| 344 | if (exists $derived_meta->{$field} | ||
| 345 | and defined $derived_meta->{$field} | ||
| 346 | and $derived_meta->{$field} | ||
| 347 | and !exists $readonly_fields->{$field}) | ||
| 348 | { | ||
| 349 | my $value = $derived_meta->{$field}; | ||
| 350 | |||
| 351 | # If this is a reference, we need to convert it back to a string | ||
| 352 | # because the methods are not expecting an array reference! | ||
| 353 | if (ref $value eq "ARRAY") | ||
| 354 | { | ||
| 355 | $value = join(',', @{$value}); | ||
| 356 | } | ||
| 357 | $scribe->add_field_to_file( | ||
| 358 | filename=>$filename, | ||
| 359 | field=>$field, | ||
| 360 | value=>$value, | ||
| 361 | old_meta=>$old_meta); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | } # we have a scribe | ||
| 365 | } # refill_fields_for_file | ||
| 366 | |||
| 367 | =head2 delete_field_from_file | ||
| 368 | |||
| 369 | Completely remove the given field. | ||
| 370 | For multi-value fields, it removes ALL the values. | ||
| 371 | |||
| 372 | $sticker->delete_field_from_file(filename=>$filename,field=>$field); | ||
| 373 | |||
| 374 | =cut | ||
| 375 | |||
| 376 | sub delete_field_from_file { | ||
| 377 | my $self = shift; | ||
| 378 | my %args = @_; | ||
| 379 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 380 | |||
| 381 | my $filename = $args{filename}; | ||
| 382 | my $field = $args{field}; | ||
| 383 | |||
| 384 | if (!-w $filename) | ||
| 385 | { | ||
| 386 | return undef; | ||
| 387 | } | ||
| 388 | my $scribe = $self->_get_scribe($filename); | ||
| 389 | if (defined $scribe) | ||
| 390 | { | ||
| 391 | # Never delete readonly fields. | ||
| 392 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 393 | if (exists $readonly_fields->{$field} | ||
| 394 | or defined $readonly_fields->{$field}) | ||
| 395 | { | ||
| 396 | return undef; | ||
| 397 | } | ||
| 398 | |||
| 399 | $scribe->delete_field_from_file( | ||
| 400 | filename=>$filename, | ||
| 401 | field=>$field); | ||
| 402 | } | ||
| 403 | } # delete_field_from_file | ||
| 404 | |||
| 405 | =head2 replace_all_meta | ||
| 406 | |||
| 407 | Overwrite the existing meta-data with that given. | ||
| 408 | |||
| 409 | $sticker->replace_all_meta(filename=>$filename,meta=>\%meta); | ||
| 410 | |||
| 411 | =cut | ||
| 412 | |||
| 413 | sub replace_all_meta { | ||
| 414 | my $self = shift; | ||
| 415 | my %args = @_; | ||
| 416 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 417 | |||
| 418 | my $filename = $args{filename}; | ||
| 419 | my $meta = $args{meta}; | ||
| 420 | |||
| 421 | if (!-w $filename) | ||
| 422 | { | ||
| 423 | return undef; | ||
| 424 | } | ||
| 425 | |||
| 426 | my $scribe = $self->_get_scribe($filename); | ||
| 427 | if (defined $scribe) | ||
| 428 | { | ||
| 429 | # We don't want to replace readonly fields so take this meta-data | ||
| 430 | # and remove the readonly fields from it so they don't get written to. | ||
| 431 | # But we don't want to alter the passed-in hashref, so make a new hash. | ||
| 432 | my $writable_fields = $scribe->writable_fields(); | ||
| 433 | my %new_meta = (); | ||
| 434 | foreach my $field (sort keys %{$meta}) | ||
| 435 | { | ||
| 436 | if (exists $writable_fields->{$field} | ||
| 437 | and defined $writable_fields->{$field}) | ||
| 438 | { | ||
| 439 | # A writable field | ||
| 440 | $new_meta{$field} = $meta->{$field}; | ||
| 441 | } | ||
| 442 | } | ||
| 443 | $scribe->replace_all_meta( | ||
| 444 | filename=>$filename, | ||
| 445 | meta=>\%new_meta); | ||
| 446 | } | ||
| 447 | } # replace_all_meta | ||
| 448 | |||
| 449 | =head2 query_by_tags | ||
| 450 | |||
| 451 | Search using +tag -tag nomenclature. | ||
| 452 | |||
| 453 | $sticker->do_search($query_string); | ||
| 454 | |||
| 455 | =cut | ||
| 456 | sub query_by_tags { | ||
| 457 | my $self = shift; | ||
| 458 | my $query = shift; | ||
| 459 | |||
| 460 | return $self->{db}->query_by_tags($query); | ||
| 461 | } # query_by_tags | ||
| 462 | |||
| 463 | =head2 query_one_file | ||
| 464 | |||
| 465 | Get the database info about the given file. This is different from read_meta, | ||
| 466 | since this is getting the info from the database, not from the file. | ||
| 467 | |||
| 468 | $sticker->query_one_file($file); | ||
| 469 | |||
| 470 | =cut | ||
| 471 | sub query_one_file { | ||
| 472 | my $self = shift; | ||
| 473 | my $file = shift; | ||
| 474 | |||
| 475 | if ($self->{db}) | ||
| 476 | { | ||
| 477 | return $self->{db}->get_file_meta($file); | ||
| 478 | } | ||
| 479 | return undef; | ||
| 480 | } # query_one_file | ||
| 481 | |||
| 482 | =head2 missing_files | ||
| 483 | |||
| 484 | Check through the database to see which files in the database no longer exist. | ||
| 485 | |||
| 486 | my $files = $sticker->missing_files(); | ||
| 487 | |||
| 488 | =cut | ||
| 489 | sub missing_files { | ||
| 490 | my $self = shift; | ||
| 491 | |||
| 492 | my @missing_files = (); | ||
| 493 | my @files = @{$self->{db}->get_all_files()}; | ||
| 494 | foreach my $file (@files) | ||
| 495 | { | ||
| 496 | say STDERR "checking $file" if $self->{verbose} > 2; | ||
| 497 | if (!-r $file and !-d $file) | ||
| 498 | { | ||
| 499 | push @missing_files, $file; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | return \@missing_files; | ||
| 503 | } # missing_files | ||
| 504 | |||
| 505 | =head2 overlooked_files | ||
| 506 | |||
| 507 | Check through the database to see which of the given files are not in the database. | ||
| 508 | |||
| 509 | my $files = $sticker->overlooked_files(@files); | ||
| 510 | |||
| 511 | =cut | ||
| 512 | sub overlooked_files { | ||
| 513 | my $self = shift; | ||
| 514 | my @files = @_; | ||
| 515 | |||
| 516 | my @overlooked = (); | ||
| 517 | foreach my $file (@files) | ||
| 518 | { | ||
| 519 | my $id = $self->{db}->get_file_id($file); | ||
| 520 | if (!$id) | ||
| 521 | { | ||
| 522 | push @overlooked, $file; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | return \@overlooked; | ||
| 526 | } # overlooked_files | ||
| 527 | |||
| 528 | =head2 list_tags | ||
| 529 | |||
| 530 | List the faceted-tags from the info table in the database. | ||
| 531 | |||
| 532 | my @tags = @{$sticker->list_tags()}; | ||
| 533 | |||
| 534 | =cut | ||
| 535 | sub list_tags { | ||
| 536 | my $self = shift; | ||
| 537 | |||
| 538 | my $tags = $self->{db}->get_all_tags(); | ||
| 539 | return $tags; | ||
| 540 | } # list_tags | ||
| 541 | |||
| 542 | =head2 update_db | ||
| 543 | |||
| 544 | Add/Update the given files into the database. | ||
| 545 | |||
| 546 | $sticker->update_db(@files); | ||
| 547 | |||
| 548 | =cut | ||
| 549 | sub update_db { | ||
| 550 | my $self = shift; | ||
| 551 | my @files = @_; | ||
| 552 | |||
| 553 | my $transaction_on = 0; | ||
| 554 | my $num_trans = 0; | ||
| 555 | |||
| 556 | foreach my $filename (@files) | ||
| 557 | { | ||
| 558 | if (-r $filename) | ||
| 559 | { | ||
| 560 | say $filename if !$self->{quiet}; | ||
| 561 | if (!$transaction_on) | ||
| 562 | { | ||
| 563 | $self->{db}->start_transaction(); | ||
| 564 | $transaction_on = 1; | ||
| 565 | $num_trans = 0; | ||
| 566 | } | ||
| 567 | my $meta = $self->read_meta(filename=>$filename,read_all=>0); | ||
| 568 | my $all_meta = $self->read_meta(filename=>$filename,read_all=>1); | ||
| 569 | my $scribe = $self->_get_scribe($filename); | ||
| 570 | |||
| 571 | my $readonly_fields = $scribe->readonly_fields(); | ||
| 572 | foreach my $field (@{$self->{field_order}}) | ||
| 573 | { | ||
| 574 | # If it is a readonly field (such as filesize) | ||
| 575 | # then it needs to overwrite any old value. | ||
| 576 | if (exists $readonly_fields->{$field} | ||
| 577 | and defined $readonly_fields->{$field} | ||
| 578 | and defined $all_meta->{$field}) | ||
| 579 | { | ||
| 580 | $meta->{$field} = $all_meta->{$field}; | ||
| 581 | } | ||
| 582 | } | ||
| 583 | |||
| 584 | $self->{db}->add_meta_to_db($filename,%{$meta}); | ||
| 585 | # do the commits in bursts | ||
| 586 | $num_trans++; | ||
| 587 | if ($transaction_on and $num_trans > 100) | ||
| 588 | { | ||
| 589 | $self->{db}->commit(); | ||
| 590 | $transaction_on = 0; | ||
| 591 | $num_trans = 0; | ||
| 592 | say " " if $self->{verbose}; | ||
| 593 | } | ||
| 594 | } # if the file is readable | ||
| 595 | } | ||
| 596 | $self->{db}->commit(); | ||
| 597 | } # update_db | ||
| 598 | |||
| 599 | =head2 delete_file_from_db | ||
| 600 | |||
| 601 | Delete the given file from the database. | ||
| 602 | |||
| 603 | $sticker->delete_file_from_db($filename); | ||
| 604 | |||
| 605 | =cut | ||
| 606 | sub delete_file_from_db { | ||
| 607 | my $self = shift; | ||
| 608 | my $filename = shift; | ||
| 609 | |||
| 610 | return $self->{db}->delete_file_from_db($filename); | ||
| 611 | } # delete_file_from_db | ||
| 612 | |||
| 613 | =head2 delete_missing_files | ||
| 614 | |||
| 615 | Delete from the database the files which no longer exist. | ||
| 616 | |||
| 617 | my $files = $sticker->delete_missing_files(); | ||
| 618 | |||
| 619 | =cut | ||
| 620 | sub delete_missing_files { | ||
| 621 | my $self = shift; | ||
| 622 | |||
| 623 | my $missing_files = $self->missing_files(); | ||
| 624 | foreach my $file (@{$missing_files}) | ||
| 625 | { | ||
| 626 | $self->delete_file_from_db($file); | ||
| 627 | } | ||
| 628 | return $missing_files; | ||
| 629 | } # delete_missing_files | ||
| 630 | |||
| 631 | =head2 move_file | ||
| 632 | |||
| 633 | Move/rename a file, and update the database accordingly. | ||
| 634 | If the destination is a directory, then this will move | ||
| 635 | all the given files; if the destination is a file, this | ||
| 636 | will only move the first file. | ||
| 637 | |||
| 638 | my $okay = $sticker->move_one_file(file=>$file,newfile=>$newfile); | ||
| 639 | |||
| 640 | =cut | ||
| 641 | sub move_file { | ||
| 642 | my $self = shift; | ||
| 643 | my %args = @_; | ||
| 644 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 645 | |||
| 646 | my @files = @{$args{files}}; | ||
| 647 | my $dest = $args{dest}; | ||
| 648 | |||
| 649 | foreach my $file (@files) | ||
| 650 | { | ||
| 651 | my ($old_bn,$old_dir,$old_suffix) = fileparse($file, qr/\.[^.]*/); | ||
| 652 | my $newfile = $dest; | ||
| 653 | if (-d $dest) # is a directory | ||
| 654 | { | ||
| 655 | # Need to figure what the new filename will be | ||
| 656 | $newfile = File::Spec->catfile($dest,$old_bn . $old_suffix); | ||
| 657 | } | ||
| 658 | my ($new_bn,$dest_dir,$new_suffix) = fileparse($newfile, qr/\.[^.]*/); | ||
| 659 | |||
| 660 | if (-f $newfile) # already exists | ||
| 661 | { | ||
| 662 | say STDERR "$newfile already exists, cannot MOVE $file"; | ||
| 663 | return 0; | ||
| 664 | } | ||
| 665 | if (!move($file, $newfile)) # error | ||
| 666 | { | ||
| 667 | say STDERR "MV $file $newfile FAIL: $!"; | ||
| 668 | return 0; | ||
| 669 | } | ||
| 670 | # ASSERT: file now moved | ||
| 671 | |||
| 672 | # Check if there is a separate thumbnail, because we'll have to move it too | ||
| 673 | # The thumbnail could be a .jpg or a .png | ||
| 674 | my $old_thumb = $old_dir . ".thumbnails/" . $old_bn . ".jpg"; | ||
| 675 | if (!-r $old_thumb) # might be a .png | ||
| 676 | { | ||
| 677 | $old_thumb = $old_dir . ".thumbnails/" . $old_bn . ".png"; | ||
| 678 | } | ||
| 679 | if (-r $old_thumb) | ||
| 680 | { | ||
| 681 | # We have a thumbnail! | ||
| 682 | my ($old_thumb_bn,$old_thumb_dir,$old_thumb_suffix) = fileparse($old_thumb, qr/\.[^.]*/); | ||
| 683 | my $new_thumb = $dest_dir . ".thumbnails/" . $new_bn . $old_thumb_suffix; | ||
| 684 | if (!move($old_thumb, $new_thumb)) # error | ||
| 685 | { | ||
| 686 | say STDERR "MV $old_thumb $new_thumb FAIL: $!"; | ||
| 687 | return 0; | ||
| 688 | } | ||
| 689 | } | ||
| 690 | # No point in trying to update the database if we don't have one | ||
| 691 | if ($self->{db}) | ||
| 692 | { | ||
| 693 | $self->delete_file_from_db($file); | ||
| 694 | $self->update_db(($newfile)); | ||
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | } # move_file | ||
| 699 | |||
| 700 | =head2 derive_values | ||
| 701 | |||
| 702 | Derive values from the existing meta-data. Calls plugins to do so. | ||
| 703 | UPDATES the passed-in hash. Will overwrite values ONLY IF that field | ||
| 704 | is in the 'refill' array (converts this to an 'overwrite' hash) | ||
| 705 | |||
| 706 | my $new_meta = $sticker->derive_values(filename=>$filename, | ||
| 707 | meta=>$meta, refill=>\@refill); | ||
| 708 | |||
| 709 | =cut | ||
| 710 | |||
| 711 | sub derive_values { | ||
| 712 | my $self = shift; | ||
| 713 | my %args = @_; | ||
| 714 | say STDERR whoami() if $self->{verbose} > 2; | ||
| 715 | |||
| 716 | my $meta = $args{meta}; | ||
| 717 | my $overwrite = {}; | ||
| 718 | if ($args{refill} and ref $args{refill}) | ||
| 719 | { | ||
| 720 | foreach my $field (keys @{$args{refill}}) | ||
| 721 | { | ||
| 722 | $overwrite->{$field} = 1; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | foreach my $dd (@{$self->{_derivers}}) | ||
| 727 | { | ||
| 728 | my $nm = $dd->name(); | ||
| 729 | say STDERR "Derive($nm)" if $self->{verbose} > 2; | ||
| 730 | $dd->derive(%args,meta=>$meta,overwrite=>$overwrite); | ||
| 731 | } | ||
| 732 | |||
| 733 | return $meta; | ||
| 734 | } # derive_values | ||
| 735 | |||
| 736 | =head2 _get_scribe | ||
| 737 | |||
| 738 | Pick the appropriate scribe for this file | ||
| 739 | |||
| 740 | my $scribe = $sticker->_get_scribe($filename); | ||
| 741 | |||
| 742 | =cut | ||
| 743 | sub _get_scribe { | ||
| 744 | my $self = shift; | ||
| 745 | my $filename = shift; | ||
| 746 | |||
| 747 | my $scribe; | ||
| 748 | foreach my $pri (reverse sort keys %{$self->{_scribe_pri}}) | ||
| 749 | { | ||
| 750 | foreach my $wt (@{$self->{_scribe_pri}->{$pri}}) | ||
| 751 | { | ||
| 752 | if ($wt->allow($filename)) | ||
| 753 | { | ||
| 754 | $scribe = $wt; | ||
| 755 | say STDERR "Scribe($pri) ", $scribe->name() if $self->{verbose} > 1; | ||
| 756 | last; | ||
| 757 | } | ||
| 758 | } | ||
| 759 | if (defined $scribe) | ||
| 760 | { | ||
| 761 | last; | ||
| 762 | } | ||
| 763 | } | ||
| 764 | return $scribe; | ||
| 765 | } # _get_scribe | ||
| 766 | |||
| 767 | =head1 BUGS | ||
| 768 | |||
| 769 | Please report any bugs or feature requests to the author. | ||
| 770 | |||
| 771 | =cut | ||
| 772 | |||
| 773 | 1; # End of File::Sticker | ||
| 774 | __END__ | ||