Advertisement
pouar

addimg.patch for cgit

Mar 18th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.25 KB | None | 0 0
  1. diff -aur cgit-0.11.1.orig/ui-tree.c cgit-0.11.1/ui-tree.c
  2. --- cgit-0.11.1.orig/ui-tree.c  2015-03-05 08:58:28.000000000 -0600
  3. +++ cgit-0.11.1/ui-tree.c   2015-03-17 14:33:29.921631785 -0500
  4. @@ -60,6 +60,116 @@
  5.     html("</code></pre></td></tr></table>\n");
  6.  }
  7.  
  8. +static char *get_mimetype_from_file(const char *filename, const char *ext)
  9. +{
  10. +   static const char *delimiters;
  11. +   char *result;
  12. +   FILE *fd;
  13. +   char line[1024];
  14. +   char *mimetype;
  15. +   char *token;
  16. +
  17. +   if (!filename)
  18. +       return NULL;
  19. +
  20. +   fd = fopen(filename, "r");
  21. +   if (!fd)
  22. +       return NULL;
  23. +
  24. +   delimiters = " \t\r\n";
  25. +   result = NULL;
  26. +
  27. +   /* loop over all lines in the file */
  28. +   while (!result && fgets(line, sizeof(line), fd)) {
  29. +       mimetype = strtok(line, delimiters);
  30. +
  31. +       /* skip empty lines and comment lines */
  32. +       if (!mimetype || (mimetype[0] == '#'))
  33. +           continue;
  34. +
  35. +       /* loop over all extensions of mimetype */
  36. +       while ((token = strtok(NULL, delimiters))) {
  37. +           if (!strcasecmp(ext, token)) {
  38. +               result = xstrdup(mimetype);
  39. +               break;
  40. +           }
  41. +       }
  42. +   }
  43. +   fclose(fd);
  44. +
  45. +   return result;
  46. +}
  47. +
  48. +int isimg(const unsigned char *sha1, char *path)
  49. +{
  50. +   enum object_type type;
  51. +   char *buf, *ext, *mimetype;
  52. +   unsigned long size;
  53. +   struct string_list_item *mime;
  54. +
  55. +   type = sha1_object_info(sha1, &size);
  56. +
  57. +   buf = read_sha1_file(sha1, &type, &size);
  58. +   mimetype = NULL;
  59. +   ext = strrchr(path, '.');
  60. +   if (ext && *(++ext)) {
  61. +       mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
  62. +       if (mime) {
  63. +           mimetype = (char *)mime->util;
  64. +       } else {
  65. +           mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
  66. +       }
  67. +   }
  68. +   if (!mimetype) {
  69. +       if (buffer_is_binary(buf, size)) {
  70. +           mimetype = "application/octet-stream";
  71. +       } else {
  72. +           mimetype = "text/plain";
  73. +       }
  74. +   }
  75. +   if(strcmp(mimetype,"image/png")==0||strcmp(mimetype,"image/jpeg")==0||strcmp(mimetype,"image/gif")==0)
  76. +       return 0;
  77. +  
  78. +   return 1;
  79. +}
  80. +static void print_img(char *path, const char *rev){
  81. +   char *delim = "?";
  82. +       htmlf("<table summary='blob content' class='blob'><tr><td><img src=\"");
  83. +   if (ctx.cfg.virtual_root) {
  84. +       html_url_path(ctx.cfg.virtual_root);
  85. +       html_url_path(ctx.repo->url);
  86. +       if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
  87. +           html("/");
  88. +           html_url_path("plain");
  89. +           html("/");
  90. +           if (path)
  91. +               html_url_path(path);
  92. +   } else {
  93. +       html_url_path(ctx.cfg.script_name);
  94. +       html("?url=");
  95. +       html_url_arg(ctx.repo->url);
  96. +       if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
  97. +           html("/");
  98. +           html_url_arg("plain");
  99. +           html("/");
  100. +           if (path)
  101. +               html_url_arg(path);
  102. +           delim = "&amp;";
  103. +   }
  104. +   if (ctx.qry.head && strcmp(ctx.qry.head, ctx.repo->defbranch)) {
  105. +       html(delim);
  106. +       html("h=");
  107. +       html_url_arg(ctx.qry.head);
  108. +       delim = "&amp;";
  109. +   }
  110. +   if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
  111. +       html(delim);
  112. +       html("id=");
  113. +       html_url_arg(rev);
  114. +   }
  115. +       htmlf("\" /></td></tr></table>");
  116. +}
  117. +
  118.  #define ROWLEN 32
  119.  
  120.  static void print_binary_buffer(char *buf, unsigned long size)
  121. @@ -114,7 +224,9 @@
  122.         return;
  123.     }
  124.  
  125. -   if (buffer_is_binary(buf, size))
  126. +   if(isimg(sha1,path)==0)
  127. +       print_img(path, rev);
  128. +   else if (buffer_is_binary(buf, size))
  129.         print_binary_buffer(buf, size);
  130.     else
  131.         print_text_buffer(basename, buf, size);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement