]> code.citadel.org Git - citadel.git/blob - webcit-ng/server/upload.c
866be6762b3cc8a6b204965f031594fb513cd2fd
[citadel.git] / webcit-ng / server / upload.c
1 // Upload handler
2 //
3 // Copyright (c) 1996-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10
11 // This function is called by the MIME parser to handle data uploaded by the browser.
12 void upload_handler(char *name, char *filename, char *partnum, char *disp,
13                     void *content, char *cbtype, char *cbcharset,
14                     size_t length, char *encoding, char *cbid, void *userdata)
15 {
16         syslog(LOG_DEBUG, "upload_handler()");
17         syslog(LOG_DEBUG, "        name: %s", name);
18         syslog(LOG_DEBUG, "    filename: %s", filename);
19         syslog(LOG_DEBUG, " part number: %s", partnum);
20         syslog(LOG_DEBUG, " disposition: %s", disp);
21         syslog(LOG_DEBUG, "content type: %s", cbtype);
22         syslog(LOG_DEBUG, "    char set: %s", cbcharset);
23         syslog(LOG_DEBUG, "      length: %ld", length);
24         syslog(LOG_DEBUG, "    encoding: %s", encoding);
25         syslog(LOG_DEBUG, "          id: %s", cbid);
26
27         // Write the upload to a file that we can pull later when the user saves the message.
28         char tempfile[PATH_MAX];
29         snprintf(tempfile, sizeof tempfile, "/tmp/ctdl_upload_XXXXXX");
30         int fd = mkstemp(tempfile);
31         if (fd < 0) {
32                 syslog(LOG_ERR, "upload: %s: %m", tempfile);
33                 return;
34         }
35         write(fd, content, length);
36         close(fd);
37
38         JsonValue *j_uploads = (JsonValue *) userdata;
39         JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("foo"), 1));
40         JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("bar"), 2));
41         JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("baz"), 3));
42 }
43
44 // upload handler
45 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
46         // FIXME reject uploads if we're not logged in
47
48         // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
49         // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
50         JsonValue *j_uploads = NewJsonArray(HKEY(""));
51
52         // h->request_body will contain the upload(s) in MIME format
53         mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
54
55         // probably do something more clever here
56         h->response_code = 200;
57         h->response_string = strdup("OK");
58
59         // send back a JSON array of all files uploaded
60         StrBuf *sj = NewStrBuf();
61         SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
62         add_response_header(h, strdup("Content-type"), strdup("application/json"));
63         h->response_code = 200;
64         h->response_string = strdup("OK");
65         h->response_body_length = StrLength(sj);
66         h->response_body = SmashStrBuf(&sj);
67 }