-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathupload_cgi.c
551 lines (438 loc) · 13.4 KB
/
upload_cgi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
* @file upload_cgi.c
* @brief 上传文件后台CGI程序
* @author liu_danbing
* @version 1.0
* @date 2016-08-29
*/
#include "fcgi_config.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "fcgi_stdio.h"
#include "util_cgi.h"
#include "redis_keys.h"
#include "redis_op.h"
#define UPLOAD_LOG_MODULE "cgi"
#define UPLOAD_LOG_PROC "upload"
extern char g_storage_web_port[PORT_LEN];
/* -------------------------------------------*/
/**
* @brief 得到上传文件所属用户的 用户名
* @return 0 -succ
* -1 fail
* http://ip:port/upload/UploadAction?user=gailun
*/
/* -------------------------------------------*/
int get_username(char *user)
{
char *query_string = getenv("QUERY_STRING");
//得到用户名
query_parse_key_value(query_string, "user", user, NULL);
if (strlen(user) == 0) {
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "get user has no value!!", user);
return -1;
}
else {
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "get user = [%s]", user);
}
return 0;
}
/* -------------------------------------------*/
/**
* @brief 解析上传的post数据 保存到本地临时路径
* 同时得到文件名称
*
* @param len (in) post数据的长度
* @param file_name (out) 文件的文件名
*
* @returns
* 0 succ, -1 fail
*/
/* -------------------------------------------*/
int recv_save_file(int len, char *filename)
{
int retn = 0;
int i, ch;
char *file_buf = NULL;
char *begin = NULL;
char *end = NULL;
char *p, *q, *k;
char content_text[TEMP_BUF_MAX_LEN] = {0};
char boundary[TEMP_BUF_MAX_LEN] = {0};
//==========> 开辟存放文件的 内存 <===========
file_buf = malloc(len);
if (file_buf == NULL) {
printf("malloc error! file size is to big!!!!\n");
return -1;
}
begin = file_buf;
p = begin;
for (i = 0; i < len; i++) {
if ((ch = getchar()) < 0) {
printf("Error: Not enough bytes received on standard input<p>\n");
retn = -1;
goto END;
}
//putchar(ch);
*p = ch;
p++;
}
//===========> 开始处理前端发送过来的post数据格式 <============
//begin deal
end = p;
p = begin;
//get boundary
p = strstr(begin, "\r\n");
if (p == NULL) {
printf("wrong no boundary!\n");
retn = -1;
goto END;
}
strncpy(boundary, begin, p-begin);
boundary[p-begin] = '\0';
//printf("boundary: [%s]\n", boundary);
p+=2;//\r\n
//已经处理了p-begin的长度
len -= (p-begin);
//get content text head
begin = p;
p = strstr(begin, "\r\n");
if(p == NULL) {
printf("ERROR: get context text error, no filename?\n");
retn = -1;
goto END;
}
strncpy(content_text, begin, p-begin);
content_text[p-begin] = '\0';
//printf("content_text: [%s]\n", content_text);
p+=2;//\r\n
len -= (p-begin);
//get filename
// filename="123123.png"
// ↑
q = begin;
q = strstr(begin, "filename=");
q+=strlen("filename=");
q++;
k = strchr(q, '"');
strncpy(filename, q, k-q);
filename[k-q] = '\0';
trim_space(filename);
//printf("filename: [%s]\n", filename);
//get file
begin = p;
p = strstr(begin, "\r\n");
p+=4;//\r\n\r\n
len -= (p-begin);
begin = p;
// now begin -->file's begin
//find file's end
p = memstr(begin, len, boundary);
if (p == NULL) {
p = end-2; //\r\n
}
else {
p = p -2;//\r\n
}
//begin---> file_len = (p-begin)
//=====> 此时begin-->p两个指针的区间就是post的文件二进制数据
//======>将数据写入文件中,其中文件名也是从post数据解析得来 <===========
int fd = 0;
fd = open(filename, O_CREAT|O_WRONLY, 0644);
if (fd < 0) {
printf("open %s error\n", filename);
retn = -1;
goto END;
}
ftruncate(fd, (p-begin));
write(fd, begin, (p-begin));
close(fd);
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "upload [%s] succ!", filename);
END:
free(file_buf);
return retn;
}
/* -------------------------------------------*/
/**
* @brief 将一个本地文件上传到 后台分布式文件系统中
*
* @param filename (in) 本地文件的路径
* @param fileid (out)得到上传之后的文件ID路径
*
* @returns
* 0 succ, -1 fail
*/
/* -------------------------------------------*/
int upload_to_dstorage(char *filename, char *fileid)
{
int retn = 0;
pid_t pid;
int pfd[2];
if (pipe(pfd) < 0) {
printf("pip error\n");
}
pid = fork();
if (pid < 0) {
printf("fork error\n");
}
if (pid == 0) {
//child
//关闭读管道
close(pfd[0]);
//将标准输出重定向到写管道
dup2(pfd[1], STDOUT_FILENO);
execlp("fdfs_upload_file", "fdfs_upload_file", FDFS_CLIENT_CONF, filename, NULL);
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "execlp fdfs_upload_file error\n");
close(pfd[1]);
}
//关闭写管道
close(pfd[1]);
//read from 读管道
read(pfd[0],fileid, TEMP_BUF_MAX_LEN);
trim_space(fileid);
if (strlen(fileid) == 0) {
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC,"[upload FAILED!]");
retn = -1;
goto END;
}
wait(NULL);
close(pfd[0]);
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "get [%s] succ!\n", fileid);
END:
return retn;
}
/* -------------------------------------------*/
/**
* @brief 封装文件存储在分布式系统中的 完整 url
*
* @param fileid (in) 文件分布式id路径
* @param fdfs_file_url (out) 文件的完整url地址
*
* @returns
* 0 succ, -1 fail
*/
/* -------------------------------------------*/
int make_file_url(char *fileid, char *fdfs_file_url)
{
int retn = 0;
char *p = NULL;
char *q = NULL;
char *k = NULL;
char fdfs_file_stat_buf[TEMP_BUF_MAX_LEN] = {0};
char fdfs_file_host_name[HOST_NAME_LEN] = {0};
int pfd[2];
pid_t pid;
pipe(pfd);
pid = fork();
if (pid == 0) {
//child
//关闭读管道
close(pfd[0]);
//将标准输出重定向到写管道
dup2(pfd[1], STDOUT_FILENO);
execlp("fdfs_file_info", "fdfs_file_info", FDFS_CLIENT_CONF, fileid, NULL);
}
//关闭写管道
close(pfd[1]);
//read from 读管道
read(pfd[0], fdfs_file_stat_buf, TEMP_BUF_MAX_LEN);
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "get file_ip [%s] succ\n", fdfs_file_stat_buf);
wait(NULL);
close(pfd[0]);
//拼接上传文件的完整url地址--->http://host_name/group1/M00/00/00/D12313123232312.png
p = strstr(fdfs_file_stat_buf, "source ip address: ");
//192.168.123.123
//q k
q = p + strlen("source ip address: ");
k = strstr(q, "\n");
strncpy(fdfs_file_host_name, q, k-q);
fdfs_file_host_name[k-q] = '\0';
//printf("host_name:[%s]\n", fdfs_file_host_name);
strcat(fdfs_file_url, "http://");
strcat(fdfs_file_url, fdfs_file_host_name);
strcat(fdfs_file_url, ":");
strcat(fdfs_file_url, g_storage_web_port);
strcat(fdfs_file_url, "/");
strcat(fdfs_file_url, fileid);
printf("[%s]\n", fdfs_file_url);
return retn;
}
/* -------------------------------------------*/
/**
* @brief 将文件信息存储到redis数据库 多个hash表形式
*
* @param fileid
* @param fdfs_file_url
* @param filename
* @param user
*
* @returns
* 0 succ, -1 fail
*/
/* -------------------------------------------*/
int store_file_to_redis1(char *fileid, char *fdfs_file_url, char *filename, char *user)
{
int retn = 0;
redisContext * redis_conn = NULL;
time_t now;;
char create_time[TIME_STRING_LEN];
char suffix[SUFFIX_LEN];
char user_id[10] = {0};
char file_user_list[KEY_NAME_SIZ] = {0};
redis_conn = rop_connectdb_nopwd(REDIS_SERVER_IP, REDIS_SERVER_PORT);
if (redis_conn == NULL) {
return retn;
}
//FILEID_URL_HASH
rop_hash_set(redis_conn, FILEID_URL_HASH, fileid, fdfs_file_url);
//FILEID_NAME_HASH
rop_hash_set(redis_conn, FILEID_NAME_HASH, fileid, filename);
//FILEID_TIME_HASH
//create time
now = time(NULL);
strftime(create_time, TIME_STRING_LEN-1, "%Y-%m-%d %H:%M:%S", localtime(&now));
rop_hash_set(redis_conn, FILEID_TIME_HASH, fileid, create_time);
//FILEID_USER_HASH
rop_hash_set(redis_conn, FILEID_USER_HASH, fileid, user);
//FILEID_TYPE_HASH
get_file_suffix(filename, suffix);
rop_hash_set(redis_conn, FILEID_TYPE_HASH, fileid, suffix);
//FILEID_SHARED_STATUS_HASH(文件的共享状态)
rop_hash_set(redis_conn, FILEID_SHARED_STATUS_HASH, fileid, "0");
//将文件插入到FILE_HOT_ZSET中,默认的权值是1
rop_zset_increment(redis_conn, FILE_HOT_ZSET, fileid);
//将FILEID 插入到 用户私有列表中FILE_USER_LIST
rop_hash_get(redis_conn, USER_USERID_HASH, user, user_id);
sprintf(file_user_list, "%s%s", FILE_USER_LIST, user_id);
rop_list_push(redis_conn, file_user_list, fileid);
//文件引用计数+1
rop_hincrement_one_field(redis_conn,FILE_REFERENCE_COUNT_HASH , fileid, 1);
rop_disconnect(redis_conn);
return retn;
}
/* -------------------------------------------*/
/**
* @brief 将文件信息存储到redis数据库
*
* @param fileid
* @param fdfs_file_url
* @param filename
* @param user
*
* @returns
* 0 succ, -1 fail
*/
/* -------------------------------------------*/
int store_file_to_redis(char *fileid, char *fdfs_file_url, char *filename, char *user)
{
int retn = 0;
redisContext * redis_conn = NULL;
char *redis_value_buf = NULL;
time_t now;;
char create_time[TIME_STRING_LEN];
char suffix[SUFFIX_LEN];
redis_conn = rop_connectdb_nopwd(REDIS_SERVER_IP, REDIS_SERVER_PORT);
if (redis_conn == NULL) {
return retn;
}
//make file value
redis_value_buf = malloc(VALUES_ID_SIZE);
if (redis_value_buf == NULL) {
LOG(UPLOAD_LOG_MODULE, UPLOAD_LOG_PROC, "[-]malloc redis value error\n");
retn = -1;
goto END;
}
memset(redis_value_buf, 0, VALUES_ID_SIZE);
//fileid
strcat(redis_value_buf, fileid);
strcat(redis_value_buf, REDIS_DILIMT);
//url
strcat(redis_value_buf, fdfs_file_url);
strcat(redis_value_buf, REDIS_DILIMT);
//filename
strcat(redis_value_buf, filename);
strcat(redis_value_buf, REDIS_DILIMT);
//create time
now = time(NULL);
strftime(create_time, TIME_STRING_LEN-1, "%Y-%m-%d %H:%M:%S", localtime(&now));
strcat(redis_value_buf, create_time);
strcat(redis_value_buf, REDIS_DILIMT);
strcat(redis_value_buf, user);
strcat(redis_value_buf, REDIS_DILIMT);
//type
get_file_suffix(filename, suffix);
strcat(redis_value_buf, suffix);
//将文件插入到FILE_INFO_LIST表中
rop_list_push(redis_conn, FILE_INFO_LIST, redis_value_buf);
//将文件插入到FILE_HOT_ZSET中,默认的权值是1
rop_zset_increment(redis_conn, FILE_HOT_ZSET, fileid);
free(redis_value_buf);
END:
rop_disconnect(redis_conn);
return retn;
}
int store_file(char *fileid, char *fdfs_file_url, char *filename, char *user)
{
//
// 存入mysql
// 存入redis中
#if 0
return store_file_to_redis(fileid, fdfs_file_url, filename, user);
#endif
return store_file_to_redis1(fileid, fdfs_file_url, filename, user);
}
int main ()
{
char user[USER_NAME_LEN] = {0};
char filename[FILE_NAME_LEN] = {0};
char fileid[TEMP_BUF_MAX_LEN] = {0};
char fdfs_file_url[FILE_URL_LEN] = {0};
while (FCGI_Accept() >= 0) {
char *contentLength = getenv("CONTENT_LENGTH");
int len;
printf("Content-type: text/html\r\n"
"\r\n");
if (get_username(user) < 0) {
break;
}
if (contentLength != NULL) {
len = strtol(contentLength, NULL, 10);
}
else {
len = 0;
}
if (len <= 0) {
printf("No data from standard input\n");
}
else {
//===============> 得到上传文件 <============
if (recv_save_file(len, filename) < 0) {
goto END;
}
//===============> 将该文件存入fastDFS中,并得到文件的file_id <============
if (upload_to_dstorage(filename, fileid) < 0) {
goto END;
}
//================> 得到文件所存放storage的host_name <=================
//http://ip:port/group1/M00/00/00/ddsadasd.jpg
if (make_file_url(fileid, fdfs_file_url) < 0) {
goto END;
}
//================> 删除本地临时存放的上传文件 <===============
unlink(filename);
//===============> 将该文件的fastDFS路径和名称和上传者 存入redis中 <======
if (store_file(fileid, fdfs_file_url, filename, user) < 0) {
goto END;
}
END:
memset(filename, 0, FILE_NAME_LEN);
memset(fileid, 0, TEMP_BUF_MAX_LEN);
memset(fdfs_file_url, 0, FILE_URL_LEN);
memset(user, 0, USER_NAME_LEN);
//printf("date: %s\r\n", getenv("QUERY_STRING"));
}
} /* while */
return 0;
}