From: mail at eworm.de (Christian Hesse)
Subject: [PATCH 1/1] about: return images in plain mode
Date: Thu, 17 Apr 2014 15:19:24 +0200 [thread overview]
Message-ID: <1397740764-32681-1-git-send-email-mail@eworm.de> (raw)
In-Reply-To: <20140417141439.3b4043c4@leda.localdomain>
---
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
next prev parent reply other threads:[~2014-04-17 13:19 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-17 10:13 Images on about page list
2014-04-17 10:18 ` list
2014-04-17 10:19 ` [PATCH 1/1] filters: in markdown get images from their plain url mail
2014-04-17 10:57 ` Jason
2014-04-17 11:02 ` mail
2014-04-17 11:42 ` mail
2014-04-17 11:44 ` Jason
2014-04-17 12:14 ` list
2014-04-17 13:19 ` mail [this message]
2014-04-17 13:21 ` [PATCH 1/1] about: return images in plain mode list
2014-04-30 8:37 ` mail
2014-06-29 20:14 ` list
2014-06-29 21:07 ` Jason
2014-06-30 6:31 ` list
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1397740764-32681-1-git-send-email-mail@eworm.de \
--to=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).