clean seafile api

This commit is contained in:
shell committed 2026-07-21 22:00:38 +08:00
1 parent 133424f5f8
commit 3e4694ceb5
14 files changed
+449 -263

No files matched your search

+2
View File
@@ -64,6 +64,8 @@ add_executable(seadisk src/main.cpp
src/seafile/fs/seafile_store.cpp src/seafile/fs/seafile_store.cpp
include/seafile/fs/storage/repo_entry.hpp include/seafile/fs/storage/repo_entry.hpp
src/seafile/fs/storage/repo_entry.cpp src/seafile/fs/storage/repo_entry.cpp
include/seafile/util/version.hpp
src/seafile/util/version.cpp
) )
target_compile_definitions(seadisk PRIVATE target_compile_definitions(seadisk PRIVATE
+41 -102
View File
@@ -7,13 +7,11 @@
#include "seafile_api_interface.hpp" #include "seafile_api_interface.hpp"
#include "seafile/cache/cache.hpp" #include "seafile/cache/cache.hpp"
namespace seafile::api namespace seafile::api {
{ class CachedSeafileApi final : public ISeaFileApi {
class CachedSeafileApi final : public ISeaFileApi
{
mutable cache::Cache<std::string, FileInfo> file_info_cache; mutable cache::Cache<std::string, FileInfo> file_info_cache;
mutable cache::Cache<std::string, DirInfo> dir_info_cache; mutable cache::Cache<std::string, DirInfo> dir_info_cache;
mutable cache::Cache<std::string, std::vector<RepoEntry>> repo_entries_cache; mutable cache::Cache<std::string, std::vector<RepoEntry> > repo_entries_cache;
mutable cache::Cache<std::string, AccountInfo> account_info_cache; mutable cache::Cache<std::string, AccountInfo> account_info_cache;
std::shared_ptr<ISeaFileApi> underlayer_api; std::shared_ptr<ISeaFileApi> underlayer_api;
@@ -22,13 +20,11 @@ namespace seafile::api
static std::string makeKey( static std::string makeKey(
std::string_view prefix, std::string_view prefix,
std::initializer_list<std::string_view> args std::initializer_list<std::string_view> args
) ) {
{
std::string key(prefix); std::string key(prefix);
for (auto& arg : args) for (auto &arg: args) {
{ key += ':';
key += ":";
key += arg; key += arg;
} }
@@ -36,66 +32,46 @@ namespace seafile::api
} }
public: public:
explicit CachedSeafileApi(std::shared_ptr<ISeaFileApi> api) : underlayer_api(std::move(api)) explicit CachedSeafileApi(std::shared_ptr<ISeaFileApi> api) : underlayer_api(std::move(api)) {
{
}; };
tl::expected<AuthToken, SeafileException> tl::expected<AuthToken, SeafileException>
authToken( auth(
const std::string& username, const std::string &username,
const std::string& password const std::string &password
) override ) override {
{ return underlayer_api->auth(username, password);
return underlayer_api->authToken(username, password);
} }
tl::expected<ServerInfo, SeafileException> tl::expected<ServerInfo, SeafileException>
serverInfo() const override serverInfo() const override {
{
return underlayer_api->serverInfo(); return underlayer_api->serverInfo();
} }
tl::expected<DefaultRepo, SeafileException> tl::expected<DefaultRepo, SeafileException>
defaultRepo( defaultRepo() const override {
const std::string& token, return underlayer_api->defaultRepo();
TokenType type
) const override
{
return underlayer_api->defaultRepo(token, type);
} }
tl::expected<RepoInfo, SeafileException> tl::expected<RepoInfo, SeafileException>
repoInfo( repoInfo(const std::string &repo_id) const override {
const std::string& repo_id, return underlayer_api->repoInfo(repo_id);
const std::string& token,
TokenType type
) const override
{
return underlayer_api->repoInfo(repo_id, token, type);
} }
tl::expected<std::vector<RepoEntry>, SeafileException> tl::expected<std::vector<RepoEntry>, SeafileException>
repoDir( repoDir(const std::string &repo_id, const std::string &path) const override {
const std::string& repo_id,
const std::string& path,
const std::string& token,
TokenType type
) const override
{
const auto key = makeKey("repoDir", {repo_id, path}); const auto key = makeKey("repoDir", {repo_id, path});
if (const auto value = repo_entries_cache.get(key)) if (const auto value = repo_entries_cache.get(key)) {
{
return *value; return *value;
} }
auto result = underlayer_api->repoDir(repo_id, path, token, type); auto result = underlayer_api->repoDir(repo_id, path);
if (result) if (result) {
{
repo_entries_cache.put(key, result.value(), default_ttl); repo_entries_cache.put(key, result.value(), default_ttl);
} }
return result; return result;
@@ -103,43 +79,22 @@ namespace seafile::api
tl::expected<AccountInfo, SeafileException> tl::expected<AccountInfo, SeafileException>
accountInfo( accountInfo() const override {
const std::string& token, return underlayer_api->accountInfo();
TokenType type
) const override
{
if (const auto info = account_info_cache.get(token); info.has_value())
{
return *info;
}
const auto result = underlayer_api->accountInfo(token, type);
if (result)
{
account_info_cache.put(token, result.value(), default_ttl);
}
return result;
} }
tl::expected<FileInfo, SeafileException> tl::expected<FileInfo, SeafileException>
fileDetail( fileDetail(const std::string &repo_id, const std::string &path) const override {
const std::string& repo_id,
const std::string& path,
const std::string& token,
TokenType type
) const override
{
const auto key = makeKey("file", {repo_id, path}); const auto key = makeKey("file", {repo_id, path});
if (const auto value = file_info_cache.get(key)) if (const auto value = file_info_cache.get(key)) {
{
return *value; return *value;
} }
auto result = underlayer_api->fileDetail(repo_id, path, token, type); auto result = underlayer_api->fileDetail(repo_id, path);
if (result) if (result) {
{
file_info_cache.put(key, result.value(), default_ttl); file_info_cache.put(key, result.value(), default_ttl);
} }
@@ -149,55 +104,39 @@ namespace seafile::api
tl::expected<std::string, SeafileException> tl::expected<std::string, SeafileException>
downloadFileUrl( downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
const std::string& repo_id, return underlayer_api->downloadFileUrl(repo_id, path);
const std::string& path,
const std::string& token,
TokenType type
) const override
{
return underlayer_api->downloadFileUrl(repo_id, path, token, type);
} }
tl::expected<std::string, SeafileException> tl::expected<std::string, SeafileException>
downloadFile( downloadFile(const std::string &repo_id, const std::string &path, std::optional<size_t> start,
const std::string& repo_id, std::optional<size_t> end) const override {
const std::string& path, return underlayer_api->downloadFile(repo_id, path, start, end);
std::optional<size_t> start,
std::optional<size_t> end,
const std::string& token,
TokenType type
) const override
{
return underlayer_api->downloadFile(repo_id, path, start, end, token, type);
} }
tl::expected<DirInfo, SeafileException> tl::expected<DirInfo, SeafileException>
dirDetail( dirDetail(const std::string &repo_id, const std::string &path) const override {
const std::string& repo_id,
const std::string& path,
const std::string& token,
TokenType type
) const override
{
const auto key = makeKey("dir", {repo_id, path}); const auto key = makeKey("dir", {repo_id, path});
if (auto value = dir_info_cache.get(key)) if (auto value = dir_info_cache.get(key)) {
{
return *value; return *value;
} }
auto result = underlayer_api->dirDetail(repo_id, path, token, type); auto result = underlayer_api->dirDetail(repo_id, path);
if (result) if (result) {
{
dir_info_cache.put(key, result.value(), default_ttl); dir_info_cache.put(key, result.value(), default_ttl);
} }
return result; return result;
} }
tl::expected<std::vector<RepoInfo>, SeafileException> repos(const std::string &type,
const std::string &name_contains) const override {
return underlayer_api->repos(type, name_contains);
}
}; };
} }
#endif //SEADISK_CACHED_SEAFILE_API_HPP #endif //SEADISK_CACHED_SEAFILE_API_HPP
+46 -24
View File
@@ -23,11 +23,14 @@
#include "seafile_api_interface.hpp" #include "seafile_api_interface.hpp"
#include "server_info.hpp" #include "server_info.hpp"
#include "token_type.hpp" #include "token_type.hpp"
#include "seafile/util/version.hpp"
namespace seafile::api { namespace seafile::api {
class SeaFileApi final:public ISeaFileApi { class SeaFileApi final : public ISeaFileApi {
//base url of the seafile api server //base url of the seafile api server
std::string base_url; std::string base_url;
std::string token;
TokenType token_type = TokenType::BEARER;
/** /**
* @brief make a url string from given path * @brief make a url string from given path
@@ -105,16 +108,27 @@ namespace seafile::api {
* @return token when success else SeafileException * @return token when success else SeafileException
*/ */
tl::expected<AuthToken, SeafileException> tl::expected<AuthToken, SeafileException>
authToken(const std::string &username, const std::string &password) override { auth(const std::string &username, const std::string &password) override {
const nlohmann::json body = { const nlohmann::json body = {
{"username", username}, {"username", username},
{"password", password} {"password", password}
}; };
return this->post<AuthToken>("/api2/auth-token/", body); const auto &auth_token = this->post<AuthToken>("/api2/auth-token/", body);
if (auth_token) {
this->token = auth_token->token;
if (const auto &server_info = this->serverInfo()) {
if (const auto version = util::Version(server_info->version); version.major() < 11) {
this->token_type = TokenType::TOKEN;
}
} else {
return tl::make_unexpected(server_info.error());
}
}
return auth_token;
} }
tl::expected<ServerInfo, SeafileException> tl::expected<ServerInfo, SeafileException>
serverInfo() const override{ serverInfo() const override {
const auto response = cpr::Get( const auto response = cpr::Get(
cpr::Url{makeUrl("/api2/server-info/")}, cpr::Url{makeUrl("/api2/server-info/")},
cpr::Header{ cpr::Header{
@@ -126,12 +140,10 @@ namespace seafile::api {
/** /**
* @brief get the default repo of user * @brief get the default repo of user
* @param token token
* @param token_type token type
* @return default repo info * @return default repo info
*/ */
tl::expected<DefaultRepo, SeafileException> tl::expected<DefaultRepo, SeafileException>
defaultRepo(const std::string &token, const TokenType token_type) const override{ defaultRepo() const override {
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
const auto url = makeUrl("/api2/default-repo/"); const auto url = makeUrl("/api2/default-repo/");
const auto response = cpr::Get(cpr::Url{url}, const auto response = cpr::Get(cpr::Url{url},
@@ -143,7 +155,7 @@ namespace seafile::api {
} }
tl::expected<RepoInfo, SeafileException> tl::expected<RepoInfo, SeafileException>
repoInfo(const std::string &repo_id, const std::string &token, const TokenType token_type) const override{ repoInfo(const std::string &repo_id) const override {
const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id)); const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id));
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
@@ -155,17 +167,32 @@ namespace seafile::api {
return parseResponse<RepoInfo>(response); return parseResponse<RepoInfo>(response);
} }
tl::expected<std::vector<RepoInfo>, SeafileException>
repos(const std::string &type, const std::string &name_contains) const override {
const auto url = makeUrl("/api2/repos/");
const auto auth = makeAuthToken(token, token_type);
const auto response = cpr::Get(cpr::Url{url},
cpr::Header{
{"accept", "application/json"},
{"authorization", auth}
},
cpr::Parameters{
{"type", type},
{"nameContains", name_contains}
});
return parseResponse<std::vector<RepoInfo> >(response);
}
/** /**
* @brief Lists all the items information in the directory * @brief Lists all the items information in the directory
* @param repo_id repo id * @param repo_id repo id
* @param path path to query * @param path path to query
* @param token token
* @param token_type token type
* @return repo entries * @return repo entries
*/ */
tl::expected<std::vector<RepoEntry>, SeafileException> tl::expected<std::vector<RepoEntry>, SeafileException>
repoDir(const std::string &repo_id, const std::string &path, const std::string &token, repoDir(const std::string &repo_id, const std::string &path) const override {
const TokenType token_type) const override{
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))}; const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))};
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
@@ -181,8 +208,7 @@ namespace seafile::api {
} }
tl::expected<AccountInfo, SeafileException> tl::expected<AccountInfo, SeafileException>
accountInfo(const std::string &token, accountInfo() const override {
const TokenType token_type) const override{
const auto url = cpr::Url{makeUrl("/api2/account/info/")}; const auto url = cpr::Url{makeUrl("/api2/account/info/")};
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
@@ -195,8 +221,7 @@ namespace seafile::api {
} }
tl::expected<FileInfo, SeafileException> tl::expected<FileInfo, SeafileException>
fileDetail(const std::string &repo_id, const std::string &path, const std::string &token, fileDetail(const std::string &repo_id, const std::string &path) const override {
const TokenType token_type) const override{
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))}; const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))};
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
@@ -211,8 +236,7 @@ namespace seafile::api {
} }
tl::expected<std::string, SeafileException> tl::expected<std::string, SeafileException>
downloadFileUrl(const std::string &repo_id, const std::string &path, const std::string &token, downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
const TokenType token_type) const override{
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/", repo_id))}; const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/", repo_id))};
const auto response = cpr::Get(url, const auto response = cpr::Get(url,
@@ -230,10 +254,8 @@ namespace seafile::api {
downloadFile(const std::string &repo_id, downloadFile(const std::string &repo_id,
const std::string &path, const std::string &path,
const std::optional<size_t> start, const std::optional<size_t> start,
const std::optional<size_t> end, const std::optional<size_t> end) const override {
const std::string &token, if (auto download_url = downloadFileUrl(repo_id, path); download_url.has_value()) {
const TokenType token_type) const override{
if (auto download_url = downloadFileUrl(repo_id, path, token, token_type); download_url.has_value()) {
const auto auth = makeAuthToken(token, token_type); const auto auth = makeAuthToken(token, token_type);
const auto url = cpr::Url{download_url.value()}; const auto url = cpr::Url{download_url.value()};
auto header = cpr::Header{ auto header = cpr::Header{
@@ -255,8 +277,8 @@ namespace seafile::api {
} }
} }
tl::expected<DirInfo, SeafileException> dirDetail(const std::string &repo_id, const std::string &path, tl::expected<DirInfo, SeafileException>
const std::string &token, const TokenType token_type) const override{ dirDetail(const std::string &repo_id, const std::string &path) const override {
const auto auth = makeAuthToken(token, token_type); 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 url = cpr::Url{makeUrl(std::format("/api/v2.1/repos/{}/dir/detail/", repo_id))};
const auto response = cpr::Get(url, cpr::Header{ const auto response = cpr::Get(url, cpr::Header{
+24 -42
View File
@@ -23,94 +23,76 @@
namespace seafile::api { namespace seafile::api {
class ISeaFileApi {
class ISeaFileApi { public:
public:
virtual ~ISeaFileApi() = default; virtual ~ISeaFileApi() = default;
virtual tl::expected<AuthToken, SeafileException> virtual tl::expected<AuthToken, SeafileException>
authToken( auth(
const std::string& username, const std::string &username,
const std::string& password const std::string &password
) = 0; ) = 0;
virtual tl::expected<ServerInfo, SeafileException> virtual tl::expected<ServerInfo, SeafileException>
serverInfo() const = 0; serverInfo() const = 0;
virtual tl::expected<DefaultRepo, SeafileException> virtual tl::expected<DefaultRepo, SeafileException>
defaultRepo( defaultRepo() const = 0;
const std::string& token,
TokenType token_type
) const = 0;
virtual tl::expected<RepoInfo, SeafileException> virtual tl::expected<RepoInfo, SeafileException>
repoInfo( repoInfo(
const std::string& repo_id, const std::string &repo_id
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
virtual tl::expected<std::vector<RepoEntry>, SeafileException> virtual tl::expected<std::vector<RepoEntry>, SeafileException>
repoDir( repoDir(
const std::string& repo_id, const std::string &repo_id,
const std::string& path, const std::string &path
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
virtual tl::expected<std::vector<RepoInfo>, SeafileException>
repos(const std::string &type, const std::string &name_contains) const = 0;
virtual tl::expected<AccountInfo, SeafileException> virtual tl::expected<AccountInfo, SeafileException>
accountInfo( accountInfo() const = 0;
const std::string& token,
TokenType token_type
) const = 0;
virtual tl::expected<FileInfo, SeafileException> virtual tl::expected<FileInfo, SeafileException>
fileDetail( fileDetail(
const std::string& repo_id, const std::string &repo_id,
const std::string& path, const std::string &path
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
virtual tl::expected<std::string, SeafileException> virtual tl::expected<std::string, SeafileException>
downloadFileUrl( downloadFileUrl(
const std::string& repo_id, const std::string &repo_id,
const std::string& path, const std::string &path
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
virtual tl::expected<std::string, SeafileException> virtual tl::expected<std::string, SeafileException>
downloadFile( downloadFile(
const std::string& repo_id, const std::string &repo_id,
const std::string& path, const std::string &path,
std::optional<size_t> start, std::optional<size_t> start,
std::optional<size_t> end, std::optional<size_t> end
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
virtual tl::expected<DirInfo, SeafileException> virtual tl::expected<DirInfo, SeafileException>
dirDetail( dirDetail(
const std::string& repo_id, const std::string &repo_id,
const std::string& path, const std::string &path
const std::string& token,
TokenType token_type
) const = 0; ) const = 0;
};
};
} }
#endif //SEADISK_SEAFILE_API_INTERFACE_HPP #endif //SEADISK_SEAFILE_API_INTERFACE_HPP
@@ -23,8 +23,6 @@ namespace seafile::fs
std::string repo_id; std::string repo_id;
std::string token;
TokenType token_type = TokenType::TOKEN;
std::shared_ptr<ISeaFileApi> api; std::shared_ptr<ISeaFileApi> api;
std::wstring volume_label; std::wstring volume_label;
+15 -5
View File
@@ -29,7 +29,7 @@ namespace seafile::fs
[[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const; [[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const;
///初始化数据库信息 ///初始化数据库信息
[[nodiscard]] tl::expected<void, api::SeafileException> init() const; [[nodiscard]] tl::expected<void, api::SeafileException> init();
/// 检查是否已经更新到最新 /// 检查是否已经更新到最新
/// @return true 已经最新,false 需要更新 /// @return true 已经最新,false 需要更新
@@ -39,7 +39,19 @@ namespace seafile::fs
tl::expected<void, api::SeafileException> checkAndSync(const std::string& repo_id); tl::expected<void, api::SeafileException> checkAndSync(const std::string& repo_id);
/// 同步元数据 /// 同步元数据
[[nodiscard]] tl::expected<void, api::SeafileException> sync(const std::string& repo_id) const; [[nodiscard]] tl::expected<void, api::SeafileException> syncRepoEntry(const std::string& repo_id) const;
/// 同步用户信息
tl::expected<void, api::SeafileException> syncUserInfo();
/// 同步repo信息
tl::expected<void, api::SeafileException> syncRepos();
/// 同步
tl::expected<void, api::SeafileException> sync();
std::filesystem::path storePath() const;
/// 获取路径信息 /// 获取路径信息
/// @param path 路径 /// @param path 路径
@@ -51,10 +63,8 @@ namespace seafile::fs
struct Database; struct Database;
std::unique_ptr<Database> database; std::unique_ptr<Database> database;
std::string api_server; std::string api_server;
std::string user; std::string username;
std::string password; std::string password;
std::string token;
api::TokenType token_type = api::TokenType::TOKEN;
api::SeaFileApi api; api::SeaFileApi api;
}; };
} }
+55
View File
@@ -0,0 +1,55 @@
//
// Created by Shell on 2026/7/21.
//
#ifndef SEADISK_VERSION_HPP
#define SEADISK_VERSION_HPP
#include <string>
#include <optional>
namespace seafile::util
{
/**
* @brief Semantic version parser.
*
* Supports:
* 10
* 10.0
* 10.0.2
*/
class Version final
{
public:
Version() = default;
explicit Version(const std::string& version);
[[nodiscard]]
int major() const noexcept;
[[nodiscard]]
std::optional<int> minor() const noexcept;
[[nodiscard]]
std::optional<int> patch() const noexcept;
[[nodiscard]]
std::string toString() const;
bool operator==(const Version& other) const;
bool operator<(const Version& other) const;
private:
void parse(const std::string& version);
private:
int major_{0};
std::optional<int> minor_;
std::optional<int> patch_;
};
}
#endif //SEADISK_VERSION_HPP
+20
View File
@@ -0,0 +1,20 @@
//
// Created by shell on 2025/12/4.
//
#ifndef SEADISK_APPLICATION_PATH_HPP
#define SEADISK_APPLICATION_PATH_HPP
#include <filesystem>
namespace shell::util {
class ApplicationPath final {
public:
/**
*@brief 获取当前程序的路径
* @return 返回当前的程序路径
*/
static std::filesystem::path get();
};
}
#endif //SEADISK_APPLICATION_PATH_HPP
+3 -3
View File
@@ -2,8 +2,8 @@
// Created by Shell on 2026-04-13. // Created by Shell on 2026-04-13.
// //
#ifndef FORCAST_API_ICU_RAII_H #ifndef SEADISK_ICU_RAII_H
#define FORCAST_API_ICU_RAII_H #define SEADISK_ICU_RAII_H
#include <unicode/ucsdet.h> #include <unicode/ucsdet.h>
struct UCharsetDetectorDeleter final struct UCharsetDetectorDeleter final
@@ -19,4 +19,4 @@ struct UCharsetDetectorDeleter final
using UCharsetDetectorPtr = std::unique_ptr<UCharsetDetector, UCharsetDetectorDeleter>; using UCharsetDetectorPtr = std::unique_ptr<UCharsetDetector, UCharsetDetectorDeleter>;
#endif //FORCAST_API_ICU_RAII_H #endif //SEADISK_ICU_RAII_H
+3 -3
View File
@@ -2,8 +2,8 @@
// Created by Shell on 2025-12-17. // Created by Shell on 2025-12-17.
// //
#ifndef FORCAST_API_CLIENT_STRING_UTIL_HPP #ifndef SEADISK_STRING_UTIL_HPP
#define FORCAST_API_CLIENT_STRING_UTIL_HPP #define SEADISK_STRING_UTIL_HPP
#include <string> #include <string>
#include <vector> #include <vector>
@@ -24,4 +24,4 @@ namespace shell::util {
#endif #endif
} }
#endif //FORCAST_API_CLIENT_STRING_UTIL_HPP #endif //SEADISK_STRING_UTIL_HPP
+11 -12
View File
@@ -51,11 +51,11 @@ namespace seafile::fs
tl::expected<void, api::SeafileException> SeafileFileSystem::initialize(const std::string& username, tl::expected<void, api::SeafileException> SeafileFileSystem::initialize(const std::string& username,
const std::string& password) const std::string& password)
{ {
const auto authToken = api->authToken(username, password); const auto authToken = api->auth(username, password);
if (authToken.has_value()) if (authToken.has_value())
{ {
this->token = authToken.value().token; this->token = authToken.value().token;
if (const auto repo = api->defaultRepo(this->token, this->token_type); repo.has_value()) if (const auto repo = api->defaultRepo(); repo.has_value())
{ {
this->repo_id = repo.value().repo_id.value_or(""); this->repo_id = repo.value().repo_id.value_or("");
return {}; return {};
@@ -102,7 +102,7 @@ namespace seafile::fs
NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO* VolumeInfo) const NTSTATUS SeafileFileSystem::GetVolumeInfo(FSP_FSCTL_VOLUME_INFO* VolumeInfo) const
{ {
if (const auto userInfo = api->accountInfo(this->token, this->token_type); userInfo.has_value()) if (const auto userInfo = api->accountInfo(); userInfo.has_value())
{ {
const auto& info = userInfo.value(); const auto& info = userInfo.value();
VolumeInfo->FreeSize = info.total - info.usage; VolumeInfo->FreeSize = info.total - info.usage;
@@ -137,7 +137,7 @@ namespace seafile::fs
if (path == "/") if (path == "/")
{ {
auto repo = api->repoInfo(repo_id, token, token_type); auto repo = api->repoInfo(repo_id);
if (!repo.has_value()) if (!repo.has_value())
return STATUS_OBJECT_NAME_NOT_FOUND; return STATUS_OBJECT_NAME_NOT_FOUND;
const auto ctx = new SeaFileContext{repo->id, path, SeaFileContext::Type::DIRECTORY, 0, ""}; const auto ctx = new SeaFileContext{repo->id, path, SeaFileContext::Type::DIRECTORY, 0, ""};
@@ -147,7 +147,7 @@ namespace seafile::fs
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
if (const auto dirInfo = api->dirDetail(repo_id, path, token, token_type); dirInfo.has_value()) if (const auto dirInfo = api->dirDetail(repo_id, path); dirInfo.has_value())
{ {
const auto& info = dirInfo.value(); const auto& info = dirInfo.value();
const auto ctx = new SeaFileContext{info.repo_id, info.path, SeaFileContext::Type::DIRECTORY, 0, ""}; const auto ctx = new SeaFileContext{info.repo_id, info.path, SeaFileContext::Type::DIRECTORY, 0, ""};
@@ -158,7 +158,7 @@ namespace seafile::fs
} }
if (const auto fileInfo = api->fileDetail(repo_id, path, token, token_type); fileInfo.has_value()) if (const auto fileInfo = api->fileDetail(repo_id, path); fileInfo.has_value())
{ {
const auto& info = fileInfo.value(); const auto& info = fileInfo.value();
const auto ctx = new SeaFileContext(repo_id, path, SeaFileContext::Type::FILE, info.size, ""); const auto ctx = new SeaFileContext(repo_id, path, SeaFileContext::Type::FILE, info.size, "");
@@ -177,8 +177,7 @@ namespace seafile::fs
const auto fc = static_cast<SeaFileContext*>(FileContext); const auto fc = static_cast<SeaFileContext*>(FileContext);
auto result = api->downloadFile( auto result = api->downloadFile(
fc->repo_id, fc->path, fc->repo_id, fc->path,
Offset, Offset + Length - 1, Offset, Offset + Length - 1);
token, token_type);
if (result.has_value()) if (result.has_value())
{ {
const auto& data = result.value(); const auto& data = result.value();
@@ -197,7 +196,7 @@ namespace seafile::fs
const auto fc = static_cast<SeaFileContext*>(FileContext); const auto fc = static_cast<SeaFileContext*>(FileContext);
*PBytesTransferred = 0; *PBytesTransferred = 0;
if (auto entries_result = api->repoDir(fc->repo_id, fc->path, token, token_type); entries_result.has_value()) if (auto entries_result = api->repoDir(fc->repo_id, fc->path); entries_result.has_value())
{ {
const auto& entries = entries_result.value(); const auto& entries = entries_result.value();
size_t start = 0; size_t start = 0;
@@ -266,13 +265,13 @@ namespace seafile::fs
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0); SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
auto repo = api->repoInfo(fc->repo_id, token, token_type); auto repo = api->repoInfo(fc->repo_id);
if (!repo.has_value()) if (!repo.has_value())
return STATUS_UNSUCCESSFUL; return STATUS_UNSUCCESSFUL;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime); SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime);
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
auto fdetail = api->fileDetail(fc->repo_id, fc->path, token, token_type); auto fdetail = api->fileDetail(fc->repo_id, fc->path);
if (!fdetail.has_value()) if (!fdetail.has_value())
return STATUS_UNSUCCESSFUL; return STATUS_UNSUCCESSFUL;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime); SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime);
@@ -311,7 +310,7 @@ namespace seafile::fs
return STATUS_OBJECT_NAME_NOT_FOUND; return STATUS_OBJECT_NAME_NOT_FOUND;
} }
auto entries = api->repoDir(fc->repo_id, fc->path, token, token_type); auto entries = api->repoDir(fc->repo_id, fc->path);
if (!entries.has_value()) if (!entries.has_value())
return STATUS_OBJECT_NAME_NOT_FOUND; return STATUS_OBJECT_NAME_NOT_FOUND;
+66 -70
View File
@@ -7,20 +7,18 @@
#include "seafile/api/repo_info.hpp" #include "seafile/api/repo_info.hpp"
#include "seafile/fs/storage/repo_entry.hpp" #include "seafile/fs/storage/repo_entry.hpp"
#include <stack> #include <stack>
#include "util/application_path.hpp"
// //
// Created by Shell on 2026/7/16. // Created by Shell on 2026/7/16.
// //
namespace seafile::fs namespace seafile::fs {
{
using RepoEntry = storage::RepoEntry; using RepoEntry = storage::RepoEntry;
using AccountInfo = api::AccountInfo; using AccountInfo = api::AccountInfo;
using RepoInfo = api::RepoInfo; using RepoInfo = api::RepoInfo;
namespace namespace {
{ auto make_storage(const std::string &path) {
auto make_storage(const std::string& path)
{
// clang-format off // clang-format off
return sqlite_orm::make_storage(path, return sqlite_orm::make_storage(path,
sqlite_orm::make_table("account_info", sqlite_orm::make_table("account_info",
@@ -30,7 +28,7 @@ namespace seafile::fs
sqlite_orm::make_column("name", &AccountInfo::name), sqlite_orm::make_column("name", &AccountInfo::name),
sqlite_orm::make_column("total", &AccountInfo::total), sqlite_orm::make_column("total", &AccountInfo::total),
sqlite_orm::make_column("usage", &AccountInfo::usage), sqlite_orm::make_column("usage", &AccountInfo::usage),
sqlite_orm::make_column("login_id", &AccountInfo::login_id), sqlite_orm::make_column("login_id", &AccountInfo::login_id,sqlite_orm::primary_key()),
sqlite_orm::make_column("department", &AccountInfo::department), sqlite_orm::make_column("department", &AccountInfo::department),
sqlite_orm::make_column("contact_email", &AccountInfo::contact_email), sqlite_orm::make_column("contact_email", &AccountInfo::contact_email),
sqlite_orm::make_column("institution", &AccountInfo::institution), sqlite_orm::make_column("institution", &AccountInfo::institution),
@@ -79,44 +77,34 @@ namespace seafile::fs
} }
} }
struct SeafileStore::Database struct SeafileStore::Database {
{
using Storage = decltype(make_storage("")); using Storage = decltype(make_storage(""));
Storage storage; Storage storage;
explicit Database(const std::string& path) explicit Database(const std::string &path)
: storage(make_storage(path)) : storage(make_storage(path)) {
{
} }
}; };
tl::expected<void, api::SeafileException> SeafileStore::auth() tl::expected<void, api::SeafileException> SeafileStore::auth() {
{ if (const auto auth_info = this->api.auth(this->username, this->password)) {
if (const auto auth_info = this->api.authToken(this->user, this->password))
{
this->token = auth_info->token;
this->token_type = api::TokenType::TOKEN;
return {}; return {};
} } else {
else
{
return tl::make_unexpected(api::SeafileException(auth_info.error())); return tl::make_unexpected(api::SeafileException(auth_info.error()));
} }
} }
SeafileStore::SeafileStore(const std::string& db_path, const std::string& server, const std::string& user, SeafileStore::SeafileStore(const std::string &db_path, const std::string &server, const std::string &user,
const std::string& password) : api(server) const std::string &password) : api(server) {
{
this->database = std::make_unique<Database>(db_path); this->database = std::make_unique<Database>(db_path);
this->user = user; this->username = user;
this->password = password; this->password = password;
this->api_server = server; this->api_server = server;
} }
tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry( tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry(
const std::string& repo_id, const std::string& path) const std::string &repo_id, const std::string &path) {
{ auto &storage = this->database->storage;
auto& storage = this->database->storage;
const auto entry = storage.get_optional<RepoEntry>( const auto entry = storage.get_optional<RepoEntry>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::and_( sqlite_orm::and_(
@@ -124,20 +112,16 @@ namespace seafile::fs
sqlite_orm::c(&RepoEntry::repo_id) == repo_id sqlite_orm::c(&RepoEntry::repo_id) == repo_id
) )
)); ));
if (entry) if (entry) {
{
return *entry; return *entry;
} }
return tl::make_unexpected(api::SeafileException("entry not exists")); return tl::make_unexpected(api::SeafileException("entry not exists"));
} }
tl::expected<std::string,api::SeafileException> SeafileStore::defaultRepoId() const tl::expected<std::string, api::SeafileException> SeafileStore::defaultRepoId() const {
{ const auto repo = this->api.defaultRepo();
const auto repo = this->api.defaultRepo(this->token,this->token_type); if (repo) {
if (repo) if (repo->exists) {
{
if (repo->exists)
{
return repo->repo_id.value(); return repo->repo_id.value();
} }
return tl::make_unexpected(api::SeafileException("No default repo")); return tl::make_unexpected(api::SeafileException("No default repo"));
@@ -145,78 +129,90 @@ namespace seafile::fs
return tl::make_unexpected(api::SeafileException(repo.error())); return tl::make_unexpected(api::SeafileException(repo.error()));
} }
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id) tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string &repo_id) {
{ auto &storage = this->database->storage;
auto& storage = this->database->storage;
const auto repo = storage.get_optional<RepoInfo>( const auto repo = storage.get_optional<RepoInfo>(
sqlite_orm::where( sqlite_orm::where(
sqlite_orm::c(&RepoInfo::id) == repo_id sqlite_orm::c(&RepoInfo::id) == repo_id
)); ));
if (!repo) if (!repo) {
{
return false; return false;
} }
const auto& commit_id = repo.value().head_commit_id; const auto &commit_id = repo.value().head_commit_id;
const auto repo_info = this->api.repoInfo(repo_id, this->token, this->token_type); const auto repo_info = this->api.repoInfo(repo_id);
if (repo_info) if (repo_info) {
{
return repo_info.value().head_commit_id == commit_id; return repo_info.value().head_commit_id == commit_id;
} }
return tl::make_unexpected(repo_info.error()); return tl::make_unexpected(repo_info.error());
} }
tl::expected<void, api::SeafileException> SeafileStore::sync(const std::string& repo_id) const tl::expected<void, api::SeafileException> SeafileStore::syncRepoEntry(const std::string &repo_id) const {
{
std::stack<RepoEntry> entry_stack; std::stack<RepoEntry> entry_stack;
std::string path = "/"; std::string path = "/";
std::vector<RepoEntry> data; std::vector<RepoEntry> data;
entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path}); entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path});
while (!entry_stack.empty()) while (!entry_stack.empty()) {
{
const auto parent = entry_stack.top(); const auto parent = entry_stack.top();
entry_stack.pop(); entry_stack.pop();
path = parent.path; path = parent.path;
auto entries = this->api.repoDir(repo_id, path, this->token, this->token_type); auto entries = this->api.repoDir(repo_id, path);
if (!entries) if (!entries) {
{
return tl::make_unexpected(entries.error()); return tl::make_unexpected(entries.error());
} }
for (auto& entry : *entries) for (auto &entry: *entries) {
{
const auto entry_info = RepoEntry::from_api_model(repo_id, std::format("{}/{}", path, entry.name), const auto entry_info = RepoEntry::from_api_model(repo_id, std::format("{}/{}", path, entry.name),
entry); entry);
if (entry_info.type == "dir") if (entry_info.type == "dir") {
{
entry_stack.push(entry_info); entry_stack.push(entry_info);
} }
data.push_back(entry_info); data.push_back(entry_info);
} }
}try }
{ try {
this->database->storage.transaction([&] this->database->storage.transaction([&] {
{ for (const auto &entry: data) {
for (const auto& entry : data)
{
this->database->storage.insert(entry); this->database->storage.insert(entry);
} }
return true; return true;
}); });
}catch (std::exception& e) } catch (std::exception &e) {
{
return tl::make_unexpected(api::SeafileException(e.what())); return tl::make_unexpected(api::SeafileException(e.what()));
} }
return {}; return {};
} }
tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string& repo_id) tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string &repo_id) {
{ if (!this->checkIsUpdated(repo_id)) {
if (!this->checkIsUpdated(repo_id)) return this->syncRepoEntry(repo_id);
{
return this->sync(repo_id);
} }
return {}; return {};
} }
tl::expected<void, api::SeafileException> SeafileStore::init() {
if (const auto &store_path = this->storePath(); !std::filesystem::exists(store_path)) {
this->database->storage.sync_schema();
return this->sync();
}
return {};
}
tl::expected<void, api::SeafileException> SeafileStore::syncUserInfo() {
const auto user_info = this->api.accountInfo();
if (!user_info) {
return tl::make_unexpected(user_info.error());
}
this->database->storage.replace(*user_info);
return {};
}
tl::expected<void, api::SeafileException> SeafileStore::syncRepos() {
return {};
}
std::filesystem::path SeafileStore::storePath() const {
return shell::util::ApplicationPath::get() / "seafile.sqlite";
}
} }
+115
View File
@@ -0,0 +1,115 @@
//
// Created by Shell on 2026/7/21.
//
#include <sstream>
#include <vector>
#include <stdexcept>
#include <seafile/util/version.hpp>
namespace seafile::util {
Version::Version(const std::string &version) {
parse(version);
}
int Version::major() const noexcept {
return major_;
}
std::optional<int> Version::minor() const noexcept {
return minor_;
}
std::optional<int> Version::patch() const noexcept {
return patch_;
}
std::string Version::toString() const {
std::string result = std::to_string(major_);
if (minor_) {
result += "." + std::to_string(*minor_);
}
if (patch_) {
result += "." + std::to_string(*patch_);
}
return result;
}
bool Version::operator==(const Version &other) const {
return major_ == other.major_
&& minor().value_or(0) == other.minor().value_or(0)
&& patch().value_or(0) == other.patch().value_or(0);
}
bool Version::operator<(const Version &other) const {
if (major_ != other.major_) {
return major_ < other.major_;
}
if (minor().value_or(0) != other.minor().value_or(0)) {
return minor().value_or(0) < other.minor().value_or(0);
}
return patch().value_or(0) < other.patch().value_or(0);
}
void Version::parse(const std::string &version) {
std::stringstream ss(version);
std::string part;
std::vector<int> parts;
while (std::getline(ss, part, '.')) {
if (part.empty()) {
throw std::invalid_argument(
"Invalid version: " + version
);
}
try {
parts.push_back(std::stoi(part));
} catch (...) {
throw std::invalid_argument(
"Invalid version number: " + part
);
}
}
if (parts.empty()) {
throw std::invalid_argument(
"Version cannot be empty"
);
}
if (parts.size() > 3) {
throw std::invalid_argument(
"Version supports at most 3 components"
);
}
major_ = parts[0];
if (parts.size() >= 2) {
minor_ = parts[1];
}
if (parts.size() >= 3) {
patch_ = parts[2];
}
}
}
+48
View File
@@ -0,0 +1,48 @@
//
// Created by shell on 2025/12/4.
//
#include "util/application_path.hpp"
#include <stdexcept>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <unistd.h>
#else
#error "Getting application path operation is only supported on Windows or Linux"
#endif
namespace shell::util {
#ifdef _WIN32
std::filesystem::path ApplicationPath::get() {
std::wstring path(20, L'\0');
while (true) {
const DWORD len = GetModuleFileNameW(nullptr, &path[0], path.size());
if (len == 0) {
throw std::runtime_error("GetModuleFileName() failed");
}
if (len < path.size()) {
path.resize(len);
return std::filesystem::path{path};
}
path.resize(path.size() * 2);
}
}
#elif __linux__
std::filesystem::path ApplicationPath::get() {
std::vector<char> buffer(256);
while (true) {
const size_t len = readlink("/proc/self/exe", buffer.data(), buffer.size());
if (len == -1) {
throw std::runtime_error("failed to read /proc/self/exe");
}
if (len < buffer.size()) {
return std::filesystem::path{std::string(buffer.data(), len)};
}
buffer.reserve(buffer.size() * 2);
}
}
#endif
}