]> git.cameronkatri.com Git - cgit.git/blobdiff - shared.c
mime: rewrite detection function
[cgit.git] / shared.c
index a83afcb3e275bed29d41ec779bf019ddde7589c5..3d91a76ec09c8a88aefa2d1148f3542aa6850565 100644 (file)
--- a/shared.c
+++ b/shared.c
@@ -7,7 +7,6 @@
  */
 
 #include "cgit.h"
-#include <stdio.h>
 
 struct cgit_repolist cgit_repolist;
 struct cgit_context ctx;
@@ -250,8 +249,8 @@ int cgit_refs_cb(const char *refname, const struct object_id *oid, int flags,
        return 0;
 }
 
-static void cgit_diff_tree_cb(struct diff_queue_struct *q,
-                             struct diff_options *options, void *data)
+void cgit_diff_tree_cb(struct diff_queue_struct *q,
+                      struct diff_options *options, void *data)
 {
        int i;
 
@@ -561,3 +560,42 @@ char *expand_macros(const char *txt)
        }
        return result;
 }
+
+char *get_mimetype_for_filename(const char *filename)
+{
+       char *ext, *mimetype, *token, line[1024];
+       FILE *file;
+       struct string_list_item *mime;
+
+       if (!filename)
+               return NULL;
+
+       ext = strrchr(filename, '.');
+       if (!ext)
+               return NULL;
+       ++ext;
+       if (!ext[0])
+               return NULL;
+       mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
+       if (mime)
+               return xstrdup(mime->util);
+
+       if (!ctx.cfg.mimetype_file)
+               return NULL;
+       file = fopen(ctx.cfg.mimetype_file, "r");
+       if (!file)
+               return NULL;
+       while (fgets(line, sizeof(line), file)) {
+               if (!line[0] || line[0] == '#')
+                       continue;
+               mimetype = strtok(line, " \t\r\n");
+               while ((token = strtok(NULL, " \t\r\n"))) {
+                       if (!strcasecmp(ext, token)) {
+                               fclose(file);
+                               return xstrdup(mimetype);
+                       }
+               }
+       }
+       fclose(file);
+       return NULL;
+}