#include "seafile/fs/seafile_store.hpp" #include #include #include "seafile/api/account_info.hpp" #include "seafile/api/repo_info.hpp" #include "seafile/fs/storage/repo_entry.hpp" #include #include "util/application_path.hpp" // // Created by Shell on 2026/7/16. // namespace seafile::fs { using RepoEntry = storage::RepoEntry; using AccountInfo = api::AccountInfo; using RepoInfo = api::RepoInfo; namespace { auto make_storage(const std::string &path) { // clang-format off return sqlite_orm::make_storage(path, sqlite_orm::make_table("account_info", sqlite_orm::make_column("space_usage", &AccountInfo::space_usage), sqlite_orm::make_column("avatar_url", &AccountInfo::avatar_url), sqlite_orm::make_column("email", &AccountInfo::email), 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::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), sqlite_orm::make_column("is_staff", &AccountInfo::is_staff), sqlite_orm::make_column("file_updates_email_interval", &AccountInfo::file_updates_email_interval), sqlite_orm::make_column("collaborate_email_interval",&AccountInfo::collaborate_email_interval) ), sqlite_orm::make_table("repo_info", sqlite_orm::make_column("type",&RepoInfo::type), sqlite_orm::make_column("id",&RepoInfo::id), sqlite_orm::make_column("owner",&RepoInfo::owner), sqlite_orm::make_column("name",&RepoInfo::name), sqlite_orm::make_column("mtime",&RepoInfo::mtime), sqlite_orm::make_column("size",&RepoInfo::size), sqlite_orm::make_column("encrypted",&RepoInfo::encrypted), sqlite_orm::make_column("root",&RepoInfo::root), sqlite_orm::make_column("permission",&RepoInfo::permission), sqlite_orm::make_column("modifier_email",&RepoInfo::modifier_email), sqlite_orm::make_column("modifier_contact_email",&RepoInfo::modifier_contact_email), sqlite_orm::make_column("modifier_name",&RepoInfo::modifier_name), sqlite_orm::make_column("file_count",&RepoInfo::file_count), sqlite_orm::make_column("head_commit_id",&RepoInfo::head_commit_id), sqlite_orm::make_column("enc_version",&RepoInfo::enc_version), sqlite_orm::make_column("salt",&RepoInfo::salt), sqlite_orm::make_column("magic",&RepoInfo::magic), sqlite_orm::make_column("random_key",&RepoInfo::random_key) ), sqlite_orm::make_table("repo_entry", sqlite_orm::make_column("type",&RepoEntry::type), sqlite_orm::make_column("name",&RepoEntry::name), sqlite_orm::make_column("path",&RepoEntry::path), sqlite_orm::make_column("id",&RepoEntry::id), sqlite_orm::make_column("mtime",&RepoEntry::mtime), sqlite_orm::make_column("modifier_email",&RepoEntry::modifier_email), sqlite_orm::make_column("size",&RepoEntry::size), sqlite_orm::make_column("is_locked",&RepoEntry::is_locked), sqlite_orm::make_column("lock_owner",&RepoEntry::lock_owner), sqlite_orm::make_column("lock_time",&RepoEntry::lock_time), sqlite_orm::make_column("locked_by_me",&RepoEntry::locked_by_me), sqlite_orm::make_column("permission",&RepoEntry::permission), sqlite_orm::make_column("modifier_contact_email",&RepoEntry::modifier_contact_email), sqlite_orm::make_column("modifier_name",&RepoEntry::modifier_name), sqlite_orm::make_column("starred",&RepoEntry::starred)) ); //clang-format on } } struct SeafileStore::Database { using Storage = decltype(make_storage("")); Storage storage; explicit Database(const std::string &path) : storage(make_storage(path)) { } }; tl::expected SeafileStore::auth() { if (const auto auth_info = this->api.auth(this->username, this->password)) { return {}; } 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) { this->database = std::make_unique(db_path); this->username = user; this->password = password; this->api_server = server; } tl::expected SeafileStore::findRepoEntry( const std::string &repo_id, const std::string &path) { auto &storage = this->database->storage; const auto entry = storage.get_optional( sqlite_orm::where( sqlite_orm::and_( sqlite_orm::c(&RepoEntry::path) == path, sqlite_orm::c(&RepoEntry::repo_id) == repo_id ) )); if (entry) { return *entry; } return tl::make_unexpected(api::SeafileException("entry not exists")); } tl::expected 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")); } return tl::make_unexpected(api::SeafileException(repo.error())); } tl::expected SeafileStore::checkIsUpdated(const std::string &repo_id) { auto &storage = this->database->storage; const auto repo = storage.get_optional( sqlite_orm::where( sqlite_orm::c(&RepoInfo::id) == repo_id )); if (!repo) { return false; } 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 SeafileStore::syncRepoEntry(const std::string &repo_id) const { std::stack entry_stack; std::string path = "/"; std::vector data; entry_stack.push(RepoEntry{.repo_id = repo_id, .path = path}); while (!entry_stack.empty()) { const auto parent = entry_stack.top(); entry_stack.pop(); path = parent.path; auto entries = this->api.repoDir(repo_id, path); if (!entries) { return tl::make_unexpected(entries.error()); } 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") { entry_stack.push(entry_info); } data.push_back(entry_info); } } try { this->database->storage.transaction([&] { for (const auto &entry: data) { this->database->storage.insert(entry); } return true; }); } catch (std::exception &e) { return tl::make_unexpected(api::SeafileException(e.what())); } return {}; } tl::expected SeafileStore::checkAndSync(const std::string &repo_id) { if (!this->checkIsUpdated(repo_id)) { return this->syncRepoEntry(repo_id); } return {}; } tl::expected 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 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 SeafileStore::syncRepos() { return {}; } std::filesystem::path SeafileStore::storePath() const { return shell::util::ApplicationPath::get() / "seafile.sqlite"; } }