初步实现功能
This commit is contained in:
1 parent
f23c6a3fbb
commit
f0b6efb126
15 files changed
+783
-708
No files matched your search
@@ -1,142 +0,0 @@
|
||||
//
|
||||
// Created by Shell on 2026-07-14.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_CACHED_SEAFILE_API_HPP
|
||||
#define SEADISK_CACHED_SEAFILE_API_HPP
|
||||
#include "seafile_api_interface.hpp"
|
||||
#include "seafile/cache/cache.hpp"
|
||||
|
||||
namespace seafile::api {
|
||||
class CachedSeafileApi final : public ISeaFileApi {
|
||||
mutable cache::Cache<std::string, FileInfo> file_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, AccountInfo> account_info_cache;
|
||||
|
||||
std::shared_ptr<ISeaFileApi> underlayer_api;
|
||||
std::chrono::seconds default_ttl = std::chrono::seconds(300);
|
||||
|
||||
static std::string makeKey(
|
||||
std::string_view prefix,
|
||||
std::initializer_list<std::string_view> args
|
||||
) {
|
||||
std::string key(prefix);
|
||||
|
||||
for (auto &arg: args) {
|
||||
key += ':';
|
||||
key += arg;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit CachedSeafileApi(std::shared_ptr<ISeaFileApi> api) : underlayer_api(std::move(api)) {
|
||||
};
|
||||
|
||||
tl::expected<AuthToken, SeafileException>
|
||||
auth(
|
||||
const std::string &username,
|
||||
const std::string &password
|
||||
) override {
|
||||
return underlayer_api->auth(username, password);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const override {
|
||||
return underlayer_api->serverInfo();
|
||||
}
|
||||
|
||||
|
||||
tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo() const override {
|
||||
return underlayer_api->defaultRepo();
|
||||
}
|
||||
|
||||
|
||||
tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(const std::string &repo_id) const override {
|
||||
return underlayer_api->repoInfo(repo_id);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("repoDir", {repo_id, path});
|
||||
|
||||
if (const auto value = repo_entries_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->repoDir(repo_id, path);
|
||||
if (result) {
|
||||
repo_entries_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo() const override {
|
||||
return underlayer_api->accountInfo();
|
||||
}
|
||||
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("file", {repo_id, path});
|
||||
|
||||
if (const auto value = file_info_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->fileDetail(repo_id, path);
|
||||
|
||||
if (result) {
|
||||
file_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
|
||||
return underlayer_api->downloadFileUrl(repo_id, path);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFile(const std::string &repo_id, const std::string &path, std::optional<size_t> start,
|
||||
std::optional<size_t> end) const override {
|
||||
return underlayer_api->downloadFile(repo_id, path, start, end);
|
||||
}
|
||||
|
||||
|
||||
tl::expected<DirInfo, SeafileException>
|
||||
dirDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
const auto key = makeKey("dir", {repo_id, path});
|
||||
|
||||
if (auto value = dir_info_cache.get(key)) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
auto result = underlayer_api->dirDetail(repo_id, path);
|
||||
|
||||
if (result) {
|
||||
dir_info_cache.put(key, result.value(), default_ttl);
|
||||
}
|
||||
|
||||
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
|
||||
@@ -268,7 +268,9 @@ namespace seafile::api {
|
||||
header.emplace("range", range);
|
||||
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)});
|
||||
return tl::make_unexpected(SeafileException{
|
||||
std::format("Download bytes range {} failed, HTTP Error:{}", range, response.status_code)
|
||||
});
|
||||
} else {
|
||||
return response.text;
|
||||
}
|
||||
|
||||
Vendored
-112
@@ -1,112 +0,0 @@
|
||||
//
|
||||
// Created by Shell on 2026/7/13.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_CACHE_HPP
|
||||
#define SEADISK_CACHE_HPP
|
||||
#include <chrono>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace seafile::cache {
|
||||
template<typename K, typename V>
|
||||
class Cache final{
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using Duration = Clock::duration;
|
||||
|
||||
/// 设置缓存
|
||||
/// @param key 键
|
||||
/// @param value 值
|
||||
/// @param ttl 超时时间
|
||||
void put(const K &key, V value, const Duration ttl) {
|
||||
std::unique_lock lock(mutex_);
|
||||
|
||||
cache_[key] = Entry{
|
||||
std::move(value),
|
||||
Clock::now() + ttl
|
||||
};
|
||||
}
|
||||
|
||||
/// 获取缓存值
|
||||
/// @param key 键
|
||||
/// @return 缓存值
|
||||
std::optional<V> get(const K &key){
|
||||
// 第一阶段:读锁
|
||||
{
|
||||
std::shared_lock lock(mutex_);
|
||||
|
||||
auto it = cache_.find(key);
|
||||
if (it == cache_.end())
|
||||
return std::nullopt;
|
||||
|
||||
if (Clock::now() < it->second.expire_time)
|
||||
return it->second.value;
|
||||
}
|
||||
|
||||
// 第二阶段:删除过期数据
|
||||
{
|
||||
std::unique_lock lock(mutex_);
|
||||
|
||||
auto it = cache_.find(key);
|
||||
if (it != cache_.end() &&
|
||||
Clock::now() >= it->second.expire_time)
|
||||
{
|
||||
cache_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/// 移除缓存
|
||||
/// @param key 键
|
||||
void remove(const K &key) {
|
||||
std::unique_lock lock(mutex_);
|
||||
cache_.erase(key);
|
||||
}
|
||||
|
||||
///清空缓存
|
||||
void clear() {
|
||||
std::unique_lock lock(mutex_);
|
||||
cache_.clear();
|
||||
}
|
||||
|
||||
///清理过时的缓存
|
||||
void cleanup() {
|
||||
auto now = Clock::now();
|
||||
|
||||
std::unique_lock lock(mutex_);
|
||||
|
||||
for (auto it = cache_.begin(); it != cache_.end();)
|
||||
{
|
||||
if (it->second.expireTime <= now)
|
||||
{
|
||||
it = cache_.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///缓存大小
|
||||
size_t size() const {
|
||||
std::shared_lock lock(mutex_);
|
||||
return cache_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
struct Entry final {
|
||||
V value;
|
||||
Clock::time_point expire_time;
|
||||
};
|
||||
|
||||
mutable std::shared_mutex mutex_;
|
||||
|
||||
std::unordered_map<K, Entry> cache_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //SEADISK_CACHE_HPP
|
||||
@@ -15,9 +15,14 @@ namespace seafile::fs
|
||||
DIRECTORY,
|
||||
};
|
||||
|
||||
|
||||
/// 仓库 id
|
||||
std::string repo_id;
|
||||
std::string path;
|
||||
|
||||
/// 仓库内绝对路径(如 /folder/file.txt)
|
||||
std::string repo_path;
|
||||
|
||||
/// 虚拟全路径(如 /repo-name/folder/file.txt)
|
||||
std::string full_path;
|
||||
|
||||
Type type;
|
||||
|
||||
|
||||
@@ -4,38 +4,34 @@
|
||||
|
||||
#ifndef SEADISK_SEAFILE_FILE_SYSTEM_HPP
|
||||
#define SEADISK_SEAFILE_FILE_SYSTEM_HPP
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <winfsp/winfsp.h>
|
||||
|
||||
#include "seafile/api/seafile_api.hpp"
|
||||
#include "seafile/api/token_type.hpp"
|
||||
#include "seafile/api/cached_seafile_api.hpp"
|
||||
#include "seafile/cache/cache.hpp"
|
||||
#include "seafile/fs/seafile_store.hpp"
|
||||
#include "seafile/fs/storage/repo_entry.hpp"
|
||||
#include "seafile/fs/storage/repo_info.hpp"
|
||||
|
||||
namespace seafile::fs
|
||||
{
|
||||
class SeafileFileSystem final
|
||||
{
|
||||
using SeafileException = api::SeafileException;
|
||||
using TokenType = api::TokenType;
|
||||
using ISeaFileApi = api::ISeaFileApi;
|
||||
using RepoEntry = api::RepoEntry;
|
||||
using RepoInfo = storage::RepoInfo;
|
||||
using RepoEntry = storage::RepoEntry;
|
||||
|
||||
|
||||
std::string repo_id;
|
||||
std::shared_ptr<ISeaFileApi> api;
|
||||
std::unique_ptr<SeafileStore> store;
|
||||
std::wstring volume_label;
|
||||
|
||||
public:
|
||||
explicit SeafileFileSystem(const std::string& server,
|
||||
const std::wstring& volume_label) : volume_label(volume_label)
|
||||
explicit SeafileFileSystem(std::unique_ptr<SeafileStore> store,
|
||||
const std::wstring& volume_label)
|
||||
: store(std::move(store)), volume_label(volume_label)
|
||||
{
|
||||
this->api = std::make_shared<api::CachedSeafileApi>(
|
||||
std::make_shared<api::SeaFileApi>(server)
|
||||
);
|
||||
}
|
||||
|
||||
tl::expected<void, SeafileException> initialize(const std::string& username, const std::string& password);
|
||||
tl::expected<void, SeafileException> initialize();
|
||||
|
||||
SeafileFileSystem(const SeafileFileSystem&) = delete;
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <sqlite_orm/sqlite_orm.h>
|
||||
|
||||
#include "storage/repo_entry.hpp"
|
||||
#include "storage/repo_info.hpp"
|
||||
|
||||
namespace seafile::fs
|
||||
{
|
||||
@@ -40,20 +41,41 @@ namespace seafile::fs
|
||||
/// 检查是否需要更新元素据,如果需要同步则自动同步
|
||||
tl::expected<void, api::SeafileException> checkAndSync(const std::string& repo_id);
|
||||
|
||||
/// 同步元数据
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> syncRepoEntry(const std::string& repo_id) const;
|
||||
/// 返回路径的信息
|
||||
/// @param path 需要查询的路径
|
||||
/// @return 路径信息
|
||||
[[nodiscard]] tl::expected<std::variant<storage::RepoInfo, storage::RepoEntry>, api::SeafileException> pathInfo(
|
||||
const std::string& path);
|
||||
|
||||
/// 返回路径下面的子项目信息
|
||||
/// @param path 路径
|
||||
/// @return 子项目信息
|
||||
[[nodiscard]] tl::expected<std::variant<std::vector<storage::RepoInfo>, std::vector<storage::RepoEntry> >,
|
||||
api::SeafileException> listDir(const std::string& path);
|
||||
|
||||
/// 读取文件内容
|
||||
/// @param repo_id 仓库 id
|
||||
/// @param path 仓库内绝对路径
|
||||
/// @param offset 起始偏移
|
||||
/// @param length 读取长度
|
||||
/// @return 文件内容
|
||||
[[nodiscard]] tl::expected<std::string, api::SeafileException> readFile(
|
||||
const std::string& repo_id, const std::string& path, size_t offset, size_t length) const;
|
||||
|
||||
/// 获取已缓存的账号信息(用于卷大小)
|
||||
/// @return 账号信息
|
||||
[[nodiscard]] tl::expected<api::AccountInfo, api::SeafileException> accountInfo() const;
|
||||
|
||||
/// 同步用户信息
|
||||
tl::expected<void, api::SeafileException> syncUserInfo();
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> syncUserInfo();
|
||||
|
||||
/// 同步repo信息
|
||||
tl::expected<void, api::SeafileException> syncRepos();
|
||||
[[nodiscard]] tl::expected<std::vector<storage::RepoInfo>, api::SeafileException> syncRepos();
|
||||
|
||||
/// 同步
|
||||
tl::expected<void, api::SeafileException> sync();
|
||||
|
||||
static std::filesystem::path storePath() ;
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> sync();
|
||||
|
||||
static std::filesystem::path storePath();
|
||||
|
||||
/// 获取路径信息
|
||||
/// @param path 路径
|
||||
@@ -62,6 +84,14 @@ namespace seafile::fs
|
||||
const std::string& repo_id, const std::string& path);
|
||||
|
||||
private:
|
||||
tl::expected<storage::RepoInfo, api::SeafileException> findRepoByName(const std::string& name);
|
||||
|
||||
tl::expected<void, api::SeafileException> fetchAndStoreRepoDir(
|
||||
const std::string& repo_id, const std::string& parent_path);
|
||||
|
||||
std::vector<storage::RepoEntry> queryDirectChildren(
|
||||
const std::string& repo_id, const std::string& parent_path);
|
||||
|
||||
struct Database;
|
||||
std::unique_ptr<Database> database;
|
||||
std::string api_server;
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace seafile::fs::storage
|
||||
/// Whether the entry is starred by the current user.
|
||||
std::optional<bool> starred{};
|
||||
|
||||
static auto from_api_model(const std::string& repo_id, const std::string& path,
|
||||
static auto from_api_model(const std::string& repo_id, const std::string& parent_path,
|
||||
const api::RepoEntry& entry) -> RepoEntry;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// Created by Shell on 2026/7/30.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_STORAGE_REPO_INFO_HPP
|
||||
#define SEADISK_STORAGE_REPO_INFO_HPP
|
||||
#include <string>
|
||||
|
||||
#include "seafile/api/repo_info.hpp"
|
||||
|
||||
namespace seafile::fs::storage {
|
||||
/**
|
||||
*@brief Seafile repository metadata.
|
||||
*/
|
||||
struct RepoInfo final {
|
||||
/// Object type, usually "repo".
|
||||
std::string type;
|
||||
|
||||
/// Repository UUID.
|
||||
std::string id;
|
||||
|
||||
/// Repository owner identifier.
|
||||
std::string owner;
|
||||
|
||||
/// Repository name.
|
||||
std::string name;
|
||||
|
||||
/// Last modification time (Unix timestamp, seconds).
|
||||
std::int64_t mtime{};
|
||||
|
||||
/// Repository size in bytes.
|
||||
std::uint64_t size{};
|
||||
|
||||
/// Indicates whether the repository is encrypted.
|
||||
bool encrypted{};
|
||||
|
||||
/// Root directory object ID.
|
||||
std::string root;
|
||||
|
||||
/// Current user's permission (e.g. "r", "rw").
|
||||
std::string permission;
|
||||
|
||||
/// Internal email address of the last modifier.
|
||||
std::string modifier_email;
|
||||
|
||||
/// Contact email address of the last modifier.
|
||||
std::string modifier_contact_email;
|
||||
|
||||
/// Display name of the last modifier.
|
||||
std::string modifier_name;
|
||||
|
||||
/// Total number of files in the repository.
|
||||
std::uint64_t file_count{};
|
||||
|
||||
/// Head commit identifier.
|
||||
std::string head_commit_id;
|
||||
|
||||
/// Repository encryption version.
|
||||
int enc_version{};
|
||||
|
||||
/// Encryption salt.
|
||||
std::string salt;
|
||||
|
||||
/// Encryption magic value.
|
||||
std::string magic;
|
||||
|
||||
/// Repository random key.
|
||||
std::string random_key;
|
||||
|
||||
static auto from_api_model(const api::RepoInfo& repo_info) -> RepoInfo;
|
||||
};
|
||||
}
|
||||
#endif //SEADISK_STORAGE_REPO_INFO_HPP
|
||||
Reference in new issue
Block a user