diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2024-02-20 21:17:01 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2024-02-20 22:13:53 +0100 |
commit | 0effd40bad7ae753102e7040159afd76be4e8732 (patch) | |
tree | 20c80821ba637a395d8874414e00f4d120a837ea /browse.c | |
parent | 42f538cc66546997a166dbe67e489c9afabbb908 (diff) |
browse: http page
Diffstat (limited to 'browse.c')
-rw-r--r-- | browse.c | 87 |
1 files changed, 52 insertions, 35 deletions
@@ -44,8 +44,8 @@ #include "download.h" #include "file.h" #include "http.h" +#include "template.h" #include "url.h" -#include "util.h" /* * a list of files @@ -136,13 +136,14 @@ build_file_list(struct kreq * r, const char *request_dir) return 0; } -const char *const template_keys[] = {"name", "url"}; +const char *const template_keys[] = {"icon", "name", "url", "size"}; /* * data required in the template function */ struct template_data { struct kreq *r; + struct khtmlreq *html; struct file *f; }; @@ -150,7 +151,11 @@ static int template_callback(size_t index, void *arg) { struct kreq *r; + struct khtmlreq *html; struct template_data *data; + float file_size; + int i; + char size_read[] = {'k', 'M', 'G'}; if (arg == NULL) { kutil_warn(NULL, NULL, "Invalid data for browse template"); @@ -158,17 +163,40 @@ template_callback(size_t index, void *arg) } data = arg; r = data->r; + html = data->html; + switch (index) { case 0: - /* name */ - K_OK(khttp_puts(r, data->f->name), r); + /* icon */ + K_OK(khtml_puts(html, data->f->is_dir ? "folder" : "file"), r); break; case 1: + /* name */ + K_OK(khtml_puts(html, data->f->name), r); + break; + case 2: /* url */ - K_OK(khttp_puts(r, data->f->action_url), r); + K_OK(khtml_puts(html, data->f->action_url), r); + break; + case 3: + /* size, print as human readable */ + file_size = data->f->size; + i = 0; + while (file_size > 1024 && i < 3) { + file_size /= 1024; + i++; + } + if (i == 0) { + K_OK(khtml_printf(html, "%zu B", data->f->size), r); + } else { + K_OK(khtml_printf(html, "%.1f %cB", file_size, + size_read[i - 1]), r); + } break; default: - kutil_warnx(r, NULL, "Invalid key index for browse template: %zd", index); + kutil_warnx(r, NULL, + "Invalid key index for browse template: %zd", + index); return 0; } @@ -178,11 +206,10 @@ template_callback(size_t index, void *arg) void browse(struct kreq * r) { - size_t url_len, template_buf_len; + size_t url_len; char current_dir[PATH_MAX]; - char template_path[PATH_MAX]; - char *template_buf; struct file *file; + struct page_template *tmpl; struct khtmlreq html; struct ktemplate template; struct template_data data; @@ -202,49 +229,39 @@ browse(struct kreq * r) http_exit(r, KHTTP_500, "browse: Unable to build file list"); /* read template */ - template_path[0] = '\0'; - strlcat(template_path, TEMPLATE_DIR, PATH_MAX); - url_len = strlcat(template_path, "/browse_file.html", PATH_MAX); - if (url_len == 0 || url_len >= PATH_MAX) - http_exit(r, KHTTP_404, "browse: Unable to read template"); - - template_buf = NULL; - if (read_file(template_path, &template_buf, &template_buf_len) < 0 - || template_buf == NULL) - http_exit(r, KHTTP_500, "browse: Unable to read template"); + tmpl = page_template_new(BROWSE_URL); + if (tmpl == NULL) + http_exit(r, KHTTP_500, "browse: unable to read template"); /* we have all the data we need, we can start to write output page */ http_open(r, KHTTP_200, r->mime); - K_OK(khtml_open(&html, r, 0), r); + K_OK(khttp_puts(r, tmpl->header), r); + K_OK(khttp_puts(r, tmpl->page_header), r); - K_OK(khtml_elem(&html, KELEM_DOCTYPE), r); - K_OK(khtml_elem(&html, KELEM_HEAD), r); - K_OK(khtml_attr(&html, KELEM_META, KATTR_CHARSET, "utf-8", KATTR__MAX), - r); - K_OK(khtml_elem(&html, KELEM_HTML), r); - K_OK(khtml_elem(&html, KELEM_BODY), r); - K_OK(khtml_elem(&html, KELEM_P), r); - K_OK(khtml_puts(&html, "/"), r); - K_OK(khtml_closeelem(&html, 1), r); + K_OK(khtml_open(&html, r, 0), r); /* print file list */ template.key = template_keys; - template.keysz = 2; + template.keysz = 4; template.cb = template_callback; template.arg = &data; data.r = r; + data.html = &html; - K_OK(khtml_elem(&html, KELEM_UL), r); SLIST_FOREACH(file, &list, files) { data.f = file; - K_OK(khttp_template_buf(r, &template, template_buf, - template_buf_len), r); + K_OK(khttp_template_buf(r, &template, tmpl->page_item, + strlen(tmpl->page_item)), r); } + + K_OK(khttp_puts(r, tmpl->page_footer), r); + K_OK(khttp_puts(r, tmpl->footer), r); + K_OK(khtml_close(&html), r); - /* free list template and list */ - free(template_buf); + /* free page template and file list */ + page_template_free(tmpl); file = NULL; while (!SLIST_EMPTY(&list)) { file = SLIST_FIRST(&list); |