1
0

Upload manager changes

This commit is contained in:
Scott E. Graves
2017-02-20 00:53:46 -06:00
parent 2f0677e060
commit 6ee4d92e29
2 changed files with 66 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "UploadManager.h"
#include "SiaDriveConfig.h"
#include "SiaApi.h"
using namespace Sia::Api;
@@ -9,6 +10,7 @@ CUploadManager::CUploadManager(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriv
CAutoThread(siaCurl, siaDriveConfig),
_uploadDatabase(siaDriveConfig->GetRenter_UploadDbFilePath(), SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE)
{
//CreateTableIfNotFound(L"upload_table", UPLOAD_TABLE_CREATE);
StartAutoThread();
}
@@ -19,7 +21,68 @@ CUploadManager::~CUploadManager()
void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig)
{
// TODO Lock here - if file is modified again before a prior upload is complete, delete it and start again later
bool processNext = true;
{
try
{
SQLite::Statement query(_uploadDatabase, "select sia_path, status from upload_table where status=@status1 or status=@status2");
query.bind("@status1", static_cast<unsigned>(UploadStatus::Uploading));
query.bind("@status2", static_cast<unsigned>(UploadStatus::Modified));
CSiaFileTreePtr fileTree(new CSiaFileTree(siaCurl, siaDriveConfig));
json result;
if (ApiSuccess(siaCurl.Get(L"/renter/files", result)))
{
fileTree->BuildTree(result);
while (processNext && query.executeStep())
{
std::string tempSiaPath = query.getColumn(0);
String siaPath = CA2W(tempSiaPath.c_str()).m_psz;
UploadStatus uploadStatus = static_cast<UploadStatus>(query.getColumn(1).getUInt());
auto fileList = fileTree->GetFileList();
auto it = std::find_if(fileList.begin(), fileList.end(), [&](const CSiaFilePtr& ptr)
{
return ptr->GetSiaPath() == siaPath;
});
if (it == fileList.end())
{
// error condition - should always exist. delete from db and log warning, but continue processing
}
else if (uploadStatus == UploadStatus::Modified)
{
// delete existing, change status to uploading and upload to sia
processNext = false;
}
else if ((*it)->GetUploadProgress() >= 100)
{
// upload complete - change status
}
else
{
processNext = false;
}
}
}
else
{
// error condition - host down?
processNext = false;
}
}
catch (const SQLite::Exception& e)
{
// error condition - database not initialized (i.e. no table)?
std::string msg = e.what();
}
}
if (processNext)
{
}
}

View File

@@ -10,10 +10,11 @@ class AFX_EXT_CLASS CUploadManager :
public CAutoThread
{
public:
enum class _UploadStatus
enum class _UploadStatus : unsigned
{
NotFound,
Queued,
Modified,
Uploading,
Complete,
Error