Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff -aur cgit-0.11.1.orig/ui-tree.c cgit-0.11.1/ui-tree.c
- --- cgit-0.11.1.orig/ui-tree.c 2015-03-05 08:58:28.000000000 -0600
- +++ cgit-0.11.1/ui-tree.c 2015-03-17 14:33:29.921631785 -0500
- @@ -60,6 +60,116 @@
- html("</code></pre></td></tr></table>\n");
- }
- +static char *get_mimetype_from_file(const char *filename, const char *ext)
- +{
- + static const char *delimiters;
- + char *result;
- + FILE *fd;
- + char line[1024];
- + char *mimetype;
- + char *token;
- +
- + if (!filename)
- + return NULL;
- +
- + fd = fopen(filename, "r");
- + if (!fd)
- + return NULL;
- +
- + delimiters = " \t\r\n";
- + result = NULL;
- +
- + /* loop over all lines in the file */
- + while (!result && fgets(line, sizeof(line), fd)) {
- + mimetype = strtok(line, delimiters);
- +
- + /* skip empty lines and comment lines */
- + if (!mimetype || (mimetype[0] == '#'))
- + continue;
- +
- + /* loop over all extensions of mimetype */
- + while ((token = strtok(NULL, delimiters))) {
- + if (!strcasecmp(ext, token)) {
- + result = xstrdup(mimetype);
- + break;
- + }
- + }
- + }
- + fclose(fd);
- +
- + return result;
- +}
- +
- +int isimg(const unsigned char *sha1, char *path)
- +{
- + enum object_type type;
- + char *buf, *ext, *mimetype;
- + unsigned long size;
- + struct string_list_item *mime;
- +
- + type = sha1_object_info(sha1, &size);
- +
- + buf = read_sha1_file(sha1, &type, &size);
- + mimetype = NULL;
- + ext = strrchr(path, '.');
- + if (ext && *(++ext)) {
- + mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
- + if (mime) {
- + mimetype = (char *)mime->util;
- + } else {
- + mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext);
- + }
- + }
- + if (!mimetype) {
- + if (buffer_is_binary(buf, size)) {
- + mimetype = "application/octet-stream";
- + } else {
- + mimetype = "text/plain";
- + }
- + }
- + if(strcmp(mimetype,"image/png")==0||strcmp(mimetype,"image/jpeg")==0||strcmp(mimetype,"image/gif")==0)
- + return 0;
- +
- + return 1;
- +}
- +static void print_img(char *path, const char *rev){
- + char *delim = "?";
- + htmlf("<table summary='blob content' class='blob'><tr><td><img src=\"");
- + if (ctx.cfg.virtual_root) {
- + html_url_path(ctx.cfg.virtual_root);
- + html_url_path(ctx.repo->url);
- + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
- + html("/");
- + html_url_path("plain");
- + html("/");
- + if (path)
- + html_url_path(path);
- + } else {
- + html_url_path(ctx.cfg.script_name);
- + html("?url=");
- + html_url_arg(ctx.repo->url);
- + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
- + html("/");
- + html_url_arg("plain");
- + html("/");
- + if (path)
- + html_url_arg(path);
- + delim = "&";
- + }
- + if (ctx.qry.head && strcmp(ctx.qry.head, ctx.repo->defbranch)) {
- + html(delim);
- + html("h=");
- + html_url_arg(ctx.qry.head);
- + delim = "&";
- + }
- + if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
- + html(delim);
- + html("id=");
- + html_url_arg(rev);
- + }
- + htmlf("\" /></td></tr></table>");
- +}
- +
- #define ROWLEN 32
- static void print_binary_buffer(char *buf, unsigned long size)
- @@ -114,7 +224,9 @@
- return;
- }
- - if (buffer_is_binary(buf, size))
- + if(isimg(sha1,path)==0)
- + print_img(path, rev);
- + else if (buffer_is_binary(buf, size))
- print_binary_buffer(buf, size);
- else
- print_text_buffer(basename, buf, size);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement