3 // Copyright (c) 1996-2022 by the citadel.org team
5 // This program is open source software. Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
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)
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);
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);
32 syslog(LOG_ERR, "upload: %s: %m", tempfile);
35 write(fd, content, length);
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));
45 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
46 // FIXME reject uploads if we're not logged in
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(""));
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);
55 // probably do something more clever here
56 h->response_code = 200;
57 h->response_string = strdup("OK");
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);