upload.c: completed the code to reload attachments.
[citadel.git] / webcit-ng / server / upload.c
index f917f9a495f642f38b83a8b24bb9ced3ffe0da20..961d1845748f79a10b4d3002b5fce89a0c8864ea 100644 (file)
@@ -152,6 +152,19 @@ struct uploaded_file pop_upload(char *id) {
 }
 
 
+// When reloading attachments already in an existing message, accept only parts that are tagged as attachments.
+void attachment_filter(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, char *cbcharset,
+                   size_t length, char *encoding, char *cbid, void *userdata)
+{
+       struct uploaded_file u;
+
+       if (!strcasecmp(disp, "attachment")) {
+               upload_handler(name, filename, partnum, disp, content, cbtype, cbcharset, length, encoding, cbid, userdata);
+       }
+}
+
+
 // Load the attachments from an existing message.  This is typically used when forwarding a message,
 // so the attachments don't have to be sent out to the browser and back.
 void load_attachments_from_message(struct http_transaction *h, struct ctdlsession *c, char *name) {
@@ -167,11 +180,28 @@ void load_attachments_from_message(struct http_transaction *h, struct ctdlsessio
                return;
        }
 
+       JsonValue *j_uploads = NewJsonArray(HKEY(""));
+       Body = NewStrBuf();
        while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
-               // do something
+               StrBufAppendPrintf(Body, "%s\n", buf);
        }
+       char *raw_message = SmashStrBuf(&Body);
+       mime_parser(raw_message, NULL, *attachment_filter, NULL, NULL, j_uploads, 0);
+       free(raw_message);
 
-       do_405(h);
+       // probably do something more clever here
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+
+       // send back a JSON array of all files uploaded
+       StrBuf *sj = NewStrBuf();
+       SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
+       add_response_header(h, strdup("Content-type"), strdup("application/json"));
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       h->response_body_length = StrLength(sj);
+       h->response_body = SmashStrBuf(&sj);
+       syslog(LOG_DEBUG, "upload: %s", h->response_body);
 }