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
+476 -290

No files matched your search

+11 -12
View File
@@ -51,11 +51,11 @@ namespace seafile::fs
tl::expected<void, api::SeafileException> SeafileFileSystem::initialize(const std::string& username,
const std::string& password)
{
const auto authToken = api->authToken(username, password);
const auto authToken = api->auth(username, password);
if (authToken.has_value())
{
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("");
return {};
@@ -102,7 +102,7 @@ namespace seafile::fs
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();
VolumeInfo->FreeSize = info.total - info.usage;
@@ -137,7 +137,7 @@ namespace seafile::fs
if (path == "/")
{
auto repo = api->repoInfo(repo_id, token, token_type);
auto repo = api->repoInfo(repo_id);
if (!repo.has_value())
return STATUS_OBJECT_NAME_NOT_FOUND;
const auto ctx = new SeaFileContext{repo->id, path, SeaFileContext::Type::DIRECTORY, 0, ""};
@@ -147,7 +147,7 @@ namespace seafile::fs
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 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 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);
auto result = api->downloadFile(
fc->repo_id, fc->path,
Offset, Offset + Length - 1,
token, token_type);
Offset, Offset + Length - 1);
if (result.has_value())
{
const auto& data = result.value();
@@ -197,7 +196,7 @@ namespace seafile::fs
const auto fc = static_cast<SeaFileContext*>(FileContext);
*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();
size_t start = 0;
@@ -266,13 +265,13 @@ namespace seafile::fs
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, 0, 0);
return STATUS_SUCCESS;
}
auto repo = api->repoInfo(fc->repo_id, token, token_type);
auto repo = api->repoInfo(fc->repo_id);
if (!repo.has_value())
return STATUS_UNSUCCESSFUL;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_DIRECTORY, repo->size, repo->mtime);
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())
return STATUS_UNSUCCESSFUL;
SetFileInfo(FileInfo, FILE_ATTRIBUTE_NORMAL, fdetail->size, fdetail->mtime);
@@ -311,7 +310,7 @@ namespace seafile::fs
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())
return STATUS_OBJECT_NAME_NOT_FOUND;
+66 -70
View File
@@ -7,20 +7,18 @@
#include "seafile/api/repo_info.hpp"
#include "seafile/fs/storage/repo_entry.hpp"
#include <stack>
#include "util/application_path.hpp"
//
// Created by Shell on 2026/7/16.
//
namespace seafile::fs
{
namespace seafile::fs {
using RepoEntry = storage::RepoEntry;
using AccountInfo = api::AccountInfo;
using RepoInfo = api::RepoInfo;
namespace
{
auto make_storage(const std::string& path)
{
namespace {
auto make_storage(const std::string &path) {
// clang-format off
return sqlite_orm::make_storage(path,
sqlite_orm::make_table("account_info",
@@ -30,7 +28,7 @@ namespace seafile::fs
sqlite_orm::make_column("name", &AccountInfo::name),
sqlite_orm::make_column("total", &AccountInfo::total),
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("contact_email", &AccountInfo::contact_email),
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(""));
Storage storage;
explicit Database(const std::string& path)
: storage(make_storage(path))
{
explicit Database(const std::string &path)
: storage(make_storage(path)) {
}
};
tl::expected<void, api::SeafileException> SeafileStore::auth()
{
if (const auto auth_info = this->api.authToken(this->user, this->password))
{
this->token = auth_info->token;
this->token_type = api::TokenType::TOKEN;
tl::expected<void, api::SeafileException> SeafileStore::auth() {
if (const auto auth_info = this->api.auth(this->username, this->password)) {
return {};
}
else
{
} else {
return tl::make_unexpected(api::SeafileException(auth_info.error()));
}
}
SeafileStore::SeafileStore(const std::string& db_path, const std::string& server, const std::string& user,
const std::string& password) : api(server)
{
SeafileStore::SeafileStore(const std::string &db_path, const std::string &server, const std::string &user,
const std::string &password) : api(server) {
this->database = std::make_unique<Database>(db_path);
this->user = user;
this->username = user;
this->password = password;
this->api_server = server;
}
tl::expected<RepoEntry, api::SeafileException> SeafileStore::findRepoEntry(
const std::string& repo_id, const std::string& path)
{
auto& storage = this->database->storage;
const std::string &repo_id, const std::string &path) {
auto &storage = this->database->storage;
const auto entry = storage.get_optional<RepoEntry>(
sqlite_orm::where(
sqlite_orm::and_(
@@ -124,20 +112,16 @@ namespace seafile::fs
sqlite_orm::c(&RepoEntry::repo_id) == repo_id
)
));
if (entry)
{
if (entry) {
return *entry;
}
return tl::make_unexpected(api::SeafileException("entry not exists"));
}
tl::expected<std::string,api::SeafileException> SeafileStore::defaultRepoId() const
{
const auto repo = this->api.defaultRepo(this->token,this->token_type);
if (repo)
{
if (repo->exists)
{
tl::expected<std::string, api::SeafileException> SeafileStore::defaultRepoId() const {
const auto repo = this->api.defaultRepo();
if (repo) {
if (repo->exists) {
return repo->repo_id.value();
}
return tl::make_unexpected(api::SeafileException("No default repo"));
@@ -145,78 +129,90 @@ namespace seafile::fs
return tl::make_unexpected(api::SeafileException(repo.error()));
}
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string& repo_id)
{
auto& storage = this->database->storage;
tl::expected<bool, api::SeafileException> SeafileStore::checkIsUpdated(const std::string &repo_id) {
auto &storage = this->database->storage;
const auto repo = storage.get_optional<RepoInfo>(
sqlite_orm::where(
sqlite_orm::c(&RepoInfo::id) == repo_id
));
if (!repo)
{
if (!repo) {
return false;
}
const auto& commit_id = repo.value().head_commit_id;
const auto repo_info = this->api.repoInfo(repo_id, this->token, this->token_type);
if (repo_info)
{
const auto &commit_id = repo.value().head_commit_id;
const auto repo_info = this->api.repoInfo(repo_id);
if (repo_info) {
return repo_info.value().head_commit_id == commit_id;
}
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::string path = "/";
std::vector<RepoEntry> data;
entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path});
while (!entry_stack.empty())
{
while (!entry_stack.empty()) {
const auto parent = entry_stack.top();
entry_stack.pop();
path = parent.path;
auto entries = this->api.repoDir(repo_id, path, this->token, this->token_type);
if (!entries)
{
auto entries = this->api.repoDir(repo_id, path);
if (!entries) {
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),
entry);
if (entry_info.type == "dir")
{
if (entry_info.type == "dir") {
entry_stack.push(entry_info);
}
data.push_back(entry_info);
}
}try
{
this->database->storage.transaction([&]
{
for (const auto& entry : data)
{
}
try {
this->database->storage.transaction([&] {
for (const auto &entry: data) {
this->database->storage.insert(entry);
}
return true;
});
}catch (std::exception& e)
{
} catch (std::exception &e) {
return tl::make_unexpected(api::SeafileException(e.what()));
}
return {};
}
tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string& repo_id)
{
if (!this->checkIsUpdated(repo_id))
{
return this->sync(repo_id);
tl::expected<void, api::SeafileException> SeafileStore::checkAndSync(const std::string &repo_id) {
if (!this->checkIsUpdated(repo_id)) {
return this->syncRepoEntry(repo_id);
}
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];
}
}
}