upload.c: write uploaded data to a temp file
authorArt Cancro <ajc@citadel.org>
Wed, 27 Sep 2023 03:44:05 +0000 (23:44 -0400)
committerArt Cancro <ajc@citadel.org>
Wed, 27 Sep 2023 03:44:05 +0000 (23:44 -0400)
webcit-ng/server/upload.c [new file with mode: 0644]

diff --git a/webcit-ng/server/upload.c b/webcit-ng/server/upload.c
new file mode 100644 (file)
index 0000000..e2d7d1c
--- /dev/null
@@ -0,0 +1,51 @@
+// Upload handler
+//
+// Copyright (c) 1996-2022 by the citadel.org team
+//
+// This program is open source software.  Use, duplication, or
+// disclosure are subject to the GNU General Public License v3.
+
+#include "webcit.h"
+
+// This function is called by the MIME parser to handle data uploaded by the browser.
+void upload_handler(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, char *cbcharset,
+                   size_t length, char *encoding, char *cbid, void *userdata)
+{
+       syslog(LOG_DEBUG, "upload_handler()");
+       syslog(LOG_DEBUG, "        name: %s", name);
+       syslog(LOG_DEBUG, "    filename: %s", filename);
+       syslog(LOG_DEBUG, " part number: %s", partnum);
+       syslog(LOG_DEBUG, " disposition: %s", disp);
+       syslog(LOG_DEBUG, "content type: %s", cbtype);
+       syslog(LOG_DEBUG, "    char set: %s", cbcharset);
+       syslog(LOG_DEBUG, "      length: %ld", length);
+       syslog(LOG_DEBUG, "    encoding: %s", encoding);
+       syslog(LOG_DEBUG, "          id: %s", cbid);
+
+       // Write the upload to a file that we can pull later when the user saves the message.
+       char tempfile[PATH_MAX];
+       snprintf(tempfile, sizeof tempfile, "/tmp/ctdl_upload_XXXXXX");
+       int fd = mkstemp(tempfile);
+       if (fd < 0) {
+               syslog(LOG_ERR, "upload: %s: %m", tempfile);
+               return;
+       }
+       write(fd, content, length);
+       close(fd);
+}
+
+// upload handler
+void upload_files(struct http_transaction *h, struct ctdlsession *c) {
+       // FIXME reject uploads if we're not logged in
+
+       // h->request_body will contain the upload(s) in MIME format
+       mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, NULL, 0);
+
+       // probably do something more clever here
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       add_response_header(h, strdup("Content-type"), strdup("application/json"));
+       h->response_body = "{}";
+       h->response_body_length = strlen(h->response_body);
+}
\ No newline at end of file