initial commit
This commit is contained in:
commit
d306f261bd
8 files changed
+302
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
cmake-build-debug
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
cmake_minimum_required(VERSION 4.2)
|
||||||
|
project(seadisk C CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
find_package(tl-expected CONFIG REQUIRED)
|
||||||
|
find_package(nlohmann_json CONFIG REQUIRED)
|
||||||
|
find_package(date CONFIG REQUIRED)
|
||||||
|
find_package(cpr CONFIG REQUIRED)
|
||||||
|
find_package(argparse CONFIG REQUIRED)
|
||||||
|
find_package(ada CONFIG REQUIRED)
|
||||||
|
|
||||||
|
cmake_host_system_information(
|
||||||
|
RESULT WINFSP_DIR
|
||||||
|
QUERY WINDOWS_REGISTRY
|
||||||
|
"HKLM\\SOFTWARE\\WinFsp"
|
||||||
|
VALUE "InstallDir"
|
||||||
|
)
|
||||||
|
|
||||||
|
message(STATUS "WinFsp=${WINFSP_DIR}")
|
||||||
|
|
||||||
|
set(WINFSP_INCLUDE_DIR "${WINFSP_DIR}/inc")
|
||||||
|
|
||||||
|
message(STATUS "${CMAKE_SYSTEM_PROCESSOR}")
|
||||||
|
if (CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64")
|
||||||
|
set(WINFSP_PLATFORM x64)
|
||||||
|
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "ARM64|aarch64")
|
||||||
|
set(WINFSP_PLATFORM a64)
|
||||||
|
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "X86|i386|i686")
|
||||||
|
set(WINFSP_PLATFORM x86)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
set(WINFSP_LIB "winfsp-${WINFSP_PLATFORM}.lib")
|
||||||
|
|
||||||
|
message(STATUS "WinFSP lib: ${WINFSP_LIB}")
|
||||||
|
|
||||||
|
add_executable(seadisk src/main.cpp
|
||||||
|
include/seafile/seafile_api.hpp
|
||||||
|
include/seafile/exception.hpp
|
||||||
|
include/seafile/server_info.hpp
|
||||||
|
include/seafile/auth_token.hpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(seadisk PRIVATE
|
||||||
|
UNICODE
|
||||||
|
_UNICODE
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(seadisk PRIVATE
|
||||||
|
"${WINFSP_INCLUDE_DIR}"
|
||||||
|
include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_directories(seadisk PRIVATE
|
||||||
|
"${WINFSP_DIR}/lib"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(seadisk PRIVATE
|
||||||
|
${WINFSP_LIB}
|
||||||
|
delayimp
|
||||||
|
tl::expected
|
||||||
|
nlohmann_json::nlohmann_json
|
||||||
|
date::date
|
||||||
|
date::date-tz
|
||||||
|
cpr::cpr
|
||||||
|
argparse::argparse
|
||||||
|
ada::ada
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_options(seadisk PRIVATE
|
||||||
|
"/DELAYLOAD:winfsp-${WINFSP_PLATFORM}.dll"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_options(seadisk PRIVATE
|
||||||
|
/Zc:preprocessor
|
||||||
|
)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by Shell on 2026/6/18.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SEADISK_AUTH_TOKEN_HPP
|
||||||
|
#define SEADISK_AUTH_TOKEN_HPP
|
||||||
|
|
||||||
|
namespace seafile {
|
||||||
|
struct AuthToken final {
|
||||||
|
std::string token;
|
||||||
|
|
||||||
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE(AuthToken, token);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //SEADISK_AUTH_TOKEN_HPP
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
//
|
||||||
|
// Created by Shell on 2026/6/17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SEADISK_EXCEPTION_HPP
|
||||||
|
#define SEADISK_EXCEPTION_HPP
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace seafile {
|
||||||
|
class SeafileException : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
explicit SeafileException(const std::string &msg)
|
||||||
|
: std::runtime_error(msg) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //SEADISK_EXCEPTION_HPP
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
//
|
||||||
|
// Created by Shell on 2026/6/17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SEADISK_SEAFILE_API_HPP
|
||||||
|
#define SEADISK_SEAFILE_API_HPP
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "exception.hpp"
|
||||||
|
#include <tl/expected.hpp>
|
||||||
|
#include <ada.h>
|
||||||
|
#include <cpr/cpr.h>
|
||||||
|
|
||||||
|
#include "auth_token.hpp"
|
||||||
|
#include "server_info.hpp"
|
||||||
|
|
||||||
|
namespace seafile {
|
||||||
|
class SeaFileApi final {
|
||||||
|
//base url of the seafile api server
|
||||||
|
std::string base_url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* make a url string from given path
|
||||||
|
* @param path url path
|
||||||
|
* @return the whole url string
|
||||||
|
*/
|
||||||
|
std::string makeUrl(const std::string &path) {
|
||||||
|
auto url = ada::parse(this->base_url);
|
||||||
|
url->set_pathname(path);
|
||||||
|
return std::string(url->get_href());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
tl::expected<T, SeafileException>
|
||||||
|
parseResponse(const cpr::Response &response) {
|
||||||
|
if (response.error) {
|
||||||
|
return tl::make_unexpected(
|
||||||
|
SeafileException(
|
||||||
|
response.error.message));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status_code != 200) {
|
||||||
|
return tl::make_unexpected(
|
||||||
|
SeafileException(
|
||||||
|
std::format("Http Error: {}",
|
||||||
|
response.status_code)));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return nlohmann::json::parse(response.text)
|
||||||
|
.get<T>();
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
return tl::make_unexpected(
|
||||||
|
SeafileException(e.what()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post body and fetch result
|
||||||
|
* @tparam T return type
|
||||||
|
* @param path target url path
|
||||||
|
* @param body post body data
|
||||||
|
* @return T if success else SeafileException
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
tl::expected<T, SeafileException> post(const std::string &path, const nlohmann::json &body) {
|
||||||
|
const auto url = makeUrl(path);
|
||||||
|
auto response = cpr::Post(
|
||||||
|
cpr::Url{url},
|
||||||
|
cpr::Header{
|
||||||
|
{"accept", "application/json"},
|
||||||
|
{"content-type", "application/json"}
|
||||||
|
}, cpr::Body{body.dump()});
|
||||||
|
return parseResponse<T>(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* primary constructor
|
||||||
|
* @param base_url base url of seafile server
|
||||||
|
*/
|
||||||
|
explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param username username
|
||||||
|
* @param password password
|
||||||
|
* @return token when success else SeafileException
|
||||||
|
*/
|
||||||
|
tl::expected<AuthToken, SeafileException>
|
||||||
|
authToken(const std::string &username, const std::string &password) {
|
||||||
|
const nlohmann::json body = {
|
||||||
|
{"username", username},
|
||||||
|
{"password", password}
|
||||||
|
};
|
||||||
|
return this->post<AuthToken>("/api2/auth-token/", body);
|
||||||
|
}
|
||||||
|
|
||||||
|
tl::expected<ServerInfo, SeafileException>
|
||||||
|
serverInfo() {
|
||||||
|
auto response = cpr::Get(
|
||||||
|
cpr::Url{makeUrl("/api2/server-info/")},
|
||||||
|
cpr::Header{
|
||||||
|
{"accept", "application/json"}
|
||||||
|
});
|
||||||
|
|
||||||
|
return parseResponse<ServerInfo>(response);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //SEADISK_SEAFILE_API_HPP
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// Created by Shell on 2026/6/17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SEADISK_SERVER_INFO_HPP
|
||||||
|
#define SEADISK_SERVER_INFO_HPP
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace seafile {
|
||||||
|
/**
|
||||||
|
* seafile server info
|
||||||
|
*/
|
||||||
|
struct ServerInfo final {
|
||||||
|
/**
|
||||||
|
* server version
|
||||||
|
*/
|
||||||
|
std::string version;
|
||||||
|
/**
|
||||||
|
* encrypted library version
|
||||||
|
*/
|
||||||
|
int encrypted_library_version = 0;
|
||||||
|
/**
|
||||||
|
* supported features
|
||||||
|
*/
|
||||||
|
std::vector<std::string> features;
|
||||||
|
|
||||||
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE(ServerInfo, version, encrypted_library_version, features);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //SEADISK_SERVER_INFO_HPP
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include "seafile/seafile_api.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc,char** argv) {
|
||||||
|
seafile::SeaFileApi api("https://pan.hanwangsoft.com/");
|
||||||
|
if (const auto token = api.authToken("asypost@yeah.net","Shell0805@china"); token.has_value()) {
|
||||||
|
std::cout<<token.value<seafile::AuthToken>().token<<std::endl;
|
||||||
|
}else {
|
||||||
|
std::cout<<token.error().what()<<std::endl;
|
||||||
|
}
|
||||||
|
if (const auto server = api.serverInfo(); server.has_value()) {
|
||||||
|
std::cout<<server.value<seafile::ServerInfo>().version<<std::endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"version-string" : "0.1.0",
|
||||||
|
"name" : "seadisk",
|
||||||
|
"builtin-baseline" : "81e3c888ef717a026eb57bb2f3cb0dbd30246271",
|
||||||
|
"dependencies" : [ {
|
||||||
|
"name" : "argparse",
|
||||||
|
"version>=" : "3.2"
|
||||||
|
}, {
|
||||||
|
"name" : "nlohmann-json",
|
||||||
|
"version>=" : "3.12.0#2"
|
||||||
|
}, {
|
||||||
|
"name" : "cpr",
|
||||||
|
"features" : [ "ssl" ],
|
||||||
|
"version>=" : "1.14.2"
|
||||||
|
}, {
|
||||||
|
"name" : "date",
|
||||||
|
"version>=" : "3.0.4"
|
||||||
|
}, {
|
||||||
|
"name" : "icu",
|
||||||
|
"version>=" : "78.2"
|
||||||
|
}, {
|
||||||
|
"name" : "ada-url",
|
||||||
|
"version>=" : "3.4.4"
|
||||||
|
}, {
|
||||||
|
"name" : "tl-expected",
|
||||||
|
"version>=" : "1.3.1"
|
||||||
|
} ]
|
||||||
|
}
|
||||||
Reference in new issue
Block a user