[PATCH 1/1] about: return images in plain mode

Christian Hesse mail at eworm.de
Thu Apr 17 15:19:24 CEST 2014


---
 cgit.h     |  2 ++
 cmd.c      | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 shared.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 ui-plain.c | 40 ----------------------------------------
 4 files changed, 91 insertions(+), 44 deletions(-)

diff --git a/cgit.h b/cgit.h
index 0badc64..f2a1d3b 100644
--- a/cgit.h
+++ b/cgit.h
@@ -373,4 +373,6 @@ extern int readfile(const char *path, char **buf, size_t *size);
 
 extern char *expand_macros(const char *txt);
 
+extern char *get_mimetype_from_file(const char *filename, const char *ext);
+
 #endif /* CGIT_H */
diff --git a/cmd.c b/cmd.c
index 188cd56..76d892e 100644
--- a/cmd.c
+++ b/cmd.c
@@ -38,10 +38,54 @@ static void atom_fn(void)
 
 static void about_fn(void)
 {
-	if (ctx.repo)
-		cgit_print_repo_readme(ctx.qry.path);
-	else
+	if (ctx.repo) {
+		char *ext = NULL;
+		int freemime = 0;
+		struct string_list_item *mime;
+		char * mimetype = NULL;
+
+	        if (ctx.qry.path)
+			ext = strrchr(ctx.qry.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)
+					freemime = 1;
+	                }
+	        }
+
+		if (mimetype && strncmp(mimetype, "image/", 6) == 0) {
+			ctx.page.mimetype = mimetype;
+			ctx.page.charset = NULL;
+			cgit_print_plain();
+		} else {
+			cgit_print_http_headers();
+			cgit_print_docstart();
+			cgit_print_pageheader();
+
+			cgit_print_repo_readme(ctx.qry.path);
+
+			cgit_print_docend();
+		}
+
+	        /* If we allocated this, then casting away const is safe. */
+	        if (freemime)
+	                free(mimetype);
+
+	} else {
+		cgit_print_http_headers();
+		cgit_print_docstart();
+		cgit_print_pageheader();
+
 		cgit_print_site_readme();
+
+		cgit_print_docend();
+	}
+
 }
 
 static void blob_fn(void)
@@ -144,7 +188,7 @@ struct cgit_cmd *cgit_get_cmd(void)
 	static struct cgit_cmd cmds[] = {
 		def_cmd(HEAD, 1, 0, 0, 1),
 		def_cmd(atom, 1, 0, 0, 0),
-		def_cmd(about, 0, 1, 0, 0),
+		def_cmd(about, 0, 0, 0, 0),
 		def_cmd(blob, 1, 0, 0, 0),
 		def_cmd(commit, 1, 1, 1, 0),
 		def_cmd(diff, 1, 1, 1, 0),
diff --git a/shared.c b/shared.c
index 8ed14c0..651b771 100644
--- a/shared.c
+++ b/shared.c
@@ -557,3 +557,44 @@ char *expand_macros(const char *txt)
 	}
 	return result;
 }
+
+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;
+}
+
diff --git a/ui-plain.c b/ui-plain.c
index 30fff89..ef7b274 100644
--- a/ui-plain.c
+++ b/ui-plain.c
@@ -17,46 +17,6 @@ struct walk_tree_context {
 	int match;
 };
 
-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;
-}
-
 static int print_object(const unsigned char *sha1, const char *path)
 {
 	enum object_type type;
-- 
1.9.2



More information about the CGit mailing list