618 lines
13 KiB
C++
618 lines
13 KiB
C++
#pragma once
|
|
#include "AutoThread.h"
|
|
#include "SQLiteCpp/Database.h"
|
|
#include <deque>
|
|
#include <EventSystem.h>
|
|
|
|
NS_BEGIN(Sia)
|
|
NS_BEGIN(Api)
|
|
|
|
class AFX_EXT_CLASS CUploadManager :
|
|
public CAutoThread
|
|
{
|
|
public:
|
|
enum class _UploadStatus : unsigned
|
|
{
|
|
NotFound,
|
|
Copying,
|
|
Queued,
|
|
Modified,
|
|
Uploading,
|
|
Complete,
|
|
Error
|
|
};
|
|
|
|
enum class _UploadError
|
|
{
|
|
Success,
|
|
SourceFileNotFound,
|
|
DatabaseError
|
|
};
|
|
|
|
private:
|
|
typedef struct
|
|
{
|
|
std::uint64_t Id;
|
|
String SiaPath;
|
|
String FilePath;
|
|
String TempPath;
|
|
_UploadStatus Status;
|
|
} UploadData;
|
|
|
|
public:
|
|
CUploadManager(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
|
|
|
public:
|
|
virtual ~CUploadManager();
|
|
|
|
private:
|
|
SQLite::Database _uploadDatabase;
|
|
std::mutex _uploadMutex;
|
|
CSiaDriveConfig* _siaDriveConfig;
|
|
CAutoThread _fileThread;
|
|
std::mutex _fileQueueMutex;
|
|
std::deque<std::function<void()>> _fileQueue;
|
|
|
|
private:
|
|
void NewFileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath);
|
|
|
|
protected:
|
|
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override;
|
|
void FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
|
|
|
public:
|
|
static String UploadStatusToString(const _UploadStatus& uploadStatus);
|
|
|
|
public:
|
|
_UploadStatus GetUploadStatus(const String& siaPath);
|
|
_UploadError AddOrUpdate(const String& siaPath, String filePath);
|
|
_UploadError Remove(const String& siaPath);
|
|
void PurgeCompleteStatus();
|
|
void PurgeErrorStatus();
|
|
};
|
|
|
|
typedef Sia::Api::CUploadManager::_UploadStatus UploadStatus;
|
|
typedef Sia::Api::CUploadManager::_UploadError UploadError;
|
|
|
|
// Event Notifications
|
|
class CreatingTemporarySiaDriveFile :
|
|
public CEvent
|
|
{
|
|
public:
|
|
CreatingTemporarySiaDriveFile(const String& siaPath, const String& filePath, const String& tempSourcePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_tempSourcePath(tempSourcePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~CreatingTemporarySiaDriveFile()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _tempSourcePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"CreatingTemporarySiaDriveFile|SP|" + _siaPath + L"|FP|" + _filePath + L"|TSP|" + _tempSourcePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new CreatingTemporarySiaDriveFile(_siaPath, _filePath, _tempSourcePath));
|
|
}
|
|
};
|
|
|
|
class UploadAddedToQueue :
|
|
public CEvent
|
|
{
|
|
public:
|
|
UploadAddedToQueue(const String& siaPath, const String& filePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~UploadAddedToQueue()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"UploadAddedToQueue|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new UploadAddedToQueue(_siaPath, _filePath));
|
|
}
|
|
};
|
|
|
|
class ModifiedUploadQueued :
|
|
public CEvent
|
|
{
|
|
public:
|
|
ModifiedUploadQueued(const String& siaPath, const String& filePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~ModifiedUploadQueued()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"ModifiedUploadQueued|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new ModifiedUploadQueued(_siaPath, _filePath));
|
|
}
|
|
};
|
|
|
|
class UploadComplete :
|
|
public CEvent
|
|
{
|
|
public:
|
|
UploadComplete(const String& siaPath, const String& filePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~UploadComplete()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"UploadComplete|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new UploadComplete(_siaPath, _filePath));
|
|
}
|
|
};
|
|
|
|
class UploadModifiedInQueue :
|
|
public CEvent
|
|
{
|
|
public:
|
|
UploadModifiedInQueue(const String& siaPath, const String& filePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~UploadModifiedInQueue()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"UploadModifiedInQueue|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new UploadModifiedInQueue(_siaPath, _filePath));
|
|
}
|
|
};
|
|
|
|
class FailedToDeleteFromSia :
|
|
public CEvent
|
|
{
|
|
public:
|
|
FailedToDeleteFromSia(const String& siaPath, const String& filePath, const SiaCurlError& curlError) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_curlError(curlError)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~FailedToDeleteFromSia()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const SiaCurlError _curlError = SiaCurlError::Success;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"FailedToDeleteFromSia|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new FailedToDeleteFromSia(_siaPath, _filePath, _curlError));
|
|
}
|
|
};
|
|
|
|
class ModifyUploadStatusFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
ModifyUploadStatusFailed(const String& siaPath, const String& filePath, const UploadStatus& uploadStatus, const String& errorMsg) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_uploadStatus(uploadStatus),
|
|
_errorMsg(errorMsg)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~ModifyUploadStatusFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const UploadStatus _uploadStatus;
|
|
const String _errorMsg;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"ModifyUploadStatusFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|ST|" + CUploadManager::UploadStatusToString(_uploadStatus) + L"|MSG|" + _errorMsg;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new ModifyUploadStatusFailed(_siaPath, _filePath, _uploadStatus, _errorMsg));
|
|
}
|
|
};
|
|
|
|
class CreatingTemporarySiaDriveFileFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
CreatingTemporarySiaDriveFileFailed(const String& siaPath, const String& filePath, const String& tempSourcePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_tempSourcePath(tempSourcePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~CreatingTemporarySiaDriveFileFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _tempSourcePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"CreatingTemporarySiaDriveFileFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|TSP|" + _tempSourcePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new CreatingTemporarySiaDriveFileFailed(_siaPath, _filePath, _tempSourcePath));
|
|
}
|
|
};
|
|
|
|
class DeleteSiaDriveFileFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
DeleteSiaDriveFileFailed(const String& siaPath, const String& filePath, const String& siaDriveFilePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_siaDriveFilePath(siaDriveFilePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~DeleteSiaDriveFileFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _siaDriveFilePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"DeleteSiaDriveFileFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|SDP|" + _siaDriveFilePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new DeleteSiaDriveFileFailed(_siaPath, _filePath, _siaDriveFilePath));
|
|
}
|
|
};
|
|
|
|
class DeleteTemporarySiaDriveFileFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
DeleteTemporarySiaDriveFileFailed(const String& siaPath, const String& filePath, const String& tempSourcePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_tempSourcePath(tempSourcePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~DeleteTemporarySiaDriveFileFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _tempSourcePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"DeleteTemporarySiaDriveFileFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|TSP|" + _tempSourcePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new DeleteTemporarySiaDriveFileFailed(_siaPath, _filePath, _tempSourcePath));
|
|
}
|
|
};
|
|
|
|
class RenamingTemporarySiaDriveFile :
|
|
public CEvent
|
|
{
|
|
public:
|
|
RenamingTemporarySiaDriveFile(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_tempSourcePath(tempSourcePath),
|
|
_siaDriveFilePath(siaDriveFilePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~RenamingTemporarySiaDriveFile()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _tempSourcePath;
|
|
const String _siaDriveFilePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"RenamingTemporarySiaDriveFile|SP|" + _siaPath + L"|FP|" + _filePath + L"|TSP|" + _tempSourcePath + L"|SDP|" + _siaDriveFilePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new RenamingTemporarySiaDriveFile(_siaPath, _filePath, _tempSourcePath, _siaDriveFilePath));
|
|
}
|
|
};
|
|
|
|
class RenamingTemporarySiaDriveFileFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
RenamingTemporarySiaDriveFileFailed(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_tempSourcePath(tempSourcePath),
|
|
_siaDriveFilePath(siaDriveFilePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~RenamingTemporarySiaDriveFileFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _tempSourcePath;
|
|
const String _siaDriveFilePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"RenamingTemporarySiaDriveFileFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|TSP|" + _tempSourcePath + L"|SDP|" + _siaDriveFilePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new RenamingTemporarySiaDriveFileFailed(_siaPath, _filePath, _tempSourcePath, _siaDriveFilePath));
|
|
}
|
|
};
|
|
|
|
class DatabaseInsertFailed :
|
|
public CEvent
|
|
{
|
|
public:
|
|
DatabaseInsertFailed(const String& siaPath, const String& filePath, const String& errorMessage) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_errorMessage(errorMessage)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~DatabaseInsertFailed()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _errorMessage;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"DatabaseInsertFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|MSG|" + _errorMessage;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new DatabaseInsertFailed(_siaPath, _filePath, _errorMessage));
|
|
}
|
|
};
|
|
|
|
class ExistingUploadFound :
|
|
public CEvent
|
|
{
|
|
public:
|
|
ExistingUploadFound(const String& siaPath, const String& filePath, const UploadStatus& uploadStatus) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_uploadStatus(uploadStatus)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~ExistingUploadFound()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const UploadStatus _uploadStatus;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"ExistingUploadFound|SP|" + _siaPath + L"|FP|" + _filePath + L"|ST|" + CUploadManager::UploadStatusToString(_uploadStatus);
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new ExistingUploadFound(_siaPath, _filePath, _uploadStatus));
|
|
}
|
|
};
|
|
|
|
class NewFileAdded :
|
|
public CEvent
|
|
{
|
|
public:
|
|
NewFileAdded(const String& siaPath, const String& filePath, const String& siaDriveFilePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath),
|
|
_siaDriveFilePath(siaDriveFilePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~NewFileAdded()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
const String _siaDriveFilePath;
|
|
|
|
public:
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"NewFileAdded|SP|" + _siaPath + L"|FP|" + _filePath + L"|SDP|" + _siaDriveFilePath;
|
|
}
|
|
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new NewFileAdded(_siaPath, _filePath, _siaDriveFilePath));
|
|
}
|
|
};
|
|
|
|
class SourceFileNotFound :
|
|
public CEvent
|
|
{
|
|
public:
|
|
SourceFileNotFound(const String& siaPath, const String& filePath) :
|
|
_siaPath(siaPath),
|
|
_filePath(filePath)
|
|
{
|
|
|
|
}
|
|
|
|
public:
|
|
virtual ~SourceFileNotFound()
|
|
{
|
|
}
|
|
|
|
private:
|
|
const String _siaPath;
|
|
const String _filePath;
|
|
|
|
public:
|
|
virtual std::shared_ptr<CEvent> Clone() const override
|
|
{
|
|
return std::shared_ptr<CEvent>(new SourceFileNotFound(_siaPath, _filePath));
|
|
}
|
|
|
|
virtual String GetSingleLineMessage() const override
|
|
{
|
|
return L"SourceFileNotFound|SP|" + _siaPath + L"|FP|" + _filePath;
|
|
}
|
|
};
|
|
|
|
NS_END(2) |