]> Pileus Git - ~andy/ct/blob - gallery/gallery.c
Add timestamp checking
[~andy/ct] / gallery / gallery.c
1 #include <glib.h>
2 #include <glib/gstdio.h>
3 #include <ct.h>
4 #include "html.h"
5
6 void resize(gchar *orig, gchar *thumb, gchar *size)
7 {
8         GStatBuf oinfo, tinfo;
9         int oval = g_stat(orig,  &oinfo);
10         int tval = g_stat(thumb, &tinfo);
11         if (tval || (!oval && oinfo.st_mtime > tinfo.st_mtime)) {
12                 /* god damn glib */
13                 gchar *argv[] = {"convert", "-resize", size, orig, thumb, NULL};
14                 g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH,
15                         NULL, NULL, NULL, NULL, NULL, NULL);
16         }
17 }
18
19 GList *gen_thumbs(GList *images)
20 {
21         if (!g_file_test("large", G_FILE_TEST_EXISTS)) g_mkdir("large", 0775);
22         if (!g_file_test("small", G_FILE_TEST_EXISTS)) g_mkdir("small", 0775);
23         if (!g_file_test("thumb", G_FILE_TEST_EXISTS)) g_mkdir("thumb", 0775);
24         for (GList *cur = images; cur; cur = cur->next) {
25                 gchar *name = cur->data;
26                 gchar *large = g_build_filename("large", name, NULL);
27                 gchar *small = g_build_filename("small", name, NULL);
28                 gchar *thumb = g_build_filename("thumb", name, NULL);
29                 resize(large, thumb, "200x200");
30                 resize(large, small, "800x800");
31                 g_free(large);
32                 g_free(small);
33                 g_free(thumb);
34         }
35         return images;
36 }
37
38 GList *read_dir(gchar *dirname)
39 {
40         GDir *dir = g_dir_open(dirname, 0, NULL);
41         const gchar *name = NULL;
42         GList *images = NULL;
43         while ((name = g_dir_read_name(dir)))
44                 images = g_list_prepend(images, g_strdup(name));
45         images = g_list_sort(images, (GCompareFunc)g_strcmp0);
46         return images;
47 }
48
49 int main()
50 {
51         ct_print_header("text/html", NULL);
52
53         const gchar *path  = ct_get_path_info();
54         const gchar *query = ct_get_query_string();
55
56         GList *thumbs = gen_thumbs(read_dir("large"));
57
58         if (!path || g_str_equal(path, "/"))
59                 frame_index();
60         else if (g_str_equal(path, "/head"))
61                 frame_head();
62         else if (g_str_equal(path, "/nav"))
63                 frame_nav(FALSE, thumbs);
64         else if (g_str_equal(path, "/noframe"))
65                 frame_nav(TRUE, thumbs);
66         else if (g_str_equal(path, "/show_small"))
67                 frame_show("small", "show_large", query);
68         else if (g_str_equal(path, "/show_large"))
69                 frame_show("large", "show_small", query);
70 }