#pragma once #include #include #include "FileTransferManager.h" namespace tc { namespace ft { class TransferFileSource : public TransferSource { public: TransferFileSource(std::string /* path */, std::string /* name */); [[nodiscard]] inline std::string file_path() const { return this->_path; } [[nodiscard]] inline std::string file_name() const { return this->_name; } std::string name() const override { return "TransferFileSource"; } bool initialize(std::string &string) override; void finalize() override; uint64_t byte_length() const override; uint64_t stream_index() const override; error::value read_bytes(std::string &string, uint8_t *uint8, uint64_t &uint64) override; #ifdef NODEJS_API static NAN_METHOD(create); #endif private: std::string _path; std::string _name; uint64_t position{0}; std::ifstream file_stream{}; mutable std::optional file_size; }; #ifdef NODEJS_API class TransferObjectWrap : public Nan::ObjectWrap { public: static NAN_MODULE_INIT(Init); static NAN_METHOD(NewInstance); static inline bool is_wrap(const v8::Local& value) { if(value.As().IsEmpty()) return false; return value->InstanceOf(Nan::GetCurrentContext(), Nan::New(constructor())).FromMaybe(false); } static inline Nan::Persistent & constructor() { static Nan::Persistent my_constructor; return my_constructor; } explicit TransferObjectWrap(std::shared_ptr object) : _transfer(std::move(object)) { } ~TransferObjectWrap() = default; void do_wrap(v8::Local object); std::shared_ptr target() { return this->_transfer; } private: std::shared_ptr _transfer; }; class TransferJSBufferSource : public TransferSource { public: TransferJSBufferSource(); virtual ~TransferJSBufferSource(); std::string name() const override { return "TransferJSBufferSource"; } bool initialize(std::string &string) override; void finalize() override; uint64_t stream_index() const override; uint64_t byte_length() const override; error::value read_bytes(std::string &string, uint8_t *uint8, uint64_t &uint64) override; static NAN_METHOD(create_from_buffer); private: v8::Global _js_buffer; void* _js_buffer_source; uint64_t _js_buffer_length; uint64_t _js_buffer_index; }; class TransferJSBufferTarget : public TransferTarget { public: TransferJSBufferTarget(); virtual ~TransferJSBufferTarget(); std::string name() const override { return "TransferJSBufferTarget"; } bool initialize(std::string &string) override; void finalize() override; uint64_t stream_index() const override; uint64_t expected_length() const override { return this->_js_buffer_length; } error::value write_bytes(std::string &string, uint8_t *uint8, uint64_t uint64) override; static NAN_METHOD(create_from_buffer); private: v8::Global _js_buffer; void* _js_buffer_source; uint64_t _js_buffer_length; uint64_t _js_buffer_index; }; #endif } }