1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Api/UploadManager.cpp
2017-02-20 00:53:46 -06:00

112 lines
2.7 KiB
C++

#include "stdafx.h"
#include "UploadManager.h"
#include "SiaDriveConfig.h"
#include "SiaApi.h"
using namespace Sia::Api;
CUploadManager::CUploadManager(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) :
CAutoThread(siaCurl, siaDriveConfig),
_uploadDatabase(siaDriveConfig->GetRenter_UploadDbFilePath(), SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE)
{
//CreateTableIfNotFound(L"upload_table", UPLOAD_TABLE_CREATE);
StartAutoThread();
}
CUploadManager::~CUploadManager()
{
StopAutoThread();
}
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)
{
}
}
UploadStatus CUploadManager::GetUploadStatus(const String& siaPath) const
{
return UploadStatus::NotFound;
}
void CUploadManager::AddOrUpdate(const String& siaPath, const String& filePath)
{
}
void CUploadManager::PurgeCompleteStatus()
{
}
void CUploadManager::PurgeErrorStatus()
{
}
void CUploadManager::Remove(const String& siaPath)
{
}