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