| 1 | package Catalyst::Controller::AutoAssets::Handler::ImageSet; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | # VERSION | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | extends 'Catalyst::Controller::AutoAssets::Handler::Directory'; | ||
| 11 | |||
| 12 | # Whether or not to include parent directories in the image name if this is true, | ||
| 13 | # a warning will be thrown if there are any duplicate filenames (and only the | ||
| 14 | # first will be used) | ||
| 15 | has 'flatten_paths', is => 'ro', isa => 'Bool', default => 0; | ||
| 16 | |||
| 17 | has 'include_regex', is => 'ro', isa => 'Maybe[Str]', default => '\.(png|jpg|jpeg|gif|bmp|tif|tiff|svg)$'; | ||
| 18 | has 'regex_ignore_case', is => 'ro', isa => 'Bool', default => 1; | ||
| 19 | |||
| 20 | has 'manifest', is => 'rw', isa => 'HashRef', default => sub {{}}; | ||
| 21 | sub rebuild_manifest { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my $manifest = {}; | ||
| 25 | for my $path (sort keys %{$self->subfile_meta}) { | ||
| 26 | my $name = $path; | ||
| 27 | # strip parent directories if flatten_paths is on: | ||
| 28 | ($name) = reverse split(/\//,$path) if($self->flatten_paths); | ||
| 29 | if(exists $manifest->{$name}) { | ||
| 30 | $self->_app->log->warn("Not including duplicate filename '$name' ('$path')"); | ||
| 31 | next; | ||
| 32 | } | ||
| 33 | # This is being done this way to make it easy for derived classes to put | ||
| 34 | # additional data in the manifest associated with each item | ||
| 35 | $manifest->{$name} = { subfile => $path }; | ||
| 36 | } | ||
| 37 | |||
| 38 | $self->manifest($manifest); | ||
| 39 | } | ||
| 40 | after set_subfile_meta => sub { (shift)->rebuild_manifest }; | ||
| 41 | |||
| 42 | |||
| 43 | around asset_request => sub { | ||
| 44 | my ( $orig, $self, $c, $sha1, @args ) = @_; | ||
| 45 | |||
| 46 | ## Just for debugging: | ||
| 47 | #if($sha1 eq 'dump_manifest') { | ||
| 48 | # $self->clear_asset; | ||
| 49 | # $self->prepare_asset; | ||
| 50 | # | ||
| 51 | # use Data::Dumper::Concise 'Dumper'; | ||
| 52 | # $c->response->header('Content-Type' => 'text/plain'); | ||
| 53 | # $c->response->body( | ||
| 54 | # $self->asset_name . ': ' . scalar(keys %{$self->manifest}) . " Items\n\n" . | ||
| 55 | # Dumper($self->manifest) | ||
| 56 | # ); | ||
| 57 | # return $c->detach; | ||
| 58 | #} | ||
| 59 | |||
| 60 | # If we wanted both/either the flattened and orig paths to work we could | ||
| 61 | # call $self->_unflatten_path(join('/',@args)) instead below | ||
| 62 | return $self->$orig($c, $sha1, $self->_unflatten_path($args[0])) | ||
| 63 | if ($self->flatten_paths); | ||
| 64 | |||
| 65 | return $self->$orig($c, $sha1, @args); | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub _unflatten_path { | ||
| 69 | my ($self, $name) = @_; | ||
| 70 | return undef unless (defined $name); | ||
| 71 | my $itm = $self->manifest->{$name} or return $name; | ||
| 72 | return $itm->{subfile}; | ||
| 73 | } | ||
| 74 | |||
| 75 | around _subfile_mtime_verify => sub { | ||
| 76 | my ($orig, $self, $path) = @_; | ||
| 77 | return $self->$orig($self->_unflatten_path($path)); | ||
| 78 | }; | ||
| 79 | |||
| 80 | around asset_path => sub { | ||
| 81 | my ($orig, $self, @subpath) = @_; | ||
| 82 | return $self->flatten_paths | ||
| 83 | ? join('/',$self->base_path,$self->asset_name,@subpath) | ||
| 84 | : $self->$orig(@subpath); | ||
| 85 | }; | ||
| 86 | |||
| 87 | |||
| 88 | has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw( | ||
| 89 | built_mtime | ||
| 90 | inc_mtimes | ||
| 91 | last_fingerprint_calculated | ||
| 92 | subfile_meta | ||
| 93 | _excluded_paths | ||
| 94 | manifest | ||
| 95 | )]}; | ||
| 96 | |||
| 97 | |||
| 98 | sub img_tag { | ||
| 99 | my ($self, @path) = @_; | ||
| 100 | my $name = join('/',@path); | ||
| 101 | return '<img alt="' . $name . | ||
| 102 | '" src="' . $self->asset_path($name) . '">'; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub img_tag_title { | ||
| 106 | my ($self, @path) = @_; | ||
| 107 | my $name = join('/',@path); | ||
| 108 | return '<img alt="' . $name . '" title="' . $name . | ||
| 109 | '" src="' . $self->asset_path($name) . '">'; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub dump_all_img_tags { | ||
| 114 | my $self = shift; | ||
| 115 | return join("\n",map { $self->img_tag_title($_) } (sort keys %{$self->manifest})); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | __END__ |