a1d50b9e44e790369b5cdaef5918cf80c7d078d0
[citadel.git] / webcit / graphics.c
1 /*
2  * $Id$
3  *
4  * Handles HTTP upload of graphics files into the system.
5  * \ingroup WebcitHttpServer
6  */
7
8 #include "webcit.h"
9
10 void display_graphics_upload(char *description, char *filename, char *uplurl)
11 {
12         char buf[SIZ];
13
14
15         snprintf(buf, SIZ, "UIMG 0||%s", filename);
16         serv_puts(buf);
17         serv_getln(buf, sizeof buf);
18         if (buf[0] != '2') {
19                 strcpy(WC->ImportantMessage, &buf[4]);
20                 display_main_menu();
21                 return;
22         }
23         output_headers(1, 1, 0, 0, 0, 0);
24
25         output_headers(1, 1, 1, 0, 0, 0);
26
27         svput("BOXTITLE", WCS_STRING, _("Image upload"));
28         do_template("beginbox");
29
30         wprintf("<form enctype=\"multipart/form-data\" action=\"%s\" "
31                 "method=\"post\" name=\"graphicsupload\">\n", uplurl);
32
33         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
34         wprintf("<input type=\"hidden\" name=\"which_room\" value=\"");
35         urlescputs(bstr("which_room"));
36         wprintf("\">\n");
37
38         wprintf(_("You can upload an image directly from your computer"));
39         wprintf("<br /><br />\n");
40
41         wprintf(_("Please select a file to upload:"));
42         wprintf("<input type=\"file\" name=\"filename\" size=\"35\">\n");
43
44         wprintf("<div class=\"uploadpic\"><img src=\"image&name=%s\"></div>\n", filename);
45
46         wprintf("<div class=\"buttons\">");
47         wprintf("<input type=\"submit\" name=\"upload_button\" value=\"%s\">\n", _("Upload"));
48         wprintf("&nbsp;");
49         wprintf("<input type=\"reset\" value=\"%s\">\n", _("Reset form"));
50         wprintf("&nbsp;");
51         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
52         wprintf("</div>\n");
53         wprintf("</form>\n");
54
55         do_template("endbox");
56
57         wDumpContent(1);
58 }
59
60 void do_graphics_upload(char *filename)
61 {
62         const char *MimeType;
63         char buf[SIZ];
64         int bytes_remaining;
65         int pos = 0;
66         int thisblock;
67         bytes_remaining = WC->upload_length;
68
69         if (havebstr("cancel_button")) {
70                 strcpy(WC->ImportantMessage,
71                         _("Graphics upload has been cancelled."));
72                 display_main_menu();
73                 return;
74         }
75
76         if (WC->upload_length == 0) {
77                 strcpy(WC->ImportantMessage,
78                         _("You didn't upload a file."));
79                 display_main_menu();
80                 return;
81         }
82         
83         MimeType = GuessMimeType(&WC->upload[0], bytes_remaining);
84         snprintf(buf, SIZ, "UIMG 1|%s|%s", MimeType, filename);
85         serv_puts(buf);
86
87         serv_getln(buf, sizeof buf);
88         if (buf[0] != '2') {
89                 strcpy(WC->ImportantMessage, &buf[4]);
90                 display_main_menu();
91                 return;
92         }
93         while (bytes_remaining) {
94                 thisblock = ((bytes_remaining > 4096) ? 4096 : bytes_remaining);
95                 serv_printf("WRIT %d", thisblock);
96                 serv_getln(buf, sizeof buf);
97                 if (buf[0] != '7') {
98                         strcpy(WC->ImportantMessage, &buf[4]);
99                         serv_puts("UCLS 0");
100                         serv_getln(buf, sizeof buf);
101                         display_main_menu();
102                         return;
103                 }
104                 thisblock = extract_int(&buf[4], 0);
105                 serv_write(&WC->upload[pos], thisblock);
106                 pos = pos + thisblock;
107                 bytes_remaining = bytes_remaining - thisblock;
108         }
109
110         serv_puts("UCLS 1");
111         serv_getln(buf, sizeof buf);
112         if (buf[0] != 'x') {
113                 display_success(&buf[4]);
114                 return;
115         }
116 }