| 1 | ####################################################################### | ||
| 2 | # $URL: svn+ssh://equilibrious@equilibrious.net/home/equilibrious/svnrepos/chrisdolan/Fuse-PDF/lib/Fuse/PDF/ContentFS.pm $ | ||
| 3 | # $Date: 2008-06-06 22:47:54 -0500 (Fri, 06 Jun 2008) $ | ||
| 4 | # $Author: equilibrious $ | ||
| 5 | # $Revision: 767 $ | ||
| 6 | ######################################################################## | ||
| 7 | |||
| 8 | package Fuse::PDF::ContentFS; | ||
| 9 | |||
| 10 | use warnings; | ||
| 11 | use strict; | ||
| 12 | use 5.008; | ||
| 13 | |||
| 14 | use Carp qw(carp); | ||
| 15 | use Readonly; | ||
| 16 | use POSIX qw(:errno_h); | ||
| 17 | use Fcntl qw(:mode); | ||
| 18 | use English qw(-no_match_vars); | ||
| 19 | use CAM::PDF; | ||
| 20 | use CAM::PDF::Node; | ||
| 21 | use CAM::PDF::Renderer::Images; # included so PAR picks it up | ||
| 22 | use CAM::PDF::Renderer::Text; # included so PAR picks it up | ||
| 23 | use Fuse::PDF::ErrnoHacks; | ||
| 24 | use Fuse::PDF::FS; | ||
| 25 | use Fuse::PDF::ImageTemplate; | ||
| 26 | |||
| 27 | our $VERSION = '0.09'; | ||
| 28 | |||
| 29 | Readonly::Scalar my $PATHLEN => 255; | ||
| 30 | Readonly::Scalar my $BLOCKSIZE => 4096; | ||
| 31 | Readonly::Scalar my $ELOOP_LIMIT => 100; | ||
| 32 | Readonly::Hash my %PERMS => ( | ||
| 33 | d => S_IFDIR() | oct 555, | ||
| 34 | l => S_IFLNK() | oct 777, | ||
| 35 | f => S_IFREG() | oct 444, | ||
| 36 | ); | ||
| 37 | |||
| 38 | Readonly::Scalar my $USED_FILES => 1000; | ||
| 39 | Readonly::Scalar my $FREE_FILES => 1_000_000; | ||
| 40 | Readonly::Scalar my $MAX_BLOCKS => 1_000_000; | ||
| 41 | Readonly::Scalar my $FREE_BLOCKS => 500_000; | ||
| 42 | |||
| 43 | Readonly::Scalar my $FS_ROOT_KEY => 'FusePDF'; # track value from Fuse::PDF::FS | ||
| 44 | |||
| 45 | Readonly::Hash my %SCALARS => (map {$_ => 1} qw(string hexstring number boolean label)); | ||
| 46 | |||
| 47 | Readonly::Scalar my $IMAGE_CACHE_TIMEOUT => 15; # seconds | ||
| 48 | |||
| 49 | # -------------------------------------------------- | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my ($pkg, $options) = @_; | ||
| 53 | return if ! $options; | ||
| 54 | return if ! $options->{pdf}; | ||
| 55 | |||
| 56 | my $self = bless { %{$options} }, $pkg; | ||
| 57 | $self->{pdf_mtime} ||= $BASETIME; # aka $^T | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub compact { ## no critic(ArgUnpacking) | ||
| 63 | my ($self, $boolean) = @_; | ||
| 64 | return $self->{compact} if @_ == 1; | ||
| 65 | $self->{compact} = $boolean ? 1 : undef; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub backup { ## no critic(ArgUnpacking) | ||
| 70 | my ($self, $boolean) = @_; | ||
| 71 | return $self->{backup} if @_ == 1; | ||
| 72 | $self->{backup} = $boolean ? 1 : undef; | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub autosave_filename { ## no critic(ArgUnpacking) | ||
| 77 | my ($self, $filename) = @_; | ||
| 78 | return $self->{autosave_filename} if @_ == 1; | ||
| 79 | $self->{autosave_filename} = $filename; | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub previous_revision { | ||
| 84 | my ($self) = @_; | ||
| 85 | |||
| 86 | my $prev_pdf = $self->{pdf}->previousRevision(); | ||
| 87 | return if !$prev_pdf; | ||
| 88 | |||
| 89 | return __PACKAGE__->new({ | ||
| 90 | pdf => $prev_pdf, | ||
| 91 | pdf_mtime => $self->{pdf_mtime}, | ||
| 92 | }); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub all_revisions { | ||
| 96 | my ($self) = @_; | ||
| 97 | my @revs; | ||
| 98 | for (my $fs = $self; $fs; $fs = $fs->previous_revision) { ## no critic(ProhibitCStyleForLoops) | ||
| 99 | push @revs, $fs; | ||
| 100 | } | ||
| 101 | return @revs; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub statistics { | ||
| 105 | my ($self) = @_; | ||
| 106 | my %stats; | ||
| 107 | $stats{pages} = $self->{pdf}->numPages; | ||
| 108 | return \%stats; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub to_string { | ||
| 112 | my ($self) = @_; | ||
| 113 | my @stats = ($self->statistics); | ||
| 114 | my $fs = $self; | ||
| 115 | while ($fs = $fs->previous_revision) { | ||
| 116 | push @stats, $fs->statistics; | ||
| 117 | } | ||
| 118 | my @rows = ( | ||
| 119 | 'Name: ' . $stats[0]->{name}, | ||
| 120 | ); | ||
| 121 | for my $i (0 .. $#stats) { | ||
| 122 | my $s = $stats[$i]; | ||
| 123 | push @rows, 'Revision: ' . (@stats - $i); | ||
| 124 | push @rows, ' Pages: ' . $s->{pages}; | ||
| 125 | } | ||
| 126 | |||
| 127 | return join "\n", @rows, q{}; | ||
| 128 | } | ||
| 129 | |||
| 130 | # -------------------------------------------------- | ||
| 131 | |||
| 132 | sub fs_getattr { | ||
| 133 | my ($self, $abspath) = @_; | ||
| 134 | my ($f, $path) = $self->_file($abspath); | ||
| 135 | if (defined $path) { | ||
| 136 | return -EIO() if !$f->can('fs_getattr'); | ||
| 137 | return $f->fs_getattr($path); | ||
| 138 | } | ||
| 139 | return -$f if !ref $f; | ||
| 140 | my $type = $f->{type}; | ||
| 141 | my $size = 'd' eq $type ? 0 : length $f->{content}; | ||
| 142 | my $blocks = 0 == $size ? 0 : (($size - 1) % $BLOCKSIZE) + 1; # round up | ||
| 143 | return | ||
| 144 | 0, # dev | ||
| 145 | 0, # inode | ||
| 146 | $PERMS{$type}, | ||
| 147 | ('d' eq $type ? (2 + scalar keys %{$f->{content}}) : 1), # nlink | ||
| 148 | $EFFECTIVE_USER_ID, # uid | ||
| 149 | 0+$EFFECTIVE_GROUP_ID, # gid | ||
| 150 | 0, # rdev | ||
| 151 | $size, | ||
| 152 | $self->{pdf_mtime}, | ||
| 153 | $self->{pdf_mtime}, | ||
| 154 | $self->{pdf_mtime}, | ||
| 155 | $BLOCKSIZE, | ||
| 156 | $blocks; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub fs_readlink { | ||
| 160 | my ($self, $abspath) = @_; | ||
| 161 | my ($f, $path) = $self->_file($abspath); | ||
| 162 | if (defined $path) { | ||
| 163 | return -EIO() if !$f->can('fs_readlink'); | ||
| 164 | return $f->fs_readlink($path); | ||
| 165 | } | ||
| 166 | return -$f if !ref $f; | ||
| 167 | my $type = $f->{type}; | ||
| 168 | return -EINVAL() if 'l' ne $type; | ||
| 169 | return $f->{content}; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub fs_getdir { | ||
| 173 | my ($self, $abspath) = @_; | ||
| 174 | my ($f, $path) = $self->_file($abspath); | ||
| 175 | if (defined $path) { | ||
| 176 | return -EIO() if !$f->can('fs_getdir'); | ||
| 177 | return $f->fs_getdir($path); | ||
| 178 | } | ||
| 179 | return -$f if !ref $f; | ||
| 180 | return q{.}, q{..}, (keys %{$f->{content}}), 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub fs_open { | ||
| 184 | my ($self, $abspath, $flags) = @_; | ||
| 185 | my ($f, $path) = $self->_file($abspath); | ||
| 186 | if (defined $path) { | ||
| 187 | return -EIO() if !$f->can('fs_open'); | ||
| 188 | return $f->fs_open($path); | ||
| 189 | } | ||
| 190 | return -$f if !ref $f; | ||
| 191 | # check flags? | ||
| 192 | return 0; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub fs_read { | ||
| 196 | my ($self, $abspath, $size, $offset) = @_; | ||
| 197 | my ($f, $path) = $self->_file($abspath); | ||
| 198 | if (defined $path) { | ||
| 199 | return -EIO() if !$f->can('fs_read'); | ||
| 200 | return $f->fs_read($path); | ||
| 201 | } | ||
| 202 | return -$f if !ref $f; | ||
| 203 | return substr $f->{content}, $offset, $size; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub fs_statfs { | ||
| 207 | my ($self) = @_; | ||
| 208 | return $PATHLEN, $USED_FILES, $FREE_FILES, $MAX_BLOCKS, $FREE_BLOCKS, $BLOCKSIZE; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub fs_mknod { | ||
| 212 | my ($self, $abspath, $perms, $dev) = @_; | ||
| 213 | my ($f, $path) = $self->_file($abspath); | ||
| 214 | if (defined $path) { | ||
| 215 | return -EIO() if !$f->can('fs_mknod'); | ||
| 216 | return $f->fs_mknod($path, $perms, $dev); | ||
| 217 | } | ||
| 218 | return -EIO(); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub fs_mkdir { | ||
| 222 | my ($self, $abspath, $perm) = @_; | ||
| 223 | my ($f, $path) = $self->_file($abspath); | ||
| 224 | if (defined $path) { | ||
| 225 | return -EIO() if !$f->can('fs_mkdir'); | ||
| 226 | return $f->fs_mkdir($path, $perm); | ||
| 227 | } | ||
| 228 | return -EIO(); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub fs_unlink { | ||
| 232 | my ($self, $abspath) = @_; | ||
| 233 | my ($f, $path) = $self->_file($abspath); | ||
| 234 | if (defined $path) { | ||
| 235 | return -EIO() if !$f->can('fs_unlink'); | ||
| 236 | return $f->fs_unlink($path); | ||
| 237 | } | ||
| 238 | return -EIO(); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub fs_rmdir { | ||
| 242 | my ($self, $abspath) = @_; | ||
| 243 | my ($f, $path) = $self->_file($abspath); | ||
| 244 | if (defined $path) { | ||
| 245 | return -EIO() if !$f->can('fs_rmdir'); | ||
| 246 | return $f->fs_rmdir($path); | ||
| 247 | } | ||
| 248 | return -EIO(); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub fs_symlink { | ||
| 252 | my ($self, $link, $abspath) = @_; | ||
| 253 | my ($f, $path) = $self->_file($abspath); | ||
| 254 | if (defined $path) { | ||
| 255 | return -EIO() if !$f->can('fs_symlink'); | ||
| 256 | return $f->fs_symlink($link, $path); | ||
| 257 | } | ||
| 258 | return -EIO(); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub fs_rename { | ||
| 262 | my ($self, $srcpath, $destpath) = @_; | ||
| 263 | my ($f_s, $src) = $self->_file($srcpath); | ||
| 264 | if (defined $src) { | ||
| 265 | return -EIO() if !$f_s->can('fs_rename'); | ||
| 266 | my ($f_d, $dest) = $self->_file($destpath); | ||
| 267 | if (defined $dest) { | ||
| 268 | return -EXDEV() if $f_s != $f_d; | ||
| 269 | return $f_s->fs_rename($src, $dest); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | return -EIO(); | ||
| 273 | } | ||
| 274 | |||
| 275 | sub fs_link { | ||
| 276 | return -EIO(); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub fs_chmod { | ||
| 280 | my ($self, $abspath, $perms) = @_; | ||
| 281 | my ($f, $path) = $self->_file($abspath); | ||
| 282 | if (defined $path) { | ||
| 283 | return -EIO() if !$f->can('fs_chmod'); | ||
| 284 | return $f->fs_chmod($path, $perms); | ||
| 285 | } | ||
| 286 | return -EIO(); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub fs_chown { | ||
| 290 | my ($self, $abspath, $uid, $gid) = @_; | ||
| 291 | my ($f, $path) = $self->_file($abspath); | ||
| 292 | if (defined $path) { | ||
| 293 | return -EIO() if !$f->can('fs_chown'); | ||
| 294 | return $f->fs_chown($path, $uid, $gid); | ||
| 295 | } | ||
| 296 | return -EIO(); | ||
| 297 | } | ||
| 298 | |||
| 299 | sub fs_truncate { | ||
| 300 | my ($self, $abspath, $length) = @_; | ||
| 301 | my ($f, $path) = $self->_file($abspath); | ||
| 302 | if (defined $path) { | ||
| 303 | return -EIO() if !$f->can('fs_truncate'); | ||
| 304 | return $f->fs_truncate($path, $length); | ||
| 305 | } | ||
| 306 | return -EIO(); | ||
| 307 | } | ||
| 308 | |||
| 309 | sub fs_utime { | ||
| 310 | my ($self, $abspath, $atime, $mtime) = @_; | ||
| 311 | my ($f, $path) = $self->_file($abspath); | ||
| 312 | if (defined $path) { | ||
| 313 | return -EIO() if !$f->can('fs_utime'); | ||
| 314 | return $f->fs_utime($path, $atime, $mtime); | ||
| 315 | } | ||
| 316 | return -EIO(); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub fs_write { | ||
| 320 | my ($self, $abspath, $str, $offset) = @_; | ||
| 321 | my ($f, $path) = $self->_file($abspath); | ||
| 322 | if (defined $path) { | ||
| 323 | return -EIO() if !$f->can('fs_write'); | ||
| 324 | return $f->fs_write($path, $str, $offset); | ||
| 325 | } | ||
| 326 | return -EIO(); | ||
| 327 | } | ||
| 328 | |||
| 329 | sub fs_flush { | ||
| 330 | my ($self, $abspath) = @_; | ||
| 331 | my ($f, $path) = $self->_file($abspath); | ||
| 332 | if (defined $path) { | ||
| 333 | return -EIO() if !$f->can('fs_flush'); | ||
| 334 | return $f->fs_flush($path); | ||
| 335 | } | ||
| 336 | return 0; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub fs_release { | ||
| 340 | my ($self, $abspath, $flags) = @_; | ||
| 341 | my ($f, $path) = $self->_file($abspath); | ||
| 342 | if (defined $path) { | ||
| 343 | return -EIO() if !$f->can('fs_release'); | ||
| 344 | return $f->fs_release($path, $flags); | ||
| 345 | } | ||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub fs_fsync { | ||
| 350 | my ($self, $abspath, $flags) = @_; | ||
| 351 | my ($f, $path) = $self->_file($abspath); | ||
| 352 | if (defined $path) { | ||
| 353 | return -EIO() if !$f->can('fs_fsync'); | ||
| 354 | return $f->fs_fsync($path, $flags); | ||
| 355 | } | ||
| 356 | return 0; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub fs_setxattr { | ||
| 360 | my ($self, $abspath, $key, $value, $flags) = @_; | ||
| 361 | my ($f, $path) = $self->_file($abspath); | ||
| 362 | if (defined $path) { | ||
| 363 | return -EIO() if !$f->can('fs_setxattr'); | ||
| 364 | return $f->fs_setxattr($path, $key, $value, $flags); | ||
| 365 | } | ||
| 366 | return -EIO(); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub fs_getxattr { | ||
| 370 | my ($self, $abspath, $key) = @_; | ||
| 371 | my ($f, $path) = $self->_file($abspath); | ||
| 372 | if (defined $path) { | ||
| 373 | return -EIO() if !$f->can('fs_getxattr'); | ||
| 374 | return $f->fs_getxattr($path, $key); | ||
| 375 | } | ||
| 376 | return 0; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub fs_listxattr { | ||
| 380 | my ($self, $abspath, $key) = @_; | ||
| 381 | my ($f, $path) = $self->_file($abspath); | ||
| 382 | if (defined $path) { | ||
| 383 | return -EIO() if !$f->can('fs_listxattr'); | ||
| 384 | return $f->fs_listxattr($path, $key); | ||
| 385 | } | ||
| 386 | return 0; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub fs_removexattr { | ||
| 390 | my ($self, $abspath, $key) = @_; | ||
| 391 | my ($f, $path) = $self->_file($abspath); | ||
| 392 | if (defined $path) { | ||
| 393 | return -EIO() if !$f->can('fs_removexattr'); | ||
| 394 | return $f->fs_removexattr($path, $key); | ||
| 395 | } | ||
| 396 | return -EIO(); | ||
| 397 | } | ||
| 398 | |||
| 399 | # -------------------------------------------------- | ||
| 400 | |||
| 401 | sub _filesystems { | ||
| 402 | my ($self) = @_; | ||
| 403 | |||
| 404 | $self->{filesystems} ||= {}; | ||
| 405 | my %filesystems; | ||
| 406 | |||
| 407 | # lookup fs object in PDF | ||
| 408 | my $root = $self->{pdf}->getRootDict(); | ||
| 409 | if ($root->{$FS_ROOT_KEY}) { | ||
| 410 | my $fs_holder = $root->{$FS_ROOT_KEY}->{value}; | ||
| 411 | for my $fs_name (keys %{$fs_holder}) { | ||
| 412 | $filesystems{$fs_name} = $self->{filesystems}->{$fs_name} || Fuse::PDF::FS->new({ | ||
| 413 | pdf => $self->{pdf}, | ||
| 414 | pdf_mtime => $self->{pdf_mtime}, | ||
| 415 | fs_name => $fs_name, | ||
| 416 | autosave_filename => $self->{autosave_filename}, | ||
| 417 | compact => $self->{compact}, | ||
| 418 | backup => $self->{backup}, | ||
| 419 | }); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | $self->{filesystems} = \%filesystems; | ||
| 423 | |||
| 424 | return { | ||
| 425 | type => 'd', | ||
| 426 | content => { | ||
| 427 | %filesystems, | ||
| 428 | }, | ||
| 429 | }; | ||
| 430 | } | ||
| 431 | |||
| 432 | sub _page_content { | ||
| 433 | my ($self, $i, $path) = @_; | ||
| 434 | my $pagenum = $path->[$i - 1]; | ||
| 435 | |||
| 436 | return { | ||
| 437 | type => 'f', | ||
| 438 | content => $self->{pdf}->getPageContent($pagenum), | ||
| 439 | }; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub _page_text { | ||
| 443 | my ($self, $i, $path) = @_; | ||
| 444 | my $pagenum = $path->[$i - 2]; | ||
| 445 | |||
| 446 | return { | ||
| 447 | type => 'f', | ||
| 448 | content => $self->{pdf}->getPageText($pagenum), | ||
| 449 | }; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub _page_textfb { | ||
| 453 | my ($self, $i, $path) = @_; | ||
| 454 | my $pagenum = $path->[$i - 2]; | ||
| 455 | |||
| 456 | my $gs = $self->{pdf}->getPageContentTree($pagenum)->render('CAM::PDF::Renderer::Text'); | ||
| 457 | |||
| 458 | return { | ||
| 459 | type => 'f', | ||
| 460 | content => $gs->toString(), | ||
| 461 | }; | ||
| 462 | } | ||
| 463 | |||
| 464 | sub _page_font { | ||
| 465 | my ($self, $i, $path) = @_; | ||
| 466 | my $pagenum = $path->[$i - 2]; | ||
| 467 | my $fontname = $path->[$i]; | ||
| 468 | my $font = $self->{pdf}->getFont($pagenum, $fontname); | ||
| 469 | my %meta = %{$font}; | ||
| 470 | my @keys = grep { $SCALARS{$meta{$_}->{type}} } keys %meta; | ||
| 471 | return { | ||
| 472 | type => 'd', | ||
| 473 | content => { | ||
| 474 | map { $_ => { type => 'f', content => $meta{$_}->{value} } } @keys, | ||
| 475 | }, | ||
| 476 | }; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub _page_fonts { | ||
| 480 | my ($self, $i, $path) = @_; | ||
| 481 | my $pagenum = $path->[$i - 1]; | ||
| 482 | |||
| 483 | return { | ||
| 484 | type => 'd', | ||
| 485 | content => { | ||
| 486 | map { $_ => \&_page_font } $self->{pdf}->getFontNames($pagenum), | ||
| 487 | }, | ||
| 488 | }; | ||
| 489 | } | ||
| 490 | |||
| 491 | sub _page_image { | ||
| 492 | my ($self, $i, $path) = @_; | ||
| 493 | my $pagenum = $path->[$i - 2]; | ||
| 494 | my ($imagenum) = $path->[$i] =~ m/\A(\d+)/xms; | ||
| 495 | |||
| 496 | $self->{image_cache} ||= {}; | ||
| 497 | $self->{image_cache}->{$pagenum} ||= {}; | ||
| 498 | my $cache = $self->{image_cache}->{$pagenum}->{$imagenum} ||= {}; | ||
| 499 | |||
| 500 | my $now = time; | ||
| 501 | if (!$cache->{timestamp} || $now - $cache->{timestamp} > $IMAGE_CACHE_TIMEOUT) { | ||
| 502 | my $content_tree = $self->{pdf}->getPageContentTree($pagenum); | ||
| 503 | my $gs = $content_tree->findImages(); | ||
| 504 | my $image_node = $gs->{images}->[$imagenum - 1]; | ||
| 505 | return if !$image_node; | ||
| 506 | |||
| 507 | #use Data::Dumper; print STDERR Dumper($image_node); | ||
| 508 | |||
| 509 | my $image; | ||
| 510 | if ('Do' eq $image_node->{type}) { | ||
| 511 | my $label = $image_node->{value}->[0]; | ||
| 512 | $image = $self->{pdf}->dereference(q{/} . $label, $pagenum); | ||
| 513 | if ($image) { | ||
| 514 | $image = $image->{value}; | ||
| 515 | } | ||
| 516 | } elsif ('BI' eq $image_node->{type}) { | ||
| 517 | $image = $image_node->{value}->[0]; | ||
| 518 | } | ||
| 519 | return if !$image; | ||
| 520 | |||
| 521 | #{ | ||
| 522 | # local $image->{value}->{StreamData}->{value} | ||
| 523 | # = q{.} x length($image->{value}->{StreamData}->{value}); | ||
| 524 | # use Data::Dumper; print STDERR "image $imagenum\n", Dumper($image); | ||
| 525 | #} | ||
| 526 | |||
| 527 | my $w = $image->{value}->{Width} || $image->{value}->{W} || 0; | ||
| 528 | if ($w) { | ||
| 529 | $w = $self->{pdf}->getValue($w); | ||
| 530 | } | ||
| 531 | my $h = $image->{value}->{Height} || $image->{value}->{H} || 0; | ||
| 532 | if ($h) { | ||
| 533 | $h = $self->{pdf}->getValue($h); | ||
| 534 | } | ||
| 535 | |||
| 536 | my $tmpl = Fuse::PDF::ImageTemplate->get_template_pdf(); | ||
| 537 | my $media_array = $tmpl->getValue($tmpl->getPage(1)->{MediaBox}); | ||
| 538 | $media_array->[2]->{value} = $w; | ||
| 539 | $media_array->[3]->{value} = $h; ## no critic(MagicNumber) | ||
| 540 | my $page = $tmpl->getPageContent(1); | ||
| 541 | $page =~ s/xxx/$w/igxms; | ||
| 542 | $page =~ s/yyy/$h/igxms; | ||
| 543 | $tmpl->setPageContent(1, $page); | ||
| 544 | my $tmpl_im_objnum = $tmpl->dereference('/Im0', 1)->{objnum}; | ||
| 545 | if ($image->{objnum}) { | ||
| 546 | $tmpl->replaceObject($tmpl_im_objnum, $self->{pdf}, $image->{objnum}, 1); | ||
| 547 | } else { | ||
| 548 | $tmpl->replaceObject($tmpl_im_objnum, undef, CAM::PDF::Node->new('object', $image), 1); | ||
| 549 | } | ||
| 550 | $tmpl->cleanse(); | ||
| 551 | $tmpl->cleansave(); # writes to RAM, not disk | ||
| 552 | |||
| 553 | #my $image_bytes = $image->{value}->{StreamData}->{value}; | ||
| 554 | #my $image_bytes = $self->{pdf}->decodeOne($image); | ||
| 555 | |||
| 556 | $cache->{timestamp} = $now; | ||
| 557 | $cache->{content} = $tmpl->{content}; | ||
| 558 | } | ||
| 559 | |||
| 560 | return { | ||
| 561 | type => 'f', | ||
| 562 | content => $cache->{content}, | ||
| 563 | }; | ||
| 564 | } | ||
| 565 | |||
| 566 | |||
| 567 | sub _page_images { | ||
| 568 | my ($self, $i, $path) = @_; | ||
| 569 | my $pagenum = $path->[$i - 1]; | ||
| 570 | |||
| 571 | my $content_tree = $self->{pdf}->getPageContentTree($pagenum); | ||
| 572 | my $gs = $content_tree->findImages(); | ||
| 573 | |||
| 574 | return { | ||
| 575 | type => 'd', | ||
| 576 | content => { | ||
| 577 | map { ($_ . '.pdf') => \&_page_image } 1 .. @{$gs->{images}}, | ||
| 578 | }, | ||
| 579 | }; | ||
| 580 | } | ||
| 581 | |||
| 582 | sub _page { | ||
| 583 | my ($self, $i, $path) = @_; | ||
| 584 | my $pagenum = $path->[$i]; | ||
| 585 | return { | ||
| 586 | type => 'd', | ||
| 587 | content => { | ||
| 588 | 'layout.txt' => \&_page_content, | ||
| 589 | 'fonts' => \&_page_fonts, | ||
| 590 | 'images' => \&_page_images, | ||
| 591 | 'text' => { | ||
| 592 | type => 'd', | ||
| 593 | content => { | ||
| 594 | 'plain_text.txt' => \&_page_text, | ||
| 595 | 'formatted_text.txt' => \&_page_textfb, | ||
| 596 | }, | ||
| 597 | }, | ||
| 598 | }, | ||
| 599 | }; | ||
| 600 | } | ||
| 601 | |||
| 602 | sub _pages { | ||
| 603 | my ($self) = @_; | ||
| 604 | return { | ||
| 605 | type => 'd', | ||
| 606 | content => { | ||
| 607 | map { $_ => \&_page } 1 .. $self->{pdf}->numPages, | ||
| 608 | }, | ||
| 609 | }; | ||
| 610 | } | ||
| 611 | |||
| 612 | sub _revisions { | ||
| 613 | my ($self) = @_; | ||
| 614 | my @revisions = map { $_->{pdf}->{content} } $self->all_revisions; | ||
| 615 | return { | ||
| 616 | type => 'd', | ||
| 617 | content => { | ||
| 618 | map { @revisions - $_ => { type => 'f', content => $revisions[$_] } } 0 .. $#revisions, | ||
| 619 | }, | ||
| 620 | }; | ||
| 621 | } | ||
| 622 | |||
| 623 | sub _metadata { | ||
| 624 | my ($self) = @_; | ||
| 625 | |||
| 626 | my $trailer = $self->{pdf}->{trailer}; | ||
| 627 | my %meta; | ||
| 628 | if ($trailer->{Info}) { | ||
| 629 | %meta = (%{$self->{pdf}->getValue($trailer->{Info})}, %meta); | ||
| 630 | } | ||
| 631 | if ($trailer->{ID} && 'array' eq $trailer->{ID}->{type}) { | ||
| 632 | $meta{ID} = CAM::PDF::Node->new('string', $self->{pdf}->writeAny($trailer->{ID})); | ||
| 633 | } | ||
| 634 | #print STDERR "@{[sort keys %meta]}\n"; | ||
| 635 | my @keys = grep { $SCALARS{$meta{$_}->{type}} } keys %meta; | ||
| 636 | return { | ||
| 637 | type => 'd', | ||
| 638 | content => { | ||
| 639 | map { $_ => { type => 'f', content => $meta{$_}->{value} } } @keys, | ||
| 640 | }, | ||
| 641 | }; | ||
| 642 | } | ||
| 643 | |||
| 644 | sub _root { | ||
| 645 | my ($self) = @_; | ||
| 646 | return { | ||
| 647 | type => 'd', | ||
| 648 | content => { | ||
| 649 | metadata => \&_metadata, | ||
| 650 | revisions => \&_revisions, | ||
| 651 | pages => \&_pages, | ||
| 652 | filesystems => \&_filesystems, | ||
| 653 | }, | ||
| 654 | }; | ||
| 655 | } | ||
| 656 | |||
| 657 | sub _file { | ||
| 658 | my ($self, $path) = @_; | ||
| 659 | |||
| 660 | my $nsymlinks = 0; | ||
| 661 | |||
| 662 | my @dirs = ($self->_root); | ||
| 663 | my @path = split m{/}xms, $path; | ||
| 664 | |||
| 665 | for (my $i = 0; $i < @path; ++$i) { ##no critic(ProhibitCStyleForLoops) | ||
| 666 | my $entry = $path[$i]; | ||
| 667 | next if q{} eq $entry; | ||
| 668 | |||
| 669 | my $type = $dirs[-1]->{type}; | ||
| 670 | return ENOTDIR() if 'd' ne $type; | ||
| 671 | next if q{.} eq $entry; | ||
| 672 | if (q{..} eq $entry) { | ||
| 673 | pop @dirs; | ||
| 674 | return EACCESS() if !@dirs; # tried to get parent of root | ||
| 675 | } | ||
| 676 | |||
| 677 | my $next = $dirs[-1]->{content}->{$entry}; | ||
| 678 | return ENOENT() if !$next; | ||
| 679 | |||
| 680 | if ('CODE' eq ref $next) { | ||
| 681 | $next = $self->$next($i, \@path); | ||
| 682 | } | ||
| 683 | return ENOENT() if !$next; | ||
| 684 | if ('HASH' ne ref $next) { | ||
| 685 | my $rest_of_path = join q{/}, q{}, @path[$i+1 .. $#path]; | ||
| 686 | #print STDERR "passing on $rest_of_path to ".ref($next)."\n"; | ||
| 687 | return ($next, $rest_of_path); | ||
| 688 | } | ||
| 689 | |||
| 690 | my $f = $next; | ||
| 691 | if ('l' eq $f->{type}) { | ||
| 692 | if ($i != $#path) { | ||
| 693 | return ELOOP() if ++$nsymlinks >= $ELOOP_LIMIT; | ||
| 694 | my $linkpath = $f->{content}; | ||
| 695 | |||
| 696 | # cannot leave the filesystem; must be relative | ||
| 697 | return EACCESS() if $linkpath =~ m{\A /}xms; | ||
| 698 | |||
| 699 | splice @path, $i + 1, 0, split m{/}xms, $linkpath; | ||
| 700 | } | ||
| 701 | } | ||
| 702 | push @dirs, $f; | ||
| 703 | } | ||
| 704 | |||
| 705 | return $dirs[-1] || ENOENT(); | ||
| 706 | } | ||
| 707 | |||
| 708 | 1; | ||
| 709 | |||
| 710 | __END__ |