|
|
|
@@ -14,7 +14,8 @@ using namespace Sia::Api;
|
|
|
|
|
#define QUERY_UPLOADS_BY_SIA_PATH "select id, sia_path, status from upload_table where sia_path=@sia_path order by id desc limit 1;"
|
|
|
|
|
#define QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS "select id, sia_path, file_path, sd_file_path, status from upload_table where sia_path=@sia_path and (status=@status1 or status=@status2) order by id desc limit 1;"
|
|
|
|
|
#define UPDATE_STATUS "update upload_table set status=@status where sia_path=@sia_path;"
|
|
|
|
|
#define INSERT_UPLOAD "insert into upload_table (sia_path, status, file_path, sd_file_path) values (@sia_path, @status, @file_path, @sd_file_path)"
|
|
|
|
|
#define INSERT_UPLOAD "insert into upload_table (sia_path, status, file_path, sd_file_path) values (@sia_path, @status, @file_path, @sd_file_path);"
|
|
|
|
|
#define DELETE_UPLOAD "delete from upload_table where sia_path=@sia_path;"
|
|
|
|
|
|
|
|
|
|
#define SET_STATUS(status, success_event, fail_event)\
|
|
|
|
|
bool statusUpdated = false;\
|
|
|
|
@@ -35,7 +36,7 @@ try\
|
|
|
|
|
}\
|
|
|
|
|
catch (SQLite::Exception e)\
|
|
|
|
|
{\
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(fail_event(siaPath, filePath, status, CA2W(e.getErrorStr()).m_psz)));\
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e)));\
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename... Ts>
|
|
|
|
@@ -128,7 +129,7 @@ void CUploadManager::FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CUploadManager::FileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove)
|
|
|
|
|
void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> l(_fileActionMutex);
|
|
|
|
@@ -144,30 +145,51 @@ void CUploadManager::FileAction(const String& siaPath, const String& filePath, c
|
|
|
|
|
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
|
|
|
|
|
if (query.executeStep())
|
|
|
|
|
{
|
|
|
|
|
String filePath = CA2W(query.getColumn(3)).m_psz;
|
|
|
|
|
String removeFilePath = CA2W(query.getColumn(3)).m_psz;
|
|
|
|
|
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(5)));
|
|
|
|
|
if (uploadStatus == UploadStatus::Remove)
|
|
|
|
|
{
|
|
|
|
|
if (::PathFileExists(filePath.c_str()))
|
|
|
|
|
bool deleteFromDb = false;
|
|
|
|
|
if (::PathFileExists(removeFilePath.c_str()))
|
|
|
|
|
{
|
|
|
|
|
if (RetryDeleteFileIfExists(filePath.c_str()))
|
|
|
|
|
if (RetryDeleteFileIfExists(removeFilePath.c_str()))
|
|
|
|
|
{
|
|
|
|
|
if (!RetryDeleteFileIfExists(siaDriveFilePath))
|
|
|
|
|
{
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath)));
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, removeFilePath, siaDriveFilePath)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO Delete from database
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoved(siaPath, filePath)));
|
|
|
|
|
|
|
|
|
|
deleteFromDb = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// TODO Error condition
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RemoveFileFailed(siaPath, removeFilePath)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
if (deleteFromDb)
|
|
|
|
|
{
|
|
|
|
|
// TODO Delete from database
|
|
|
|
|
json response;
|
|
|
|
|
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response);
|
|
|
|
|
if (ApiSuccess(cerror))
|
|
|
|
|
{
|
|
|
|
|
// TODO validate response
|
|
|
|
|
|
|
|
|
|
SQLite::Statement del(_uploadDatabase, DELETE_UPLOAD);
|
|
|
|
|
del.bind("@sia_path", CW2A(siaPath.c_str()));
|
|
|
|
|
if (del.exec() == 1)
|
|
|
|
|
{
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoved(siaPath, removeFilePath)));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseDeleteFailed(siaPath, removeFilePath, CA2W(del.getErrorMsg()).m_psz)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FailedToDeleteFromSia(siaPath, removeFilePath, cerror)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@@ -318,7 +340,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
|
|
|
|
|
else if (uploadStatus == UploadStatus::Modified)
|
|
|
|
|
{
|
|
|
|
|
json response;
|
|
|
|
|
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete/") + siaPath, {}, response);
|
|
|
|
|
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response);
|
|
|
|
|
if (ApiSuccess(cerror))
|
|
|
|
|
{
|
|
|
|
|
// TODO validate response
|
|
|
|
@@ -374,7 +396,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
|
|
|
|
|
|
|
|
|
|
// TODO Validate response
|
|
|
|
|
json response;
|
|
|
|
|
SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload/") + siaPath, { {L"source", filePath} }, response);
|
|
|
|
|
SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload") + siaPath, { {L"source", filePath} }, response);
|
|
|
|
|
if (ApiSuccess(cerror))
|
|
|
|
|
{
|
|
|
|
|
SET_STATUS(UploadStatus::Uploading, UploadToSiaStarted, ModifyUploadStatusFailed)
|
|
|
|
@@ -490,7 +512,7 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath)
|
|
|
|
|
{
|
|
|
|
|
// Queue file upload operation
|
|
|
|
|
std::lock_guard<std::mutex> l2(_fileQueueMutex);
|
|
|
|
|
_fileQueue.push_back([=]() { this->FileAction(siaPath, filePath, tempSourcePath, siaDriveFilePath, false); });
|
|
|
|
|
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, tempSourcePath, siaDriveFilePath, false); });
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewFileAdded(siaPath, filePath, siaDriveFilePath)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -567,7 +589,7 @@ UploadError CUploadManager::Remove(const String& siaPath)
|
|
|
|
|
if (remove)
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> l2(_fileQueueMutex);
|
|
|
|
|
_fileQueue.push_back([=]() { this->FileAction(siaPath, filePath, nullptr, siaDriveFilePath, true); });
|
|
|
|
|
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, nullptr, siaDriveFilePath, true); });
|
|
|
|
|
|
|
|
|
|
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoveAdded(siaPath)));
|
|
|
|
|
}
|
|
|
|
|