Upload manager changes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "UploadManager.h"
|
#include "UploadManager.h"
|
||||||
#include "SiaDriveConfig.h"
|
#include "SiaDriveConfig.h"
|
||||||
|
#include "SiaApi.h"
|
||||||
|
|
||||||
using namespace Sia::Api;
|
using namespace Sia::Api;
|
||||||
|
|
||||||
@@ -9,6 +10,7 @@ CUploadManager::CUploadManager(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriv
|
|||||||
CAutoThread(siaCurl, siaDriveConfig),
|
CAutoThread(siaCurl, siaDriveConfig),
|
||||||
_uploadDatabase(siaDriveConfig->GetRenter_UploadDbFilePath(), SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE)
|
_uploadDatabase(siaDriveConfig->GetRenter_UploadDbFilePath(), SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE)
|
||||||
{
|
{
|
||||||
|
//CreateTableIfNotFound(L"upload_table", UPLOAD_TABLE_CREATE);
|
||||||
StartAutoThread();
|
StartAutoThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,7 +21,68 @@ CUploadManager::~CUploadManager()
|
|||||||
|
|
||||||
void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig)
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -10,10 +10,11 @@ class AFX_EXT_CLASS CUploadManager :
|
|||||||
public CAutoThread
|
public CAutoThread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum class _UploadStatus
|
enum class _UploadStatus : unsigned
|
||||||
{
|
{
|
||||||
NotFound,
|
NotFound,
|
||||||
Queued,
|
Queued,
|
||||||
|
Modified,
|
||||||
Uploading,
|
Uploading,
|
||||||
Complete,
|
Complete,
|
||||||
Error
|
Error
|
||||||
|
Reference in New Issue
Block a user