From 847c3663fb6084f8b13f03fc51ab7178372f94e8 Mon Sep 17 00:00:00 2001 From: gmega Date: Fri, 30 Jan 2026 17:55:37 -0300 Subject: [PATCH] feat: add uploader/downloader examples --- CMakeLists.txt | 39 +++++++++++++++++++++++++++------------ examples/downloader.c | 29 +++++++++++++++++++++++++++++ examples/uploader.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 examples/downloader.c create mode 100644 examples/uploader.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d609140..4972e19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,24 +2,24 @@ cmake_minimum_required(VERSION 3.14) project(easylibstorage C) set(CMAKE_C_STANDARD 11) -if(NOT DEFINED LOGOS_STORAGE_NIM_ROOT) +if (NOT DEFINED LOGOS_STORAGE_NIM_ROOT) message(FATAL_ERROR "Need to set LOGOS_STORAGE_NIM_ROOT") -endif() +endif () # --- Find libstorage --- -if(APPLE) +if (APPLE) set(LIBSTORAGE_NAMES libstorage.dylib) -elseif(WIN32) +elseif (WIN32) set(LIBSTORAGE_NAMES libstorage.dll) -else() +else () set(LIBSTORAGE_NAMES libstorage.so) -endif() +endif () find_library(LIBSTORAGE_PATH NAMES ${LIBSTORAGE_NAMES} PATHS ${LOGOS_STORAGE_NIM_ROOT}/build NO_DEFAULT_PATH) -if(NOT LIBSTORAGE_PATH) +if (NOT LIBSTORAGE_PATH) message(WARNING "libstorage not found. Build or provide it before linking.") -endif() +endif () # --- Shared library: easystorage --- add_library(easystorage SHARED @@ -32,9 +32,9 @@ target_include_directories(easystorage PUBLIC "${LOGOS_STORAGE_NIM_ROOT}/library" ) -if(LIBSTORAGE_PATH) +if (LIBSTORAGE_PATH) target_link_libraries(easystorage PRIVATE ${LIBSTORAGE_PATH}) -endif() +endif () # --- Example: storageconsole --- add_executable(storageconsole @@ -43,9 +43,24 @@ add_executable(storageconsole target_link_libraries(storageconsole PRIVATE easystorage) -if(LIBSTORAGE_PATH) +if (LIBSTORAGE_PATH) target_link_libraries(storageconsole PRIVATE ${LIBSTORAGE_PATH}) -endif() +endif () + +# --- Example: uploader/downloader --- +add_executable(uploader + examples/uploader.c) + +add_executable(downloader + examples/downloader.c) + +target_link_libraries(uploader PRIVATE easystorage) +target_link_libraries(downloader PRIVATE easystorage) + +if (LIBSTORAGE_PATH) + target_link_libraries(uploader PRIVATE ${LIBSTORAGE_PATH}) + target_link_libraries(downloader PRIVATE ${LIBSTORAGE_PATH}) +endif () # --- Tests --- enable_testing() diff --git a/examples/downloader.c b/examples/downloader.c new file mode 100644 index 0000000..58f81e0 --- /dev/null +++ b/examples/downloader.c @@ -0,0 +1,29 @@ +#include +#include +#include "easystorage.h" + +void progress(int total, int complete, int status) { + printf("\r %d / %d bytes", complete, total); + fflush(stdout); +} + +int main(int argc, char *argv[]) { + if (argc < 4) { + printf("Usage: %s \n", argv[0]); + exit(1); + } + + node_config cfg = { + .api_port = 8081, + .disc_port = 9091, + .data_dir = "./downloader-data", + .log_level = "INFO", + .bootstrap_node = argv[1], + }; + + STORAGE_NODE node = e_storage_new(cfg); + e_storage_start(node); + e_storage_download(node, argv[2], argv[3], progress); + e_storage_stop(node); + e_storage_destroy(node); +} diff --git a/examples/uploader.c b/examples/uploader.c new file mode 100644 index 0000000..c9b1673 --- /dev/null +++ b/examples/uploader.c @@ -0,0 +1,40 @@ +#include +#include +#include "easystorage.h" + +void progress(int total, int complete, int status) { + printf("\r %d / %d bytes", complete, total); + fflush(stdout); +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + exit(1); + } + + node_config cfg = { + .api_port = 8080, + .disc_port = 9090, + .data_dir = "./uploader-data", + .log_level = "INFO", + .bootstrap_node = NULL, + }; + + STORAGE_NODE node = e_storage_new(cfg); + e_storage_start(node); + + char *cid = e_storage_upload(node, argv[1], progress); + char *spr = e_storage_spr(node); + + printf("\nCID: %s\n", cid); + printf("SPR: %s\n", spr); + + printf("\nPress Enter to exit\n"); + getchar(); + + e_storage_stop(node); + e_storage_destroy(node); + + return 0; +}