📄 ps_usp_upload.cpp
字号:
// PS_usp_upload.cpp: implementation of the PS_usp_upload class.
//
//////////////////////////////////////////////////////////////////////
#include "PS_usp_upload.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sha_1.h"
#include "PS_client_dbfsms.h"
#include "PS_client_zip.h"
#include "base64.h"
#include "PS_usp_log.h"
#include "PS_usp_rspfile.h"
#include "PS_usp_common.h"
#define DEAL_BUF_LEN 1024 //每次处理HTTP信息的大小
#define BLOCK_LEN 524288 //每次处理的文件块的大小
#define SIGN_CODE_LEN 100 //标记符的大小
#define PROCESS_LEN 100 //进度的刻度
#define FILE_NAME_LEN 100 //文件名的大小
#define STRING_LEN 128 //字符串的长度
#define FILE_SAVE_DIR "/home/kevin/upload/" //上传文件的存储目录
#define UPLOAD_ID_PARM "UPLOAD_ID" //页面上传代号
#define IP_ADDRESS_LEN 16 //IP地址长度
#define HASH_LEN 41 //HASH值长度的大小
#define LOCAL_IP_ADDRESS "127.0.0.1"
/***************************************************************************
全局变量
****************************************************************************/
CPSDBFSMS bdb;//写入BDB数据库
char * bdbNormalIni = "/opt/fsp/dbs/db.ini";
char * bdbTmpIni = "/opt/fsp/dbs/dbtmp.ini";
char * bdbEnvPath = "/opt/fsp/dbs";
char * bdbEnvPath_seed = "/opt/fsp/seeddb";
//定义Log,种子文件
CUspLog log;
CUspLog *logP;
CUspRspfile rspfile;
//下面定义进度的变量
int currentread = 0 ; //当前读的字节数
int processScale[PROCESS_LEN] = {0};//初始化刻度
int currentScale = 0;//当前刻度
float processNum[PROCESS_LEN] = {0.0};//初始化本次传输的进度值
int HAS_SHOW = -1;//把已经显示的标记出来
//用于切片上传用
char *blockWriteP;//块指针
const char *blockStartP;//块的头指针
char fileblock[BLOCK_LEN];//文件块
int currentBlocksize = 0;//当前文件块遍历的大小,递增的
int filesize = 0;
enum
{
STATE_START,
STATE_GET_SIGN_CODE,
STATE_GET_FILE_NAME,
STATE_GET_FILE_START,
STATE_GET_FILE_CONTENT,
STATE_CHECK_END,
STATE_END
};
int blockIndex =0;//块序号
//FILE *fp; /* 文件指针,保存我们要获得的文件 */
int getState = STATE_START;
int contentLength;/*标准输入内容长度*/
int nowReadLen;//当前还需要读取的内容长度
int signCodeLen;
int tmpLen;
char *nowReadP;//当前读指针
char *nowWriteP;//当前写指针
char dealBuf[DEAL_BUF_LEN];//循环使用的处理字符数组
char signCode[SIGN_CODE_LEN]; /*存储本次的特征码*/
char tmpSignCode[SIGN_CODE_LEN];
char fileName[FILE_NAME_LEN];
char uploadid[12];//上传文件的唯一ID号
/***************************************************************************
//打印进度
****************************************************************************/
static void printProcess(int _currentread)
{
if(_currentread>=processNum[currentScale])
{
if(processScale[currentScale]==HAS_SHOW)
{
return;
}
char l_tmp[STRING_LEN] = {0};
//sprintf(l_tmp,"process = %d%%",processScale[currentScale]);
if(processScale[currentScale]==0)
{
sprintf(l_tmp,"<");
}else if(processScale[currentScale]==100)
{
sprintf(l_tmp,">\r\n");
}else{
sprintf(l_tmp,"=");
}
log.writeLog(l_tmp);
//fwrite(l_tmp,sizeof(char),strlen(l_tmp),fp_logfile);
processScale[currentScale] = HAS_SHOW;
currentScale++;
}
}
static void printProcess(int i_currentread,int span)
{
i_currentread += span;
printProcess(i_currentread);
}
//取Hash值
static void sha160(char *sha, unsigned char *src, unsigned long len)
{
unsigned char hl[20];
sha1(hl, src, len);
for (int i = 0 ; i < 20 ; i++)
{
char tmp[3];
itoa(hl[i], tmp, 16);
if (strlen(tmp) == 1)
{
tmp[1] = tmp[0];
tmp[0] = '0';
tmp[2] = '\0';
}
strncat(sha, tmp , 3);
}
}
//打印16进制值
void printASC( char* src )
{
int len = strlen( src );
if( 0 >= len )
return;
char *p = (char*)malloc( len * 3 );
memset( p , 0x0 , len * 3 );
char l_tmp[3];
int i = 0;
for( i = 0; i < len ;i++ )
{
memset(l_tmp,0x0,3);
sprintf(l_tmp,"\\%X",*src);
strcat( p , l_tmp );
src++;
}
//printf("%s\n" , p );
//log.writeLog("len = ",len);
log.writeLog(p);
free( p );
return;
}
/**
int tmpfileseq = 0;
static void writeTmpFile(char * tmpFileContent,int filesize){
char l_tmp[STRING_LEN] = {0};
sprintf(l_tmp,"%stmp%d",FILE_SAVE_DIR,tmpfileseq);
tmpfileseq++;
FILE *tmpfile;
tmpfile = fopen(l_tmp,"a+b");
fwrite(tmpFileContent,sizeof(char),filesize,tmpfile);
if(tmpfile!=NULL)
{
fclose(tmpfile);
}
}
*/
//进行块操作
static void operateBlock(char ** _blockWriteP,char * _nowReadP){
**_blockWriteP = *_nowReadP;
//char l_tmp[64] = {0};
//sprintf( l_tmp , "write:%x\tread:%x\tcurrentBlocksize:%d\n" , *_blockWriteP, _nowReadP , currentBlocksize);
//log.writeLog(l_tmp);
currentBlocksize++;
if(currentBlocksize==BLOCK_LEN)
{
log.debugLog("=========================operateBlock ing=======================\r\n");
//char * tmpP = (char *)blockStartP;
//printASC((char *)tmpP);
char * hashString= new char[HASH_LEN];//在本程序释放
memset(hashString,0,HASH_LEN);
char * ipString = new char[IP_ADDRESS_LEN];//在本程序释放
memset(ipString,0,IP_ADDRESS_LEN);
strncpy(ipString ,LOCAL_IP_ADDRESS,IP_ADDRESS_LEN);
//取SHA值
sha160(hashString,(unsigned char *)blockStartP,BLOCK_LEN);
//处理Hash值与种子文件
BlockData * blockData = new BlockData;//在genrateRspFile释放
blockData->sha = new char[HASH_LEN];//在genrateRspFile释放
blockData->peer = new char[IP_ADDRESS_LEN];//在genrateRspFile释放
memset(blockData->sha,0,HASH_LEN);
memset(blockData->peer,0,IP_ADDRESS_LEN);
strncpy(blockData->sha,hashString,HASH_LEN);
blockData->size = BLOCK_LEN;
blockData->id = blockIndex;
strncpy(blockData->peer,ipString,IP_ADDRESS_LEN);
rspfile.addBlockData(blockData);
//写入数据库
bdb.PS_dbfsms_write(hashString,(char *)blockStartP,BLOCK_LEN);
log.writeLog("block Hash = ",hashString);
log.writeLog("currentBlocksize 0 = ",currentBlocksize);
memset(fileblock,0,BLOCK_LEN);
*_blockWriteP = (char *)blockStartP;
currentBlocksize = 0;
blockIndex++;
delete []hashString;
delete []ipString;
}
else
{
//log.writeLog( "add _blockWriteP\n");
(*_blockWriteP)++;
}
}
//将字串写到块中去
static void operateBlock(char ** _blockWriteP,char * str,int len){
char *tmpstrP;
tmpstrP = str;
//**_blockWriteP = *tmpstrP;
while(len>0)
{
operateBlock(_blockWriteP,tmpstrP);
len--;
tmpstrP++;
}
}
//操作最后一个块
static void operateLastBlock()
{
log.debugLog("=========================operateLastBlock.start=======================\r\n");
//取Hash值
char * hashString= new char[HASH_LEN];//在本程序释放
memset(hashString,0,HASH_LEN);
sha160(hashString,(unsigned char *)blockStartP,currentBlocksize);
char * ipString = new char[IP_ADDRESS_LEN];//在本程序释放
memset(ipString,0,IP_ADDRESS_LEN);
strncpy(ipString,LOCAL_IP_ADDRESS,IP_ADDRESS_LEN);
//处理Hash值与种子文件
BlockData * blockData = new BlockData;//在genrateRspFile释放
blockData->sha = new char[HASH_LEN];//在genrateRspFile释放
blockData->peer = new char[IP_ADDRESS_LEN];//在genrateRspFile释放
memset(blockData->sha,0,HASH_LEN);
memset(blockData->peer,0,IP_ADDRESS_LEN);
strncpy(blockData->sha,hashString,HASH_LEN);
blockData->size = currentBlocksize;
blockData->id = blockIndex;
strncpy(blockData->peer , ipString,IP_ADDRESS_LEN);
log.debugLog("=========================addBlockData.start=======================\r\n");
rspfile.addBlockData(blockData);
//写入数据库
log.debugLog("=========================PS_dbfsms_write.start=======================\r\n");
bdb.PS_dbfsms_write(hashString,(char *)blockStartP,strlen(blockStartP));
log.writeLog("last block Hash = ",hashString);
log.writeLog("last block size = ",currentBlocksize);
memset(fileblock,0,BLOCK_LEN);
//生成种子文件
FileData * filedata = new FileData;//在genrateRspFile释放
filedata->block = 524288;
char * base64code = new char[strlen(fileName)*2 ];//在本程序释放
memset(base64code,0,strlen(fileName)*2 );
int testlen = strlen(fileName)*2;
log.writeLog("debug00000..............",testlen);
//取文件的Base64的值
Base64Encode(base64code, fileName, strlen(fileName));
int base64codelen =strlen(base64code);
filedata->name = new char[base64codelen+1];//在genrateRspFile释放
filedata->sfid = new char[20];//在genrateRspFile释放
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -