upload.c: hold uploads in temporary file handles
[citadel.git] / webcit-ng / server / upload.c
1 // Upload handler
2 //
3 // Copyright (c) 1996-2023 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 struct uploaded_file {
11         char id[64];
12         char filename[256];
13         char content_type[256];
14         long length;
15         FILE *fp;
16 };
17
18 Array *upload_list = NULL;                                      // all files uploaded to this webcit instance
19 pthread_mutex_t upload_list_mutex = PTHREAD_MUTEX_INITIALIZER;  // Lock it before modifying
20
21
22 // This function is called by the MIME parser to handle data uploaded by the browser.
23 void upload_handler(char *name, char *filename, char *partnum, char *disp,
24                     void *content, char *cbtype, char *cbcharset,
25                     size_t length, char *encoding, char *cbid, void *userdata)
26 {
27         syslog(LOG_DEBUG, "upload_handler()");
28         syslog(LOG_DEBUG, "        name: %s", name);
29         syslog(LOG_DEBUG, "    filename: %s", filename);
30         syslog(LOG_DEBUG, " part number: %s", partnum);
31         syslog(LOG_DEBUG, " disposition: %s", disp);
32         syslog(LOG_DEBUG, "content type: %s", cbtype);
33         syslog(LOG_DEBUG, "    char set: %s", cbcharset);
34         syslog(LOG_DEBUG, "      length: %ld", length);
35         syslog(LOG_DEBUG, "    encoding: %s", encoding);
36         syslog(LOG_DEBUG, "          id: %s", cbid);
37
38         struct uploaded_file u;
39         generate_uuid(u.id);
40         safestrncpy(u.filename, filename, sizeof(u.filename));
41         safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
42         u.length = length;
43
44         // Write the upload to a file that we can access later when the user saves the message.
45         u.fp = tmpfile();
46         if (!u.fp) {
47                 syslog(LOG_ERR, "upload: %m");
48                 return;
49         }
50         fwrite(content, length, 1, u.fp);                       // this file will be deleted by the OS when it is closed
51
52         // Create a JSON object describing this upload
53         JsonValue *j_one_upload = NewJsonObject(HKEY(""));
54         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
55         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
56         JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
57         JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
58
59         // ...and attach it to the array of uploads
60         JsonValue *j_uploads = (JsonValue *) userdata;
61         JsonArrayAppend(j_uploads, j_one_upload);
62 }
63
64 // upload handler
65 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
66         // FIXME reject uploads if we're not logged in
67
68         // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
69         // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
70         JsonValue *j_uploads = NewJsonArray(HKEY(""));
71
72         // h->request_body will contain the upload(s) in MIME format
73         mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
74
75         // probably do something more clever here
76         h->response_code = 200;
77         h->response_string = strdup("OK");
78
79         // send back a JSON array of all files uploaded
80         StrBuf *sj = NewStrBuf();
81         SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
82         add_response_header(h, strdup("Content-type"), strdup("application/json"));
83         h->response_code = 200;
84         h->response_string = strdup("OK");
85         h->response_body_length = StrLength(sj);
86         h->response_body = SmashStrBuf(&sj);
87 }
88
89
90 // Dispatcher for paths starting with /ctdl/p/
91 void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
92         if (!strcasecmp(h->url, "/ctdl/p/")) {          // upload files
93                 upload_files(h, c);
94                 return;
95         }
96
97         do_404(h);                                      // unknown
98 }