From 133f28e1556f04cfdc36e5392796622e9832c8fe Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 26 Feb 2022 11:33:17 +0100 Subject: [PATCH] Add a bit more info to file list such as depth and file type --- fileobject.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ functions.cpp | 25 +++++++++++++++---------- functions.hpp | 5 +++-- main.cpp | 16 +++++++++------- 4 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 fileobject.hpp diff --git a/fileobject.hpp b/fileobject.hpp new file mode 100644 index 0000000..b4fcd7a --- /dev/null +++ b/fileobject.hpp @@ -0,0 +1,49 @@ +#ifndef FILEOBJECT_H +#define FILEOBJECT_H + +#include + +enum FileType { + TYPE_FILE, + TYPE_DIRECTORY, +}; + +class FileObject { +public: + FileObject( const std::string &name ) + : _name( name ) {} + + const std::string &getName() const { + return _name; + } + + const int &getDepth() const { + return _depth; + } + + const FileType &getFileType() const { + return _type; + } + + void setFileType(FileType file_type) { + _type = file_type; + } + + void setDepth(int depth) { + _depth = depth; + } + + bool operator < (const FileObject &other) const { + return _name < other.getName(); + } + + bool operator > (const FileObject &other) const { + return _name > other.getName(); + } + +private: + std::string _name; + int _depth = -1; + FileType _type = TYPE_FILE; +}; +#endif diff --git a/functions.cpp b/functions.cpp index 2278320..db6004b 100644 --- a/functions.cpp +++ b/functions.cpp @@ -58,32 +58,37 @@ void closeLibraries( std::vector< RenameLibrary > &libraries ) { } } -void addFilesRecursive(const std::string &prefix, std::vector< std::string > &results, const std::string &filename, const std::string &containing_directory, bool dir_only = false ) { +void addFilesRecursive(const FileObject &parent, std::vector< FileObject > &results, const std::string &filename, const std::string &containing_directory, bool dir_only = false ) { auto path = containing_directory + FSLib::dir_divisor + filename; if(!dir_only || FSLib::isDirectory(path)) { - results.push_back(prefix + filename); + if(!parent.getName().empty()) { + results.emplace_back(parent.getName() + FSLib::dir_divisor + filename); + } else { + results.emplace_back(filename); + } + results.back().setDepth(parent.getDepth() + 1); } if( FSLib::isDirectory(path) ) { - auto newprefix = prefix + filename + FSLib::dir_divisor; + results.back().setFileType(TYPE_DIRECTORY); for(const auto &entry : FSLib::Directory(path)) { - addFilesRecursive(newprefix, results, entry, path, dir_only); + addFilesRecursive(results.back(), results, entry, path, dir_only); } } } -std::vector< std::string > getFilesInSource( const std::string &source_dir ) { - std::vector< std::string > result; +std::vector< FileObject > getFilesInSource( const std::string &source_dir ) { + std::vector< FileObject > result; for(const auto &entry : FSLib::Directory(source_dir)) { - addFilesRecursive("", result, entry, source_dir); + addFilesRecursive(FileObject(""), result, entry, source_dir); } std::sort(result.begin(), result.end()); return result; } -std::vector< std::string > getTargetDirectories( const std::string &target_dir ) { - std::vector< std::string > result; +std::vector< FileObject > getTargetDirectories( const std::string &target_dir ) { + std::vector< FileObject > result; for(const auto &entry : FSLib::Directory(target_dir)) { - addFilesRecursive("", result, entry, target_dir, true); + addFilesRecursive(FileObject(""), result, entry, target_dir, true); } std::sort(result.begin(), result.end()); return result; diff --git a/functions.hpp b/functions.hpp index 8cc17ce..779733d 100644 --- a/functions.hpp +++ b/functions.hpp @@ -3,11 +3,12 @@ #include #include "rename_library.hpp" +#include "fileobject.hpp" std::vector< RenameLibrary > getLibraries(const std::vector> &libraries); void closeLibraries( std::vector< RenameLibrary > &libraries ); -std::vector< std::string > getFilesInSource( const std::string &source_dir ); -std::vector< std::string > getTargetDirectories( const std::string &target_dir ); +std::vector< FileObject > getFilesInSource( const std::string &source_dir ); +std::vector< FileObject > getTargetDirectories( const std::string &target_dir ); std::string safeJson(std::string input); #endif diff --git a/main.cpp b/main.cpp index 44e9d7c..30dc71c 100644 --- a/main.cpp +++ b/main.cpp @@ -12,10 +12,9 @@ #include #include "jwt.hpp" - #include "filesystem/filesystem.hpp" - #include "config/config.hpp" +#include "fileobject.hpp" std::vector libraries{}; Configuration cfg; @@ -204,7 +203,7 @@ std::string renamePathJson(const std::string &path, const RenameObject &renamer) } else { res << "false"; } - res << "\n"; + res << ",\n"; if(!rename_result.first) { res << " \"error\": \"" << safeJson(rename_result.second) << "\"\n"; } @@ -236,12 +235,15 @@ void renamePathRest( const std::shared_ptr< restbed::Session > &session, rapidjs } std::string getFilesJson() { + std::cout << "GETTING FILES JSON" << std::endl; std::ostringstream res; res << "{\n \"files\": [\n"; auto files = getFilesInSource(cfg.getSourcePath()); if(!files.empty()) { for(const auto &file : files) { - res << "\"" << safeJson(file) << "\",\n"; + res << " {\n \"path\": \"" << safeJson(file.getName()) << "\",\n"; + res << " \"depth\": " << file.getDepth() << ",\n"; + res << " \"type\": \"" << (file.getFileType() == TYPE_FILE ? "file" : "directory") << "\"\n },\n"; } res.seekp(-2, std::ios_base::end); } @@ -298,7 +300,7 @@ std::string getTargetDirectoriesJson(uint64_t id) { auto dirs = getTargetDirectories(cfg.getTargetPaths()[id].first); if(!dirs.empty()) { for(const auto &dir : dirs) { - res << " \"" << safeJson(dir) << "\",\n"; + res << " \"" << safeJson(dir.getName()) << "\",\n"; } res.seekp(-2, std::ios_base::end); } @@ -360,7 +362,7 @@ std::string moveJson(const std::string &path, uint64_t target_id, const std::str } else { res << "false"; } - res << "\n"; + res << ",\n"; if(!move_result.first) { res << " \"error\": \"" << safeJson(move_result.second) << "\"\n"; } @@ -417,7 +419,7 @@ std::string removeJson(const std::string &path) { } else { res << "false"; } - res << "\n"; + res << ",\n"; if(!remove_result.first) { res << " \"error\": \"" << safeJson(remove_result.second) << "\"\n"; }