aboutsummaryrefslogtreecommitdiff
path: root/src/site.c
blob: 6fe8d85cd09758d7bc3e2225485431f46ecc9bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "site.h"

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>

#include "fs.h"
#include "log.h"
#include "hashmap.h"
#include "render.h"

/* TODO: Probably shouldn't use PATH_MAX, but i'll leave it for now */
/* TODO: handle error cases for paths that are too long */

#define THUMB_SUFFIX "_thumb"

static const char *index_html = "index.html";

static bool
wand_passfail(MagickWand *wand, MagickPassFail status)
{
	if (status != MagickPass) {
		char *desc;
		ExceptionType severity;
		desc = MagickGetException(wand, &severity);
		log_printl(LOG_FATAL, "GraphicsMagick error: %.1024s severity: %d\n",
				desc, severity);
		return false;
	}

	return true;
}

#define TRYWAND(w, f) if (!wand_passfail(w, f)) goto magick_fail

static bool
optimize_image(MagickWand *wand, const char *dst,
		const struct image_config *conf, 
		const struct timespec *srcmtim, bool dry)
{
	int update = file_is_uptodate(dst, srcmtim);
	if (update == -1) return false;
	if (update == 1) return true;

	log_print(LOG_DETAIL, "Converting %s...", dst);
	if (dry) goto out;

	unsigned long nx = conf->max_width, ny = conf->max_height;
	if (conf->strip) {
		TRYWAND(wand, MagickStripImage(wand));
	}
	TRYWAND(wand, MagickSetCompressionQuality(wand, conf->quality));
	unsigned long x = MagickGetImageWidth(wand),
				  y = MagickGetImageHeight(wand);
	/* Resize only if the image is actually bigger. No point in making small
	 * images bigger. */
	if (x > nx || y > ny) {
		if (conf->smart_resize) {
			double ratio = (double)x / y;
			if (x > y) {
				ny = ny / ratio;
			} else {
				nx = nx * ratio;
			}
		}
		TRYWAND(wand, MagickResizeImage(wand, nx, ny, LanczosFilter, 0));
	}
	TRYWAND(wand, MagickWriteImage(wand, dst));
	setdatetime(dst, srcmtim);

out:
	log_printf(LOG_DETAIL, " done.\n");
	return true;
magick_fail:
	return false;
}

static bool
images_walk(struct bstnode *node, void *data)
{
	struct site *site = data;
	struct image *image = node->value;
	struct stat dstat;
	char htmlpath[PATH_MAX];
	const char *base = rbasename(image->dst);

	log_printl(LOG_DEBUG, "Image: %s, datetime %s", image->basename, 
			image->datestr);

	if (!nmkdir(image->dst, &dstat, site->dry_run)) return false;

	if (!site->dry_run) {
		TRYWAND(site->wand, MagickReadImage(site->wand, image->source));
	}
	if (!optimize_image(site->wand, image->dst_image, &site->config->images,
			&image->modtime, site->dry_run)) {
		goto magick_fail;
	}
	if (!optimize_image(site->wand, image->dst_thumb, &site->config->thumbnails,
			&image->modtime, site->dry_run)) {
		goto magick_fail;
	}
	if (!site->dry_run) {
		MagickRemoveImage(site->wand);
	}

	joinpathb(htmlpath, image->dst, index_html);
	hashmap_insert(image->album->preserved, base, (char *)base);
	return render_make_image(&site->render, htmlpath, image);
magick_fail:
	return false;
}

static bool
albums_walk(struct bstnode *node, void *data)
{
	struct site *site = data;
	struct album *album = node->value;
	struct stat dstat;
	if (!nmkdir(album->slug, &dstat, site->dry_run)) return false;

	if (!site->dry_run) {
		hashmap_insert(site->album_dirs, album->slug, (char *)album->slug);
		if (!render_set_album_vars(&site->render, album)) return false;
	}

	char htmlpath[PATH_MAX];
	joinpathb(htmlpath, album->slug, index_html);
	if (!render_make_album(&site->render, htmlpath, album)) return false;

	log_printl(LOG_DEBUG, "Album: %s, datetime %s", album->slug, album->datestr);
	if (!bstree_inorder_walk(album->images->root, images_walk, site)) {
		return false;
	}

	hashmap_insert(album->preserved, index_html, (char *)index_html);
	if (rmextra(album->slug, album->preserved) < 0) {
		log_printl_errno(LOG_ERROR, 
					"Something happened while deleting extraneous files");
	}

	return true;
}

/*
 * Recursively traverse the content directory. If there are images in the
 * directory, "create" an album. If an album.ini was found, then the title and
 * description in that file are used. Otherwise, the date of the album is used
 * as its title. If the images are in the root of the content directory, then a
 * special "unorganized" album will be created. The title and description will
 * be used, but the slug will always be "unorganized".
 */
