Merge pull request #1281 from G10h4ck/filesharing_apiv2_improvements

Extend a bit filesharing JSON API v2
This commit is contained in:
G10h4ck 2018-06-07 17:53:26 +02:00 committed by GitHub
commit e5c72481ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.