code_app/sdk/upload/main.cpp

112 lines
3.0 KiB
C++
Raw Blame History

#include "base/HGDef.h"
#include "base/HGInc.h"
#include "base/HGUtility.h"
#include <string>
#include "cgic.h"
int cgiMain()
{
char mimiType[32] = "text/html; charset=utf-8";
char fileName[32] = "filename";
char remoteFileName[32] = "remote_filename";
cgiHeaderContentType(mimiType);
char* scriptPath = getenv("PATH_INFO");
fprintf(cgiOut, "scriptPath: %s<br>\n", scriptPath);
char* scriptFullPath = getenv("PATH_TRANSLATED");
fprintf(cgiOut, "scriptFullPath: %s<br>\n", scriptFullPath);
if (NULL == scriptPath || NULL == scriptFullPath)
{
fprintf(cgiOut, "<p> get PATH_INFO or PATH_TRANSLATED failed. </p>\n");
return cgiFormNotFound;
}
char stdScriptPath[256];
HGBase_StandardiseFileName(scriptPath, stdScriptPath, 256);
char* p = strstr(scriptFullPath, stdScriptPath);
if (NULL == p)
{
fprintf(cgiOut, "<p> PATH_INFO or PATH_TRANSLATED error. </p>\n");
return cgiFormNotFound;
}
std::string rootPath(scriptFullPath, p - scriptFullPath);
fprintf(cgiOut, "rootPath: ");
cgiHtmlEscape(rootPath.c_str());
fprintf(cgiOut, "<br>\n");
char name[512];
if (cgiFormFileName(fileName, name, sizeof(name)) != cgiFormSuccess)
{
fprintf(cgiOut, "<p> no filename. </p>\n");
return cgiFormNoFileName;
}
fprintf(cgiOut, "filename: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "<br>\n");
int size = 0;
cgiFormFileSize(fileName, &size);
if (size <= 0)
{
fprintf(cgiOut, "<p> bad file size. </p>\n");
return cgiFormEmpty;
}
fprintf(cgiOut, "file size: %d bytes<br>\n", size);
char remoteFilePath[512];
if (cgiFormString(remoteFileName, remoteFilePath, sizeof(remoteFilePath)) != cgiFormSuccess)
{
fprintf(cgiOut, "<p> no remote filename. </p>\n");
return cgiFormNoFileName;
}
fprintf(cgiOut, "remote_filepath: ");
cgiHtmlEscape(remoteFilePath);
fprintf(cgiOut, "<br>\n");
cgiFilePtr file = NULL;
if (cgiFormFileOpen(fileName, &file) != cgiFormSuccess)
{
fprintf(cgiOut, "<p> Could not open the file. </p>\n");
return cgiFormOpenFailed;
}
std::string savePath = rootPath + remoteFilePath;
char stdSavePath[256];
HGBase_StandardiseFileName(savePath.c_str(), stdSavePath, 256);
char stdSaveDir[256];
HGBase_GetFilePath(stdSavePath, stdSaveDir, 256);
HGBase_CreateDir(stdSaveDir);
fprintf(cgiOut, "savePath: ");
cgiHtmlEscape(stdSavePath);
fprintf(cgiOut, "<br>\n");
FILE *fd = fopen(stdSavePath, "wb+");
if (fd == NULL)
{
fprintf(cgiOut, "<p> create file failed. </p>\n");
cgiFormFileClose(file);
return cgiFormOpenFailed;
}
char tmp[1024];
int got = 0;
while (cgiFormFileRead(file, tmp, 1024, &got) == cgiFormSuccess)
{
fwrite(tmp, got, sizeof(char), fd); //<2F>Ѷ<EFBFBD><D1B6><EFBFBD><EFBFBD><EFBFBD>contentд<74><D0B4><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
}
fclose(fd);
cgiFormFileClose(file);
//<2F><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
fprintf(cgiOut, "<p> upload file success. </p>\n");
return cgiFormSuccess;
}