diff --git a/README.md b/README.md index 7e607cd..28c1002 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,8 @@ This example demonstrates how to upload a file using the **TusClient** ```cpp #include #include -#include +#include +#include // Function to generate a test file (example) std::filesystem::path generateTestFile(int sizeMB) { @@ -176,10 +177,11 @@ int main() { // Generate a test file of size 1MB std::filesystem::path testFilePath = generateTestFile(1); std::cout << "Test file path: " << testFilePath << std::endl; - + // Create a TusClient instance with the necessary parameters: app name, server URL, file path, and log level - std::string url = "http://your-tus-server-url"; // Replace with your server URL - TUS::TusClient client("testapp", url, testFilePath, TUS::LogLevel::INFO); + std::string url = "http://localhost:8080/files/"; // Replace with your server URL + + TUS::TusClient client("testapp", url, testFilePath, TUS::Logging::LogLevel::_INFO_); // Perform the upload std::cout << "Starting upload..." << std::endl; diff --git a/cmake/qt-utils.cmake b/cmake/qt-utils.cmake deleted file mode 100644 index f0d7184..0000000 --- a/cmake/qt-utils.cmake +++ /dev/null @@ -1,30 +0,0 @@ -function(add_program_icon target icon_path) - if (WIN32) - #todo: implement - elseif(APPLE) - #todo: implement - - elseif(UNIX AND NOT APPLE) - #todo: implement - - endif() -endfunction() - -function (auto_deploy_libraries target_name) - if (WIN32) - #todo: implement - - endif () - if (APPLE) - #todo: implement - - endif () - if (UNIX AND NOT APPLE) - #todo: implement - - endif () -endfunction() - -function(create_i18n_language target_name language) - #todo: implement -endfunction() \ No newline at end of file diff --git a/lib/tusclient/CMakeLists.txt b/lib/tusclient/CMakeLists.txt index 7c5f9da..9041299 100644 --- a/lib/tusclient/CMakeLists.txt +++ b/lib/tusclient/CMakeLists.txt @@ -19,7 +19,10 @@ target_include_directories(tusclient PUBLIC # Link libraries target_link_libraries(tusclient PUBLIC CURL::libcurl Boost::uuid Boost::lexical_cast nlohmann_json::nlohmann_json glog::glog) -target_compile_definitions(tusclient PRIVATE TUSCLIENT_EXPORTS) +if(BUILD_SHARED_LIBS) + target_compile_definitions(tusclient PRIVATE TUSCLIENT_EXPORTS) + target_compile_definitions(tusclient PRIVATE TUSCLIENT_SHARED) +endif () # Enable testing if tests exist include(CTest) if(BUILD_TESTING) diff --git a/lib/tusclient/include/tusclient/TusClient.h b/lib/tusclient/include/tusclient/TusClient.h index 7a8009a..b2138ab 100644 --- a/lib/tusclient/include/tusclient/TusClient.h +++ b/lib/tusclient/include/tusclient/TusClient.h @@ -108,7 +108,6 @@ namespace TUS { int m_uploadedChunks = 0; string m_tusLocation; int m_uploadOffset = 0; - bool m_nextChunk = false; size_t m_uploadLength = 0; std::chrono::milliseconds m_requestTimeout = std::chrono::milliseconds( 0); /*This timeout is the time waited between one requests, it is in ms, diff --git a/lib/tusclient/include/tusclient/libtusclient.h b/lib/tusclient/include/tusclient/libtusclient.h index c183b5d..b2601ff 100644 --- a/lib/tusclient/include/tusclient/libtusclient.h +++ b/lib/tusclient/include/tusclient/libtusclient.h @@ -3,19 +3,21 @@ #include constexpr auto TUS_PROTOCOL_VERSION = "1.0.0"; - -#if defined(_WIN32) || defined(__CYGWIN__) - #ifdef TUSCLIENT_EXPORTS - #define EXPORT_LIBTUSCLIENT __declspec(dllexport) +#if defined(TUSCLIENT_SHARED) + #if defined(_WIN32) || defined(__CYGWIN__) + #ifdef TUSCLIENT_EXPORTS + #define EXPORT_LIBTUSCLIENT __declspec(dllexport) + #else + #define EXPORT_LIBTUSCLIENT __declspec(dllimport) + #endif #else - #define EXPORT_LIBTUSCLIENT __declspec(dllimport) + #ifdef TUSCLIENT_EXPORTS + #define EXPORT_LIBTUSCLIENT __attribute__((visibility("default"))) + #else + #define EXPORT_LIBTUSCLIENT + #endif #endif -#else - #ifdef TUSCLIENT_EXPORTS - #define EXPORT_LIBTUSCLIENT __attribute__((visibility("default"))) #else #define EXPORT_LIBTUSCLIENT #endif -#endif - #endif // INCLUDE_LIBTUSCLIENT_H_ \ No newline at end of file diff --git a/lib/tusclient/package.json b/lib/tusclient/package.json index 5a4eedf..fb6bc36 100644 --- a/lib/tusclient/package.json +++ b/lib/tusclient/package.json @@ -1,6 +1,6 @@ { "name": "tusclient", - "version": "1.0.1", + "version": "1.0.0", "type": "lib", "description": "tusclient module of type lib" } \ No newline at end of file diff --git a/lib/tusclient/src/tusclient/TusClient.cpp b/lib/tusclient/src/tusclient/TusClient.cpp index 6092c8c..2d7ac2c 100644 --- a/lib/tusclient/src/tusclient/TusClient.cpp +++ b/lib/tusclient/src/tusclient/TusClient.cpp @@ -81,7 +81,7 @@ std::string extractHeaderValue(const std::string &header, const auto toLower = [](const std::string &s) { std::string result = s; std::ranges::transform(result, result.begin(), - [](unsigned char c) { return std::tolower(c); }); + [](unsigned char c) { return std::tolower(c); }); return result; }; @@ -152,10 +152,10 @@ bool TusClient::upload() { if (lastSlashPosition != std::string::npos) { // remove the url from the location m_tusLocation.replace(0, lastSlashPosition + 1, ""); - std::cout << "slash position:" << lastSlashPosition << std::endl; } else { // Handle the case where '/' is not found in m_tusLocation - std::cerr << "Error: '/' not found in m_tusLocation" << std::endl; + std::cerr << "Error: '/' not found in m_tusLocation, check if the upload url point to a TUS route" << + std::endl; } }; OnErrorCallback onError = [this]([[maybe_unused]] const std::string &header, const std::string &data) { @@ -210,9 +210,17 @@ bool TusClient::uploadChunks() { } void TusClient::handleSuccessfulUpload(const string &header) { + if (m_status.load() == TusStatus::CANCELED) { + m_logger->debug("Upload canceled"); + return; + } m_uploadedChunks++; - m_nextChunk = true; - m_uploadOffset = std::stoi(extractHeaderValue(header, "Upload-Offset")); + try { + m_uploadOffset = std::stoi(extractHeaderValue(header, "Upload-Offset")); + }catch (const std::exception &e) { + m_logger->error("Failed to parse header: " + std::string(e.what())); + return; + } float progress = static_cast(m_uploadOffset) / static_cast(std::filesystem::file_size(m_filePath)) * 100; @@ -233,7 +241,7 @@ void TusClient::handleUploadConflict(const string &header) { std::this_thread::sleep_for(m_requestTimeout); } - void TusClient::handleUploadError(const string &header) { +void TusClient::handleUploadError(const string &header) { m_logger->error(std::format("Error: Unable to upload chunk {}", m_uploadedChunks)); m_logger->error(header); m_status.store(TusStatus::FAILED); @@ -249,8 +257,6 @@ void TusClient::uploadChunk(int chunkNumber) { Chunk::TUSChunk chunk = m_fileChunker->getChunks().at(chunkNumber); std::map patchHeaders; - m_nextChunk = false; - patchHeaders["Tus-Resumable"] = TUS_PROTOCOL_VERSION; patchHeaders["Content-Type"] = "application/offset+octet-stream"; patchHeaders["Content-Length"] = std::to_string(chunk.getChunkSize()); @@ -314,8 +320,12 @@ void TusClient::getUploadInfo() { OnSuccessCallback headSuccess = [this](const std::string &header, [[maybe_unused]] const std::string &data) { - m_uploadOffset = std::stoi(extractHeaderValue(header, "Upload-Offset")); - m_uploadLength = std::stoi(extractHeaderValue(header, "Upload-Length")); + try { + m_uploadOffset = std::stoi(extractHeaderValue(header, "Upload-Offset")); + m_uploadLength = std::stoi(extractHeaderValue(header, "Upload-Length")); + } catch (const std::exception &e) { + m_logger->error("Failed to parse header: " + std::string(e.what())); + } }; OnErrorCallback onError = [this]([[maybe_unused]] const std::string &header, const std::string &data) { @@ -409,7 +419,6 @@ bool TusClient::retry() { m_fileChunker->clearChunks(); m_uploadedChunks = 0; m_uploadOffset = 0; - m_nextChunk = false; m_progress.store(0); return upload(); } else { diff --git a/project.json b/project.json index 212341f..39c149d 100644 --- a/project.json +++ b/project.json @@ -2,6 +2,6 @@ "name": "tusclient", "organization": "Cadons", "package_manager": "vcpkg", - "version": "1.0.1", + "version": "1.0.0", "subtrees": [] } \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json index 7dea9f6..f4394d1 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,7 +1,7 @@ { "name": "tusclient", "license": "MIT", - "version": "1.0.1", + "version": "1.0.0", "description": "Library implementing the tus protocol for resumable uploads", "homepage": "https://github.com/Cadons/libtusclient", "dependencies": [