summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--main.c8
-rw-r--r--template.c12
-rw-r--r--template/browse_head.html2
-rw-r--r--template/upload_head.html11
-rw-r--r--upload.c122
6 files changed, 148 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index 69c5b9b..cd47f53 100644
--- a/Makefile
+++ b/Makefile
@@ -66,6 +66,7 @@ install:
install -D -o www -g www -m 0440 template/browse_head.html /var/www/usr/share/vault/template/browse_head.html
install -D -o www -g www -m 0440 template/browse_foot.html /var/www/usr/share/vault/template/browse_foot.html
install -D -o www -g www -m 0440 template/browse_item.html /var/www/usr/share/vault/template/browse_item.html
+ install -D -o www -g www -m 0440 template/upload_head.html /var/www/usr/share/vault/template/upload_head.html
install -D -o www -g www -m 0440 fontawesome-6.5.1/css/fontawesome.css /var/www/vault-static/fontawesome-6.5.1/css/fontawesome.css
gzip -fk /var/www/vault-static/fontawesome-6.5.1/css/fontawesome.css
install -D -o www -g www -m 0440 fontawesome-6.5.1/css/solid.css /var/www/vault-static/fontawesome-6.5.1/css/solid.css
diff --git a/main.c b/main.c
index 1561a09..137ac14 100644
--- a/main.c
+++ b/main.c
@@ -38,16 +38,19 @@
#include "config.h"
#include "download.h"
#include "http.h"
+#include "upload.h"
enum page {
PAGE_BROWSE,
PAGE_DOWNLOAD,
+ PAGE_UPLOAD,
PAGE__MAX
};
static const char *const pages[PAGE__MAX] = {
BROWSE_URL,
- DOWNLOAD_URL
+ DOWNLOAD_URL,
+ UPLOAD_URL
};
int
@@ -96,6 +99,9 @@ main(void)
case PAGE_DOWNLOAD:
ret = download(&r);
break;
+ case PAGE_UPLOAD:
+ ret = upload(&r);
+ break;
default:
http_exit(&r, KHTTP_404, NULL);
}
diff --git a/template.c b/template.c
index 4ac3e9f..a7b2922 100644
--- a/template.c
+++ b/template.c
@@ -117,22 +117,22 @@ page_template_new(const char *page)
page_template_free(tmpl);
return NULL;
}
- /* read page footer */
+ /* read page footer (optional) */
path_size = snprintf(template_path, sizeof(template_path), "%s/%s_%s",
TEMPLATE_DIR, page, "foot.html");
- if (path_size < 0 || path_size >= sizeof(template_path) ||
- read_file(template_path, &tmpl->page_footer, &file_size) < 0) {
+ if (path_size < 0 || path_size >= sizeof(template_path)) {
page_template_free(tmpl);
return NULL;
}
- /* read page item */
+ read_file(template_path, &tmpl->page_footer, &file_size);
+ /* read page item (optional) */
path_size = snprintf(template_path, sizeof(template_path), "%s/%s_%s",
TEMPLATE_DIR, page, "item.html");
- if (path_size < 0 || path_size >= sizeof(template_path) ||
- read_file(template_path, &tmpl->page_item, &file_size) < 0) {
+ if (path_size < 0 || path_size >= sizeof(template_path)) {
page_template_free(tmpl);
return NULL;
}
+ read_file(template_path, &tmpl->page_item, &file_size);
return tmpl;
}
diff --git a/template/browse_head.html b/template/browse_head.html
index 7ed3c58..cfc1081 100644
--- a/template/browse_head.html
+++ b/template/browse_head.html
@@ -1,4 +1,4 @@
-<h1><i class="fa-regular fa-folder-open"></i> Vault</h1>
+<h1><i class="fa-regular fa-folder-open"></i> Browsing</h1>
<a href="@@upload_url@@">Upload here</a>
<table id="file-list">
<thead>
diff --git a/template/upload_head.html b/template/upload_head.html
new file mode 100644
index 0000000..fbdb209
--- /dev/null
+++ b/template/upload_head.html
@@ -0,0 +1,11 @@
+<h1><i class="fa-regular fa-folder-open"></i> Uploading</h1>
+<p>to <pre>@@path@@</pre></p>
+<form action="@@submit_url@@" method="post">
+ <div>
+ <label for="file">File(s):</label>
+ <input type="file" name="file" id="file" multiple />
+ </div>
+ <div>
+ <input type="submit" value="Upload" />
+ </div>
+</form>
diff --git a/upload.c b/upload.c
index 03e4169..fef628e 100644
--- a/upload.c
+++ b/upload.c
@@ -30,9 +30,13 @@
#include <stdlib.h>
#include <kcgi.h>
+#include <kcgihtml.h>
#include <limits.h>
#include <string.h>
+#include "cgi.h"
+#include "http.h"
+#include "template.h"
#include "upload.h"
#include "url.h"
@@ -64,3 +68,121 @@ build_upload_url(const struct kreq * r, struct file * file)
}
return action_url_len;
}
+
+/*
+ * data required in the template functions
+ */
+struct template_data {
+ struct kreq *r;
+ struct khtmlreq *html;
+ struct file *f;
+};
+
+static const char *const header_template_keys[] = {"path", "submit_url"};
+
+static int
+header_template_callback(size_t index, void *arg)
+{
+ struct kreq *r;
+ struct khtmlreq *html;
+ struct template_data *data;
+
+ if (arg == NULL) {
+ kutil_warn(NULL, NULL,
+ "Invalid data for upload header template");
+ return 0;
+ }
+ data = arg;
+ r = data->r;
+ html = data->html;
+
+ switch (index) {
+ case 0:
+ /* path */
+ K_OK(khtml_puts(html, data->f->path), r);
+ break;
+ case 1:
+ /* submit_url */
+ K_OK(khtml_puts(html, data->f->action_url), r);
+ break;
+ default:
+ kutil_warnx(r, NULL,
+ "Invalid key index for upload header template: %zd",
+ index);
+ return 0;
+ }
+
+ return 1;
+}
+
+struct http_ret
+upload(struct kreq * r)
+{
+ struct file *file;
+ struct http_ret ret;
+ struct khtmlreq html;
+ struct ktemplate template;
+ struct page_template *tmpl;
+ struct template_data data;
+
+ /* initialize vars and return structure for success */
+ file = NULL;
+ tmpl = NULL;
+ ret = (struct http_ret) {
+ KHTTP_200, ""
+ };
+
+ /* build file and submit url */
+ file = file_new(r->path);
+ if (file == NULL) {
+ ret = (struct http_ret) {
+ KHTTP_404,
+ "upload: Unable to build data file"
+ };
+ goto end;
+ }
+ if (build_upload_url(r, file) == 0) {
+ ret = (struct http_ret) {
+ KHTTP_500,
+ "upload: Can't build upload url"
+ };
+ goto end;
+ }
+
+ /* read template */
+ tmpl = page_template_new(UPLOAD_URL);
+ if (tmpl == NULL) {
+ ret = (struct http_ret) {
+ KHTTP_500,
+ "upload: Unable to read template"
+ };
+ goto end;
+ }
+
+ http_open(r, KHTTP_200, r->mime);
+
+ K_OK(khttp_puts(r, tmpl->header), r);
+ K_OK(khtml_open(&html, r, 0), r);
+
+ /* print upload form */
+ template.key = header_template_keys;
+ template.keysz = 2;
+ template.cb = header_template_callback;
+ template.arg = &data;
+ data.r = r;
+ data.html = &html;
+ data.f = file;
+ K_OK(khttp_template_buf(r, &template, tmpl->page_header,
+ strlen(tmpl->page_header)), 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 allocated memory */
+end:
+ file_free(file);
+ page_template_free(tmpl);
+ return ret;
+}