JD2022-TU1/main/extern/Camcam/TestApp/upload_task.cpp

267 lines
No EOL
8.7 KiB
C++

#include "Scene.h"
#ifdef ONLINE_MODE
//#pragma optimize("",off)
#include "upload_task.h"
#include "sha1.h"
#include <string>
#undef CAMCAM_BRANCH_DEV
#define CAMCAM_BRANCH_LIVE
// TODO: Scramble URL?
/* // Commented out, now the urls are given by create-article.php
#ifdef CAMCAM_BRANCH_DEV
//const char UbitubeVideoCreateURL[]= "http://mtp-dgrigore/UbitubeCamCamWebServices/VideoCreate.aspx";
//const char UbitubeVideoUploadURL[]= "http://mtp-dgrigore/UbitubeCamCamWebServices/VideoUpload.aspx";
const char UbitubeVideoCreateURL[]= "http://twk-ocado/util/ubitube_test.php?testVideoCreate";
const char UbitubeVideoUploadURL[]= "http://twk-ocado/util/ubitube_test.php?testVideoUpload";
//const char UbitubeVideoCreateURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoCreate.aspx";
//const char UbitubeVideoUploadURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoUpload.aspx";
#elif defined CAMCAM_BRANCH_TEST
const char UbitubeVideoCreateURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoCreate.aspx";
const char UbitubeVideoUploadURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoUpload.aspx";
#elif defined CAMCAM_BRANCH_LIVE
const char UbitubeVideoCreateURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoCreate.aspx";
const char UbitubeVideoUploadURL[]= "http://camcam.ubi.com/UbitubeCamCamWebService/VideoUpload.aspx";
#else
#error "CamCam branch not defined"
#endif
*/
// ***************************************************************************
unsigned int CUbitubeUploadTask::getNewVideoId()
{
// Get a new video Id from Ubitube
SimpleString<128> args;
args.sprintf("cipherToken=%s", _Params->CipherToken);
if (!_pHttpClient->sendRequest(_Params->VideoCreateUrl.c_str(), args.c_str(), true) ||
(_pHttpClient->getRequestCode() >= 400) ||
(strcmp(_pHttpClient->getResponsePage().c_str(), "-1") == 0) ||
(strcmp(_pHttpClient->getResponsePage().c_str(), "0") == 0))
{
// HTTP problem?
_ErrorString.sprintf("Could not get video id, error code %u: ", _pHttpClient->getRequestCode());
if (_pHttpClient->getRequestCode() >= 200) // including correct response (-1)
_ErrorString.appendStr(_pHttpClient->getResponsePage().c_str(), ~0);
_Aborted = true;
return ~0;
}
//FILE *f = fopen("logoutput.html", "w");
//fwrite(_pHttpClient->getResponsePage().c_str(), 1, _pHttpClient->getResponsePage().size(), f);
//CC_fclose(f);
// TODO: check if is number
return atoi(_pHttpClient->getResponsePage().c_str());
}
// ***************************************************************************
bool CUbitubeUploadTask::init(const char *proxy)
{
if (_pHttpClient)
release();
CBackgroundTask::init();
// Initialize new Curl handle
_pHttpClient = new CHttpClient();
if (!_pHttpClient->init())
{
_ErrorString.sprintf("Could not init upload task");
_Aborted = true;
return false;
}
_pHttpClient->setProxy(proxy); // use the proxy that works for account
_Completed = false;
_Aborted = false;
freeParams();
return true;
}
// ***************************************************************************
void CUbitubeUploadTask::release()
{
if (_pHttpClient)
{
_pHttpClient->release();
delete _pHttpClient;
_pHttpClient = NULL;
}
}
// ***************************************************************************
CUbitubeUploadTask::~CUbitubeUploadTask()
{
freeParams();
release();
}
// Helper
std::string getFilePath(const std::string& pathFilename)
{
std::string::size_type p = pathFilename.rfind("\\");
if (p == std::string::npos)
{
p = pathFilename.rfind("/");
if (p == std::string::npos)
return std::string();
}
return pathFilename.substr(0, p+1);
}
// ***************************************************************************
void CUbitubeUploadTask::run()
{
char FilenameToUpload[256];
_ErrorString.clear();
// Get hash codes from files
SimpleString<41> fileHash, thumbnailHash; // initialized to empty for ReportHash()
CSHA1 sha1;
// Conversion en FLV si necessaire ...
strcpy(FilenameToUpload, _Params->TempFilename);
unsigned int GetFileNameExtend(char *TRACKqs);
if (!_Aborted)
{
int AVI2FLV(char *avi, char *flv, bool *Aborted=NULL);
if (GetFileNameExtend(_Params->Filename) == '.AVI'
|| GetFileNameExtend(_Params->Filename) == '.WMV')
{
// Convert File to FLV
FilenameToUpload[strlen(FilenameToUpload)-4] = 0;
strcat(FilenameToUpload, ".FLV");
AVI2FLV(_Params->Filename, FilenameToUpload, &_Aborted);
}
}
if (!_Aborted)
{
sha1.HashFile(FilenameToUpload);
sha1.Final();
sha1.ReportHash(fileHash.Buf, CSHA1::REPORT_HEX_NOBLANKS);
sha1.Reset();
sha1.HashFile(_Params->TbFilename);
sha1.Final();
sha1.ReportHash(thumbnailHash.Buf, CSHA1::REPORT_HEX_NOBLANKS);
}
// Duration
SimpleString<10> durationStr;
durationStr.sprintf("%u", _Params->DurationInSec);
std::string videoIdStr = std::string(_pHttpClient->getResponsePage().c_str());
//TEMP
char path[MAX_PATH];
GetTempPath(MAX_PATH-20, path);
std::string origFilename = FilenameToUpload;
std::string srcPath = getFilePath(origFilename), tmpPath(path); // TODO
std::string newFilename = srcPath + videoIdStr+".flv", newTbFilename = tmpPath + videoIdStr+".png"; //".jpg"; // Ubitube only accepts PNG, web browser manages to display it as JPEG anyway!
if (!_Aborted)
{
if (MoveFile(origFilename.c_str(), newFilename.c_str()) == 0)
{
_ErrorString.sprintf("Could not move %s to %s (error %u)", origFilename.c_str(), newFilename.c_str(), GetLastError());
_Aborted = true;
}
if (CopyFile(_Params->TbFilename, newTbFilename.c_str(), false) == 0)
{
SimpleString<1024> error2;
error2.sprintf(" - Could not copy %s to %s (error %u)", _Params->TbFilename, newTbFilename.c_str(), GetLastError());
_ErrorString.appendStrCat(error2.c_str(), 1024);
_Aborted = true;
}
} else
{
DeleteFile(FilenameToUpload);
}
if (!_Aborted)
{
// Upload!
_pHttpClient->addMultipartParam("cipherToken", _Params->CipherToken);
_pHttpClient->addMultipartParam("VideoFile", CHttpClient::MultipartParam(newFilename.c_str(), CHttpClient::ParamLoadFile));
_pHttpClient->addMultipartParam("VideoFileHashCode", fileHash.c_str());
_pHttpClient->addMultipartParam("ThumbnailFile", CHttpClient::MultipartParam(newTbFilename.c_str(), CHttpClient::ParamLoadFile));
_pHttpClient->addMultipartParam("ThumbnailFileHashCode", thumbnailHash.c_str());
_pHttpClient->addMultipartParam("Duration", durationStr.c_str());
//_pHttpClient->setCredentials("UBISOFT-ORG\\TEST_PORTAL_USER_11", "Guinness11"); //TEMP
// From this point, _Params must not be used anymore because freeParams() might be called if the request is aborted
if (!_pHttpClient->sendMultipartRequest(_Params->VideoUploadUrl.c_str()) || (_pHttpClient->getRequestCode() >= 400))
{
if (_pHttpClient->getRequestCode() == CHttpClient::ErrorInvalidFile)
{
_ErrorString.sprintf("Could not upload files, a file could not be read");
}
else
{
// HTTP problem?
_ErrorString.sprintf("Could not upload files, error code %u", _pHttpClient->getRequestCode());
if (_pHttpClient->getRequestCode() >= 400)
_ErrorString.appendStr(_pHttpClient->getResponsePage().c_str(), ~0);
}
}
if (((_pHttpClient->getResponsePage().size() != 1) || (_pHttpClient->getResponsePage()[0] != '1')
) && !_Aborted)
_ErrorString.sprintf("Could not upload files, server did not accept them: %s", _pHttpClient->getResponsePage());
}
// Cleanup
// Delete FLV File, no rename MoveFile(newFilename.c_str(), origFilename.c_str());
if (!_Aborted)
{
DeleteFile(newFilename.c_str());
DeleteFile(newTbFilename.c_str());
_Completed = true;
}
_IsRunning = false;
// Don't release, params can be accessed to know the last sent file
}
// ***************************************************************************
double CUbitubeUploadTask::uploadProgress() const
{
if (!_pHttpClient || !_pHttpClient->initialized())
return 0.;
return _pHttpClient->ulNow() / _pHttpClient->ulTotal();
}
// ***************************************************************************
void CUbitubeUploadTask::abortUploading()
{
_Aborted = true;
if (!_pHttpClient || !_pHttpClient->initialized())
return;
if (isRunning())
_pHttpClient->abortRequest();
}
// ***************************************************************************
#ifndef USER_MODE
unsigned int CUbitubeUploadTask::getMemoryUsage() const
{
unsigned int size = 0;
size += sizeof(*this);
if(_pHttpClient)
size += _pHttpClient->getMemoryUsage();
// Params
if(_Params)
size += strlen(_Params->CipherToken) * sizeof(char)
+ sizeof(_Params->DurationInSec)
+ strlen(_Params->Filename) * sizeof(char)
+ strlen(_Params->TbFilename) * sizeof(char)
+ sizeof(std::string::value_type) * (_Params->VideoCreateUrl.capacity() + _Params->VideoUploadUrl.capacity());
return size;
}
#endif
#endif // ONLINE_MODE