GrpcPrint/PrintS/job/JobFileProcessor.cpp

355 lines
7.9 KiB
C++
Raw Normal View History

2024-03-26 10:33:00 +08:00
#include "JobFileProcessor.h"
#include "../utils/StringHelper.h"
#include <direct.h>
#include <io.h>
JobFileProcessor::JobFileProcessor()
{
m_MachineCfg = ConfigManager::GetInstance()->GetMachineCfg();
}
JobFileProcessor::~JobFileProcessor()
{
}
int JobFileProcessor::Process(string jobfile)
{
string extpath;
int ret = 0;
if((ret=UnZipJobFile(jobfile, extpath))!=0)return ret;
if(!m_job_content.LoadFile(extpath))return BP_ERR_LOAD_CONTENT_FILE_FAILED;
if (m_job_content.m_container_content->container_files->metadata_file->encryption_strategy_ref=="none") {
if (!m_MachineCfg->m_SupportNonEncMagic) {
return BP_ERR_NOT_SUPPORT_DATA;
}
}
vector<BPBinary::BFileInfo> bfileinfos;
string binPath=m_job_content.GetBinaryFileName(bfileinfos);
if (bfileinfos.empty())return BP_ERR_NO_BINARY_FILE;
string metaFileName=m_job_content.GetMetadataFileName();
if (metaFileName=="")return BP_ERR_GET_METAFILE_NAME_FAILED;
m_job_meta_data.SetJobContent(&m_job_content);
if(!m_job_meta_data.LoadFile(extpath,metaFileName, bfileinfos, binPath))return BP_ERR_LOAD_METAFILE_FAILED;
m_job_file_name=jobfile.substr(jobfile.find_last_of("\\")+1, jobfile.length()-1);
return BP_SUCCESS;
}
int JobFileProcessor::UnZipJobFile(string strfile,string& extpath)
{
unzFile uf = NULL;
zlib_filefunc64_def ffunc;
fill_win32_filefunc64W(&ffunc);
uf = unzOpen2_64(StringHelper::Str2Wstr(strfile).c_str() , &ffunc);
if (uf == NULL)return BP_ERR_OPEN_FILE_FAILED;
string path=strfile.substr(0,strfile.find_last_of("."));
CreateDirectoryW(StringHelper::Str2Wstr(path).c_str(),NULL);
_wchdir(StringHelper::Str2Wstr(path).c_str());
int ret_value = Extract(uf, 0, 0, NULL);
unzClose(uf);
extpath = path;
if (ret_value != 0)return BP_ERR_UNZIP_FAILED;
return BP_SUCCESS;
}
int JobFileProcessor::ExtractOneFile(unzFile uf, const char* filename, int opt_extract_without_path, int opt_overwrite, const char* password)
{
//int err = UNZ_OK;
if (unzLocateFile(uf, filename, CASESENSITIVITY) != UNZ_OK)
{
printf("file %s not found in the zipfile\n", filename);
return 2;
}
if (ExtractCurrentFile(uf, &opt_extract_without_path,
&opt_overwrite,
password) == UNZ_OK)
return 0;
else
return 1;
}
int JobFileProcessor::Extract(unzFile uf, int opt_extract_without_path, int opt_overwrite, const char* password)
{
uLong i;
unz_global_info64 gi;
int err;
//FILE* fout = NULL;
err = unzGetGlobalInfo64(uf, &gi);
if (err != UNZ_OK)
printf("error %d with zipfile in unzGetGlobalInfo \n", err);
for (i = 0; i < gi.number_entry; i++)
{
if (ExtractCurrentFile(uf, &opt_extract_without_path,
&opt_overwrite,
password) != UNZ_OK)
break;
if ((i + 1) < gi.number_entry)
{
err = unzGoToNextFile(uf);
if (err != UNZ_OK)
{
printf("error %d with zipfile in unzGoToNextFile\n", err);
break;
}
}
}
return 0;
}
int JobFileProcessor::ExtractCurrentFile(unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password)
{
char filename_inzip[256];
char* filename_withoutpath;
char* p;
int err = UNZ_OK;
FILE *fout = NULL;
void* buf;
uInt size_buf;
unz_file_info64 file_info;
//uLong ratio = 0;
err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
if (err != UNZ_OK)
{
printf("error %d with zipfile in unzGetCurrentFileInfo\n", err);
return err;
}
size_buf = WRITEBUFFERSIZE;
buf = (void*)malloc(size_buf);
if (buf == NULL)
{
printf("Error allocating memory\n");
return UNZ_INTERNALERROR;
}
p = filename_withoutpath = filename_inzip;
while ((*p) != '\0')
{
if (((*p) == '/') || ((*p) == '\\'))
filename_withoutpath = p + 1;
p++;
}
if ((*filename_withoutpath) == '\0')
{
if ((*popt_extract_without_path) == 0)
{
printf("creating directory: %s\n", filename_inzip);
_mkdir(filename_inzip);
}
}
else
{
const char* write_filename;
int skip = 0;
if ((*popt_extract_without_path) == 0)
write_filename = filename_inzip;
else
write_filename = filename_withoutpath;
err = unzOpenCurrentFilePassword(uf, password);
if (err != UNZ_OK)
{
printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err);
}
if (((*popt_overwrite) == 0) && (err == UNZ_OK))
{
//char rep = 0;
FILE* ftestexist;
int rel=fopen_s(&ftestexist,write_filename, "rb");
if (rel== 0)
{
fclose(ftestexist);
skip = 1;
}
}
if ((skip == 0) && (err == UNZ_OK))
{
int rel = fopen_s(&fout,write_filename, "wb");
/* some zipfile don't contain directory alone before file */
if ((rel != 0) && ((*popt_extract_without_path) == 0) &&
(filename_withoutpath != (char*)filename_inzip))
{
char c = *(filename_withoutpath - 1);
*(filename_withoutpath - 1) = '\0';
Makedir(write_filename);
*(filename_withoutpath - 1) = c;
rel = fopen_s(&fout,write_filename, "wb");
}
if (rel != NULL)
{
printf("error opening %s\n", write_filename);
}
}
if (fout != NULL)
{
printf(" extracting: %s\n", write_filename);
do
{
err = unzReadCurrentFile(uf, buf, size_buf);
if (err < 0)
{
printf("error %d with zipfile in unzReadCurrentFile\n", err);
break;
}
if (err > 0)
if (fwrite(buf, err, 1, fout) != 1)
{
printf("error in writing extracted file\n");
err = UNZ_ERRNO;
break;
}
} while (err > 0);
if (fout)
fclose(fout);
if (err == 0)
ChangeFileDate(write_filename, file_info.dosDate);
}
if (err == UNZ_OK)
{
err = unzCloseCurrentFile(uf);
if (err != UNZ_OK)
{
printf("error %d with zipfile in unzCloseCurrentFile\n", err);
}
}
else
unzCloseCurrentFile(uf); /* don't lose the error */
}
free(buf);
return err;
}
int JobFileProcessor::Makedir(const char* newdir)
{
char *buffer;
char *p;
int len = (int)strlen(newdir);
if (len <= 0)
return 0;
buffer = (char*)malloc(len + 1);
if (buffer == NULL)
{
printf("Error allocating memory\n");
return UNZ_INTERNALERROR;
}
strcpy_s(buffer, len + 1, newdir);
if (buffer[len - 1] == '/') {
buffer[len - 1] = '\0';
}
if (_mkdir(buffer) == 0)
{
free(buffer);
return 1;
}
p = buffer + 1;
while (1)
{
char hold;
while (*p && *p != '\\' && *p != '/')
p++;
hold = *p;
*p = 0;
if ((_mkdir(buffer) == -1) && (errno == ENOENT))
{
printf("couldn't create directory %s\n", buffer);
free(buffer);
return 0;
}
if (hold == 0)
break;
*p++ = hold;
}
free(buffer);
return 1;
}
void JobFileProcessor::ChangeFileDate(const char* filename, uLong dosdate)
{
HANDLE hFile;
FILETIME ftm, ftLocal, ftCreate, ftLastAcc, ftLastWrite;
hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL);
GetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite);
DosDateTimeToFileTime((WORD)(dosdate >> 16), (WORD)dosdate, &ftLocal);
LocalFileTimeToFileTime(&ftLocal, &ftm);
SetFileTime(hFile, &ftm, &ftLastAcc, &ftm);
CloseHandle(hFile);
}
string JobFileProcessor::GetJobTitle()
{
return m_job_meta_data.GetJobTitle();
}
string JobFileProcessor::GetMaterialName()
{
return m_job_meta_data.GetMaterial();
}
double JobFileProcessor::GetLayerThickness()
{
return *m_job_meta_data.GetLayerThickness();
}
unsigned int JobFileProcessor::GetLayerCount()
{
return m_job_meta_data.GetLayerCount();
}
int JobFileProcessor::GetComponentCount()
{
return m_job_meta_data.GetComponentCount();
}
JobMetaData::Layer* JobFileProcessor::GetLayer(unsigned int index)
{
if (index >= m_job_meta_data.GetLayerCount())return NULL;
else return m_job_meta_data.GetLayer(index);
}
int JobFileProcessor::GetLayerIndex(JobMetaData::Layer* layer)
{
int index = -1;
for (size_t i = 0; i < m_job_meta_data.GetLayerCount(); ++i)
{
if (m_job_meta_data.GetLayer(i) == layer) {
index = i;
break;
}
}
return index;
}
string JobFileProcessor::GetJobUid() {
return m_job_meta_data.GetJobUid();
}
void JobFileProcessor::SetStartIndex(unsigned int index)
{
FileProcessor::SetStartIndex(index);
m_job_meta_data.CalcRemainTime(index);
}