diff --git a/cli/uploader.cpp b/cli/uploader.cpp index 6e74275..199f56d 100644 --- a/cli/uploader.cpp +++ b/cli/uploader.cpp @@ -2,13 +2,29 @@ #include #include +#include +#include #include +#define DEFAULT_TIMEOUT 10000 + int app_main(LogosModules* modules, int argc, char* argv[]); +bool await(QEventLoop* loop, int timeoutMs) { + QTimer::singleShot(timeoutMs, loop, [loop]() { + std::cerr << "Call timed out." << std::endl; + loop->exit(1); + }); + return loop->exec() == 0; +} + +void notify(QEventLoop* loop, bool successValue) { + loop->exit(successValue ? 0 : 1); +} + int main(int argc, char* argv[]) { if (argc < 2) { - std::cerr << "Usage: " << argv[0] << " " << std::endl; + std::cerr << "Usage: " << argv[0] << " " << std::endl; return 1; } @@ -28,6 +44,7 @@ int main(int argc, char* argv[]) { } int app_main(LogosModules* modules, int argc, char* argv[]) { + if (argc < 2) { std::cerr << "Usage: " << argv[0] << " " << std::endl; return 1; @@ -44,5 +61,51 @@ int app_main(LogosModules* modules, int argc, char* argv[]) { return 1; } - return 0; + { + QEventLoop loop; + modules->storage_module.on("storageStart", [&loop](const QVariantList& data) { + bool success = data[0].toBool(); + if (!success) { + std::cerr << "Failed to start storage module: " << data[1].toString().toStdString() << std::endl; + } + notify(&loop, success); + }); + + modules->storage_module.start(); + + if (!await(&loop, DEFAULT_TIMEOUT)) { + return 1; + } + } + + { + QEventLoop loop; + + // Uploads file. + modules->storage_module.on("storageUploadDone", [&loop](const QVariantList& data) { + bool success = data[0].toBool(); + if (!success) { + std::cerr << "Failed to upload file: " << data[2].toString().toStdString() << std::endl; + notify(&loop, false); + return; + } + + std::cout << "CID: " << data[2].toString().toStdString() << std::endl; + notify(&loop, true); + }); + + QUrl url = QUrl::fromLocalFile(argv[1]); + LogosResult result = modules->storage_module.uploadUrl(url); + if (!result.success) { + std::cerr << "Failed to upload file: " << result.getValue().toStdString() << std::endl; + return 1; + } + + if (!await(&loop, DEFAULT_TIMEOUT)) { + return 1; + } + } + + std::cout << "Upload completed successfully. Type CTRL+C to exit." << std::endl; + return QCoreApplication::exec(); }