mirror of
https://github.com/AleziaKurdis/overte.git
synced 2025-04-08 02:32:43 +02:00
Update DS to use promises for backup APIs
This commit is contained in:
parent
8b07e7e28f
commit
dd398da2e0
3 changed files with 48 additions and 37 deletions
|
@ -212,54 +212,58 @@ bool DomainContentBackupManager::getMostRecentBackup(const QString& format,
|
|||
return bestBackupFound;
|
||||
}
|
||||
|
||||
bool DomainContentBackupManager::deleteBackup(const QString& backupName) {
|
||||
void DomainContentBackupManager::deleteBackup(MiniPromise::Promise promise, const QString& backupName) {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
bool result{ false };
|
||||
BLOCKING_INVOKE_METHOD(this, "deleteBackup",
|
||||
Q_RETURN_ARG(bool, result),
|
||||
Q_ARG(const QString&, backupName));
|
||||
return result;
|
||||
QMetaObject::invokeMethod(this, "deleteBackup", Q_ARG(MiniPromise::Promise, promise),
|
||||
Q_ARG(const QString&, backupName));
|
||||
return;
|
||||
}
|
||||
|
||||
bool success { false };
|
||||
QDir backupDir { _backupDirectory };
|
||||
QFile backupFile { backupDir.filePath(backupName) };
|
||||
if (backupFile.remove()) {
|
||||
return true;
|
||||
success = true;
|
||||
}
|
||||
return false;
|
||||
promise->resolve({
|
||||
{ "success", success }
|
||||
});
|
||||
}
|
||||
|
||||
bool DomainContentBackupManager::recoverFromBackup(const QString& backupName) {
|
||||
void DomainContentBackupManager::recoverFromBackup(MiniPromise::Promise promise, const QString& backupName) {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
bool result{ false };
|
||||
BLOCKING_INVOKE_METHOD(this, "recoverFromBackup",
|
||||
Q_RETURN_ARG(bool, result),
|
||||
Q_ARG(const QString&, backupName));
|
||||
return result;
|
||||
QMetaObject::invokeMethod(this, "recoverFromBackup", Q_ARG(MiniPromise::Promise, promise),
|
||||
Q_ARG(const QString&, backupName));
|
||||
return;
|
||||
}
|
||||
|
||||
qDebug() << "Recoving from" << backupName;
|
||||
|
||||
bool success { false };
|
||||
QDir backupDir { _backupDirectory };
|
||||
QFile backupFile { backupDir.filePath(backupName) };
|
||||
if (backupFile.open(QIODevice::ReadOnly)) {
|
||||
QuaZip zip { &backupFile };
|
||||
if (!zip.open(QuaZip::Mode::mdUnzip)) {
|
||||
qWarning() << "Failed to unzip file: " << backupName;
|
||||
backupFile.close();
|
||||
success = false;
|
||||
} else {
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.recoverBackup(zip);
|
||||
}
|
||||
|
||||
qDebug() << "Successfully recovered from " << backupName;
|
||||
success = true;
|
||||
}
|
||||
|
||||
for (auto& handler : _backupHandlers) {
|
||||
handler.recoverBackup(zip);
|
||||
}
|
||||
|
||||
backupFile.close();
|
||||
qDebug() << "Successfully recovered from " << backupName;
|
||||
return true;
|
||||
} else {
|
||||
success = false;
|
||||
qWarning() << "Invalid id: " << backupName;
|
||||
return false;
|
||||
}
|
||||
|
||||
promise->resolve({
|
||||
{ "success", success }
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<BackupItemInfo> DomainContentBackupManager::getAllBackups() {
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
#include "BackupHandler.h"
|
||||
|
||||
#include <shared/MiniPromises.h>
|
||||
|
||||
struct BackupItemInfo {
|
||||
QString id;
|
||||
QString name;
|
||||
|
@ -59,8 +61,8 @@ public:
|
|||
void createManualBackup(const QString& name);
|
||||
|
||||
public slots:
|
||||
bool recoverFromBackup(const QString& backupName);
|
||||
bool deleteBackup(const QString& backupName);
|
||||
void recoverFromBackup(MiniPromise::Promise promise, const QString& backupName);
|
||||
void deleteBackup(MiniPromise::Promise promise, const QString& backupName);
|
||||
|
||||
signals:
|
||||
void loadCompleted();
|
||||
|
|
|
@ -2124,11 +2124,14 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
|
|||
return true;
|
||||
} else if (url.path().startsWith(URI_API_BACKUPS_RECOVER)) {
|
||||
auto id = url.path().mid(QString(URI_API_BACKUPS_RECOVER).length());
|
||||
_contentManager->recoverFromBackup(id);
|
||||
QJsonObject rootJSON;
|
||||
rootJSON["success"] = true;
|
||||
QJsonDocument docJSON(rootJSON);
|
||||
connection->respond(HTTPConnection::StatusCode200, docJSON.toJson(), JSON_MIME_TYPE.toUtf8());
|
||||
auto deferred = makePromise("recoverFromBackup");
|
||||
deferred->then([connection, JSON_MIME_TYPE](QString error, QVariantMap result) {
|
||||
QJsonObject rootJSON;
|
||||
rootJSON["success"] = result["success"].toBool();
|
||||
QJsonDocument docJSON(rootJSON);
|
||||
connection->respond(HTTPConnection::StatusCode200, docJSON.toJson(), JSON_MIME_TYPE.toUtf8());
|
||||
});
|
||||
_contentManager->recoverFromBackup(deferred, id);
|
||||
return true;
|
||||
} else if (url.path() == URI_API_BACKUPS) {
|
||||
QJsonObject rootJSON;
|
||||
|
@ -2368,11 +2371,15 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
|
|||
|
||||
if (url.path().startsWith(URI_API_BACKUPS_ID)) {
|
||||
auto id = url.path().mid(QString(URI_API_BACKUPS_ID).length());
|
||||
auto success = _contentManager->deleteBackup(id);
|
||||
QJsonObject rootJSON;
|
||||
rootJSON["success"] = success;
|
||||
QJsonDocument docJSON(rootJSON);
|
||||
connection->respond(HTTPConnection::StatusCode200, docJSON.toJson(), JSON_MIME_TYPE.toUtf8());
|
||||
auto deferred = makePromise("deleteBackup");
|
||||
deferred->then([connection, JSON_MIME_TYPE](QString error, QVariantMap result) {
|
||||
QJsonObject rootJSON;
|
||||
rootJSON["success"] = result["success"].toBool();
|
||||
QJsonDocument docJSON(rootJSON);
|
||||
connection->respond(HTTPConnection::StatusCode200, docJSON.toJson(), JSON_MIME_TYPE.toUtf8());
|
||||
});
|
||||
_contentManager->deleteBackup(deferred, id);
|
||||
|
||||
return true;
|
||||
|
||||
} else if (nodeDeleteRegex.indexIn(url.path()) != -1) {
|
||||
|
@ -3310,8 +3317,6 @@ void DomainServer::maybeHandleReplacementEntityFile() {
|
|||
}
|
||||
|
||||
void DomainServer::handleOctreeFileReplacement(QByteArray octreeFile) {
|
||||
// enumerate the nodes and find any octree type servers with active sockets
|
||||
|
||||
//Assume we have compressed data
|
||||
auto compressedOctree = octreeFile;
|
||||
QByteArray jsonOctree;
|
||||
|
|
Loading…
Reference in a new issue