static bool
traverse(struct site *site, const char *path, struct stat *dstat)
{
	bool ok = true;
	DIR *dir = opendir(path);
	if (!dir) {
		log_printl_errno(LOG_FATAL, "Can't open directory %s", path);
		return false;
	}
	struct dirent *ent;
	struct album_config *album_conf = calloc(1, sizeof *album_conf);
	struct album *album = album_new(album_conf, site->config, path, 
			path + site->rel_content_dir, dstat);
	if (album == NULL) {
		closedir(dir);
		return false;
	}
	while ((ent = readdir(dir))) {
		if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
			continue;
		}

		struct stat fstats;
		char *subpath = joinpath(path, ent->d_name);
		if (stat(subpath, &fstats)) {
			log_printl_errno(LOG_FATAL, "Can't read %s", subpath);
			ok = false;
			goto fail;
		}

		if (S_ISDIR(fstats.st_mode)) {
			ok = traverse(site, subpath, &fstats);
			if (!ok) goto fail;
		} else if (!strcmp(ent->d_name, ALBUM_CONF)) {
			ok = album_config_read_ini(subpath, album_conf);
			if (!ok) goto fail;
		} else if (isimage(subpath)) {
			struct image *image = image_new(subpath, &fstats, album);
			if (image == NULL) {
				free(subpath);
				goto fail;
			}
			album_add_image(album, image);
			continue;
		}
		free(subpath);
	}
	
	if (album->images->root != NULL) {
		album_set_year(album);
		bstree_add(site->albums, album);
		closedir(dir);
		return true;
	}

fail:
	album_destroy(album);
	closedir(dir);
	return ok;
}

bool
site_build(struct site *site)
{
	struct stat dstat;
	char staticp[PATH_MAX];

	if (!nmkdir(site->output_dir, &dstat, false)) return false;

	if (chdir(site->output_dir)) {
		log_printl_errno(LOG_FATAL, "Can't change to directory %s",
				site->output_dir);
		return false;
	}

	if (!bstree_inorder_walk(site->albums->root, albums_walk, (void *)site)) {
		return false;
	}

	if (!render_make_index(&site->render, index_html)) return false;
	hashmap_insert(site->album_dirs, index_html, (char *)index_html);

	joinpathb(staticp, site->root_dir, STATICDIR);
	if (stat(staticp, &dstat)) {
		if (errno != ENOENT) {
			log_printl_errno(LOG_FATAL, "Couldn't read static dir");
			return false;
		}
		if (rmextra(site->output_dir, site->album_dirs) < 0) {
			log_printl_errno(LOG_ERROR,
					"Something happened while deleting extraneous files");
		}
	} else if (!site->dry_run 
			&& !filesync(staticp, site->output_dir, site->album_dirs)) {
		log_printl(LOG_FATAL, "Can't copy static files");
		return false;
	}

	chdir(site->root_dir);
	return true;
}

bool
site_load(struct site *site)
{
	struct stat cstat;
	if (stat(site->content_dir, &cstat)) {
		log_printl_errno(LOG_FATAL, "Can't read %s", site->content_dir);
		return false;
	}

	if (!traverse(site, site->content_dir, &cstat)) return false;

	return render_init(&site->render, site->root_dir, site->config, site->albums);
}

bool
site_init(struct site *site)
{
	site->config = site_config_init();
	if (!site_config_read_ini(site->root_dir, site->config)) return false;
	site->albums = bstree_new(album_cmp, album_destroy);

	if (site->root_dir == NULL) {
		site->root_dir = malloc(PATH_MAX);
		if (getcwd(site->root_dir, PATH_MAX) == NULL) {
			log_printl_errno(LOG_FATAL, "Couldn't get working directory");
			return false;
		}
	}

	site->content_dir = joinpath(site->root_dir, CONTENTDIR); 
	site->rel_content_dir = strlen(site->root_dir) + 1;
	InitializeMagick(NULL);
	site->wand = NewMagickWand();
	if (!site->dry_run) {
		site->album_dirs = hashmap_new();
	}
	site->render.dry_run = site->dry_run;

	return true;
}

void
site_deinit(struct site *site)
{
	if (site->albums) bstree_destroy(site->albums);
	site_config_destroy(site->config);
	free(site->content_dir);
	free(site->root_dir);
	if (site->wand != NULL) {
		DestroyMagickWand(site->wand);
		DestroyMagick();
	}
	if (!site->dry_run) {
		hashmap_free(site->album_dirs);
		render_deinit(&site->render);
	}
}