]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/server/upload.c
Progress on upload
[citadel.git] / webcit-ng / server / upload.c
index 711bbf3523af9f7c6d7d41aa7c5f70740b014d4a..356cd1d750493c087cc98ef1968e26be91a212e3 100644 (file)
@@ -7,14 +7,6 @@
 
 #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
 
@@ -36,18 +28,29 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        syslog(LOG_DEBUG, "          id: %s", cbid);
 
        struct uploaded_file u;
-       generate_uuid(u.id);
+
+       // create a random ID for the attachment
+       for (int i=0; i<sizeof(u.id); ++i) {
+               u.id[i] = (rand() % 26) + 'a';
+       }
+       u.id[sizeof(u.id)-1] = 0;
+
        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.
+       // 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);
@@ -106,6 +109,54 @@ void ctdl_p_base(struct http_transaction *h, struct ctdlsession *c) {
 }
 
 
+// delete an uploaded item
+void delete_upload(struct uploaded_file this_one) {
+       int i;
+       struct uploaded_file *u;
+
+       pthread_mutex_lock(&upload_list_mutex);
+       for (i=0; i<array_len(upload_list); ++i) {
+               u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
+               if (!strcmp(u->id, this_one.id)) {
+                       fclose(u->fp);                          // this deletes the file because it has 0 links
+                       array_delete_element_at(upload_list, i);
+                       i = array_len(upload_list) + 1;         // Go out of scope; we're done here
+               }
+       }
+       pthread_mutex_unlock(&upload_list_mutex);
+}
+
+
+// DAV delete an uploaded item
+void dav_delete_upload(struct http_transaction *h, struct ctdlsession *c, struct uploaded_file this_one) {
+       delete_upload(this_one);
+       do_204(h);
+}
+
+
+// Remove an uploaded item from the upload_list.  Caller now owns the file handle and is responsible for closing it.
+struct uploaded_file pop_upload(char *id) {
+       int i;
+       struct uploaded_file *u;
+       struct uploaded_file ret;
+
+       memset(&ret, 0, sizeof(struct uploaded_file));
+
+       pthread_mutex_lock(&upload_list_mutex);
+       for (i=0; i<array_len(upload_list); ++i) {
+               u = (struct uploaded_file *) array_get_element_at(upload_list, i), sizeof(struct uploaded_file);
+               if (!strcmp(u->id, id)) {
+                       ret = *u;
+                       array_delete_element_at(upload_list, i);
+                       i = array_len(upload_list) + 1;         // Go out of scope; we're done here
+               }
+       }
+       pthread_mutex_unlock(&upload_list_mutex);
+
+       return(ret);                                            // ret will be all-zeroes if we didn't find it
+}
+
+
 // Handle operations on a specific upload
 void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
        int i;
@@ -137,7 +188,7 @@ void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *na
                do_405(h);
        }
        else if (!strcasecmp(h->method, "DELETE")) {            // delete the item
-               do_405(h);
+               dav_delete_upload(h, c, this_one);
        }
        else {                                                  // unsupported method
                do_405(h);
@@ -167,4 +218,4 @@ void ctdl_p(struct http_transaction *h, struct ctdlsession *c) {
 
        // If we get to this point, the client requested an action we don't know how to perform.
        do_404(h);
-}
\ No newline at end of file
+}