From 4d7dde104d6a7b9b7458dd34ff8cb152af3b0ea2 Mon Sep 17 00:00:00 2001 From: shell Date: Sun, 12 Jul 2026 22:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=BB=84=E7=BB=87=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 22 ++--- include/seafile/{ => api}/account_info.hpp | 0 include/seafile/{ => api}/auth_token.hpp | 2 + include/seafile/{ => api}/default_repo.hpp | 0 include/seafile/{ => api}/dir_info.hpp | 0 include/seafile/{ => api}/exception.hpp | 0 include/seafile/{ => api}/file_info.hpp | 0 include/seafile/{ => api}/repo_entry.hpp | 1 - include/seafile/{ => api}/repo_info.hpp | 0 include/seafile/{ => api}/seafile_api.hpp | 107 ++++++++------------- include/seafile/{ => api}/server_info.hpp | 0 include/seafile/{ => api}/token_type.hpp | 0 include/seafile/fs/seafile_file_system.hpp | 4 +- src/main.cpp | 2 +- src/seafile/fs/seafile_file_system.cpp | 4 +- 15 files changed, 59 insertions(+), 83 deletions(-) rename include/seafile/{ => api}/account_info.hpp (100%) rename include/seafile/{ => api}/auth_token.hpp (81%) rename include/seafile/{ => api}/default_repo.hpp (100%) rename include/seafile/{ => api}/dir_info.hpp (100%) rename include/seafile/{ => api}/exception.hpp (100%) rename include/seafile/{ => api}/file_info.hpp (100%) rename include/seafile/{ => api}/repo_entry.hpp (98%) rename include/seafile/{ => api}/repo_info.hpp (100%) rename include/seafile/{ => api}/seafile_api.hpp (77%) rename include/seafile/{ => api}/server_info.hpp (100%) rename include/seafile/{ => api}/token_type.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb76b26..e2d7af8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,17 +37,17 @@ 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 - include/seafile/default_repo.hpp - include/seafile/token_type.hpp - include/seafile/repo_info.hpp - include/seafile/repo_entry.hpp - include/seafile/account_info.hpp - include/seafile/file_info.hpp - include/seafile/dir_info.hpp + include/seafile/api/seafile_api.hpp + include/seafile/api/exception.hpp + include/seafile/api/server_info.hpp + include/seafile/api/auth_token.hpp + include/seafile/api/default_repo.hpp + include/seafile/api/token_type.hpp + include/seafile/api/repo_info.hpp + include/seafile/api/repo_entry.hpp + include/seafile/api/account_info.hpp + include/seafile/api/file_info.hpp + include/seafile/api/dir_info.hpp include/seafile/fs/file_context.hpp include/seafile/fs/seafile_file_system.hpp src/seafile/fs/seafile_file_system.cpp diff --git a/include/seafile/account_info.hpp b/include/seafile/api/account_info.hpp similarity index 100% rename from include/seafile/account_info.hpp rename to include/seafile/api/account_info.hpp diff --git a/include/seafile/auth_token.hpp b/include/seafile/api/auth_token.hpp similarity index 81% rename from include/seafile/auth_token.hpp rename to include/seafile/api/auth_token.hpp index a756efe..3857e41 100644 --- a/include/seafile/auth_token.hpp +++ b/include/seafile/api/auth_token.hpp @@ -4,6 +4,8 @@ #ifndef SEADISK_AUTH_TOKEN_HPP #define SEADISK_AUTH_TOKEN_HPP +#include +#include namespace seafile { struct AuthToken final { diff --git a/include/seafile/default_repo.hpp b/include/seafile/api/default_repo.hpp similarity index 100% rename from include/seafile/default_repo.hpp rename to include/seafile/api/default_repo.hpp diff --git a/include/seafile/dir_info.hpp b/include/seafile/api/dir_info.hpp similarity index 100% rename from include/seafile/dir_info.hpp rename to include/seafile/api/dir_info.hpp diff --git a/include/seafile/exception.hpp b/include/seafile/api/exception.hpp similarity index 100% rename from include/seafile/exception.hpp rename to include/seafile/api/exception.hpp diff --git a/include/seafile/file_info.hpp b/include/seafile/api/file_info.hpp similarity index 100% rename from include/seafile/file_info.hpp rename to include/seafile/api/file_info.hpp diff --git a/include/seafile/repo_entry.hpp b/include/seafile/api/repo_entry.hpp similarity index 98% rename from include/seafile/repo_entry.hpp rename to include/seafile/api/repo_entry.hpp index 33f3223..0d4e605 100644 --- a/include/seafile/repo_entry.hpp +++ b/include/seafile/api/repo_entry.hpp @@ -7,7 +7,6 @@ #include #include #include -#include namespace seafile { diff --git a/include/seafile/repo_info.hpp b/include/seafile/api/repo_info.hpp similarity index 100% rename from include/seafile/repo_info.hpp rename to include/seafile/api/repo_info.hpp diff --git a/include/seafile/seafile_api.hpp b/include/seafile/api/seafile_api.hpp similarity index 77% rename from include/seafile/seafile_api.hpp rename to include/seafile/api/seafile_api.hpp index 505fbd0..371c6a9 100644 --- a/include/seafile/seafile_api.hpp +++ b/include/seafile/api/seafile_api.hpp @@ -23,10 +23,8 @@ #include "server_info.hpp" #include "token_type.hpp" -namespace seafile -{ - class SeaFileApi final - { +namespace seafile { + class SeaFileApi final { //base url of the seafile api server std::string base_url; @@ -35,46 +33,38 @@ namespace seafile * @param path url path * @return the whole url string */ - [[nodiscard]] std::string makeUrl(const std::string& path) const - { + [[nodiscard]] std::string makeUrl(const std::string &path) const { auto url = ada::parse(this->base_url); url->set_pathname(path); return std::string(url->get_href()); } - template + template static tl::expected - parseResponse(const cpr::Response& response) - { - if (response.error) - { + parseResponse(const cpr::Response &response) { + if (response.error) { return tl::make_unexpected( SeafileException( response.error.message)); } - if (response.status_code != 200 && response.status_code != 206) - { + if (response.status_code != 200 && response.status_code != 206) { return tl::make_unexpected( SeafileException( std::format("Http Error: {}", response.status_code))); } - try - { + try { return nlohmann::json::parse(response.text) - .get(); - } - catch (const std::exception& e) - { + .get(); + } catch (const std::exception &e) { return tl::make_unexpected( SeafileException(e.what())); } } - static std::string makeAuthToken(const std::string& token, const TokenType& token_type) - { + static std::string makeAuthToken(const std::string &token, const TokenType &token_type) { return token_type == TokenType::BEARER ? std::format("Bearer {}", token) : std::format("Token {}", token); @@ -87,9 +77,8 @@ namespace seafile * @param body post body data * @return T if success else SeafileException */ - template - tl::expected post(const std::string& path, const nlohmann::json& body) - { + template + tl::expected post(const std::string &path, const nlohmann::json &body) { const auto url = makeUrl(path); const auto response = cpr::Post( cpr::Url{url}, @@ -105,8 +94,7 @@ namespace seafile * @brief primary constructor * @param base_url base url of seafile server */ - explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url)) - { + explicit SeaFileApi(std::string base_url) : base_url(std::move(base_url)) { } /** @@ -116,8 +104,7 @@ namespace seafile * @return token when success else SeafileException */ tl::expected - authToken(const std::string& username, const std::string& password) - { + authToken(const std::string &username, const std::string &password) { const nlohmann::json body = { {"username", username}, {"password", password} @@ -126,8 +113,7 @@ namespace seafile } tl::expected - serverInfo() const - { + serverInfo() const { const auto response = cpr::Get( cpr::Url{makeUrl("/api2/server-info/")}, cpr::Header{ @@ -144,8 +130,7 @@ namespace seafile * @return default repo info */ tl::expected - defaultRepo(const std::string& token, const TokenType token_type) const - { + defaultRepo(const std::string &token, const TokenType token_type) const { const auto auth = makeAuthToken(token, token_type); const auto url = makeUrl("/api2/default-repo/"); const auto response = cpr::Get(cpr::Url{url}, @@ -157,8 +142,7 @@ namespace seafile } tl::expected - repoInfo(const std::string& repo_id, const std::string& token, const TokenType token_type) const - { + repoInfo(const std::string &repo_id, const std::string &token, const TokenType token_type) const { const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id)); const auto auth = makeAuthToken(token, token_type); @@ -179,9 +163,8 @@ namespace seafile * @return repo entries */ tl::expected, SeafileException> - repoDir(const std::string& repo_id, const std::string& path, const std::string& token, - const TokenType token_type) const - { + repoDir(const std::string &repo_id, const std::string &path, const std::string &token, + const TokenType token_type) const { const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))}; const auto auth = makeAuthToken(token, token_type); @@ -193,13 +176,12 @@ namespace seafile cpr::Parameters{ {"p", path} }); - return parseResponse>(response); + return parseResponse >(response); } tl::expected - accountInfo(const std::string& token, - const TokenType token_type) const - { + accountInfo(const std::string &token, + const TokenType token_type) const { const auto url = cpr::Url{makeUrl("/api2/account/info/")}; const auto auth = makeAuthToken(token, token_type); @@ -212,9 +194,8 @@ namespace seafile } tl::expected - fileDetail(const std::string& repo_id, const std::string& path, const std::string& token, - const TokenType token_type) const - { + fileDetail(const std::string &repo_id, const std::string &path, const std::string &token, + const TokenType token_type) const { const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))}; const auto auth = makeAuthToken(token, token_type); @@ -229,9 +210,8 @@ namespace seafile } tl::expected - downloadFileUrl(const std::string& repo_id, const std::string& path, const std::string& token, - const TokenType token_type) const - { + downloadFileUrl(const std::string &repo_id, const std::string &path, const std::string &token, + const TokenType token_type) const { const auto auth = makeAuthToken(token, token_type); const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/", repo_id))}; const auto response = cpr::Get(url, @@ -246,15 +226,13 @@ namespace seafile tl::expected - downloadFile(const std::string& repo_id, - const std::string& path, + downloadFile(const std::string &repo_id, + const std::string &path, const std::optional start, const std::optional end, - const std::string& token, - const TokenType token_type) const - { - if (auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value()) - { + const std::string &token, + const TokenType token_type) const { + if (auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value()) { const auto auth = makeAuthToken(token, token_type); const auto url = cpr::Url{download_url.value()}; auto header = cpr::Header{ @@ -265,22 +243,19 @@ namespace seafile start ? std::to_string(*start) : "", end ? std::to_string(*end) : ""); header.emplace("range", range); - const auto& response = cpr::Get(url, header); - if (response.status_code != 200 && response.status_code != 206) { - return tl::make_unexpected(SeafileException{std::format("HTTP Error:{}", response.status_code)}); - }else { - return response.text; - } - } - else - { + if (const auto &response = cpr::Get(url, header); + response.error || (response.status_code != 200 && response.status_code != 206)) { + return tl::make_unexpected(SeafileException{std::format("HTTP Error:{}", response.status_code)}); + } else { + return response.text; + } + } else { return download_url; } } - tl::expected dirDetail(const std::string& repo_id, const std::string& path, - const std::string& token, const TokenType token_type) const - { + tl::expected dirDetail(const std::string &repo_id, const std::string &path, + const std::string &token, const TokenType token_type) const { const auto auth = makeAuthToken(token, token_type); const auto url = cpr::Url{makeUrl(std::format("/api/v2.1/repos/{}/dir/detail/", repo_id))}; const auto response = cpr::Get(url, cpr::Header{ diff --git a/include/seafile/server_info.hpp b/include/seafile/api/server_info.hpp similarity index 100% rename from include/seafile/server_info.hpp rename to include/seafile/api/server_info.hpp diff --git a/include/seafile/token_type.hpp b/include/seafile/api/token_type.hpp similarity index 100% rename from include/seafile/token_type.hpp rename to include/seafile/api/token_type.hpp diff --git a/include/seafile/fs/seafile_file_system.hpp b/include/seafile/fs/seafile_file_system.hpp index e0f7c67..7bfdea5 100644 --- a/include/seafile/fs/seafile_file_system.hpp +++ b/include/seafile/fs/seafile_file_system.hpp @@ -7,8 +7,8 @@ #include #include -#include "seafile/seafile_api.hpp" -#include "seafile/token_type.hpp" +#include "../api/seafile_api.hpp" +#include "../api/token_type.hpp" namespace seafile::fs { diff --git a/src/main.cpp b/src/main.cpp index 981cef0..2493ad4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include #include "seafile/fs/winfsp_adapter.hpp" -#include "seafile/seafile_api.hpp" +#include "seafile/api/seafile_api.hpp" using namespace seafile::fs; diff --git a/src/seafile/fs/seafile_file_system.cpp b/src/seafile/fs/seafile_file_system.cpp index f498c72..10fd80e 100644 --- a/src/seafile/fs/seafile_file_system.cpp +++ b/src/seafile/fs/seafile_file_system.cpp @@ -9,7 +9,7 @@ namespace seafile::fs { constexpr int64_t UNIX_EPOCH_AS_FILETIME = 116444736000000000LL; constexpr int64_t HUNDREDS_OF_NANOSECONDS_PER_SECOND = 10000000LL; - int64_t UnixTimeToFileTime(int64_t unix_time) { + int64_t UnixTimeToFileTime(const int64_t unix_time) { return UNIX_EPOCH_AS_FILETIME + unix_time * HUNDREDS_OF_NANOSECONDS_PER_SECOND; } @@ -26,7 +26,7 @@ namespace seafile::fs { return (static_cast(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; } - void SetFileInfo(FSP_FSCTL_FILE_INFO *FileInfo, uint32_t attributes, uint64_t size, int64_t mtime) { + void SetFileInfo(FSP_FSCTL_FILE_INFO *FileInfo, const uint32_t attributes, const uint64_t size,const int64_t mtime) { FileInfo->FileAttributes = attributes; FileInfo->AllocationSize = size; FileInfo->FileSize = size;