From 9542016d5d820eef0882ed7f4c3da5f238453707 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Wed, 8 Oct 2025 01:48:14 +0200 Subject: page.c: Switch preview and download So far, clicking on a directory name previewed it, whereas clicking on a file name would download it, and file previews were on a separate column. This behaviour was found to be confusing, so it has been simplified. Now, clicking on a file or directory name would preview it. On the other hand, the "Preview" column has been replaced with a "Download" column, so that both files and directories can be downloaded. Thanks to Avron for the suggestion. [1] [1]: https://codeberg.org/xavidcr/slcl/issues/2 --- page.c | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/page.c b/page.c index 3c66e0a..d519dcd 100644 --- a/page.c +++ b/page.c @@ -171,7 +171,7 @@ static int prepare_name(struct html_node *const n, struct stat *const sb, int ret = -1; struct html_node *a; struct dynstr d, dname; - const char *const sep = S_ISDIR(sb->st_mode) ? "/" : ""; + const char *const sep = S_ISDIR(sb->st_mode) ? "/" : "", *href; char *encurl = NULL; dynstr_init(&d); @@ -192,14 +192,29 @@ static int prepare_name(struct html_node *const n, struct stat *const sb, fprintf(stderr, "%s: http_encode_url failed\n", __func__); goto end; } - else if (html_node_add_attr(a, "href", encurl)) + else if (S_ISDIR(sb->st_mode)) + href = encurl; + else + { + dynstr_free(&d); + + if (dynstr_append(&d, "%s?preview=1", encurl)) + { + fprintf(stderr, "%s: dynstr_append [2] failed\n", __func__); + goto end; + } + + href = d.str; + } + + if (html_node_add_attr(a, "href", href)) { fprintf(stderr, "%s: html_node_add_attr href failed\n", __func__); goto end; } else if (dynstr_append(&dname, "%s%s", name, sep)) { - fprintf(stderr, "%s: dynstr_append [2] failed\n", __func__); + fprintf(stderr, "%s: dynstr_append [3] failed\n", __func__); goto end; } else if (html_node_set_value(a, dname.str)) @@ -370,7 +385,7 @@ end: return ret; } -static int prepare_preview(struct html_node *const n, +static int prepare_download(struct html_node *const n, const struct stat *const sb, const char *const dir, const char *const name) { int ret = -1; @@ -380,7 +395,7 @@ static int prepare_preview(struct html_node *const n, dynstr_init(&d); - if (!S_ISREG(sb->st_mode)) + if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) return 0; else if (!(a = html_node_add_child(n, "a"))) { @@ -397,20 +412,23 @@ static int prepare_preview(struct html_node *const n, fprintf(stderr, "%s: http_encode_url failed\n", __func__); goto end; } - - dynstr_free(&d); - - if (dynstr_append(&d, "%s?preview=1", encurl)) + else if (S_ISDIR(sb->st_mode)) { - fprintf(stderr, "%s: dynstr_append encd failed\n", __func__); - goto end; + dynstr_free(&d); + + if (dynstr_append(&d, "%s?download=1", encurl)) + { + fprintf(stderr, "%s: dynstr_append encurl dir failed\n", __func__); + goto end; + } } - else if (html_node_add_attr(a, "href", d.str)) + + if (html_node_add_attr(a, "href", d.str)) { fprintf(stderr, "%s: html_node_add_attr failed\n", __func__); goto end; } - else if (html_node_set_value(a, "Preview")) + else if (html_node_set_value(a, "Download")) { fprintf(stderr, "%s: html_node_set_value value failed\n", __func__); goto end; @@ -491,12 +509,12 @@ static int add_element(struct html_node *const n, } else if (prepare_share(td[SHARE], &sb, pr->dir, name)) { - fprintf(stderr, "%s: prepare_date failed\n", __func__); + fprintf(stderr, "%s: prepare_share failed\n", __func__); goto end; } - else if (prepare_preview(td[PREVIEW], &sb, pr->dir, name)) + else if (prepare_download(td[DOWNLOAD], &sb, pr->dir, name)) { - fprintf(stderr, "%s: prepare_date failed\n", __func__); + fprintf(stderr, "%s: prepare_download failed\n", __func__); goto end; } @@ -1391,7 +1409,7 @@ static int add_thead(struct html_node *const table) [SIZE] = "Size", [DATE] = "Date", [SHARE] = "Share", - [PREVIEW] = "Preview" + [DOWNLOAD] = "Download" }; struct html_node *thead, *tr; -- cgit v1.2.3