| 1 | package Mojolicious::Plugin::AssetPack::Pipe::Favicon; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe'; | ||
| 3 | |||
| 4 | use Mojo::DOM; | ||
| 5 | use Mojo::JSON qw(false true); | ||
| 6 | use Mojo::Template; | ||
| 7 | use Mojo::Util; | ||
| 8 | use Mojolicious::Plugin::AssetPack::Util 'checksum'; | ||
| 9 | |||
| 10 | # this should be considered private | ||
| 11 | use constant API_URL => $ENV{MOJO_ASSETPACK_FAVICON_API_URL} || 'https://realfavicongenerator.net/api/favicon'; | ||
| 12 | |||
| 13 | has api_key => sub { die 'api_key() must be set' }; | ||
| 14 | has design => sub { shift->_build_design }; | ||
| 15 | has settings => sub { +{error_on_image_too_small => Mojo::JSON->true} }; | ||
| 16 | |||
| 17 | sub process { | ||
| 18 | my ($self, $assets) = @_; | ||
| 19 | return unless $self->topic =~ m!^favicon(\.\w+)?\.ico$!; | ||
| 20 | |||
| 21 | my $store = $self->assetpack->store; | ||
| 22 | my $asset = $assets->first; | ||
| 23 | my $attrs = $asset->TO_JSON; | ||
| 24 | my ($urls, $markup, %sub_assets); | ||
| 25 | |||
| 26 | $attrs->{key} = checksum(Mojo::JSON::encode_json($self->design)) | ||
| 27 | or die '[AssetPack] Invalid pipe("Favicon")->design({})'; | ||
| 28 | |||
| 29 | if (my $db = $store->load($attrs)) { | ||
| 30 | ($urls, $markup) = split /__MARKUP__/, $db->content; | ||
| 31 | $urls = [grep {/\w/} split /\n/, $urls]; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | ($urls, $markup) = $self->_fetch($asset); | ||
| 35 | $db = join "\n", @$urls, __MARKUP__ => $markup; | ||
| 36 | $store->save(\$db, $attrs); | ||
| 37 | } | ||
| 38 | |||
| 39 | my $renderer = sub { | ||
| 40 | my ($asset, $c, $args, @attrs) = @_; | ||
| 41 | my $content = $asset->content; | ||
| 42 | $content =~ s!"/([^.]+\.\w{3,})"!sprintf '"%s"', $sub_assets{$1} ? $sub_assets{$1}->url_for($c) : $1!ge; | ||
| 43 | return $content if $args; | ||
| 44 | return $c->render(data => $content); | ||
| 45 | }; | ||
| 46 | |||
| 47 | for my $url (@$urls) { | ||
| 48 | my $asset = $store->asset($url) or die "AssetPack was unable to fetch icons/assets asset $url"; | ||
| 49 | $sub_assets{join '.', $asset->name, $asset->format} = $asset; | ||
| 50 | $asset->renderer($renderer) if $asset->format =~ m!(manifest|xml|webapp)$!; | ||
| 51 | $self->assetpack->{by_checksum}{$asset->checksum} = $asset; | ||
| 52 | } | ||
| 53 | |||
| 54 | unless ($markup =~ m!msapplication-config!) { | ||
| 55 | $markup =~ s![\r\n]+$!!; | ||
| 56 | $markup .= qq(\n<meta name="msapplication-config" content="/browserconfig.xml">); | ||
| 57 | } | ||
| 58 | |||
| 59 | $asset->content($markup)->tag_for($renderer); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _build_design { | ||
| 63 | my $self = shift; | ||
| 64 | my $name = ref $self->app; | ||
| 65 | my $bg_color = '#F5F5F5'; | ||
| 66 | my $theme_color = '#536DFE'; | ||
| 67 | |||
| 68 | return { | ||
| 69 | desktop_browser => {}, | ||
| 70 | android_chrome => { | ||
| 71 | manifest => {display => 'standalone', name => $name, orientation => 'portrait'}, | ||
| 72 | picture_aspect => 'shadow', | ||
| 73 | theme_color => $theme_color, | ||
| 74 | }, | ||
| 75 | firefox_app => { | ||
| 76 | background_color => $bg_color, | ||
| 77 | circle_inner_margin => '5', | ||
| 78 | keep_picture_in_circle => 'true', | ||
| 79 | picture_aspect => 'circle', | ||
| 80 | manifest => {app_description => '', app_name => $name, developer_name => '', developer_url => '',} | ||
| 81 | }, | ||
| 82 | ios => {background_color => $bg_color, margin => '4', picture_aspect => 'background_and_margin',}, | ||
| 83 | safari_pinned_tab => {picture_aspect => 'black_and_white', threshold => 60, theme_color => $theme_color,}, | ||
| 84 | windows => { | ||
| 85 | background_color => $theme_color, | ||
| 86 | picture_aspect => "white_silhouette", | ||
| 87 | assets => { | ||
| 88 | windows_80_ie_10_tile => true, | ||
| 89 | windows_10_ie_11_edge_tiles => {big => true, medium => true, rectangle => false, small => false}, | ||
| 90 | } | ||
| 91 | }, | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _fetch { | ||
| 96 | my ($self, $asset) = @_; | ||
| 97 | $self->assetpack->ua->inactivity_timeout(60); | ||
| 98 | my $res = $self->assetpack->ua->post(API_URL, json => $self->_request($asset))->res; | ||
| 99 | |||
| 100 | unless ($res->code eq '200') { | ||
| 101 | my $json = $res->json || {}; | ||
| 102 | die sprintf '[AssetPack] Could not generate favicon: %s', | ||
| 103 | $json->{favicon_generation_result}{result}{error_message} || $res->error->{message}; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $data = $res->json->{favicon_generation_result}{favicon}; | ||
| 107 | my $files = $data->{files_urls} || []; | ||
| 108 | my $markup = $data->{html_code} or die qq|[AssetPack] No html_code generated. Invalid pipe("Favicon")->design({})..?|; | ||
| 109 | return ($files, $markup) if @$files; | ||
| 110 | die qq|[AssetPack] No favicons generated. Invalid pipe("Favicon")->design({})..?|; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _request { | ||
| 114 | my ($self, $asset) = @_; | ||
| 115 | |||
| 116 | return { | ||
| 117 | favicon_generation => { | ||
| 118 | api_key => $self->api_key, | ||
| 119 | favicon_design => $self->design, | ||
| 120 | settings => $self->settings, | ||
| 121 | files_location => {type => 'path', path => '/'}, | ||
| 122 | master_picture => {content => Mojo::Util::b64_encode($asset->content), type => 'inline'} | ||
| 123 | } | ||
| 124 | }; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | =encoding utf8 | ||
| 130 | |||
| 131 | =head1 NAME | ||
| 132 | |||
| 133 | Mojolicious::Plugin::AssetPack::Pipe::Favicon - Generate favicons | ||
| 134 | |||
| 135 | =head1 SYNOPSIS | ||
| 136 | |||
| 137 | =head2 Application | ||
| 138 | |||
| 139 | plugin AssetPack => {pipes => ["Favicon"]}; | ||
| 140 | app->asset->pipe("Favicon")->api_key("fd27cc5654345678765434567876545678765556"); | ||
| 141 | app->asset->process("favicon.ico" => "images/favicon.png"); | ||
| 142 | |||
| 143 | # Can also register variations of the favicon: | ||
| 144 | app->asset->process("favicon.variant1.ico" => "images/favicon1.png"); | ||
| 145 | app->asset->process("favicon.variant2.ico" => "images/favicon2.png"); | ||
| 146 | |||
| 147 | Note that the topic must be either "favicon.ico" or "favicon.some_identifier.ico". | ||
| 148 | |||
| 149 | The input image file should be a 260x260 PNG file for optimal results. | ||
| 150 | |||
| 151 | =head2 Template | ||
| 152 | |||
| 153 | %= asset "favicon.ico" | ||
| 154 | |||
| 155 | The above template will expand to whatever HTML that | ||
| 156 | L<http://realfavicongenerator.net> has generated, based on L</design>. Example: | ||
| 157 | |||
| 158 | <link rel="icon" type="image/png" href="/asset/52eaz7613a/favicon-16x16.png" sizes="16x16"> | ||
| 159 | <link rel="icon" type="image/png" href="/asset/65428718f1/favicon-32x32.png" sizes="32x32"> | ||
| 160 | <link rel="apple-touch-icon" sizes="114x114" href="/asset/9aab8718f1/apple-touch-icon-114x114.png"> | ||
| 161 | <link rel="apple-touch-icon" sizes="152x152" href="/asset/feee661542/apple-touch-icon-152x152.png"> | ||
| 162 | <meta name="msapplication-square310x310logo" content="/asset/123ab718f1/largelogo.png"> | ||
| 163 | <meta name="msapplication-wide310x150logo" content="/asset/a827bfddf0/widelogo.png"> | ||
| 164 | |||
| 165 | By default this pipe will only create desktop icons. Configure L</design> for | ||
| 166 | more icons. | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | L<Mojolicious::Plugin::AssetPack::Pipe::Favicon> uses | ||
| 171 | L<http://realfavicongenerator.net> to generate all the different favicons that | ||
| 172 | is required for your site. | ||
| 173 | |||
| 174 | This pipe is EXPERIMENTAL. Let me know if you are using it. | ||
| 175 | |||
| 176 | =head1 ATTRIBUTES | ||
| 177 | |||
| 178 | =head2 api_key | ||
| 179 | |||
| 180 | $self = $self->api_key($key); | ||
| 181 | $str = $self->api_key; | ||
| 182 | |||
| 183 | An API key obtained from L<http://realfavicongenerator.net/api/>. | ||
| 184 | |||
| 185 | =head2 design | ||
| 186 | |||
| 187 | $hash = $self->design; | ||
| 188 | $self = $self->design({desktop_browser => {}, ios => {}, windows => {}}); | ||
| 189 | |||
| 190 | Can be used to customize the different designs. Look for "favicon_design" on | ||
| 191 | L<http://realfavicongenerator.net/api/non_interactive_api> for details. | ||
| 192 | |||
| 193 | =head2 settings | ||
| 194 | |||
| 195 | $hash = $self->settings; | ||
| 196 | $self = $self->settings({compression => 3}); | ||
| 197 | |||
| 198 | Can be used to customize the different settings. Look for "settings" on | ||
| 199 | L<http://realfavicongenerator.net/api/non_interactive_api> for details. | ||
| 200 | |||
| 201 | =head1 METHODS | ||
| 202 | |||
| 203 | =head2 process | ||
| 204 | |||
| 205 | See L<Mojolicious::Plugin::AssetPack::Pipe/process>. | ||
| 206 | |||
| 207 | =head1 SEE ALSO | ||
| 208 | |||
| 209 | L<http://realfavicongenerator.net>. | ||
| 210 | |||
| 211 | L<https://css-tricks.com/favicon-quiz/>. | ||
| 212 | |||
| 213 | L<Mojolicious::Plugin::AssetPack>. | ||
| 214 | |||
| 215 | =cut |