//上传文件 int UploadFile(std::string UserFilePath , std::string UserFileSize , std::string ServerFilePath , std::string MD5 , int flag) { std::string sql = "select MD5 from UserFileInfo where UserFilePath="+UserFilePath+");"; MySqlQuery(sql); row = mysql_fetch_row(res); std::string str = row[0]; sql = "select * from MD5 where MD5="+str+";"; MySqlQuery(sql); int i = 1; while((row = mysql_fetch_row(res))) { int temp = atoi(row[0]); if( i%3 == 0 && temp >= 1) { std::ostringstream string; string << temp+1; std::string ss(string.str()); sql = "update MD5 set ReferCount="+ss+"where MD5="+str+";"; mysql_query(&mysql , sql.c_str()); }else { std::ostringstream string; string << Uid; std::string uid = string.str(); string << flag; std::string f = string.str(); std::string sql = "insert into UserFileInfo(Uid , UserFilePath , UserFileSize , ServerFilePath , MD5 , Flag) values("+uid+","+UserFileSize+","+UserFileSize+","+ServerFilePath+","+MD5+","+f+");"; if( 0 != mysql_query(&mysql , sql.c_str())) { return -1; } return 1; } } return 0; }
//删除目录或文件 int DeleteFileORDir(std::string File) { std::string sql = "select MD5 from UserFileInfo where UserFilePath="+File+");"; MySqlQuery(sql); row = mysql_fetch_row(res); std::string str = row[0]; sql = "select * from MD5 where MD5="+str+";"; MySqlQuery(sql); int i = 1; std::string temp; while((row = mysql_fetch_row(res))) { if( i%3 == 0 && strcmp(row[0],"1") == 0) { temp = row[0]; sql = "delete from UserFileInfo where UserFilePath="+File+";"; mysql_query(&mysql , sql.c_str()); } i++; } int num = atoi(temp.c_str()); num--; std::ostringstream string; string << num; std::string ss(string.str()); sql = "update MD5 set ReferCount="+ss+"where MD5="+str+";"; if( 0 != mysql_query(&mysql , sql.c_str())) { return -1; }else { return 1; } }
//上传文件 int UploadFile(std::string MD5,std::string UserFilePath,int uid,std::string ServIp,std::string FileSize,int have) { char sqlbuf[1024]; if( have == 1) { sprintf(sqlbuf,"insert into UserFileInfo(Uid,UserFilePath,UserFileSize,ServerIp,MD5,Flag) values('%d','%s','%s','%s','%s','%d')",uid,UserFilePath.c_str(),FileSize.c_str(),ServIp.c_str(),MD5.c_str(),1); mysql_real_query(&mysql,sqlbuf,strlen(sqlbuf)); memset(sqlbuf,'\0',sizeof(sqlbuf)); sprintf(sqlbuf,"insert into MD5Info(MD5) values('%s')",MD5.c_str()); mysql_real_query(&mysql,sqlbuf,strlen(sqlbuf)); return 2; } LOG(INFO)<<"uid="<<uid<<std::endl; //查询Md5是否存在 std::string sql = "select MD5 from MD5Info where MD5 = \""+MD5+"\""; MySqlQuery(sql); row = mysql_fetch_row(res); if(row == NULL) { mysql_free_result(res); return 0; //文件不存在 } mysql_free_result(res); sql = "update MD5Info set Count = Count+1 where MD5 = \"" + MD5 + "\""; if(mysql_real_query(&mysql,sql.c_str(),sql.length()) != 0) { std::cout<<"Md5 引用计数增加失败\n"; return -1; } sql = "select ServerIp,UserFileSize from UserFileInfo where MD5 = \"" + MD5 + "\""; MySqlQuery(sql); row = mysql_fetch_row(res); std::string spath = row[0]; std::string UserFileSize = row[1]; std::cout<<"spath:"<<spath<<" "<<"UserFileSize:"<<UserFileSize<<std::endl; mysql_free_result(res); //添加一条数据 sprintf(sqlbuf,"insert into UserFileInfo(Uid,UserFilePath,UserFileSize,ServerIp,MD5,Flag) values('%d','%s','%s','%s','%s','%d')",uid,UserFilePath.c_str(),UserFileSize.c_str(),spath.c_str(),MD5.c_str(),1); if(mysql_real_query(&mysql,sqlbuf,strlen(sqlbuf)) != 0) { std::cout<<"文件信息添加失败\n"; sql = "update MD5Info set Count = Count-1 where MD5 = \"" + MD5 + "\""; mysql_real_query(&mysql,sql.c_str(),sql.length()); return -1; } std::cout<<"文件信息添加成功\n"; return 1; }
/*注册用户 先判断有没有此用户*/ int Register(std::string UserName , std::string PassWord , std::string Email) { std::string sql = "select * from UserInfo where Acount=" + UserName+";"; MySqlQuery(sql); bool flag = false; int i = 0 ; while ( (row = mysql_fetch_row(res))) { if( row[0] == UserName ) { std::cout << "该用户已存在" << std::endl; flag = true; return 0; } } if( flag == false ) { sql = "insert into UserInfo(Account,Password,Email) values(" + UserName +","+PassWord+","+Email+");"; if( 0 == mysql_query(&mysql , sql.c_str())) { //[未完成]数据成功的log return 1; }else { //[未完成]数据失败的log mysql_close(&mysql); return -1; } } return 0; }
//下载文件 MYSQL_RES* DownloadFile(std::string UserFilePath,int id) { char sqlbuf[1024]; sprintf(sqlbuf,"select * from UserFileInfo where Uid = '%d' AND UserFilePath = '%s'",id,UserFilePath.c_str()); std::string sql = sqlbuf; MySqlQuery(sql); return res; }
//下载文件 std::string DownloadFile(std::string UserFilePath) { std::string sql = "select ServerFilePath from UserFileInfo where UserFilePath="+UserFilePath+";"; MySqlQuery(sql); std::string ServerPath; while((row = mysql_fetch_row(res))) { ServerPath = row[0]; } return ServerPath; }
/*验证账户和密码 返回-1代表登录失败 1为登录成功*/ int AccountPasswd(std::string UserName , std::string PassWord) { std::string sql = "select * from UserInfo where Acount=" + UserName+";"; MySqlQuery(sql); while(row == mysql_fetch_row(res)) { //判断UserName和Account是否相等 if(UserName != row[1] || PassWord != row[2]) { return -1; } else { Uid = atoi(row[0]); return 1; } } return -1; }
//进入目录 在数据库中查找符合条件的目录 并且将该目录下的一级文件或者目录 std::string DirFiles(std::string dir) { Json::Value root; std::string sql = "select * from UserInfo where UserFilePath like " + dir + "$;"; MySqlQuery(sql); int i = 0; int num = dir.length(); std::string temp; while((row = mysql_fetch_row(res))) { std::string str = row[0]; int loc = str.find('/' , num); if(loc) { temp = str.substr(num , loc - num + 1); }else { temp = str.substr(num - 1 , str.rfind("\0")); } std::string numstr = std::to_string(i); root[numstr] = temp; } return root.toStyledString(); }
//进入目录 在数据库中查找符合条件的目录 并且将该目录下的一级文件或者目录 std::string DirFiles(int uid,std::string dir) { char sqlbuf[1024]; int len=0; sprintf(sqlbuf,"select distinct UserFilePath from UserFileInfo where Uid = %d AND UserFilePath Like '%s%%'",uid,dir.c_str()); std::string sql = sqlbuf; MySqlQuery(sql); std::string str,files=""; std::set<std::string> stringSet; int pos; LOG(INFO) <<"folder:\n"; while((row = mysql_fetch_row(res))!=NULL) { str = row[0]; LOG(INFO) <<str<<std::endl; str = str.erase(0,dir.length()); pos = str.find_first_of('/',0); if(pos >= 0) { str = str.substr(0,pos+1); } stringSet.insert(str); } mysql_free_result(res); if(stringSet.empty()) { return files; } std::set<std::string>::iterator t = stringSet.begin(); files = *t; t++; for(;t!=stringSet.end();t++) { //std::cout<<"s:"<<*t<<std::endl; files = files + " " + *t; } //std::cout << "files_len " << files.length() << std::endl; LOG(INFO) <<"files:"<<files<<std::endl; return files; }