Added field options/default endpoints
This commit is contained in:
parent
49d32f9023
commit
3c9a987bbe
264
main.cpp
264
main.cpp
@ -16,24 +16,32 @@
|
|||||||
#include "config/config.hpp"
|
#include "config/config.hpp"
|
||||||
#include "fileobject.hpp"
|
#include "fileobject.hpp"
|
||||||
|
|
||||||
// TODO add customKeyOptions and customKeyDefault endpoints
|
|
||||||
|
|
||||||
std::vector< RenameLibrary > libraries{};
|
std::vector< RenameLibrary > libraries{};
|
||||||
Configuration cfg;
|
Configuration cfg;
|
||||||
|
|
||||||
void sendResponse(const std::string &response, int status_code, const std::shared_ptr< restbed::Session > &session) {
|
void sendResponse( const std::string &response, int status_code,
|
||||||
session->close(status_code, response, { { "Content-Length", std::to_string(response.length()) }, { "Access-Control-Allow-Origin", "*" } });
|
const std::shared_ptr< restbed::Session > &session ) {
|
||||||
|
session->close( status_code, response,
|
||||||
|
{ { "Content-Length", std::to_string( response.length() ) },
|
||||||
|
{ "Access-Control-Allow-Origin", "*" } } );
|
||||||
}
|
}
|
||||||
|
|
||||||
void performPostFunc(const std::shared_ptr<restbed::Session> &session, const std::function<void(const std::shared_ptr<restbed::Session>, rapidjson::GenericDocument<rapidjson::UTF8<>>&)>& callback) {
|
void performPostFunc(
|
||||||
|
const std::shared_ptr< restbed::Session > &session,
|
||||||
|
const std::function< void(
|
||||||
|
const std::shared_ptr< restbed::Session >,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > & ) > &callback ) {
|
||||||
const auto request = session->get_request();
|
const auto request = session->get_request();
|
||||||
|
|
||||||
int content_length = request->get_header( "Content-Length", 0 );
|
int content_length = request->get_header( "Content-Length", 0 );
|
||||||
|
|
||||||
session->fetch( content_length, [callback]( const std::shared_ptr< restbed::Session > &session, const restbed::Bytes & body )
|
session->fetch(
|
||||||
{
|
content_length,
|
||||||
|
[callback]( const std::shared_ptr< restbed::Session > &session,
|
||||||
|
const restbed::Bytes &body ) {
|
||||||
rapidjson::Document doc;
|
rapidjson::Document doc;
|
||||||
doc.Parse(reinterpret_cast<const char*>(body.data()), body.size());
|
doc.Parse( reinterpret_cast< const char * >( body.data() ),
|
||||||
|
body.size() );
|
||||||
if ( doc.HasParseError() ) {
|
if ( doc.HasParseError() ) {
|
||||||
sendResponse( "ERROR: Invalid body!", 401, session );
|
sendResponse( "ERROR: Invalid body!", 401, session );
|
||||||
return;
|
return;
|
||||||
@ -42,8 +50,10 @@ void performPostFunc(const std::shared_ptr<restbed::Session> &session, const std
|
|||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool verifyLogin( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc ) {
|
bool verifyLogin( const std::shared_ptr< restbed::Session > &session,
|
||||||
if(doc.FindMember("token") == doc.MemberEnd() || !doc["token"].IsString()) {
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
|
if ( doc.FindMember( "token" ) == doc.MemberEnd() ||
|
||||||
|
!doc["token"].IsString() ) {
|
||||||
sendResponse( "ERROR: Invalid token!", 401, session );
|
sendResponse( "ERROR: Invalid token!", 401, session );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -70,8 +80,12 @@ std::string getLibrariesJson() {
|
|||||||
if ( !libraries.empty() ) {
|
if ( !libraries.empty() ) {
|
||||||
for ( const auto &library : libraries ) {
|
for ( const auto &library : libraries ) {
|
||||||
result << " {\n \"id\": " << library.second << ",\n";
|
result << " {\n \"id\": " << library.second << ",\n";
|
||||||
result << " \"name\": \"" << safeJson(library.first->getName()) << "\"\n,";
|
result << " \"name\": \"" << safeJson( library.first->getName() )
|
||||||
result << " \"multiple_files\": " << (library.first->canRenameMultipleFiles() ? "true" : "false") << "\n },\n";
|
<< "\"\n,";
|
||||||
|
result << " \"multiple_files\": "
|
||||||
|
<< ( library.first->canRenameMultipleFiles() ? "true"
|
||||||
|
: "false" )
|
||||||
|
<< "\n },\n";
|
||||||
}
|
}
|
||||||
result.seekp( -2, std::ios_base::end );
|
result.seekp( -2, std::ios_base::end );
|
||||||
result << "\n";
|
result << "\n";
|
||||||
@ -114,8 +128,8 @@ std::string getOptionsJson(const RenameObject &search) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void getOptionsRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void getOptionsRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
{
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( doc.HasParseError() ) {
|
if ( doc.HasParseError() ) {
|
||||||
sendResponse( "ERROR: Invalid body!", 401, session );
|
sendResponse( "ERROR: Invalid body!", 401, session );
|
||||||
return;
|
return;
|
||||||
@ -123,7 +137,8 @@ void getOptionsRest( const std::shared_ptr< restbed::Session > &session, rapidjs
|
|||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("info") == doc.MemberEnd() || !doc["info"].IsObject()) {
|
if ( doc.FindMember( "info" ) == doc.MemberEnd() ||
|
||||||
|
!doc["info"].IsObject() ) {
|
||||||
sendResponse( "ERROR: Invalid search!", 401, session );
|
sendResponse( "ERROR: Invalid search!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -140,12 +155,27 @@ void getOptionsRest( const std::shared_ptr< restbed::Session > &session, rapidjs
|
|||||||
sendResponse( getOptionsJson( search ), 200, session );
|
sendResponse( getOptionsJson( search ), 200, session );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector< std::unordered_map< std::string, std::string > > getCustomKeys(size_t library_id) {
|
std::vector< std::unordered_map< std::string, std::string > >
|
||||||
|
getCustomKeys( size_t library_id ) {
|
||||||
if ( library_id >= libraries.size() ) {
|
if ( library_id >= libraries.size() ) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto result = libraries[library_id].getCustomKeys();
|
return libraries[library_id].getCustomKeys();
|
||||||
return result;
|
}
|
||||||
|
|
||||||
|
std::vector< std::pair< string, string > >
|
||||||
|
getFieldOptions( size_t library_id, const std::string &field ) {
|
||||||
|
if ( library_id >= libraries.size() ) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
return libraries[library_id].getCustomKeyOptions( field );
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getFieldDefault( size_t library_id, const std::string &field ) {
|
||||||
|
if ( library_id >= libraries.size() ) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return libraries[library_id].getCustomKeyDefault( field );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getCustomKeysJson( size_t library_id ) {
|
std::string getCustomKeysJson( size_t library_id ) {
|
||||||
@ -167,9 +197,37 @@ std::string getCustomKeysJson(size_t library_id) {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void getCustomKeysRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
std::string getFieldOptionsJson( size_t library_id, const std::string &field ) {
|
||||||
{
|
std::ostringstream res;
|
||||||
if(doc.FindMember("library_id") == doc.MemberEnd() || !doc["library_id"].IsUint64()) {
|
res << "{\n \"options\": [\n";
|
||||||
|
auto options = getFieldOptions( library_id, field );
|
||||||
|
if ( !options.empty() ) {
|
||||||
|
for ( auto &option : options ) {
|
||||||
|
res << " {\n";
|
||||||
|
// TODO replace first/second with a proper name, create option
|
||||||
|
// struct or something
|
||||||
|
res << " \"code\": \"" << safeJson( option.first ) << "\",\n";
|
||||||
|
res << " \"name\": \"" << safeJson( option.second ) << "\"\n";
|
||||||
|
res << " },\n";
|
||||||
|
}
|
||||||
|
res.seekp( -2, std::ios_base::end );
|
||||||
|
res << "\n";
|
||||||
|
}
|
||||||
|
res << " ]\n}";
|
||||||
|
return res.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getFieldDefaultJson( size_t library_id, const std::string &field ) {
|
||||||
|
std::ostringstream res;
|
||||||
|
res << "{\n \"default\": \"" << getFieldDefault( library_id, field )
|
||||||
|
<< "\"}";
|
||||||
|
return res.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void getCustomKeysRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
|
if ( doc.FindMember( "library_id" ) == doc.MemberEnd() ||
|
||||||
|
!doc["library_id"].IsUint64() ) {
|
||||||
sendResponse( "ERROR: Invalid library_id!", 401, session );
|
sendResponse( "ERROR: Invalid library_id!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -177,7 +235,44 @@ void getCustomKeysRest( const std::shared_ptr< restbed::Session > &session, rapi
|
|||||||
sendResponse( getCustomKeysJson( library_id ), 200, session );
|
sendResponse( getCustomKeysJson( library_id ), 200, session );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> renamePath(std::string path, const RenameObject &renamer) {
|
void getFieldOptionsRest(
|
||||||
|
const std::shared_ptr< restbed::Session > &session,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
|
if ( doc.FindMember( "library_id" ) == doc.MemberEnd() ||
|
||||||
|
!doc["library_id"].IsUint64() ) {
|
||||||
|
sendResponse( "ERROR: Invalid library_id!", 401, session );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( doc.FindMember( "field" ) == doc.MemberEnd() ||
|
||||||
|
!doc["field"].IsString() ) {
|
||||||
|
sendResponse( "ERROR: Invalid field!", 401, session );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto library_id = doc["library_id"].GetUint64();
|
||||||
|
auto field = doc["field"].GetString();
|
||||||
|
sendResponse( getFieldOptionsJson( library_id, field ), 200, session );
|
||||||
|
}
|
||||||
|
|
||||||
|
void getFieldDefaultRest(
|
||||||
|
const std::shared_ptr< restbed::Session > &session,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
|
if ( doc.FindMember( "library_id" ) == doc.MemberEnd() ||
|
||||||
|
!doc["library_id"].IsUint64() ) {
|
||||||
|
sendResponse( "ERROR: Invalid library_id!", 401, session );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( doc.FindMember( "field" ) == doc.MemberEnd() ||
|
||||||
|
!doc["field"].IsString() ) {
|
||||||
|
sendResponse( "ERROR: Invalid field!", 401, session );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto library_id = doc["library_id"].GetUint64();
|
||||||
|
auto field = doc["field"].GetString();
|
||||||
|
sendResponse( getFieldDefaultJson( library_id, field ), 200, session );
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair< bool, std::string > renamePath( std::string path,
|
||||||
|
const RenameObject &renamer ) {
|
||||||
if ( renamer.getLibraryId() >= libraries.size() ) {
|
if ( renamer.getLibraryId() >= libraries.size() ) {
|
||||||
return { false, "Invalid library id" };
|
return { false, "Invalid library id" };
|
||||||
}
|
}
|
||||||
@ -186,17 +281,20 @@ std::pair<bool, std::string> renamePath(std::string path, const RenameObject &re
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto canon_path = FSLib::canonical( path );
|
auto canon_path = FSLib::canonical( path );
|
||||||
if(canon_path.substr(0, cfg.getSourcePath().length()) != cfg.getSourcePath()) {
|
if ( canon_path.substr( 0, cfg.getSourcePath().length() ) !=
|
||||||
|
cfg.getSourcePath() ) {
|
||||||
return { false, "Invalid path" };
|
return { false, "Invalid path" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !FSLib::exists( path ) ) {
|
if ( !FSLib::exists( path ) ) {
|
||||||
return { false, "Source doesn't exist" };
|
return { false, "Source doesn't exist" };
|
||||||
}
|
}
|
||||||
return {libraries[renamer.getLibraryId()].renamePath(path, renamer), "Library error"};
|
return { libraries[renamer.getLibraryId()].renamePath( path, renamer ),
|
||||||
|
"Library error" };
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string renamePathJson(const std::string &path, const RenameObject &renamer) {
|
std::string renamePathJson( const std::string &path,
|
||||||
|
const RenameObject &renamer ) {
|
||||||
std::ostringstream res;
|
std::ostringstream res;
|
||||||
res << "{\n \"success\": ";
|
res << "{\n \"success\": ";
|
||||||
auto rename_result = renamePath( path, renamer );
|
auto rename_result = renamePath( path, renamer );
|
||||||
@ -213,16 +311,19 @@ std::string renamePathJson(const std::string &path, const RenameObject &renamer)
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void renamePathRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc ) {
|
void renamePathRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("path") == doc.MemberEnd() || !doc["path"].IsString()) {
|
if ( doc.FindMember( "path" ) == doc.MemberEnd() ||
|
||||||
|
!doc["path"].IsString() ) {
|
||||||
// TODO validate path, also validate against config
|
// TODO validate path, also validate against config
|
||||||
sendResponse( "ERROR: Invalid path!", 401, session );
|
sendResponse( "ERROR: Invalid path!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("info") == doc.MemberEnd() || !doc["info"].IsObject()) {
|
if ( doc.FindMember( "info" ) == doc.MemberEnd() ||
|
||||||
|
!doc["info"].IsObject() ) {
|
||||||
sendResponse( "ERROR: Invalid info!", 401, session );
|
sendResponse( "ERROR: Invalid info!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -242,9 +343,12 @@ std::string getFilesJson() {
|
|||||||
auto files = getFilesInSource( cfg.getSourcePath() );
|
auto files = getFilesInSource( cfg.getSourcePath() );
|
||||||
if ( !files.empty() ) {
|
if ( !files.empty() ) {
|
||||||
for ( const auto &file : files ) {
|
for ( const auto &file : files ) {
|
||||||
res << " {\n \"path\": \"" << safeJson(file.getPath()) << "\",\n";
|
res << " {\n \"path\": \"" << safeJson( file.getPath() )
|
||||||
|
<< "\",\n";
|
||||||
res << " \"depth\": " << file.getDepth() << ",\n";
|
res << " \"depth\": " << file.getDepth() << ",\n";
|
||||||
res << " \"type\": \"" << (file.getFileType() == TYPE_FILE ? "file" : "directory") << "\"\n },\n";
|
res << " \"type\": \""
|
||||||
|
<< ( file.getFileType() == TYPE_FILE ? "file" : "directory" )
|
||||||
|
<< "\"\n },\n";
|
||||||
}
|
}
|
||||||
res.seekp( -2, std::ios_base::end );
|
res.seekp( -2, std::ios_base::end );
|
||||||
}
|
}
|
||||||
@ -252,8 +356,8 @@ std::string getFilesJson() {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void getFilesRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void getFilesRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
{
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -275,8 +379,10 @@ std::string getTargetsJson() {
|
|||||||
auto targets = getTargets();
|
auto targets = getTargets();
|
||||||
if ( !targets.empty() ) {
|
if ( !targets.empty() ) {
|
||||||
for ( const auto &target : targets ) {
|
for ( const auto &target : targets ) {
|
||||||
res << " {\n" << " \"id\": " << target.first << ",\n";
|
res << " {\n"
|
||||||
res << " \"name\": \"" << safeJson(target.second) << "\"\n },\n";
|
<< " \"id\": " << target.first << ",\n";
|
||||||
|
res << " \"name\": \"" << safeJson( target.second )
|
||||||
|
<< "\"\n },\n";
|
||||||
}
|
}
|
||||||
res.seekp( -2, std::ios_base::end );
|
res.seekp( -2, std::ios_base::end );
|
||||||
}
|
}
|
||||||
@ -284,8 +390,8 @@ std::string getTargetsJson() {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void getTargetsRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void getTargetsRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
{
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -309,25 +415,30 @@ std::string getTargetDirectoriesJson(uint64_t id) {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void getTargetDirectoriesRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void getTargetDirectoriesRest(
|
||||||
{
|
const std::shared_ptr< restbed::Session > &session,
|
||||||
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint64_t id = 0;
|
uint64_t id = 0;
|
||||||
if(doc.FindMember("path_id") == doc.MemberEnd() || !doc["path_id"].IsUint64() || (id = doc["path_id"].GetUint64()) >= cfg.getTargetPaths().size()) {
|
if ( doc.FindMember( "path_id" ) == doc.MemberEnd() ||
|
||||||
|
!doc["path_id"].IsUint64() ||
|
||||||
|
( id = doc["path_id"].GetUint64() ) >= cfg.getTargetPaths().size() ) {
|
||||||
sendResponse( "ERROR: Invalid path_id!", 401, session );
|
sendResponse( "ERROR: Invalid path_id!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendResponse( getTargetDirectoriesJson( id ), restbed::OK, session );
|
sendResponse( getTargetDirectoriesJson( id ), restbed::OK, session );
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> move(std::string path, uint64_t target_id, const std::string &containing_dir) {
|
std::pair< bool, std::string > move( std::string path, uint64_t target_id,
|
||||||
|
const std::string &containing_dir ) {
|
||||||
if ( path[0] != '/' ) {
|
if ( path[0] != '/' ) {
|
||||||
path = cfg.getSourcePath() + "/" + path;
|
path = cfg.getSourcePath() + "/" + path;
|
||||||
}
|
}
|
||||||
auto canon_path = FSLib::canonical( path );
|
auto canon_path = FSLib::canonical( path );
|
||||||
if(canon_path.substr(0, cfg.getSourcePath().length()) != cfg.getSourcePath()) {
|
if ( canon_path.substr( 0, cfg.getSourcePath().length() ) !=
|
||||||
|
cfg.getSourcePath() ) {
|
||||||
return { false, "Invalid path" };
|
return { false, "Invalid path" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,20 +451,25 @@ std::pair<bool, std::string> move(std::string path, uint64_t target_id, const st
|
|||||||
auto target_start = cfg.getTargetPaths()[target_id].first;
|
auto target_start = cfg.getTargetPaths()[target_id].first;
|
||||||
auto target_dir = target_start + FSLib::dir_divisor + containing_dir;
|
auto target_dir = target_start + FSLib::dir_divisor + containing_dir;
|
||||||
auto target_canon = FSLib::canonical( target_dir );
|
auto target_canon = FSLib::canonical( target_dir );
|
||||||
if(target_canon.substr(0, target_start.length()) != target_start && !target_canon.empty()) {
|
if ( target_canon.substr( 0, target_start.length() ) != target_start &&
|
||||||
|
!target_canon.empty() ) {
|
||||||
return { false, "Invalid target" };
|
return { false, "Invalid target" };
|
||||||
}
|
}
|
||||||
// might result in needless false positives, but better be safe than sorry
|
// might result in needless false positives, but better be safe than sorry
|
||||||
if(target_canon.empty() && target_dir.find("..") != std::string::npos ) {
|
if ( target_canon.empty() &&
|
||||||
|
target_dir.find( ".." ) != std::string::npos ) {
|
||||||
return { false, "Invalid target" };
|
return { false, "Invalid target" };
|
||||||
}
|
}
|
||||||
if ( !FSLib::exists( target_dir ) ) {
|
if ( !FSLib::exists( target_dir ) ) {
|
||||||
FSLib::createDirectoryFull( target_dir );
|
FSLib::createDirectoryFull( target_dir );
|
||||||
}
|
}
|
||||||
return {FSLib::rename(path, target_dir + FSLib::dir_divisor + FSLib::getFileName(path)), "Library error"};
|
return { FSLib::rename( path, target_dir + FSLib::dir_divisor +
|
||||||
|
FSLib::getFileName( path ) ),
|
||||||
|
"Library error" };
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string moveJson(const std::string &path, uint64_t target_id, const std::string &containing_dir) {
|
std::string moveJson( const std::string &path, uint64_t target_id,
|
||||||
|
const std::string &containing_dir ) {
|
||||||
std::ostringstream res;
|
std::ostringstream res;
|
||||||
res << "{\n \"success\": ";
|
res << "{\n \"success\": ";
|
||||||
auto move_result = move( path, target_id, containing_dir );
|
auto move_result = move( path, target_id, containing_dir );
|
||||||
@ -370,23 +486,28 @@ std::string moveJson(const std::string &path, uint64_t target_id, const std::str
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void moveRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
{
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string containing_dir;
|
std::string containing_dir;
|
||||||
if(doc.FindMember("path") == doc.MemberEnd() || !doc["path"].IsString()) {
|
if ( doc.FindMember( "path" ) == doc.MemberEnd() ||
|
||||||
|
!doc["path"].IsString() ) {
|
||||||
// TODO validate path, also validate against config
|
// TODO validate path, also validate against config
|
||||||
sendResponse( "ERROR: Invalid path!", 401, session );
|
sendResponse( "ERROR: Invalid path!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint64_t id = 0;
|
uint64_t id = 0;
|
||||||
if(doc.FindMember("target_id") == doc.MemberEnd() || !doc["target_id"].IsUint64() || (id = doc["target_id"].GetUint64()) >= cfg.getTargetPaths().size()) {
|
if ( doc.FindMember( "target_id" ) == doc.MemberEnd() ||
|
||||||
|
!doc["target_id"].IsUint64() ||
|
||||||
|
( id = doc["target_id"].GetUint64() ) >=
|
||||||
|
cfg.getTargetPaths().size() ) {
|
||||||
sendResponse( "ERROR: Invalid target_id!", 401, session );
|
sendResponse( "ERROR: Invalid target_id!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("containing_dir") != doc.MemberEnd() && doc["containing_dir"].IsString()) {
|
if ( doc.FindMember( "containing_dir" ) != doc.MemberEnd() &&
|
||||||
|
doc["containing_dir"].IsString() ) {
|
||||||
containing_dir = doc["containing_dir"].GetString();
|
containing_dir = doc["containing_dir"].GetString();
|
||||||
}
|
}
|
||||||
std::string path = doc["path"].GetString();
|
std::string path = doc["path"].GetString();
|
||||||
@ -400,7 +521,8 @@ std::pair<bool, std::string> remove(std::string path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto canon_path = FSLib::canonical( path );
|
auto canon_path = FSLib::canonical( path );
|
||||||
if(canon_path.substr(0, cfg.getSourcePath().length()) != cfg.getSourcePath()) {
|
if ( canon_path.substr( 0, cfg.getSourcePath().length() ) !=
|
||||||
|
cfg.getSourcePath() ) {
|
||||||
return { false, "Invalid path" };
|
return { false, "Invalid path" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -427,12 +549,13 @@ std::string removeJson(const std::string &path) {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc )
|
void removeRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
{
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
if ( !verifyLogin( session, doc ) ) {
|
if ( !verifyLogin( session, doc ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("path") == doc.MemberEnd() || !doc["path"].IsString()) {
|
if ( doc.FindMember( "path" ) == doc.MemberEnd() ||
|
||||||
|
!doc["path"].IsString() ) {
|
||||||
// TODO validate path, also validate against config
|
// TODO validate path, also validate against config
|
||||||
sendResponse( "ERROR: Invalid path!", 401, session );
|
sendResponse( "ERROR: Invalid path!", 401, session );
|
||||||
return;
|
return;
|
||||||
@ -448,12 +571,15 @@ std::string loginJson(const std::string &user) {
|
|||||||
return res.str();
|
return res.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loginRest( const std::shared_ptr< restbed::Session > &session, rapidjson::GenericDocument<rapidjson::UTF8<>> &doc ) {
|
void loginRest( const std::shared_ptr< restbed::Session > &session,
|
||||||
if(doc.FindMember("user") == doc.MemberEnd() || !doc["user"].IsString()) {
|
rapidjson::GenericDocument< rapidjson::UTF8<> > &doc ) {
|
||||||
|
if ( doc.FindMember( "user" ) == doc.MemberEnd() ||
|
||||||
|
!doc["user"].IsString() ) {
|
||||||
sendResponse( "ERROR: Invalid user!", 401, session );
|
sendResponse( "ERROR: Invalid user!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(doc.FindMember("password") == doc.MemberEnd() || !doc["password"].IsString()) {
|
if ( doc.FindMember( "password" ) == doc.MemberEnd() ||
|
||||||
|
!doc["password"].IsString() ) {
|
||||||
sendResponse( "ERROR: Invalid password!", 401, session );
|
sendResponse( "ERROR: Invalid password!", 401, session );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -481,6 +607,14 @@ void getCustomKeysCall(const std::shared_ptr<restbed::Session> &session) {
|
|||||||
performPostFunc( session, getCustomKeysRest );
|
performPostFunc( session, getCustomKeysRest );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getFieldOptionsCall( const std::shared_ptr< restbed::Session > &session ) {
|
||||||
|
performPostFunc( session, getFieldOptionsRest );
|
||||||
|
}
|
||||||
|
|
||||||
|
void getFieldDefaultCall( const std::shared_ptr< restbed::Session > &session ) {
|
||||||
|
performPostFunc( session, getFieldDefaultRest );
|
||||||
|
}
|
||||||
|
|
||||||
void renameCall( const std::shared_ptr< restbed::Session > &session ) {
|
void renameCall( const std::shared_ptr< restbed::Session > &session ) {
|
||||||
performPostFunc( session, renamePathRest );
|
performPostFunc( session, renamePathRest );
|
||||||
}
|
}
|
||||||
@ -493,7 +627,8 @@ void getTargetsCall(const std::shared_ptr<restbed::Session> &session) {
|
|||||||
performPostFunc( session, getTargetsRest );
|
performPostFunc( session, getTargetsRest );
|
||||||
}
|
}
|
||||||
|
|
||||||
void getTargetDirectoriesCall(const std::shared_ptr<restbed::Session> &session) {
|
void getTargetDirectoriesCall(
|
||||||
|
const std::shared_ptr< restbed::Session > &session ) {
|
||||||
performPostFunc( session, getTargetDirectoriesRest );
|
performPostFunc( session, getTargetDirectoriesRest );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -534,6 +669,16 @@ int main(int argc, char **argv) {
|
|||||||
custom_fields->set_method_handler( "POST", getCustomKeysCall );
|
custom_fields->set_method_handler( "POST", getCustomKeysCall );
|
||||||
service.publish( custom_fields );
|
service.publish( custom_fields );
|
||||||
|
|
||||||
|
auto field_options = std::make_shared< restbed::Resource >();
|
||||||
|
field_options->set_path( "/get_field_options" );
|
||||||
|
field_options->set_method_handler( "POST", getFieldOptionsCall );
|
||||||
|
service.publish( field_options );
|
||||||
|
|
||||||
|
auto field_default = std::make_shared< restbed::Resource >();
|
||||||
|
field_default->set_path( "/get_field_default" );
|
||||||
|
field_default->set_method_handler( "POST", getFieldDefaultCall );
|
||||||
|
service.publish( field_default );
|
||||||
|
|
||||||
auto rename_path = std::make_shared< restbed::Resource >();
|
auto rename_path = std::make_shared< restbed::Resource >();
|
||||||
rename_path->set_path( "/rename" );
|
rename_path->set_path( "/rename" );
|
||||||
rename_path->set_method_handler( "POST", renameCall );
|
rename_path->set_method_handler( "POST", renameCall );
|
||||||
@ -551,7 +696,8 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
auto get_target_directories = std::make_shared< restbed::Resource >();
|
auto get_target_directories = std::make_shared< restbed::Resource >();
|
||||||
get_target_directories->set_path( "/get_target_directories" );
|
get_target_directories->set_path( "/get_target_directories" );
|
||||||
get_target_directories->set_method_handler( "POST", getTargetDirectoriesCall );
|
get_target_directories->set_method_handler( "POST",
|
||||||
|
getTargetDirectoriesCall );
|
||||||
service.publish( get_target_directories );
|
service.publish( get_target_directories );
|
||||||
|
|
||||||
auto move = std::make_shared< restbed::Resource >();
|
auto move = std::make_shared< restbed::Resource >();
|
||||||
|
Loading…
Reference in New Issue
Block a user