#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,
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;