App-PDFLibrarian

 view release on metacpan or  search on metacpan

lib/App/PDFLibrarian/Library.pm  view on Meta::CPAN

      $existinglinks{$pdffile}->{$oldlink} = 0;
    }

    # make symbolic links
    foreach (@links) {

      # normalise elements of symbolic link path
      my @linkpath = @{$_};
      foreach (@linkpath) {
        die unless defined($_) && $_ ne "";
        $_ = unidecode($_);
        s/[`'"!]//g;
        s/[^-+.A-Za-z0-9]/_/g;
        s/[-_][-_]+/_/g;
        s/^_+//;
        s/_+$//;
      }

      # make symbolic link path directories
      my $linkfilebase = pop @linkpath;
      my $linkdir = File::Spec->catdir($pdflibrarydir, @linkpath);
      File::Path::make_path($linkdir);

      # make symbolic link file
      my $linkfile = File::Spec->catfile($linkdir, "$linkfilebase.pdf");
      if (-l $linkfile) {
        unlink($linkfile) or croak "$Script: could not unlink '$linkfile': $!";
      }
      symlink($pdffile, $linkfile) or croak "$Script: could not link '$linkfile' to '$pdffile': $!";
      $existinglinks{$pdffile}->{$linkfile} = 1;

    }

    # print progress
    ++$count;
    if ($count % 50 == 0 || $count == @bibentries) {
      printf STDERR "$Script: making links to %i/%i PDF files in '$pdflibrarydir'\r", $count, scalar(@bibentries);
      flush STDERR;
    }
  }
  printf STDERR "\n";

  # remove old links
  foreach my $pdffile (keys %existinglinks) {
    foreach my $oldlink (keys %{$existinglinks{$pdffile}}) {
      if (!$existinglinks{$pdffile}->{$oldlink}) {
        unlink($oldlink) or croak "$Script: could not unlink '$oldlink': $!";
      }
    }
  }

}

sub cleanup_links {

  # remove broken links and empty directories
  my $wanted = sub {
    if (-d $_) {
      rmdir $_;
    }
    if (-l $_ && !stat($_)) {
      unlink($_) or croak "$Script: could not unlink '$_': $!";
    }
  };
  find({wanted => \&$wanted, bydepth => 1, no_chdir => 1}, $pdflibrarydir);
  printf STDERR "$Script: cleaned up PDF file links in '$pdflibrarydir'\n";

}



( run in 1.808 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )