websdk:创建压缩文件增加获取base64功能

This commit is contained in:
luoliangyi 2022-06-11 17:54:04 +08:00
parent d1f33f5cf1
commit ba76ed3c09
4 changed files with 340 additions and 105 deletions

View File

@ -726,13 +726,25 @@ namespace ver_2
return ret;
}
int ManagerV2::LocalMakeZipFile(const std::vector<std::string>& filePathList, std::string& outZipPath, std::string& errInfo)
int ManagerV2::LocalMakeZipFile(const std::vector<std::string>& filePathList, const std::vector<std::string>& nameList,
std::string& outZipPath, std::string& errInfo)
{
outZipPath.clear();
errInfo = "错误";
if (filePathList.empty())
return -1;
if (!nameList.empty())
{
if (nameList.size() != filePathList.size())
return - 1;
for (int i = 0; i < (int)nameList.size(); ++i)
{
if (nameList[i].empty())
return -1;
}
}
std::string outZipPath2 = GetFilePath("zip");
@ -748,10 +760,20 @@ namespace ver_2
{
zip_source_t* s = zip_source_file(z, StdStringToUtf8(filePathList[i]).c_str(), 0, 0);
if (NULL != s)
{
std::string zipName;
if (!nameList.empty())
{
zipName = nameList[i];
}
else
{
HGChar name[256];
HGBase_GetFileName(filePathList[i].c_str(), name, 256);
if (zip_file_add(z, StdStringToUtf8(name).c_str(), s, ZIP_FL_OVERWRITE) >= 0)
zipName = name;
}
if (zip_file_add(z, StdStringToUtf8(zipName).c_str(), s, ZIP_FL_OVERWRITE) >= 0)
{
errInfo.clear();
ret = 0;
@ -1109,6 +1131,7 @@ namespace ver_2
m_currBatchId = batchId;
if (!m_bindFolder.empty())
{
ClearBindFolderImageList();
UpdateBindFolder();
}
}
@ -1167,6 +1190,7 @@ namespace ver_2
m_currBatchId = batchId;
if (!m_bindFolder.empty())
{
ClearBindFolderImageList();
UpdateBindFolder();
}
@ -1232,6 +1256,7 @@ namespace ver_2
m_bindFolder = stdBindFolder;
m_bindNameMode = nameMode;
ClearBindFolderImageList();
UpdateBindFolder();
errInfo.clear();
@ -1523,7 +1548,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
InsertBindFolderImage(tables, insertPos, imgFormat, imgData, imgSize);
}
delete[] thumbData;
@ -1608,7 +1633,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
InsertBindFolderImage(tables, insertPos, imgFormat, imgData, imgSize);
}
delete[] thumbData;
@ -1740,7 +1765,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
DeleteBindFolderImage(tables, imageIndexList);
}
errInfo.clear();
@ -1827,7 +1852,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
ModifyBindFolderImage(tables, imageIndex, imgFormat, imgData, imgSize);
}
delete[] thumbData;
@ -1895,7 +1920,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
ModifyBindFolderImage(tables, imageIndex, imgFormat, imgData, imgSize);
}
delete[] thumbData;
@ -2030,6 +2055,13 @@ namespace ver_2
if (!m_bindFolder.empty())
{
for (int i = 0; i < (int)tables.size(); ++i)
{
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx, tables[i].format.c_str());
HGBase_DeleteFile(fileName);
}
UpdateBindFolder();
}
@ -2078,7 +2110,7 @@ namespace ver_2
if (!m_bindFolder.empty())
{
UpdateBindFolder();
ExchangeBindFolderImage(tables, imageIndex1, imageIndex2);
}
errInfo.clear();
@ -2125,6 +2157,13 @@ namespace ver_2
if (!m_bindFolder.empty())
{
for (int i = 0; i < (int)tables.size(); ++i)
{
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx, tables[i].format.c_str());
HGBase_DeleteFile(fileName);
}
UpdateBindFolder();
}
@ -2146,8 +2185,7 @@ namespace ver_2
bool ok = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i)
{
HGChar imagePath[256];
HGBase_GetTmpFileName(NULL, imagePath, 256);
std::string imagePath;
if (0 != SaveImage(imageIndexList[i], imagePath))
{
ok = false;
@ -2191,8 +2229,7 @@ namespace ver_2
bool ok = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i)
{
HGChar imagePath[256];
HGBase_GetTmpFileName(NULL, imagePath, 256);
std::string imagePath;
if (0 != SaveImage(imageIndexList[i], imagePath))
{
ok = false;
@ -2231,8 +2268,7 @@ namespace ver_2
if (NULL == m_sqlite)
return -1;
HGChar imagePath[256];
HGBase_GetTmpFileName(NULL, imagePath, 256);
std::string imagePath;
if (0 != SaveImage(imageIndex, imagePath))
{
return -1;
@ -2240,7 +2276,7 @@ namespace ver_2
int ret = SplitLocalImage(imagePath, mode, location, outImagePathList, errInfo);
HGBase_DeleteFile(imagePath);
HGBase_DeleteFile(imagePath.c_str());
return ret;
}
@ -2253,12 +2289,12 @@ namespace ver_2
return -1;
std::vector<std::string> imagePathList;
std::vector<std::string> nameList;
bool ok = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i)
{
HGChar imagePath[256];
HGBase_GetTmpFileName(NULL, imagePath, 256);
std::string imagePath;
if (0 != SaveImage(imageIndexList[i], imagePath))
{
ok = false;
@ -2266,6 +2302,12 @@ namespace ver_2
}
imagePathList.push_back(imagePath);
HGChar suffix[256];
HGBase_GetFileSuffix(imagePath.c_str(), suffix, 256);
char name[256];
sprintf(name, "%d.%s", imageIndexList[i], suffix);
nameList.push_back(name);
}
if (!ok)
@ -2278,7 +2320,7 @@ namespace ver_2
return -1;
}
int ret = LocalMakeZipFile(imagePathList, outZipPath, errInfo);
int ret = LocalMakeZipFile(imagePathList, nameList, outZipPath, errInfo);
for (int i = 0; i < (int)imagePathList.size(); ++i)
{
@ -2296,8 +2338,7 @@ namespace ver_2
if (NULL == m_sqlite)
return -1;
HGChar imagePath[256];
HGBase_GetTmpFileName(NULL, imagePath, 256);
std::string imagePath;
if (0 != SaveImage(imageIndex, imagePath))
{
return -1;
@ -2305,7 +2346,74 @@ namespace ver_2
int ret = LocalImageDeskew(imagePath, outImagePath, errInfo);
HGBase_DeleteFile(imagePath);
HGBase_DeleteFile(imagePath.c_str());
return ret;
}
int ManagerV2::LoadBase64(const std::string& fileName, std::string& base64)
{
base64.clear();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size > 0)
{
HGByte* data = new HGByte[size];
long readLen = (long)fread(data, 1, size, file);
if (readLen == size)
{
HGSize base64Size = 0;
HGBase_Base64Encode(data, size, NULL, &base64Size);
uint8_t* base64Data = new uint8_t[base64Size + 1];
HGBase_Base64Encode(data, size, base64Data, &base64Size);
base64Data[base64Size] = 0;
base64 = (const char*)base64Data;
delete[] base64Data;
ret = 0;
}
delete[] data;
}
fclose(file);
}
return ret;
}
int ManagerV2::SaveBase64(const std::string& base64, const std::string& fileName)
{
if (base64.empty())
return -1;
const char* base64Data = base64.c_str();
size_t base64Size = base64.size();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "wb");
if (NULL != file)
{
HGSize size = 0;
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &size);
uint8_t* data = new HGByte[size];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, data, &size);
size_t writeLen = fwrite(data, 1, size, file);
if (writeLen == (size_t)size)
ret = 0;
delete[] data;
fclose(file);
}
if (0 != ret)
HGBase_DeleteFile(fileName.c_str());
return ret;
}
@ -2399,73 +2507,6 @@ namespace ver_2
return (HGBASE_ERR_OK == HGBase_SetProfileInt(cfgPath, app.c_str(), key.c_str(), (HGInt)val));
}
int ManagerV2::LoadBase64(const std::string& fileName, std::string& base64)
{
base64.clear();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "rb");
if (NULL != file)
{
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
if (size > 0)
{
HGByte* data = new HGByte[size];
long readLen = (long)fread(data, 1, size, file);
if (readLen == size)
{
HGSize base64Size = 0;
HGBase_Base64Encode(data, size, NULL, &base64Size);
uint8_t* base64Data = new uint8_t[base64Size + 1];
HGBase_Base64Encode(data, size, base64Data, &base64Size);
base64Data[base64Size] = 0;
base64 = (const char*)base64Data;
delete[] base64Data;
ret = 0;
}
delete[] data;
}
fclose(file);
}
return ret;
}
int ManagerV2::SaveBase64(const std::string& base64, const std::string& fileName)
{
if (base64.empty())
return -1;
const char* base64Data = base64.c_str();
size_t base64Size = base64.size();
int ret = -1;
FILE* file = fopen(fileName.c_str(), "wb");
if (NULL != file)
{
HGSize size = 0;
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, NULL, &size);
uint8_t* data = new HGByte[size];
HGBase_Base64Decode((const HGByte*)base64Data, (HGSize)base64Size, data, &size);
size_t writeLen = fwrite(data, 1, size, file);
if (writeLen == (size_t)size)
ret = 0;
delete[] data;
fclose(file);
}
if (0 != ret)
HGBase_DeleteFile(fileName.c_str());
return ret;
}
int ManagerV2::HttpUpload(const std::string& host, int port, const std::string& path,
const std::string& filePath, const std::string& remoteFilePath)
{
@ -3056,8 +3097,10 @@ namespace ver_2
return 0;
}
int ManagerV2::SaveImage(int imageIndex, const std::string& imagePath)
int ManagerV2::SaveImage(int imageIndex, std::string& imagePath)
{
imagePath.clear();
if (NULL == m_sqlite)
return -1;
@ -3075,11 +3118,15 @@ namespace ver_2
int idx = sqlite3_column_int(stmt, 1);
if (idx == imageIndex)
{
std::string imgFmt = (const char*)sqlite3_column_text(stmt, 2);
const void* imgData = sqlite3_column_blob(stmt, 4);
int imgSize = sqlite3_column_bytes(stmt, 4);
if (SaveToFile((const HGByte*)imgData, imgSize, imagePath))
HGChar tmpPath[256];
HGBase_GetTmpFileName(imgFmt.c_str(), tmpPath, 256);
if (SaveToFile((const HGByte*)imgData, imgSize, tmpPath))
{
imagePath = tmpPath;
rc = 0;
}
@ -3381,8 +3428,6 @@ namespace ver_2
{
assert(!m_bindFolder.empty());
ClearBindFolderImageList();
if (NULL == m_sqlite)
return;
@ -3412,6 +3457,132 @@ namespace ver_2
assert(0 == ret);
}
void ManagerV2::InsertBindFolderImage(const std::vector<BatchTableInfo>& tables, int insertPos, const std::string imgFmt,
const HGByte* imgData, HGUInt imgSize)
{
assert(!m_bindFolder.empty());
for (int i = (int)tables.size() - 1; i >= 0; --i)
{
if (tables[i].idx >= insertPos)
{
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx, tables[i].format.c_str());
char destName[256];
sprintf(destName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx + 1, tables[i].format.c_str());
MoveFileA(fileName, destName);
}
}
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), insertPos, imgFmt.c_str());
SaveToFile(imgData, imgSize, fileName);
}
void ManagerV2::ModifyBindFolderImage(const std::vector<BatchTableInfo>& tables, int imageIndex, const std::string imgFmt,
const HGByte* imgData, HGUInt imgSize)
{
assert(!m_bindFolder.empty());
std::string oldFormat;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (tables[i].idx == imageIndex)
{
oldFormat = tables[i].format;
break;
}
}
char oldFileName[256];
sprintf(oldFileName, "%s%d.%s", m_bindFolder.c_str(), imageIndex, oldFormat.c_str());
HGBase_DeleteFile(oldFileName);
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), imageIndex, imgFmt.c_str());
SaveToFile(imgData, imgSize, fileName);
}
void ManagerV2::DeleteBindFolderImage(const std::vector<BatchTableInfo>& tables, const std::vector<int>& imageIndexList)
{
assert(!m_bindFolder.empty());
for (int i = 0; i < (int)imageIndexList.size(); ++i)
{
for (int j = 0; j < (int)tables.size(); ++j)
{
if (tables[j].idx == imageIndexList[i])
{
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), tables[j].idx, tables[j].format.c_str());
HGBase_DeleteFile(fileName);
break;
}
}
}
int value = 0;
for (int i = 0; i < (int)tables.size(); ++i)
{
bool find = false;
for (int j = 0; j < (int)imageIndexList.size(); ++j)
{
if (tables[i].idx == imageIndexList[j])
{
find = true;
break;
}
}
if (find)
{
++value;
}
else
{
if (value > 0)
{
char fileName[256];
sprintf(fileName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx, tables[i].format.c_str());
char destName[256];
sprintf(destName, "%s%d.%s", m_bindFolder.c_str(), tables[i].idx - value, tables[i].format.c_str());
MoveFileA(fileName, destName);
}
}
}
}
void ManagerV2::ExchangeBindFolderImage(const std::vector<BatchTableInfo>& tables, int imageIndex1, int imageIndex2)
{
assert(!m_bindFolder.empty());
std::string format1, format2;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (tables[i].idx == imageIndex1)
format1 = tables[i].format;
else if (tables[i].idx == imageIndex2)
format2 = tables[i].format;
}
char fileName1[256];
sprintf(fileName1, "%s%d.%s", m_bindFolder.c_str(), imageIndex1, format1.c_str());
char TmpFileName[256];
sprintf(TmpFileName, "%sTemp", m_bindFolder.c_str());
MoveFileA(fileName1, TmpFileName);
char fileName2[256];
sprintf(fileName2, "%s%d.%s", m_bindFolder.c_str(), imageIndex2, format2.c_str());
char fileName2Dest[256];
sprintf(fileName2Dest, "%s%d.%s", m_bindFolder.c_str(), imageIndex1, format2.c_str());
MoveFileA(fileName2, fileName2Dest);
char fileName1Dest[256];
sprintf(fileName1Dest, "%s%d.%s", m_bindFolder.c_str(), imageIndex2, format1.c_str());
MoveFileA(TmpFileName, fileName1Dest);
}
void ManagerV2::ClearBindFolderImageList()
{
assert(!m_bindFolder.empty());

View File

@ -225,7 +225,8 @@ namespace ver_2
int SplitLocalImage(const std::string& imagePath, const std::string& mode, int location,
std::vector<std::string>& outImagePathList, std::string& errInfo);
// 本地生成压缩文件
int LocalMakeZipFile(const std::vector<std::string>& filePathList, std::string& outZipPath, std::string& errInfo);
int LocalMakeZipFile(const std::vector<std::string>& filePathList, const std::vector<std::string>& nameList,
std::string& outZipPath, std::string& errInfo);
// 本地图像纠偏
int LocalImageDeskew(const std::string& imagePath, std::string& outImagePath, std::string& errInfo);
// 上传文件
@ -314,6 +315,10 @@ namespace ver_2
// 图像纠偏
int ImageDeskew(int imageIndex, std::string& outImagePath, std::string& errInfo);
public:
static int LoadBase64(const std::string& fileName, std::string& base64);
static int SaveBase64(const std::string& base64, const std::string& fileName);
private:
static std::string GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def);
static int GetCfgIntValue(const std::string& app, const std::string& key, int def);
@ -324,8 +329,6 @@ namespace ver_2
static bool SetCfgDoubleValue(const std::string& app, const std::string& key, double val);
static bool SetCfgBoolValue(const std::string& app, const std::string& key, bool val);
static int LoadBase64(const std::string& fileName, std::string& base64);
static int SaveBase64(const std::string& base64, const std::string& fileName);
static int HttpUpload(const std::string &host, int port, const std::string &path,
const std::string& filePath, const std::string& remoteFilePath);
static int FtpUpload(const std::string& user, const std::string& password, const std::string& host, int port,
@ -338,7 +341,7 @@ namespace ver_2
static void RestoreDeviceParam(const std::string& devName, const DeviceParam& devParam);
static int SetParamToDevice(SANE_Handle hdev, const DeviceParam& devParam, HGUInt mask);
static int GetParamFromDevice(SANE_Handle hdev, DeviceParam& devParam);
int SaveImage(int imageIndex, const std::string& imagePath);
int SaveImage(int imageIndex, std::string& imagePath);
static HGByte* LoadImageFromPath(const std::string& imagePath, HGUInt& size, std::string& format);
static HGByte* LoadImageFromBase64(const std::string& imageBase64, HGUInt& size, std::string& format);
@ -348,6 +351,12 @@ namespace ver_2
static bool SaveToFile(const HGByte* data, HGUInt size, const std::string &filePath);
void GetBatchTableInfo(std::vector<BatchTableInfo>& tables);
void UpdateBindFolder();
void InsertBindFolderImage(const std::vector<BatchTableInfo>& tables, int insertPos, const std::string imgFmt,
const HGByte *imgData, HGUInt imgSize);
void ModifyBindFolderImage(const std::vector<BatchTableInfo>& tables, int imageIndex, const std::string imgFmt,
const HGByte* imgData, HGUInt imgSize);
void DeleteBindFolderImage(const std::vector<BatchTableInfo>& tables, const std::vector<int>& imageIndexList);
void ExchangeBindFolderImage(const std::vector<BatchTableInfo>& tables, int imageIndex1, int imageIndex2);
void ClearBindFolderImageList();
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);

View File

@ -1654,10 +1654,18 @@ namespace ver_2
std::vector<std::string> filePathList = GetJsonStringListValue(json, "file_path_list");
for (int i = 0; i < (int)filePathList.size(); ++i)
filePathList[i] = Utf8ToStdString(filePathList[i]);
std::vector<std::string> nameList;
std::string outZipPath;
std::string errInfo;
int ret = GetManager()->LocalMakeZipFile(filePathList, outZipPath, errInfo);
int ret = GetManager()->LocalMakeZipFile(filePathList, nameList, outZipPath, errInfo);
std::string outZipBase64;
bool getBase64 = GetJsonBoolValue(json, "get_base64");
if (0 == ret && getBase64)
{
GetManager()->LoadBase64(outZipPath, outZipBase64);
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
@ -1674,8 +1682,16 @@ namespace ver_2
else
{
fmt += "\"ret\":%d, ";
if (getBase64)
{
fmt += "\"zip_path\":\"%s\", ";
fmt += "\"zip_base64\":\"%s\"}";
}
else
{
fmt += "\"zip_path\":\"%s\"}";
}
}
char resp[1024] = { 0 };
if (0 != ret)
@ -1688,10 +1704,22 @@ namespace ver_2
else
{
if (findIden)
{
if (getBase64)
sprintf(resp, fmt.c_str(), "local_make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str(),
outZipBase64.c_str());
else
sprintf(resp, fmt.c_str(), "local_make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str());
}
else
{
if (getBase64)
sprintf(resp, fmt.c_str(), "local_make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str(),
outZipBase64.c_str());
else
sprintf(resp, fmt.c_str(), "local_make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str());
}
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
@ -3856,6 +3884,13 @@ namespace ver_2
std::string errInfo;
int ret = GetManager()->MakeZipFile(imageIndexList, outZipPath, errInfo);
std::string outZipBase64;
bool getBase64 = GetJsonBoolValue(json, "get_base64");
if (0 == ret && getBase64)
{
GetManager()->LoadBase64(outZipPath, outZipBase64);
}
bool findIden = false;
std::string iden = GetJsonStringValue(json, "iden", &findIden);
@ -3871,8 +3906,16 @@ namespace ver_2
else
{
fmt += "\"ret\":%d, ";
if (getBase64)
{
fmt += "\"zip_path\":\"%s\", ";
fmt += "\"zip_base64\":\"%s\"}";
}
else
{
fmt += "\"zip_path\":\"%s\"}";
}
}
char resp[1024] = { 0 };
if (0 != ret)
@ -3885,10 +3928,22 @@ namespace ver_2
else
{
if (findIden)
{
if (getBase64)
sprintf(resp, fmt.c_str(), "make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str(),
outZipBase64.c_str());
else
sprintf(resp, fmt.c_str(), "make_zip_file", iden.c_str(), ret, StdStringToUtf8(strToJson(outZipPath)).c_str());
}
else
{
if (getBase64)
sprintf(resp, fmt.c_str(), "make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str(),
outZipBase64.c_str());
else
sprintf(resp, fmt.c_str(), "make_zip_file", ret, StdStringToUtf8(strToJson(outZipPath)).c_str());
}
}
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}