feat: add uploader/downloader examples

This commit is contained in:
gmega 2026-01-30 17:55:37 -03:00
parent 0f131937a8
commit 847c3663fb
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
3 changed files with 96 additions and 12 deletions

View File

@ -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()

29
examples/downloader.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#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 <spr> <cid> <output_file>\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);
}

40
examples/uploader.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#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 <filepath>\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;
}