This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
VideoSite/LocalVideoUpload.cpp
2018-09-02 10:22:06 +08:00

135 lines
7.2 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}