Completed the 'upload a file' dialog.
[citadel.git] / webcit / downloads.c
1 /*
2  * $Id: downloads.c 4849 2007-01-08 20:05:56Z ajc $
3  */
4 #include "webcit.h"
5
6 void display_room_directory(void)
7 {
8         char buf[1024];
9         char filename[256];
10         char filesize[256];
11         char comment[512];
12         int bg = 0;
13         char title[256];
14
15         output_headers(1, 1, 2, 0, 0, 0);
16         wprintf("<div id=\"banner\">\n"
17                 "<table class=\"downloads_banner\"><tr><td>"
18                 "<span class=\"titlebar\">");
19         snprintf(title, sizeof title, _("Files available for download in %s"), WC->wc_roomname);
20         escputs(title);
21         wprintf("</span>"
22                 "</td></tr></table>\n"
23                 "</div>\n<div id=\"content\">\n"
24         );
25
26         wprintf("<div class=\"fix_scrollbar_bug\">"
27                 "<table class=\"downloads_background\"><tr><td>\n");
28         wprintf("<tr><th>%s</th><th>%s</th><th>%s</th></tr>\n",
29                         _("Filename"),
30                         _("Size"),
31                         _("Description")
32         );
33
34         serv_puts("RDIR");
35         serv_getln(buf, sizeof buf);
36         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000"))
37         {
38                 extract_token(filename, buf, 0, '|', sizeof filename);
39                 extract_token(filesize, buf, 1, '|', sizeof filesize);
40                 extract_token(comment, buf, 2, '|', sizeof comment);
41                 bg = 1 - bg;
42                 wprintf("<tr bgcolor=\"#%s\">", (bg ? "DDDDDD" : "FFFFFF"));
43                 wprintf("<td>"
44                         "<a href=\"download_file/");
45                 urlescputs(filename);
46                 wprintf("\"><img src=\"static/diskette_24x.gif\" border=0 align=middle>\n");
47                                         escputs(filename);      wprintf("</a></td>");
48                 wprintf("<td>");        escputs(filesize);      wprintf("</td>");
49                 wprintf("<td>");        escputs(comment);       wprintf("</td>");
50                 wprintf("</tr>\n");
51         }
52
53         wprintf("</table>\n");
54
55         /** Now offer the ability to upload files... */
56         if (WC->room_flags & QR_UPLOAD)
57         {
58                 wprintf("<hr>");
59                 wprintf("<form "
60                         "enctype=\"multipart/form-data\" "
61                         "method=\"POST\" "
62                         "accept-charset=\"UTF-8\" "
63                         "action=\"upload_file\" "
64                         "name=\"upload_file_form\""
65                         ">\n"
66                 );
67
68                 wprintf(_("Upload a file:"));
69                 wprintf("&nbsp;<input NAME=\"filename\" SIZE=16 TYPE=\"file\">&nbsp;\n");
70                 wprintf(_("Description:"));
71                 wprintf("&nbsp;<input type=\"text\" name=\"description\" maxlength=\"64\" size=\"64\">&nbsp;");
72                 wprintf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">\n", _("Upload"));
73
74                 wprintf("</form>\n");
75         }
76
77         wprintf("</div>\n");
78         wDumpContent(1);
79 }
80
81
82 void download_file(char *filename)
83 {
84         char buf[256];
85         off_t bytes;
86         char content_type[256];
87         char *content = NULL;
88
89         /* Setting to nonzero forces a MIME type of application/octet-stream */
90         int force_download = 1;
91         
92         serv_printf("OPEN %s", filename);
93         serv_getln(buf, sizeof buf);
94         if (buf[0] == '2') {
95                 bytes = extract_long(&buf[4], 0);
96                 content = malloc(bytes + 2);
97                 if (force_download) {
98                         strcpy(content_type, "application/octet-stream");
99                 }
100                 else {
101                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
102                 }
103                 output_headers(0, 0, 0, 0, 0, 0);
104                 read_server_binary(content, bytes);
105                 serv_puts("CLOS");
106                 serv_getln(buf, sizeof buf);
107                 http_transmit_thing(content, bytes, content_type, 0);
108                 free(content);
109         } else {
110                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
111                 output_headers(0, 0, 0, 0, 0, 0);
112                 wprintf("Content-Type: text/plain\r\n");
113                 wprintf("\r\n");
114                 wprintf(_("An error occurred while retrieving this file: %s\n"), &buf[4]);
115         }
116
117 }
118
119
120
121 void upload_file(void)
122 {
123         char buf[1024];
124         size_t bytes_transmitted = 0;
125         size_t blocksize;
126
127         serv_printf("UOPN %s|%s", WC->upload_filename, bstr("description"));
128         serv_getln(buf, sizeof buf);
129         if (buf[0] != '2')
130         {
131                 strcpy(WC->ImportantMessage, &buf[4]);
132                 display_room_directory();
133                 return;
134         }
135
136         while (bytes_transmitted < WC->upload_length)
137         {
138                 blocksize = 4096;
139                 if (blocksize > (WC->upload_length - bytes_transmitted))
140                 {
141                         blocksize = (WC->upload_length - bytes_transmitted);
142                 }
143                 serv_printf("WRIT %d", blocksize);
144                 serv_getln(buf, sizeof buf);
145                 if (buf[0] == '7')
146                 {
147                         blocksize = atoi(&buf[4]);
148                         serv_write(&WC->upload[bytes_transmitted], blocksize);
149                         bytes_transmitted += blocksize;
150                 }
151         }
152
153         serv_puts("UCLS 1");
154         serv_getln(buf, sizeof buf);
155         strcpy(WC->ImportantMessage, &buf[4]);
156         display_room_directory();
157 }