-
Notifications
You must be signed in to change notification settings - Fork 1
/
NgxUser.cpp
663 lines (573 loc) · 21.4 KB
/
NgxUser.cpp
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <regex>
#include "NgxUser.hpp"
#include "NgxSvandex.hpp"
#include "boost/random/random_device.hpp"
#include "boost/random/uniform_int_distribution.hpp"
constexpr int SVANDEX_SESSION_EXPIRED=1800;//30分钟没有操作则失效
DbInterface TestValidation::BaseOp::BaseDao{};
DbInterface TestValidation::UploadOp::UploadDao{};
std::string TestValidation::UploadOp::sessionid{};
//返回状态和信息
void TestValidation::TObject::setStatusAndMessage(rapidjson::Document& _d,bool _b,const char* _Mess){
if(_d.HasMember("status")){
_d["status"].SetBool(_b);
}else{
_d.AddMember("status",_b,_d.GetAllocator());
}
if(_d.HasMember("message")&&_d["message"].IsArray()){
_d["message"].GetArray().PushBack(rapidjson::Value(_Mess,_d.GetAllocator()).Move(),_d.GetAllocator());
}else{
rapidjson::Value Arr(rapidjson::kArrayType);
Arr.PushBack(rapidjson::Value(_Mess,_d.GetAllocator()).Move(),_d.GetAllocator());
_d.AddMember("message",Arr,_d.GetAllocator());
}
}
TestValidation::BaseOp::BaseOp(const char *json)
{
_request.SetObject();
_response.SetObject();
if (_request.Parse(json).HasParseError())
{
throw std::logic_error("BaseOp Constrution Error!");
}
validated = true;
}
std::string TestValidation::BaseOp::GetID(){
if(!validated){
return std::string{};
}
if(_request.HasMember("id")){
return _request["id"].GetString();
}
if(_request.HasMember("sessionid")){
std::stringstream stm;
stm << "select id from userinfo where `sessionid`='" << _request["sessionid"].GetString() << "'";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if(OpStatus){
return result["sqlite"][0]["id"].GetString();
}
}
//default return
return std::string{};
}
std::string TestValidation::BaseOp::GetEMail(){
auto id=GetID();
std::stringstream stm;
stm << "select email from userinfo where `id`='" << id << "'";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if(OpStatus){
return result["sqlite"][0]["email"].GetString();
}
return std::string{};
}
bool TestValidation::BaseOp::Existence()
{
if (!validated)
{
return false;
}
if (_request.HasMember("id"))
{
std::stringstream stm;
stm << "select * from `userinfo` where `id`='" << _request["id"].GetString() << "'";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if (!OpStatus || result["sqlite"].GetArray().Size() == 0)
{
return false;
}
else
{
return true;
}
}
return false;
}
bool TestValidation::BaseOp::SessionExpired()
{
if (!validated)
{
return false;
}
//比较登录时产生的sessionid或者客户端发送的sessionid和数据库中的
if (std::difftime(std::time(nullptr), lastSessionTime) > SVANDEX_SESSION_EXPIRED)
{
_response.AddMember("expired",true,_response.GetAllocator());
return true;
}else{
_response.AddMember("expired",false,_response.GetAllocator());
return false;
}
}
TestValidation::LoginOp &TestValidation::LoginOp::Login()
{
rapidjson::Document::AllocatorType &_dallocator = _response.GetAllocator();
//是否有相应的字段
if (!validated)
{
setStatusAndMessage(_response,false,"Username or password error!");
return *this;
}
//判断用户是否存在
if (!Existence())
{
setStatusAndMessage(_response,false,"No user exists!");
return *this;
}
//判断账号密码是否正确
std::stringstream stm;
stm << "select password from userinfo where id=\'" << _request["id"].GetString() << "\'";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if(!OpStatus&&result.HasMember("errmsg")){
setStatusAndMessage(_response,false,result["errmsg"].GetString());
return *this;
}
//更新登录历史记录
if (result["sqlite"].GetArray().Size() == 1 && std::strcmp(result["sqlite"][0]["password"].GetString(), _request["password"].GetString()) == 0)
{
bool Status = UpdateSessionId("id");
bool StatusTime = UpdateLastLoginTime("id");
if (!Status&&!StatusTime)
{
setStatusAndMessage(_response,false,"sql engine execution error!");
}
else
{
setStatusAndMessage(_response,true,"login succeed!");
std::stringstream ret_sessionid_stm;
ret_sessionid_stm<<"select * from userinfo where id=\'"<<_request["id"].GetString()<<"\'";
auto result = BaseDao(SQLITE_MAIN_NAME, ret_sessionid_stm.str().c_str(), OpStatus);
_response.AddMember("sessionid",rapidjson::Value(result["sqlite"][0]["sessionid"].GetString(),_dallocator),_dallocator);
_response.AddMember("id",rapidjson::Value(result["sqlite"][0]["id"].GetString(),_dallocator),_dallocator);
}
}else{
setStatusAndMessage(_response,false,"password error!");
return *this;
}
return *this;
}
time_t TestValidation::BaseOp::GetLastSessionTime(const char *field,const char* tableName)
{
std::stringstream stm;
stm << "select * from "<<tableName <<" where " << field << " ='" << _request[field].GetString() << "'";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if (!OpStatus || result["sqlite"].GetArray().Size() == 0)
{
return 0;
}
else
{
std::istringstream lst{result["sqlite"][0]["lastupdatetime"].GetString()};
if (lst.str() == "expired")
{
return std::time(nullptr);
}
std::tm t = {};
lst >> std::get_time(&t, "%Y-%m-%d %T");
return std::mktime(&t);
}
}
bool TestValidation::BaseOp::UpdateSessionId(const char* field){
auto uuid = Svandex::tools::GetUUID();
std::stringstream updatestream;
updatestream << "update `userinfo` set `sessionid`=\'" << uuid.c_str() << "\' where " << field << "= \'" << _request[field].GetString() << "\'";
BaseDao(SQLITE_MAIN_NAME, updatestream.str().c_str(), OpStatus);
return OpStatus;
}
bool TestValidation::BaseOp::UpdateLastLoginTime(const char* field){
std::stringstream updatestream;
updatestream << "update userinfo set `lastupdatetime`=\'" << Svandex::tools::GetCurrentTimeFT().c_str() << "\' where " << field << "= \'" << _request[field].GetString() << "\'";
BaseDao(SQLITE_MAIN_NAME, updatestream.str().c_str(), OpStatus);
return OpStatus;
}
TestValidation::RegisterOp &TestValidation::RegisterOp::Register()
{
rapidjson::Document::AllocatorType &_dallocator = _response.GetAllocator();
if (Existence())
{
setStatusAndMessage(_response,false,"User Has Already Exist");
}
else
{
//用户不存在
std::stringstream stm;
stm << "insert into `userinfo` values(\'"
//<< _request["role"].GetString() << "\',\'"
<< _request["id"].GetString() << "\',\'"
<< _request["password"].GetString() << "\',\'"
//<< _request["email"].GetString() << "\',"
<< "expired\',\'"
<< Svandex::tools::GetCurrentTimeFT().c_str()
<< "\',\'"
<< _request["email"].GetString()
/*
<<"\',\'"
<< _request["group"].GetString()
*/
<< "\')";
auto result = BaseDao(SQLITE_MAIN_NAME, stm.str().c_str(), OpStatus);
if (!OpStatus)
{
if(result.HasMember("errmsg")){
setStatusAndMessage(_response,false,result["errmsg"].GetString());
}
setStatusAndMessage(_response,false,"sql engine execution error!");
return *this;
}
if (result["sqlite"].GetArray().Size() == 0&&OpStatus)
{
setStatusAndMessage(_response,true,"register succeed.");
_response.AddMember("sessionid","nosessionid",_dallocator);
}
else
{
setStatusAndMessage(_response,true,"register failed.");
}
}
return *this;
}
TestValidation::DataOp &TestValidation::DataOp::Data()
{
rapidjson::Document::AllocatorType &_dallocator = _response.GetAllocator();
if (!validated)
{
setStatusAndMessage(_response,false,"Json format or SELECT case sensitivity error!");
return *this;
}
if (_request["sessionid"].GetString()!=std::string("nosessionid")?SessionExpired():false)
{
setStatusAndMessage(_response,false,"Session expired!");
return *this;
}
auto result = BaseDao(_request["database"].GetString(), _request["statement"].GetString(), OpStatus);
if (!OpStatus&&result.HasMember("errmsg"))
{
setStatusAndMessage(_response,false,result["errmsg"].GetString());
}
else
{
_response.AddMember("data", rapidjson::Value(result["sqlite"], _dallocator), _dallocator);
setStatusAndMessage(_response,true,"Data acqusition succeed!");
}
return *this;
}
TestValidation::MailFileOp& TestValidation::MailFileOp::Construct(){
//格式是否正确
if (!validated)
{
setSAMFormatError();
return *this;
}
//是否登录
if (SessionExpired())
{
setSAMSessionError();
return *this;
}
auto _id=GetID();
auto _mail=GetEMail();
/*
//检查对应的工号文件夹是否存在
if(access((upload_root_dir+"/"+_id).c_str(),F_OK)!=0){
mkdir((upload_root_dir+"/"+_id).c_str(),S_IRWXU|S_IRWXG);
}
*/
//谁登录该系统发送的邮件都会被抄送
if(_mail!="[email protected]"){
mb.getCopyRecipients().appendAddress(vmime::make_shared<vmime::mailbox>(_mail.c_str()));
}
//添加收件人
auto& recv_array=_request["recv_users"];
std::string recvArray;
for(auto& it:recv_array.GetArray()){
mb.getRecipients().appendAddress(vmime::make_shared<vmime::mailbox>(it.GetString()));
recver.appendMailbox(vmime::make_shared<vmime::mailbox>(it.GetString()));
recver.appendMailbox(vmime::make_shared<vmime::mailbox>(_mail.c_str()));
}
//1.先读取正文部分
//根据路径读取正文内容
if(_request["email_content"].HasMember("path")){
//从文件中读取邮件内容
std::ifstream* ifs=new std::ifstream(upload_root_dir+"/"+_request["email_content"]["path"].GetString(),std::ios::in);
if(!ifs->is_open()){
setSAMFileError();
return *this;
}else{
vmime::shared_ptr<vmime::utility::inputStream> email_body_stream=vmime::make_shared<vmime::utility::inputStreamPointerAdapter>(ifs);
vmime::shared_ptr<vmime::contentHandler> email_body=vmime::make_shared<vmime::streamContentHandler>(email_body_stream,0);
mb.getTextPart()->setCharset(vmime::charsets::UTF_8);
mb.getTextPart()->setText(email_body);
}
}
//根据字符串读取正文内容
if(_request["email_content"].HasMember("body")){
if(!_request["email_content"]["body"].IsString()){
setSAMFormatError();
}
//发来的字符串作为正文部分
mb.getTextPart()->setCharset(vmime::charsets::UTF_8);
vmime::shared_ptr<vmime::stringContentHandler> email_body=vmime::make_shared<vmime::stringContentHandler>(_request["email_content"]["body"].GetString());
mb.getTextPart()->setText(email_body);
}
//2.读取附件
if(_request.HasMember("attachments")){
//检查附件路径是否都正确
auto& attachments=_request["attachments"];
size_t attachcount=0;
//附件数量
attachcount=attachments.GetArray().Size();
std::string filepath;
for(auto& it:attachments.GetArray()){
filepath=upload_root_dir+"/"+it["md5"].GetString();
if(access(filepath.c_str(),F_OK)!=0){
//邮件路径不存在
setStatusAndMessage(_response,false,"One of email body or attachment doesn't exist!");
return *this;
}
}
if(attachcount>0){
//有附件
for(size_t index=0;index<attachcount;index++){
//附件元信息,MD5,媒介类型,文件名
auto filepath=upload_root_dir+"/"+std::string(attachments[index]["md5"].GetString());
const char* mtype=attachments[index]["mtype"].GetString();
const char* mname=attachments[index]["mname"].GetString();
vmime::shared_ptr<vmime::fileAttachment> att=vmime::make_shared<vmime::fileAttachment>(
filepath.c_str(),
vmime::mediaType(mtype),
vmime::text(mname)
);
att->getFileInfo().setFilename(mname);
mb.appendAttachment(att);
}
}
}
//succeed
setStatusAndMessage(_response,true,"Mail Construction Succeed");
return *this;
}
TestValidation::VerificatonCodeOp& TestValidation::VerificatonCodeOp::Construct(){
if (!validated)
{
setSAMFormatError();
return *this;
}
// 设置邮件正文内容,验证码写入数据库
std::string chars("abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890");
boost::random::random_device rng;
boost::random::uniform_int_distribution<> index_dist(0,chars.size()-1);
std::stringstream verificationCode;
for(int i=0;i<6;++i){
verificationCode<<chars[index_dist(rng)];
}
//设置收件人
auto _mail=BaseDao(SQLITE_SECURITY_QUIZ_NAME,(std::string("select `公司邮箱` from `用户信息` where `工号` ='")+_request["id"].GetString()+"'").c_str(),OpStatus);
mb.getRecipients().appendAddress(vmime::make_shared<vmime::mailbox>(_mail["sqlite"][0]["公司邮箱"].GetString()));
//设置正文
mb.getTextPart()->setCharset(vmime::charsets::UTF_8);
std::string email_body_string="您本次的验证码为 "+verificationCode.str()+" ,将于五分钟后失效。";
vmime::shared_ptr<vmime::contentHandler> email_body=vmime::make_shared<vmime::stringContentHandler>(email_body_string);
mb.getTextPart()->setText(email_body);
std::stringstream stm;
//检查数据库中是否已有记录
stm<<"select * from verificationcode where id='"<<_request["id"].GetString()<<"'";
auto isExisted=BaseDao(SQLITE_MAIN_NAME,stm.str().c_str(),OpStatus);
if(isExisted["sqlite"].GetArray().Size()>0){
//更新数据库中记录
stm.str("");
stm<<"update verificationcode set vcode='"
<<verificationCode.str().c_str()<<"',lastupdatetime='"
<<Svandex::tools::GetCurrentTimeFT().c_str()<<"' where id='"
<<_request["id"].GetString()<<"'";
BaseDao(SQLITE_MAIN_NAME,stm.str().c_str(),OpStatus);
}else{
//将验证码写入数据库
stm.str("");
stm<<"insert into verificationcode values('"
<<_request["id"].GetString()<<"','"
<<verificationCode.str().c_str()<<"','"
<<Svandex::tools::GetCurrentTimeFT().c_str()<<"')";
BaseDao(SQLITE_MAIN_NAME,stm.str().c_str(),OpStatus);
}
if(OpStatus){
recver.appendMailbox(vmime::make_shared<vmime::mailbox>(_mail["sqlite"][0]["公司邮箱"].GetString()));
}
setStatusAndMessage(_response,true,"Send Verification Code Succeed!");
return *this;
}
void TestValidation::MailOp::SendMail() try{
//发送邮件
vmime::utility::url url("smtp://outlook.saicmotor.com");
vmime::shared_ptr<vmime::net::session> sess=vmime::net::session::create();
vmime::shared_ptr<vmime::net::transport> tr=sess->getTransport(url);
tr->connect();
tr->send(mb.construct(),sender,recver);
tr->disconnect();
setStatusAndMessage(_response,true,"Email send succeed!");
}catch(vmime::exception&e){
_response.AddMember("vmime_exception",rapidjson::Value(e.what(),_dallocator), _dallocator);
}catch(std::exception&e){
_response.AddMember("vmime_std_exception",rapidjson::Value(e.what(),_dallocator), _dallocator);
}
TestValidation::VerificatonCodeOp& TestValidation::VerificatonCodeOp::Verify(){
if(!_request.HasMember("vcode")||!_request.HasMember("password")){
setStatusAndMessage(_response,false,"Code error!!");
return *this;
}
//判断验证码是否过期
lastSessionTime=GetLastSessionTime("id","verificationcode");
constexpr int VCODE_EXPIRE_TIME=5*60;
if(std::difftime(std::time(nullptr), lastSessionTime) >VCODE_EXPIRE_TIME){
setSAMSessionError();
return *this;
}
std::stringstream stm;
//判断验证码是否正确
stm<<"select vcode from verificationcode where id='"<<_request["id"].GetString()<<"'";
auto vcode=BaseDao(SQLITE_MAIN_NAME,stm.str().c_str(),OpStatus);
if(!OpStatus||std::strcmp(vcode["sqlite"][0]["vcode"].GetString(),_request["vcode"].GetString())!=0){
setStatusAndMessage(_response,false,"Code error!!");
return *this;
}
//重新写人密码
stm.str("");
stm<<"update userinfo set password = '"<<_request["password"].GetString()
<<"' where id='"<<_request["id"].GetString()<<"'";
BaseDao(SQLITE_MAIN_NAME,stm.str().c_str(),OpStatus);
if(OpStatus){
setStatusAndMessage(_response,true,"Password change successfully.");
}
return *this;
}
//解析multipart-form数据
TestValidation::UploadOp::UploadOp(const char* multiform){
doc.SetObject();
std::string mf=multiform;
std::regex re("name=\\\"(.*)\\\"\\r\\n\\r\\n(.*)\\r\\n");
std::smatch m;
//循环查找
auto pos=mf.cbegin();
auto end=mf.cend();
size_t numOfKey=0;
rapidjson::Document _tmp;
_tmp.SetObject();
//将一个文件对应的属性组成一个JSON对象,然后整体组成一个数组
for(;std::regex_search(pos,end,m,re);pos=m.suffix().first){
numOfKey+=1;
_tmp.AddMember(rapidjson::Value(m.str(1).c_str(),_tmp.GetAllocator()),rapidjson::Value(m.str(2).c_str(),_tmp.GetAllocator()),_tmp.GetAllocator());
}
if(sessionid.empty()){
validated=false;
setStatusAndMessage(doc,false,"No sessionid key found!");
return;
}else{
//根据sessionid来找到id
std::stringstream stm;
stm.str("");
stm<<"{\"sessionid\":\""<<sessionid<<"\"}";
BaseOp bp(stm.str().c_str());
time_t lasttime=bp.GetLastSessionTime("sessionid");
if (std::difftime(std::time(nullptr), lasttime) > SVANDEX_SESSION_EXPIRED)
{
validated=false;
setStatusAndMessage(doc,false,"Session Expired!");
doc.AddMember("expired",true,doc.GetAllocator());
return;
}
doc.AddMember("expired",false,doc.GetAllocator());
//sessionid是否过期
stm.str("");
stm<<"select id from userinfo where sessionid = '"<<sessionid<<"'";
auto result=UploadDao(SQLITE_MAIN_NAME,stm.str().c_str(),UploadStatus);
if(UploadStatus&&result["sqlite"].GetArray().Size()>0){
doc.AddMember("id",rapidjson::Value(result["sqlite"][0]["id"].GetString(),doc.GetAllocator()),doc.GetAllocator());
}else{
validated=false;
setStatusAndMessage(doc,false,"No User found!");
return;
}
}
doc.AddMember("upload.number",(int)(numOfKey/5),doc.GetAllocator());
if(doc["upload.number"].GetInt64()>10){
doc.AddMember("alert","Up to only 10 files will be uploaded.",doc.GetAllocator());
}
rapidjson::Value arr(rapidjson::kArrayType);
for(size_t index=1;index<=(size_t)doc["upload.number"].GetInt64();index++){
//临时Object存储对象
rapidjson::Value oneObj(rapidjson::kObjectType);
std::string keyStr="file"+std::to_string(index)+".";
if(_tmp.HasMember((keyStr+"name").c_str()) &&
_tmp.HasMember((keyStr+"content_type").c_str()) &&
_tmp.HasMember((keyStr+"path").c_str()) &&
_tmp.HasMember((keyStr+"md5").c_str()) &&
_tmp.HasMember((keyStr+"size").c_str())
){
oneObj.AddMember("name",rapidjson::Value(_tmp[(keyStr+"name").c_str()].GetString(),doc.GetAllocator()),doc.GetAllocator());
oneObj.AddMember("content_type",rapidjson::Value(_tmp[(keyStr+"content_type").c_str()].GetString(),doc.GetAllocator()),doc.GetAllocator());
oneObj.AddMember("path",rapidjson::Value(_tmp[(keyStr+"path").c_str()].GetString(),doc.GetAllocator()),doc.GetAllocator());
oneObj.AddMember("md5",rapidjson::Value(_tmp[(keyStr+"md5").c_str()].GetString(),doc.GetAllocator()),doc.GetAllocator());
oneObj.AddMember("size",rapidjson::Value(_tmp[(keyStr+"size").c_str()].GetString(),doc.GetAllocator()),doc.GetAllocator());
//加入到数组中
arr.PushBack(oneObj.Move(),doc.GetAllocator());
}else{
validated=false;
setStatusAndMessage(doc,false,"form-data key should start with file!Such as file1, file2");
return;
}
}
doc.AddMember("metadata",arr.Move(),doc.GetAllocator());
//获取字符串大小
rapidjson::StringBuffer bf;
rapidjson::Writer<rapidjson::StringBuffer> w(bf);
doc.Accept(w);
//最少的字符串也是2,比如空JSON对象就是"{}"
if(std::strlen(bf.GetString())>2){
validated=true;
}else{
validated=false;
}
}
TestValidation::UploadOp& TestValidation::UploadOp::ToDataBase(){
if(!validated){
setStatusAndMessage(doc,false,"Client data format error.");
return *this;
}
if(!UploadDao){
setStatusAndMessage(doc,false,"Database Interface error.");
return *this;
}
//写入到upload_files_metadata的表中
for(auto& ele:doc["metadata"].GetArray()){
std::stringstream stm;
//检查md5是否已经存在
stm<<"select * from upload_files_metadata where md5='"<<
ele["md5"].GetString()<<"'";
auto result=UploadDao(SQLITE_MAIN_NAME,stm.str().c_str(),UploadStatus);
if(result["sqlite"].GetArray().Size()>0){
setStatusAndMessage(doc,true,(std::string("md5 of file ")+ele["name"].GetString()+" already exist in database.").c_str());
}else{
stm.str("");
stm<<"insert into upload_files_metadata values('"
<<ele["name"].GetString()<<"','"
<<ele["content_type"].GetString()<<"','"
<<ele["path"].GetString()<<"','"
<<ele["md5"].GetString()<<"',"
<<std::stoi(ele["size"].GetString())<<",'"
<<doc["id"].GetString()<<"')";
UploadDao(SQLITE_MAIN_NAME,stm.str().c_str(),UploadStatus);
if(!UploadStatus){
setStatusAndMessage(doc,false,"Database execution error.");
return *this;
}else{
setStatusAndMessage(doc,true,"Upload Succeed.");
}
}
}
return *this;
}