upload.c: hold uploads in temporary file handles
authorroot <root@TP-91ZK493.tierpoint.net>
Wed, 4 Oct 2023 16:49:34 +0000 (12:49 -0400)
committerroot <root@TP-91ZK493.tierpoint.net>
Wed, 4 Oct 2023 16:49:34 +0000 (12:49 -0400)
webcit-ng/server/upload.c

index 9fd363838e3cf6ff3d0a20b5e9cf7630385e2711..015fea14a38da310902d6a87153a2a256526e34e 100644 (file)
@@ -7,6 +7,17 @@
 
 #include "webcit.h"
 
+struct uploaded_file {
+       char id[64];
+       char filename[256];
+       char content_type[256];
+       long length;
+       FILE *fp;
+};
+
+Array *upload_list = NULL;                                     // all files uploaded to this webcit instance
+pthread_mutex_t upload_list_mutex = PTHREAD_MUTEX_INITIALIZER; // Lock it before modifying
+
 
 // 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,
@@ -24,23 +35,26 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        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);
+       struct uploaded_file u;
+       generate_uuid(u.id);
+       safestrncpy(u.filename, filename, sizeof(u.filename));
+       safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
+       u.length = length;
+
+       // Write the upload to a file that we can access later when the user saves the message.
+       u.fp = tmpfile();
+       if (!u.fp) {
+               syslog(LOG_ERR, "upload: %m");
                return;
        }
-       write(fd, content, length);
-       close(fd);
+       fwrite(content, length, 1, u.fp);                       // this file will be deleted by the OS when it is closed
 
        // Create a JSON object describing this upload
        JsonValue *j_one_upload = NewJsonObject(HKEY(""));
-       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), &tempfile[strlen(tempfile)-6], -1));
-       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), filename, -1));
-       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), cbtype, -1));
-       JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), length));
+       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("ref"), u.id, -1));
+       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("uploadfilename"), u.filename, -1));
+       JsonObjectAppend(j_one_upload, NewJsonPlainString(HKEY("contenttype"), u.content_type, -1));
+       JsonObjectAppend(j_one_upload, NewJsonNumber(HKEY("contentlength"), u.length));
 
        // ...and attach it to the array of uploads
        JsonValue *j_uploads = (JsonValue *) userdata;