// // Created by WolverinDEV on 16/09/2020. // #pragma once #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include "./NetTools.h" namespace ts::server::file::filesystem { #ifdef FS_INCLUDED namespace fs = std::experimental::filesystem; #endif class LocalFileSystem : public filesystem::AbstractProvider { using FileModifyError = filesystem::FileModifyError; using DirectoryModifyError = filesystem::DirectoryModifyError; public: enum struct FileCategory { ICON, AVATAR, CHANNEL }; virtual ~LocalFileSystem(); bool initialize(std::string & /* error */, const std::string & /* root path */); void finalize(); void lock_file(const std::string& /* absolute path */); void unlock_file(const std::string& /* absolute path */); [[nodiscard]] inline const auto &root_path() const { return this->root_path_; } [[nodiscard]] std::string absolute_avatar_path(const std::shared_ptr &, const std::string&); [[nodiscard]] std::string absolute_icon_path(const std::shared_ptr &, const std::string&); [[nodiscard]] std::string absolute_channel_path(const std::shared_ptr &, ChannelId, const std::string&); std::shared_ptr> initialize_server(const std::shared_ptr & /* server */) override; std::shared_ptr> delete_server(const std::shared_ptr & /* server */) override; std::shared_ptr> query_channel_info(const std::shared_ptr & /* server */, const std::vector>& /* names */) override; std::shared_ptr query_channel_directory(const std::shared_ptr & id, ChannelId channelId, const std::string &string) override; std::shared_ptr> create_channel_directory(const std::shared_ptr & id, ChannelId channelId, const std::string &string) override; std::shared_ptr> delete_channel_files(const std::shared_ptr & id, ChannelId channelId, const std::vector &string) override; std::shared_ptr> rename_channel_file(const std::shared_ptr & id, ChannelId channelId, const std::string &, ChannelId, const std::string &) override; std::shared_ptr> query_icon_info(const std::shared_ptr & /* server */, const std::vector& /* names */) override; std::shared_ptr query_icon_directory(const std::shared_ptr & id) override; std::shared_ptr> delete_icons(const std::shared_ptr & id, const std::vector &string) override; std::shared_ptr> query_avatar_info(const std::shared_ptr & /* server */, const std::vector& /* names */) override; std::shared_ptr query_avatar_directory(const std::shared_ptr & id) override; std::shared_ptr> delete_avatars(const std::shared_ptr & id, const std::vector &string) override; private: #ifdef FS_INCLUDED [[nodiscard]] fs::path server_path(const std::shared_ptr &); [[nodiscard]] fs::path server_channel_path(const std::shared_ptr &, ChannelId); [[nodiscard]] static bool exceeds_base_path(const fs::path& /* base */, const fs::path& /* target */); [[nodiscard]] bool is_any_file_locked(const fs::path& /* base */, const std::string& /* path */, std::string& /* file (relative to the base) */); [[nodiscard]] std::shared_ptr> delete_files(const fs::path& /* base */, const std::vector &string); [[nodiscard]] std::shared_ptr query_directory(const fs::path& /* base */, const std::string &string, bool); [[nodiscard]] std::shared_ptr> query_file_info(const std::vector> &string); #endif template std::shared_ptr> create_execute_response() { return std::make_shared>(this->result_notify_mutex, this->result_notify_cv); } std::string target_file_path(FileCategory type, const std::shared_ptr &sid, ts::ChannelId cid, const std::string &path); std::mutex result_notify_mutex{}; std::condition_variable result_notify_cv{}; std::string root_path_{}; std::mutex locked_files_mutex{}; std::deque locked_files_{}; }; }