135 lines
7.2 KiB
C++
135 lines
7.2 KiB
C++
#include "Request.h"
|
||
#include "Response.h"
|
||
#include <fstream>
|
||
#include <iostream>
|
||
#include <ctime>
|
||
using namespace std;
|
||
|
||
int main()
|
||
{
|
||
HTTPWrapper::Request req;
|
||
HTTPWrapper::Response res;
|
||
ofstream ofs("log.txt");
|
||
ofs << req.contentType << endl;
|
||
ofs << "formdata as following" << endl;
|
||
|
||
int cnt_success = 0;
|
||
int cnt_trans = 0;
|
||
int cnt_failed = 0;
|
||
int cnt_noname = 0;
|
||
int cnt_write_failed = 0;
|
||
|
||
res.writer << "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\"><title>Upload Result</title></head><body>";
|
||
res.writer << "<h1>Upload Result</h1>";
|
||
|
||
if (!req.formdata.empty())
|
||
{
|
||
ofs << "FormData size: " << req.formdata.size() << endl;
|
||
|
||
res.writer << "<div><p>FormData size: " << req.formdata.size() << "</p>";
|
||
|
||
for (int i = 0; i < req.formdata.size(); i++)
|
||
{
|
||
ofs << "Name: " << req.formdata[i].name << endl;
|
||
ofs << "Filename: " << req.formdata[i].filename << endl;
|
||
ofs << "ContentType: " << req.formdata[i].contentType << endl;
|
||
|
||
string outputname = req.formdata[i].filename;
|
||
bool ok = true;
|
||
if (!outputname.empty())
|
||
{
|
||
if (outputname.substr(outputname.size() - 4) == ".mp4")
|
||
{
|
||
// Ends with .mp4
|
||
ofs << outputname << " ends with mp4. No need to change." << endl;
|
||
cnt_success++;
|
||
|
||
res.writer << "<p>MP4 File: " << outputname << "</p>";
|
||
}
|
||
else if (outputname.substr(outputname.size() - 5) == ".vdat")
|
||
{
|
||
// Ends with .vdat
|
||
ofs << outputname << " ends with vdat. Change it." << endl;
|
||
outputname = outputname.substr(0, outputname.size() - 5).append(".mp4");
|
||
cnt_trans++;
|
||
|
||
res.writer << "<p>Vdat File: " << outputname << "</p>";
|
||
}
|
||
else
|
||
{
|
||
ofs << outputname << " does not fit." << endl;
|
||
cnt_failed++;
|
||
ok = false;
|
||
|
||
res.writer << "<p><font color='blue'>Invalid: " << outputname << "</font></p>";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ofs << "No filename. Use default name." << endl;
|
||
outputname = "Upload_" + to_string(time(NULL)) + ".mp4";
|
||
cnt_noname++;
|
||
|
||
res.writer << "<p>No name: " << outputname << "</p>";
|
||
}
|
||
|
||
if (ok)
|
||
{
|
||
string diskname = "F:\\faaq\\OutSideVideo\\" + outputname;
|
||
FILE* fp = fopen(diskname.c_str(), "rb");
|
||
if (fp)
|
||
{
|
||
fclose(fp);
|
||
ofs << "File already exists: " << diskname << endl;
|
||
cnt_write_failed++;
|
||
|
||
res.writer << "<p><font color='red'>File already exists: " << diskname << "</font></p>";
|
||
}
|
||
else
|
||
{
|
||
fp = fopen(diskname.c_str(), "wb");
|
||
if (fp)
|
||
{
|
||
if (fwrite(req.formdata[i].content.data(), req.formdata[i].content.size(), 1, fp) == 0)
|
||
{
|
||
ofs << "Error while writting data to opened file: " << diskname << endl;
|
||
cnt_write_failed++;
|
||
|
||
res.writer << "<p><font color='red'>Write Failed: " << diskname << "</font></p>";
|
||
}
|
||
else
|
||
{
|
||
res.writer << "<p>Write Done: " << diskname << "</p>";
|
||
}
|
||
|
||
fclose(fp);
|
||
}
|
||
else
|
||
{
|
||
ofs << "Failed to open: " << diskname << endl;
|
||
cnt_write_failed++;
|
||
|
||
res.writer << "<p><font color='red'>Open Failed: " << diskname << "</font></p>";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
res.writer << "</div>";
|
||
}
|
||
ofs << "formdata end." << endl;
|
||
|
||
res.writer << "<div><p>Upload finished.</p>"
|
||
<< "<p>success: " << cnt_success << "</p>"
|
||
<< "<p>Trans: " << cnt_trans << "</p>"
|
||
<< "<p>Failed: " << cnt_failed << "</p>"
|
||
<< "<p>Noname: " << cnt_noname << "</p>"
|
||
<< "<p>Written Failed: " << cnt_write_failed << "</p>"
|
||
<< "</div>";
|
||
|
||
res.writer << "</body></html>";
|
||
|
||
return 0;
|
||
}
|
||
|