From: Art Cancro Date: Wed, 27 Sep 2023 15:29:16 +0000 (-0400) Subject: upload.c: more progress on returning values X-Git-Tag: v996~3 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=dd637f98a45933d02dfe4d855494a17f7041bd28;p=citadel.git upload.c: more progress on returning values --- diff --git a/webcit-ng/server/upload.c b/webcit-ng/server/upload.c index e2d7d1c30..866be6762 100644 --- a/webcit-ng/server/upload.c +++ b/webcit-ng/server/upload.c @@ -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 diff --git a/webcit-ng/static/js/view_mail.js b/webcit-ng/static/js/view_mail.js index 6c2260020..3c21682ea 100644 --- a/webcit-ng/static/js/view_mail.js +++ b/webcit-ng/static/js/view_mail.js @@ -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 += "
  • succeeeeed
  • "; 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 += "
  • EPIC FAIL
  • "; - - // 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() {