Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ This example demonstrates how to upload a file using the **TusClient**
```cpp
#include <iostream>
#include <filesystem>
#include <libtusclient/TusClient.h>
#include <tusclient/TusClient.h>
#include <fstream>

// Function to generate a test file (example)
std::filesystem::path generateTestFile(int sizeMB) {
Expand All @@ -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;
Expand Down
30 changes: 0 additions & 30 deletions cmake/qt-utils.cmake

This file was deleted.

5 changes: 4 additions & 1 deletion lib/tusclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lib/tusclient/include/tusclient/TusClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 12 additions & 10 deletions lib/tusclient/include/tusclient/libtusclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
#include <string>

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_
2 changes: 1 addition & 1 deletion lib/tusclient/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tusclient",
"version": "1.0.1",
"version": "1.0.0",
"type": "lib",
"description": "tusclient module of type lib"
}
31 changes: 20 additions & 11 deletions lib/tusclient/src/tusclient/TusClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<float>(m_uploadOffset) /
static_cast<float>(std::filesystem::file_size(m_filePath)) * 100;
Expand All @@ -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);
Expand All @@ -249,8 +257,6 @@ void TusClient::uploadChunk(int chunkNumber) {
Chunk::TUSChunk chunk = m_fileChunker->getChunks().at(chunkNumber);
std::map<std::string, std::string> 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());
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "tusclient",
"organization": "Cadons",
"package_manager": "vcpkg",
"version": "1.0.1",
"version": "1.0.0",
"subtrees": []
}
2 changes: 1 addition & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
Loading