From afb92999d8da08fcac8651a9ebaed71f732f1f8c Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 24 Aug 2018 00:31:25 +0200 Subject: [PATCH] Enable JSON conversion for RsGxsImage via MemBlockProxy --- libretroshare/src/retroshare/rsfiles.h | 13 ++++--- libretroshare/src/retroshare/rsgxschannels.h | 4 +- libretroshare/src/retroshare/rsgxscommon.h | 12 +++++- .../src/serialiser/rstypeserializer.cc | 37 +++++++++++++++---- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index a4a8bfea2..4fca503a7 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -213,11 +213,14 @@ public: virtual bool getFileData( const RsFileHash& hash, uint64_t offset, uint32_t& requested_size, uint8_t* data ) = 0; - /*** - * Control of Downloads. - ***/ - - virtual bool alreadyHaveFile(const RsFileHash& hash, FileInfo &info) = 0; + /** + * @brief Check if we already have a file + * @jsonapi{development} + * @param[in] hash file identifier + * @param[out] info storage for the possibly found file information + * @return true if the file is already present, false otherwise + */ + virtual bool alreadyHaveFile(const RsFileHash& hash, FileInfo &info) = 0; /** * @brief Initiate downloading of a file diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index 75a5edab4..383ebc7df 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -56,7 +56,7 @@ struct RsGxsChannelGroup : RsSerializable { RS_SERIAL_PROCESS(mMeta); RS_SERIAL_PROCESS(mDescription); - //RS_SERIAL_PROCESS(mImage); + RS_SERIAL_PROCESS(mImage); RS_SERIAL_PROCESS(mAutoDownload); } }; @@ -90,7 +90,7 @@ struct RsGxsChannelPost : RsSerializable RS_SERIAL_PROCESS(mFiles); RS_SERIAL_PROCESS(mCount); RS_SERIAL_PROCESS(mSize); - //RS_SERIAL_PROCESS(mThumbnail); + RS_SERIAL_PROCESS(mThumbnail); } }; diff --git a/libretroshare/src/retroshare/rsgxscommon.h b/libretroshare/src/retroshare/rsgxscommon.h index 0ecd933f0..57b17ed35 100644 --- a/libretroshare/src/retroshare/rsgxscommon.h +++ b/libretroshare/src/retroshare/rsgxscommon.h @@ -50,7 +50,7 @@ struct RsGxsFile : RsSerializable } }; -struct RsGxsImage +struct RsGxsImage : RsSerializable { RsGxsImage(); ~RsGxsImage(); @@ -70,8 +70,16 @@ struct RsGxsImage void clear(); // Frees. void shallowClear(); // Clears Pointer. - uint8_t *mData; uint32_t mSize; + uint8_t* mData; + + /// @see RsSerializable + virtual void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RsTypeSerializer::TlvMemBlock_proxy b(mData, mSize); + RsTypeSerializer::serial_process(j, ctx, b, "mData"); + } }; diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 9c2ebc37b..9a21eef0f 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -25,7 +25,7 @@ #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvkeys.h" #include "serialiser/rsserializable.h" - +#include "util/radix64.h" #include "util/rsprint.h" #include @@ -646,9 +646,12 @@ bool RsTypeSerializer::to_JSON( rapidjson::Value key; key.SetString(memberName.c_str(), memberName.length(), allocator); + std::string encodedValue; + Radix64::encode( reinterpret_cast(member.first), + member.second, encodedValue ); + rapidjson::Value value; - const char* tName = typeid(member).name(); - value.SetString(tName, allocator); + value.SetString(encodedValue.data(), allocator); jDoc.AddMember(key, value, allocator); @@ -656,7 +659,27 @@ bool RsTypeSerializer::to_JSON( } template<> /*static*/ -bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, - RsTypeSerializer::TlvMemBlock_proxy&, - RsJson& /*jDoc*/) -{ return true; } +bool RsTypeSerializer::from_JSON( const std::string& memberName, + RsTypeSerializer::TlvMemBlock_proxy& member, + RsJson& jDoc) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsString(); + if(ret) + { + std::string encodedValue = v.GetString(); + std::vector decodedValue = Radix64::decode(encodedValue); + member.second = decodedValue.size(); + + if(member.second == 0) + { + member.first = nullptr; + return ret; + } + + member.first = rs_malloc(member.second); + ret = ret && member.first && + memcpy(member.first, decodedValue.data(), member.second); + } + return ret; +}