重新组织项目结构

This commit is contained in:
shell committed 2026-07-12 22:02:00 +08:00
1 parent 28206cd50e
commit 4d7dde104d
15 files changed
+59 -83

No files matched your search

File renamed without changes.
@@ -4,6 +4,8 @@
#ifndef SEADISK_AUTH_TOKEN_HPP
#define SEADISK_AUTH_TOKEN_HPP
#include <string>
#include <nlohmann/json.hpp>
namespace seafile {
struct AuthToken final {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -7,7 +7,6 @@
#include <optional>
#include <string>
#include <nlohmann/json.hpp>
#include <winfsp/fsctl.h>
namespace seafile
{
File renamed without changes.
@@ -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 <typename T>
template<typename T>
static tl::expected<T, SeafileException>
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<T>();
}
catch (const std::exception& e)
{
.get<T>();
} 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 <typename T>
tl::expected<T, SeafileException> post(const std::string& path, const nlohmann::json& body)
{
template<typename T>
tl::expected<T, SeafileException> 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, SeafileException>
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, SeafileException>
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, SeafileException>
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, SeafileException>
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<std::vector<RepoEntry>, 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<std::vector<RepoEntry>>(response);
return parseResponse<std::vector<RepoEntry> >(response);
}
tl::expected<AccountInfo, SeafileException>
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<FileInfo, SeafileException>
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<std::string, SeafileException>
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<std::string, SeafileException>
downloadFile(const std::string& repo_id,
const std::string& path,
downloadFile(const std::string &repo_id,
const std::string &path,
const std::optional<size_t> start,
const std::optional<size_t> 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<DirInfo, SeafileException> dirDetail(const std::string& repo_id, const std::string& path,
const std::string& token, const TokenType token_type) const
{
tl::expected<DirInfo, SeafileException> 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{
File renamed without changes.
File renamed without changes.
+2 -2
View File
@@ -7,8 +7,8 @@
#include <string>
#include <winfsp/winfsp.h>
#include "seafile/seafile_api.hpp"
#include "seafile/token_type.hpp"
#include "../api/seafile_api.hpp"
#include "../api/token_type.hpp"
namespace seafile::fs
{