upload.c: more progress on returning values
authorArt Cancro <ajc@citadel.org>
Wed, 27 Sep 2023 15:29:16 +0000 (11:29 -0400)
committerArt Cancro <ajc@citadel.org>
Wed, 27 Sep 2023 15:29:16 +0000 (11:29 -0400)
webcit-ng/server/upload.c
webcit-ng/static/js/view_mail.js

index e2d7d1c30995ad39d185235d925b72e811717211..866be6762b3cc8a6b204965f031594fb513cd2fd 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "webcit.h"
 
+
 // 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,
                    void *content, char *cbtype, char *cbcharset,
@@ -33,19 +34,34 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        }
        write(fd, content, length);
        close(fd);
+
+       JsonValue *j_uploads = (JsonValue *) userdata;
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("foo"), 1));
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("bar"), 2));
+       JsonArrayAppend(j_uploads, NewJsonNumber(HKEY("baz"), 3));
 }
 
 // upload handler
 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
        // FIXME reject uploads if we're not logged in
 
+       // This will be a JSON Array of all files that were uploaded during this HTTP transaction.
+       // Normally the browser will upload only one file per transaction, but that behavior is not guaranteed.
+       JsonValue *j_uploads = NewJsonArray(HKEY(""));
+
        // h->request_body will contain the upload(s) in MIME format
-       mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, NULL, 0);
+       mime_parser(h->request_body, (h->request_body + h->request_body_length), *upload_handler, NULL, NULL, j_uploads, 0);
 
        // 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_body = "{}";
-       h->response_body_length = strlen(h->response_body);
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       h->response_body_length = StrLength(sj);
+       h->response_body = SmashStrBuf(&sj);
 }
\ No newline at end of file
index 6c2260020ced461e81a7c76087a51e485ec4d697..3c21682ea309aca7c326a0f709a28f0d7fc4ddaa 100644 (file)
@@ -597,8 +597,8 @@ function upload_file(file) {
        xhr.open('POST', url, true);
       
        xhr.addEventListener('readystatechange', function(e) {
+               console.log("readyState: " + xhr.readyState);
                if (xhr.readyState == 4 && xhr.status == 200) {
-                       //document.getElementById("ctdl-upload_list").innerHTML += "<li>succeeeeed</li>";
                        num_attachments += 1;
                        document.getElementById("num_attachments").innerHTML = num_attachments;
 
@@ -606,11 +606,13 @@ function upload_file(file) {
                        let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
                        li.parentNode.removeChild(li);
                        uploads_in_progress -= 1;
+
+                       // what happened?
+                       console.log("response: " + xhr.response);
+                       console.log("responseText: " + xhr.responseText);
                }
                else if (xhr.readyState == 4 && xhr.status != 200) {
-                       //document.getElementById("ctdl-upload_list").innerHTML += "<li>EPIC FAIL</li>";
-
-                       // remove the "uploading in progress" message (maybe we should replace it with an error?)
+                       // remove the "uploading in progress" message (there was an error, so just let it disappear)
                        let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
                        li.parentNode.removeChild(li);
                        uploads_in_progress -= 1;
@@ -642,7 +644,6 @@ function upload_unhighlight(e) {
 }
 
 
-
 // Show or hide the attachments window in the composer
 function show_or_hide_attachments() {