Extend a bit filesharing JSON API v2

This commit is contained in:
Gioacchino Mazzurco 2018-05-16 16:13:44 +02:00
parent 186617fc2b
commit 5a301734a9
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
5 changed files with 128 additions and 4 deletions

View File

@ -37,6 +37,16 @@ FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files,
addResourceHandler("get_dir_parent", this, &FileSharingHandler::handleGetDirectoryParent);
addResourceHandler("get_dir_childs", this, &FileSharingHandler::handleGetDirectoryChilds);
addResourceHandler( "get_download_directory", this,
&FileSharingHandler::handleGetDownloadDirectory );
addResourceHandler( "set_download_directory", this,
&FileSharingHandler::handleSetDownloadDirectory );
addResourceHandler( "get_partials_directory", this,
&FileSharingHandler::handleGetPartialsDirectory );
addResourceHandler( "set_partials_directory", this,
&FileSharingHandler::handleSetPartialsDirectory );
addResourceHandler("is_dl_dir_shared", this, &FileSharingHandler::handleIsDownloadDirShared);
addResourceHandler("share_dl_dir", this, &FileSharingHandler::handleShareDownloadDirectory);
@ -513,4 +523,48 @@ void FileSharingHandler::handleDownload(Request& req, Response& resp)
resp.setFail("Couldn't download file");
}
void FileSharingHandler::handleGetDownloadDirectory( Request& /*req*/,
Response& resp )
{
std::string dlDir = mRsFiles->getDownloadDirectory();
resp.mDataStream << makeKeyValueReference("download_directory", dlDir);
resp.setOk();
}
void FileSharingHandler::handleSetDownloadDirectory( Request& req,
Response& resp )
{
std::string dlDir;
req.mStream << makeKeyValueReference("download_directory", dlDir);
if(dlDir.empty()) resp.setFail("missing download_directory");
else
{
mRsFiles->setDownloadDirectory(dlDir);
resp.setOk();
}
}
void FileSharingHandler::handleGetPartialsDirectory( Request& /*req*/,
Response& resp )
{
std::string partialsDir = mRsFiles->getPartialsDirectory();
resp.mDataStream << makeKeyValueReference("partials_directory", partialsDir);
resp.setOk();
}
void FileSharingHandler::handleSetPartialsDirectory( Request& req,
Response& resp )
{
std::string partialsDir;
req.mStream << makeKeyValueReference("partials_directory", partialsDir);
if(partialsDir.empty()) resp.setFail("missing partials_directory");
else
{
mRsFiles->setPartialsDirectory(partialsDir);
resp.setOk();
}
}
} // namespace resource_api

View File

@ -57,6 +57,12 @@ private:
void handleDownload(Request& req, Response& resp);
void handleGetDownloadDirectory(Request& req, Response& resp);
void handleSetDownloadDirectory(Request& req, Response& resp);
void handleGetPartialsDirectory(Request& req, Response& resp);
void handleSetPartialsDirectory(Request& req, Response& resp);
/// Token indicating change in local shared files
StateToken mLocalDirStateToken;

View File

@ -15,6 +15,14 @@ TransfersHandler::TransfersHandler(StateTokenServer *sts, RsFiles *files, RsPeer
addResourceHandler("downloads", this, &TransfersHandler::handleDownloads);
addResourceHandler("uploads", this, &TransfersHandler::handleUploads);
addResourceHandler("control_download", this, &TransfersHandler::handleControlDownload);
addResourceHandler( "set_file_destination_directory", this,
&TransfersHandler::handleSetFileDestinationDirectory );
addResourceHandler( "set_file_destination_name", this,
&TransfersHandler::handleSetFileDestinationName );
addResourceHandler( "set_file_chunk_strategy", this,
&TransfersHandler::handleSetFileChunkStrategy );
mStateToken = mStateTokenServer->getNewToken();
mStateTokenServer->registerTickClient(this);
mNotify.registerNotifyClient(this);
@ -288,4 +296,58 @@ void TransfersHandler::handleUploads(Request & /* req */, Response &resp)
resp.setOk();
}
void TransfersHandler::handleSetFileDestinationDirectory( Request& req,
Response& resp )
{
mStateTokenServer->replaceToken(mStateToken);
std::string hashString;
std::string newPath;
req.mStream << makeKeyValueReference("path", newPath);
req.mStream << makeKeyValueReference("hash", hashString);
RsFileHash hash(hashString);
if (mFiles->setDestinationDirectory(hash, newPath)) resp.setOk();
else resp.setFail();
}
void TransfersHandler::handleSetFileDestinationName( Request& req,
Response& resp )
{
mStateTokenServer->replaceToken(mStateToken);
std::string hashString;
std::string newName;
req.mStream << makeKeyValueReference("name", newName);
req.mStream << makeKeyValueReference("hash", hashString);
RsFileHash hash(hashString);
if (mFiles->setDestinationName(hash, newName)) resp.setOk();
else resp.setFail();
}
void TransfersHandler::handleSetFileChunkStrategy(Request& req, Response& resp)
{
mStateTokenServer->replaceToken(mStateToken);
std::string hashString;
std::string newChunkStrategyStr;
req.mStream << makeKeyValueReference("chuck_stategy", newChunkStrategyStr);
req.mStream << makeKeyValueReference("hash", hashString);
RsFileHash hash(hashString);
FileChunksInfo::ChunkStrategy newStrategy =
FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE;
if ( newChunkStrategyStr == "streaming" )
newStrategy = FileChunksInfo::CHUNK_STRATEGY_STREAMING;
else if ( newChunkStrategyStr == "random" )
newStrategy = FileChunksInfo::CHUNK_STRATEGY_RANDOM;
else if ( newChunkStrategyStr == "progressive" )
newStrategy = FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE;
if (mFiles->setChunkStrategy(hash, newStrategy)) resp.setOk();
else resp.setFail();
}
} // namespace resource_api

View File

@ -30,6 +30,9 @@ private:
void handleControlDownload(Request& req, Response& resp);
void handleDownloads(Request& req, Response& resp);
void handleUploads(Request& req, Response& resp);
void handleSetFileDestinationDirectory(Request& req, Response& resp);
void handleSetFileDestinationName(Request& req, Response& resp);
void handleSetFileChunkStrategy(Request& req, Response& resp);
StateTokenServer* mStateTokenServer;
RsFiles* mFiles;

View File

@ -168,10 +168,9 @@ public:
class RsFiles
{
public:
RsFiles() { return; }
virtual ~RsFiles() { return; }
public:
RsFiles() {}
virtual ~RsFiles() {}
/**
* Provides file data for the gui: media streaming or rpc clients.