Add a bit more info to file list such as depth and file type

This commit is contained in:
zv0n 2022-02-26 11:33:17 +01:00
parent 1565aabf70
commit 133f28e155
4 changed files with 76 additions and 19 deletions

49
fileobject.hpp Normal file
View File

@ -0,0 +1,49 @@
#ifndef FILEOBJECT_H
#define FILEOBJECT_H
#include <string>
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

View File

@ -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; auto path = containing_directory + FSLib::dir_divisor + filename;
if(!dir_only || FSLib::isDirectory(path)) { 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) ) { if( FSLib::isDirectory(path) ) {
auto newprefix = prefix + filename + FSLib::dir_divisor; results.back().setFileType(TYPE_DIRECTORY);
for(const auto &entry : FSLib::Directory(path)) { 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< FileObject > getFilesInSource( const std::string &source_dir ) {
std::vector< std::string > result; std::vector< FileObject > result;
for(const auto &entry : FSLib::Directory(source_dir)) { 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()); std::sort(result.begin(), result.end());
return result; return result;
} }
std::vector< std::string > getTargetDirectories( const std::string &target_dir ) { std::vector< FileObject > getTargetDirectories( const std::string &target_dir ) {
std::vector< std::string > result; std::vector< FileObject > result;
for(const auto &entry : FSLib::Directory(target_dir)) { 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()); std::sort(result.begin(), result.end());
return result; return result;

View File

@ -3,11 +3,12 @@
#include <vector> #include <vector>
#include "rename_library.hpp" #include "rename_library.hpp"
#include "fileobject.hpp"
std::vector< RenameLibrary > getLibraries(const std::vector<std::pair<std::string,std::string>> &libraries); std::vector< RenameLibrary > getLibraries(const std::vector<std::pair<std::string,std::string>> &libraries);
void closeLibraries( std::vector< RenameLibrary > &libraries ); void closeLibraries( std::vector< RenameLibrary > &libraries );
std::vector< std::string > getFilesInSource( const std::string &source_dir ); std::vector< FileObject > getFilesInSource( const std::string &source_dir );
std::vector< std::string > getTargetDirectories( const std::string &target_dir ); std::vector< FileObject > getTargetDirectories( const std::string &target_dir );
std::string safeJson(std::string input); std::string safeJson(std::string input);
#endif #endif

View File

@ -12,10 +12,9 @@
#include <chrono> #include <chrono>
#include "jwt.hpp" #include "jwt.hpp"
#include "filesystem/filesystem.hpp" #include "filesystem/filesystem.hpp"
#include "config/config.hpp" #include "config/config.hpp"
#include "fileobject.hpp"
std::vector<RenameLibrary> libraries{}; std::vector<RenameLibrary> libraries{};
Configuration cfg; Configuration cfg;
@ -204,7 +203,7 @@ std::string renamePathJson(const std::string &path, const RenameObject &renamer)
} else { } else {
res << "false"; res << "false";
} }
res << "\n"; res << ",\n";
if(!rename_result.first) { if(!rename_result.first) {
res << " \"error\": \"" << safeJson(rename_result.second) << "\"\n"; 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::string getFilesJson() {
std::cout << "GETTING FILES JSON" << std::endl;
std::ostringstream res; std::ostringstream res;
res << "{\n \"files\": [\n"; res << "{\n \"files\": [\n";
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 << "\"" << 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); res.seekp(-2, std::ios_base::end);
} }
@ -298,7 +300,7 @@ std::string getTargetDirectoriesJson(uint64_t id) {
auto dirs = getTargetDirectories(cfg.getTargetPaths()[id].first); auto dirs = getTargetDirectories(cfg.getTargetPaths()[id].first);
if(!dirs.empty()) { if(!dirs.empty()) {
for(const auto &dir : dirs) { for(const auto &dir : dirs) {
res << " \"" << safeJson(dir) << "\",\n"; res << " \"" << safeJson(dir.getName()) << "\",\n";
} }
res.seekp(-2, std::ios_base::end); 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 { } else {
res << "false"; res << "false";
} }
res << "\n"; res << ",\n";
if(!move_result.first) { if(!move_result.first) {
res << " \"error\": \"" << safeJson(move_result.second) << "\"\n"; res << " \"error\": \"" << safeJson(move_result.second) << "\"\n";
} }
@ -417,7 +419,7 @@ std::string removeJson(const std::string &path) {
} else { } else {
res << "false"; res << "false";
} }
res << "\n"; res << ",\n";
if(!remove_result.first) { if(!remove_result.first) {
res << " \"error\": \"" << safeJson(remove_result.second) << "\"\n"; res << " \"error\": \"" << safeJson(remove_result.second) << "\"\n";
} }