Progress on upload
authorArt Cancro <ajc@citadel.org>
Mon, 6 Nov 2023 16:30:08 +0000 (11:30 -0500)
committerArt Cancro <ajc@citadel.org>
Mon, 6 Nov 2023 16:30:08 +0000 (11:30 -0500)
webcit-ng/server/messages.c
webcit-ng/server/upload.c

index 9fdb678b4da088e7822cf52c28f3eb0fd4346020..f176156a51d4ccdda7728acf21464348adb80098 100644 (file)
@@ -208,7 +208,6 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
                ctdl_printf(c, "MIME-Version: 1.0\r");
                ctdl_printf(c, "Content-Type: multipart/mixed; boundary=\"%s\"\r", mime_boundary);
                ctdl_write(c, HKEY("\r\n"));
-               ctdl_write(c, HKEY("\r\n"));
                ctdl_printf(c, "--%s\r", mime_boundary);        // start of message body
        }
 
@@ -231,7 +230,11 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
                for (i=0; i<num_attachments; ++i) {
                        extract_token(attid, att, i, ',', sizeof(attid));
                        one_att = pop_upload(attid);
-                       syslog(LOG_DEBUG, "💥 attachment: %s", one_att.filename);
+
+                       // After calling pop_upload(), the attachment is no longer in the global list.
+                       // The file descriptor has zero links, so when we close it, the filesystem will remove it from disk.
+
+                       syslog(LOG_DEBUG, "💥 attachment: %s , len=%ld", one_att.filename, one_att.length);
                        ctdl_printf(c, "--%s--\r", mime_boundary);
                        ctdl_printf(c, "Content-Type: %s; name=\"%s\"\r", one_att.content_type, one_att.filename);
                        ctdl_printf(c, "Content-Disposition: attachment; filename=\"%s\"\r", one_att.filename);
@@ -240,10 +243,20 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
 
                        char *raw_att = malloc(one_att.length);
                        if (raw_att) {
-                               fread(raw_att, one_att.length, 1, one_att.fp);
+                               rewind(one_att.fp);
+                               if (fread(raw_att, one_att.length, 1, one_att.fp) != 1) {
+                                       syslog(LOG_ERR, "messages: %m");
+                               }
 
                                // now encode it
-
+                               char *encoded_att = malloc((one_att.length * 150) / 100);
+                               if (encoded_att) {
+                                       size_t encoded_length = CtdlEncodeBase64(encoded_att, raw_att, one_att.length, BASE64_YES_LINEBREAKS);
+                                       ctdl_write(c, encoded_att, encoded_length);
+                                       syslog(LOG_DEBUG, "Encoded attachment: len=%ld", encoded_length);
+                                       free(encoded_att);
+                               }
+                               free(raw_att);
                        }
 
                        fclose(one_att.fp);
index 101d7894882493363773602817a3ce1dcc5e8a91..356cd1d750493c087cc98ef1968e26be91a212e3 100644 (file)
@@ -40,12 +40,17 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        u.length = length;
 
        // Write the upload to a file that we can access later when the user saves the message.
+       // tmpfile() creates a file with zero links in the directory, so it will be deleted when it is closed.
        u.fp = tmpfile();
        if (!u.fp) {
                syslog(LOG_ERR, "upload: %m");
                return;
        }
-       fwrite(content, length, 1, u.fp);                       // this file will be deleted by the OS when it is closed
+       if (fwrite(content, length, 1, u.fp) != 1) {
+               syslog(LOG_ERR, "upload: %m");
+               fclose(u.fp);
+               return;
+       }
 
        // Add it to the list of uploads the server is holding.
        pthread_mutex_lock(&upload_list_mutex);