From 1fd6e7e97aedf9674c5bd15c4d37ce8fc709a33b Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 22 Mar 2020 15:13:09 +0100 Subject: [PATCH] Add API call to create links to extra files --- libretroshare/src/ft/ftserver.cc | 44 ++++++++++++++++++++------ libretroshare/src/ft/ftserver.h | 17 +++++++--- libretroshare/src/retroshare/rsfiles.h | 22 +++++++++++-- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 7ec4251b3..1efbb715c 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -2217,16 +2217,14 @@ const noexcept } } -std::error_condition ftServer::exportFilesLink( - std::string& link, std::uintptr_t handle, bool fragSneak, - const std::string& baseUrl ) +std::error_condition ftServer::dirDetailsToLink( + std::string& link, + const DirDetails& dirDetails, bool fragSneak, const std::string& baseUrl ) { - DirDetails tDirDet; - if(!requestDirDetails(tDirDet, handle)) - return RsFilesErrorNum::FILES_HANDLE_NOT_FOUND; - std::unique_ptr tFileTree = - RsFileTree::fromDirDetails(tDirDet, false); + RsFileTree::fromDirDetails(dirDetails, false); + if(!tFileTree) return std::errc::invalid_argument; + link = tFileTree->toBase64(); if(!baseUrl.empty()) @@ -2234,7 +2232,7 @@ std::error_condition ftServer::exportFilesLink( RsUrl tUrl(baseUrl); tUrl.setQueryKV(FILES_URL_COUNT_FIELD, std::to_string(tFileTree->mTotalFiles) ) - .setQueryKV(FILES_URL_NAME_FIELD, tDirDet.name) + .setQueryKV(FILES_URL_NAME_FIELD, dirDetails.name) .setQueryKV( FILES_URL_SIZE_FIELD, std::to_string(tFileTree->mTotalSize) ); if(fragSneak) @@ -2247,6 +2245,34 @@ std::error_condition ftServer::exportFilesLink( return std::error_condition(); } +std::error_condition ftServer::exportCollectionLink( + std::string& link, std::uintptr_t handle, bool fragSneak, + const std::string& baseUrl ) +{ + DirDetails tDirDet; + if(!requestDirDetails(tDirDet, handle)) + return RsFilesErrorNum::FILES_HANDLE_NOT_FOUND; + + return dirDetailsToLink(link, tDirDet, fragSneak, baseUrl); +} + +/// @see RsFiles +std::error_condition ftServer::exportFileLink( + std::string& link, const RsFileHash& fileHash, uint64_t fileSize, + const std::string& fileName, bool fragSneak, const std::string& baseUrl ) +{ + if(fileHash.isNull() || !fileSize || fileName.empty()) + return std::errc::invalid_argument; + + DirDetails tDirDet; + tDirDet.type = DIR_TYPE_FILE; + tDirDet.name = fileName; + tDirDet.hash = fileHash; + tDirDet.count = fileSize; + + return dirDetailsToLink(link, tDirDet, fragSneak, baseUrl); +} + std::error_condition ftServer::parseFilesLink( const std::string& link, RsFileTree& collection ) { diff --git a/libretroshare/src/ft/ftserver.h b/libretroshare/src/ft/ftserver.h index 7c8ca5ff2..789bba49d 100644 --- a/libretroshare/src/ft/ftserver.h +++ b/libretroshare/src/ft/ftserver.h @@ -224,11 +224,18 @@ public: virtual TurtleSearchRequestId turtleSearch(const RsRegularExpression::LinearizedExpression& expr) ; /// @see RsFiles - std::error_condition exportFilesLink( + std::error_condition exportCollectionLink( std::string& link, std::uintptr_t handle, bool fragSneak = false, const std::string& baseUrl = RsFiles::DEFAULT_FILES_BASE_URL ) override; + /// @see RsFiles + std::error_condition exportFileLink( + std::string& link, const RsFileHash& fileHash, uint64_t fileSize, + const std::string& fileName, bool fragSneak = false, + const std::string& baseUrl = RsFiles::DEFAULT_FILES_BASE_URL + ) override; + /// @see RsFiles std::error_condition parseFilesLink( const std::string& link, RsFileTree& collection ) override; @@ -383,11 +390,11 @@ protected: bool findEncryptedHash(const RsPeerId& virtual_peer_id, RsFileHash& encrypted_hash); bool checkUploadLimit(const RsPeerId& pid,const RsFileHash& hash); -private: - /**** INTERNAL FUNCTIONS ***/ - //virtual int reScanDirs(); - //virtual int check_dBUpdate(); + std::error_condition dirDetailsToLink( + std::string& link, + const DirDetails& dirDetails, bool fragSneak, + const std::string& baseUrl ); private: diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index 661043a20..ddd241e8f 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -912,10 +912,10 @@ public: static const std::string FILES_URL_SIZE_FIELD; /** - * @brief Get link to a forum + * @brief Export link to a collection of files * @jsonapi{development} * @param[out] link storage for the generated link - * @param[in] handle file/directory RetroShare handle + * @param[in] handle directory RetroShare handle * @param[in] fragSneak when true the file data is sneaked into fragment * instead of FILES_URL_DATA_FIELD query field, this way if using an * http[s] link to pass around a disguised file link a misconfigured host @@ -929,10 +929,26 @@ public: * plain base64 format. * @return error information if some error occurred, 0/SUCCESS otherwise */ - virtual std::error_condition exportFilesLink( + virtual std::error_condition exportCollectionLink( std::string& link, std::uintptr_t handle, bool fragSneak = false, const std::string& baseUrl = RsFiles::DEFAULT_FILES_BASE_URL ) = 0; + /** + * @brief Export link to a file + * @jsonapi{development} + * @param[out] link @see exportCollectionLink + * @param[in] fileHash hash of the file + * @param[in] fileSize size of the file + * @param[in] fileName name of the file + * @param[in] fragSneak @see exportCollectionLink + * @param[in] baseUrl @see exportCollectionLink + * @return error @see exportCollectionLink + */ + virtual std::error_condition exportFileLink( + std::string& link, const RsFileHash& fileHash, uint64_t fileSize, + const std::string& fileName, bool fragSneak = false, + const std::string& baseUrl = RsFiles::DEFAULT_FILES_BASE_URL ) = 0; + /** * @brief Parse RetroShare files link * @jsonapi{development}