From 1bc5733741f84bef8cf0e35e54f069420670c20d Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 29 Aug 2025 17:15:20 +0000 Subject: better display raw files in tree view --- ui-tree.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/ui-tree.c b/ui-tree.c index 3d8a2eb..5368596 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -86,10 +86,44 @@ static void print_binary_buffer(char *buf, unsigned long size) html("\n"); } +static void print_object_mime(const char *path, const char *rev, const char *mime) +{ + char *fileurl, *pre, *post; + + fileurl = cgit_fileurl(ctx.repo->url, "plain", path, NULL); + + if (!strncmp(mime, "image/", 6)) { + pre = ""; + } else if (!strncmp(mime, "audio/", 6)) { + pre = ""; + } else if (!strncmp(mime, "video/", 6)) { + pre = ""; + } else { + pre = ""; + } + + html("
"); + html(pre); + html_url_path(fileurl); + if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) { + html(ctx.cfg.virtual_root ? "?" : "&"); + html("id="); + html_url_arg(rev); + } + htmlf(post, mime); + html("
"); + + free(fileurl); +} + static void print_object(const struct object_id *oid, const char *path, const char *basename, const char *rev) { enum object_type type; - char *buf; + char *buf, *mime; unsigned long size; bool is_binary; @@ -100,13 +134,18 @@ static void print_object(const struct object_id *oid, const char *path, const ch return; } - buf = repo_read_object_file(the_repository, oid, &type, &size); - if (!buf) { - cgit_print_error_page(500, "Internal server error", - "Error reading object %s", oid_to_hex(oid)); - return; + mime = get_mimetype_for_filename(path); + if (mime) { + is_binary = false; + } else { + buf = repo_read_object_file(the_repository, oid, &type, &size); + if (!buf) { + cgit_print_error_page(500, "Internal server error", + "Error reading object %s", oid_to_hex(oid)); + return; + } + is_binary = buffer_is_binary(buf, size); } - is_binary = buffer_is_binary(buf, size); cgit_set_title_from_path(path); @@ -127,12 +166,15 @@ static void print_object(const struct object_id *oid, const char *path, const ch return; } - if (is_binary) + if (mime) + print_object_mime(path, rev, mime); + else if (is_binary) print_binary_buffer(buf, size); else print_text_buffer(basename, buf, size); - free(buf); + if (!mime) + free(buf); } struct single_tree_ctx { -- cgit v1.2.3-freya