{"thread":{"id":"43679","subject":"[PATCH] gitweb: Allow PNG, GIF, JPEG images to be displayed in \"blob\" view","startedAt":"2006-12-04T22:47:22Z","lastAt":"2006-12-04T22:47:22Z","messageCount":1,"participants":["Jakub Narebski"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"296620","messageId":"11652724422874-git-send-email-jnareb@gmail.com","threadId":"43679","inReplyTo":null,"subject":"[PATCH] gitweb: Allow PNG, GIF, JPEG images to be displayed in \"blob\" view","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-12-04T22:47:22Z","receivedAt":"2006-12-04T22:47:22Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Allow images in one of web formats (PNG, GIF, JPEG) - actually files\nwith mimetype of image/png, image/git, image/jpeg - to be displayed in\n\"blob\" view using <img /> element, instead of using \"blob_plain\" view\nfor them, like for all other files except also text/* mimetype files.\n\nThis makes possible to easily go to file history, to HEAD version of\nthe file, to appropriate commit etc; all of those are not available\nin \"blob_plain\" (raw) view.\n\nOnly text files can have \"blame\" view link in the formats part of\nnavbar.\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nWhat other files can be embedded in the page? What other files can\nbe displayed line by line safely?\n\n gitweb/gitweb.perl |   30 ++++++++++++++++++++++--------\n 1 files changed, 22 insertions(+), 8 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex 3c1b75d..d1d22ae 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -3304,10 +3304,13 @@ sub git_blob {\n \topen my $fd, \"-|\", git_cmd(), \"cat-file\", \"blob\", $hash\n \t\tor die_error(undef, \"Couldn't cat $file_name, $hash\");\n \tmy $mimetype = blob_mimetype($fd, $file_name);\n-\tif ($mimetype !~ m/^text\\//) {\n+\tif ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {\n \t\tclose $fd;\n \t\treturn git_blob_plain($mimetype);\n \t}\n+\t# we can have blame only for text/* mimetype\n+\t$have_blame &&= ($mimetype =~ m!^text/!);\n+\n \tgit_header_html(undef, $expires);\n \tmy $formats_nav = '';\n \tif (defined $hash_base && (my %co = parse_commit($hash_base))) {\n@@ -3344,13 +3347,24 @@ sub git_blob {\n \t}\n \tgit_print_page_path($file_name, \"blob\", $hash_base);\n \tprint \"<div class=\\\"page_body\\\">\\n\";\n-\tmy $nr;\n-\twhile (my $line = <$fd>) {\n-\t\tchomp $line;\n-\t\t$nr++;\n-\t\t$line = untabify($line);\n-\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n-\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n+\tif ($mimetype =~ m!^text/!) {\n+\t\tmy $nr;\n+\t\twhile (my $line = <$fd>) {\n+\t\t\tchomp $line;\n+\t\t\t$nr++;\n+\t\t\t$line = untabify($line);\n+\t\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\",\n+\t\t\t       $nr, $nr, $nr, esc_html($line, -nbsp=>1);\n+\t\t}\n+\t} elsif ($mimetype =~ m!^image/!) {\n+\t\tprint qq!<img type=\"$mimetype\"!;\n+\t\tif ($file_name) {\n+\t\t\tprint qq! alt=\"$file_name\" title=\"$file_name\"!;\n+\t\t}\n+\t\tprint qq! src=\"! .\n+\t\t      href(action=>\"blob_plain\", hash=>$hash,\n+\t\t           hash_base=>$hash_base, file_name=>$file_name) .\n+\t\t      qq!\" />\\n!;\n \t}\n \tclose $fd\n \t\tor print \"Reading blob failed.\\n\";\n-- \n1.4.4.1\n"}]}