bug fix
This commit is contained in:
1 parent
ae3a25857f
commit
7296b3096a
13 files changed
+467
-206
No files matched your search
@@ -25,50 +25,65 @@
|
||||
#include "token_type.hpp"
|
||||
#include "seafile/util/version.hpp"
|
||||
|
||||
namespace seafile::api {
|
||||
class SeaFileApi final : public ISeaFileApi {
|
||||
namespace seafile::api
|
||||
{
|
||||
class SeaFileApi final : public ISeaFileApi
|
||||
{
|
||||
//base url of the seafile api server
|
||||
std::string base_url;
|
||||
std::string token;
|
||||
TokenType token_type = TokenType::BEARER;
|
||||
cpr::ConnectionPool connection_pool;
|
||||
|
||||
/**
|
||||
* @brief make a url string from given path
|
||||
* @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);
|
||||
if (!url)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
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);
|
||||
@@ -81,15 +96,16 @@ namespace seafile::api {
|
||||
* @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},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"content-type", "application/json"}
|
||||
}, cpr::Body{body.dump()});
|
||||
}, cpr::Body{body.dump()}, connection_pool);
|
||||
return parseResponse<T>(response);
|
||||
}
|
||||
|
||||
@@ -98,7 +114,8 @@ namespace seafile::api {
|
||||
* @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))
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,19 +125,25 @@ namespace seafile::api {
|
||||
* @return token when success else SeafileException
|
||||
*/
|
||||
tl::expected<AuthToken, SeafileException>
|
||||
auth(const std::string &username, const std::string &password) override {
|
||||
auth(const std::string& username, const std::string& password) override
|
||||
{
|
||||
const nlohmann::json body = {
|
||||
{"username", username},
|
||||
{"password", password}
|
||||
};
|
||||
const auto &auth_token = this->post<AuthToken>("/api2/auth-token/", body);
|
||||
if (auth_token) {
|
||||
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) {
|
||||
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 {
|
||||
}
|
||||
else
|
||||
{
|
||||
return tl::make_unexpected(server_info.error());
|
||||
}
|
||||
}
|
||||
@@ -128,12 +151,13 @@ namespace seafile::api {
|
||||
}
|
||||
|
||||
tl::expected<ServerInfo, SeafileException>
|
||||
serverInfo() const override {
|
||||
serverInfo() const override
|
||||
{
|
||||
const auto response = cpr::Get(
|
||||
cpr::Url{makeUrl("/api2/server-info/")},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"}
|
||||
});
|
||||
{"accept", "application/json"},
|
||||
}, connection_pool);
|
||||
|
||||
return parseResponse<ServerInfo>(response);
|
||||
}
|
||||
@@ -143,19 +167,21 @@ namespace seafile::api {
|
||||
* @return default repo info
|
||||
*/
|
||||
tl::expected<DefaultRepo, SeafileException>
|
||||
defaultRepo() const override {
|
||||
defaultRepo() const override
|
||||
{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = makeUrl("/api2/default-repo/");
|
||||
const auto response = cpr::Get(cpr::Url{url},
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
}, connection_pool);
|
||||
return parseResponse<DefaultRepo>(response);
|
||||
}
|
||||
|
||||
tl::expected<RepoInfo, SeafileException>
|
||||
repoInfo(const std::string &repo_id) const override {
|
||||
repoInfo(const std::string& repo_id) const override
|
||||
{
|
||||
const auto url = makeUrl(std::format("/api2/repos/{}/", repo_id));
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -163,12 +189,13 @@ namespace seafile::api {
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
}, connection_pool);
|
||||
return parseResponse<RepoInfo>(response);
|
||||
}
|
||||
|
||||
tl::expected<std::vector<RepoInfo>, SeafileException>
|
||||
repos(const std::string &type, const std::string &name_contains) const override {
|
||||
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},
|
||||
@@ -179,10 +206,10 @@ namespace seafile::api {
|
||||
cpr::Parameters{
|
||||
{"type", type},
|
||||
{"nameContains", name_contains}
|
||||
});
|
||||
}, connection_pool);
|
||||
|
||||
|
||||
return parseResponse<std::vector<RepoInfo> >(response);
|
||||
return parseResponse<std::vector<RepoInfo>>(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +219,8 @@ namespace seafile::api {
|
||||
* @return repo entries
|
||||
*/
|
||||
tl::expected<std::vector<RepoEntry>, SeafileException>
|
||||
repoDir(const std::string &repo_id, const std::string &path) const override {
|
||||
repoDir(const std::string& repo_id, const std::string& path) const override
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/dir/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -203,12 +231,15 @@ namespace seafile::api {
|
||||
},
|
||||
cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
return parseResponse<std::vector<RepoEntry> >(response);
|
||||
},
|
||||
connection_pool,
|
||||
cpr::ReserveSize(512 * 1024));
|
||||
return parseResponse<std::vector<RepoEntry>>(response);
|
||||
}
|
||||
|
||||
tl::expected<AccountInfo, SeafileException>
|
||||
accountInfo() const override {
|
||||
accountInfo() const override
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl("/api2/account/info/")};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -216,12 +247,13 @@ namespace seafile::api {
|
||||
cpr::Header{
|
||||
{"accept", "application/json"},
|
||||
{"authorization", auth}
|
||||
});
|
||||
}, connection_pool);
|
||||
return parseResponse<AccountInfo>(response);
|
||||
}
|
||||
|
||||
tl::expected<FileInfo, SeafileException>
|
||||
fileDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
fileDetail(const std::string& repo_id, const std::string& path) const override
|
||||
{
|
||||
const auto url = cpr::Url{makeUrl(std::format("/api2/repos/{}/file/detail/", repo_id))};
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
|
||||
@@ -231,12 +263,13 @@ namespace seafile::api {
|
||||
{"authorization", auth}
|
||||
}, cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
}, connection_pool);
|
||||
return parseResponse<FileInfo>(response);
|
||||
}
|
||||
|
||||
tl::expected<std::string, SeafileException>
|
||||
downloadFileUrl(const std::string &repo_id, const std::string &path) const override {
|
||||
downloadFileUrl(const std::string& repo_id, const std::string& path) const override
|
||||
{
|
||||
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,
|
||||
@@ -245,17 +278,19 @@ namespace seafile::api {
|
||||
{"authorization", auth}
|
||||
}, cpr::Parameters{
|
||||
{"p", path}
|
||||
});
|
||||
}, connection_pool);
|
||||
return parseResponse<std::string>(response);
|
||||
}
|
||||
|
||||
|
||||
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 override {
|
||||
if (auto download_url = downloadFileUrl(repo_id, path); download_url.has_value()) {
|
||||
const std::optional<size_t> end) const override
|
||||
{
|
||||
if (auto download_url = downloadFileUrl(repo_id, path); download_url.has_value())
|
||||
{
|
||||
const auto auth = makeAuthToken(token, token_type);
|
||||
const auto url = cpr::Url{download_url.value()};
|
||||
auto header = cpr::Header{
|
||||
@@ -266,21 +301,33 @@ namespace seafile::api {
|
||||
start ? std::to_string(*start) : "",
|
||||
end ? std::to_string(*end) : "");
|
||||
header.emplace("range", range);
|
||||
if (const auto &response = cpr::Get(url, header);
|
||||
response.error || (response.status_code != 200 && response.status_code != 206)) {
|
||||
|
||||
size_t content_size = 1024 * 4;
|
||||
if (start.has_value() && end.has_value())
|
||||
{
|
||||
content_size = end.value() - start.value();
|
||||
}
|
||||
if (const auto& response = cpr::Get(url, header, connection_pool, cpr::ReserveSize(content_size));
|
||||
response.error || (response.status_code != 200 && response.status_code != 206))
|
||||
{
|
||||
return tl::make_unexpected(SeafileException{
|
||||
std::format("Download bytes range {} failed, HTTP Error:{}", range, response.status_code)
|
||||
});
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return response.text;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return download_url;
|
||||
}
|
||||
}
|
||||
|
||||
tl::expected<DirInfo, SeafileException>
|
||||
dirDetail(const std::string &repo_id, const std::string &path) const override {
|
||||
dirDetail(const std::string& repo_id, const std::string& path) const override
|
||||
{
|
||||
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{
|
||||
@@ -288,7 +335,7 @@ namespace seafile::api {
|
||||
{"authorization", auth}
|
||||
}, cpr::Parameters{
|
||||
{"path", path}
|
||||
});
|
||||
}, connection_pool);
|
||||
|
||||
return parseResponse<DirInfo>(response);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#ifndef SEADISK_FILE_CONTEXT_HPP
|
||||
#define SEADISK_FILE_CONTEXT_HPP
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
namespace seafile::fs
|
||||
{
|
||||
|
||||
@@ -18,8 +18,6 @@ namespace seafile::fs
|
||||
class SeafileFileSystem final
|
||||
{
|
||||
using SeafileException = api::SeafileException;
|
||||
using RepoInfo = storage::RepoInfo;
|
||||
using RepoEntry = storage::RepoEntry;
|
||||
|
||||
std::unique_ptr<SeafileStore> store;
|
||||
std::wstring volume_label;
|
||||
|
||||
@@ -29,8 +29,6 @@ namespace seafile::fs
|
||||
|
||||
tl::expected<void, api::SeafileException> auth();
|
||||
|
||||
[[nodiscard]] tl::expected<std::string, api::SeafileException> defaultRepoId() const;
|
||||
|
||||
///初始化数据库信息
|
||||
[[nodiscard]] tl::expected<void, api::SeafileException> init();
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by Shell on 2026-07-30.
|
||||
//
|
||||
|
||||
#ifndef SEADISK_SETTING_HPP
|
||||
#define SEADISK_SETTING_HPP
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace seafile::fs
|
||||
{
|
||||
struct Setting final
|
||||
{
|
||||
std::string server;
|
||||
std::string user;
|
||||
std::string password;
|
||||
std::string mount_point;
|
||||
std::string mount_label;
|
||||
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Setting, server, user, password, mount_point, mount_label);
|
||||
};
|
||||
}
|
||||
#endif //SEADISK_SETTING_HPP
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#ifndef SEADISK_ICU_RAII_H
|
||||
#define SEADISK_ICU_RAII_H
|
||||
#include <memory>
|
||||
#include <unicode/ucsdet.h>
|
||||
|
||||
struct UCharsetDetectorDeleter final
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace shell::util {
|
||||
static std::string utf16le_to_utf8(const std::wstring& input);
|
||||
|
||||
static std::string utf16le_to_utf8(const wchar_t* input);
|
||||
};
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
#endif //SEADISK_STRING_UTIL_HPP
|
||||
Reference in new issue
Block a user