Last active
June 25, 2025 00:59
-
-
Save xeecos/56c864f19db667e6b6635ddc40dec518 to your computer and use it in GitHub Desktop.
linux directory list, get,add,delete file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "json.hpp" | |
| #include <filesystem> | |
| namespace fs = std::filesystem; | |
| using json = nlohmann::json; | |
| typedef enum FILE_ACTION | |
| { | |
| READ = 0, | |
| UPLOAD, | |
| DELETE, | |
| } | |
| FILE_ACTION; | |
| /** C++11 | |
| long long get_file_size(const std::string& filePath) { | |
| struct stat st; | |
| if (stat(filePath.c_str(), &st) == 0) { | |
| return st.st_size; | |
| } | |
| return -1; // Error | |
| } | |
| int read_directory(std::string directory_path) { | |
| DIR *dp; | |
| struct dirent *dirp; | |
| dp = opendir(directory_path.c_str()); | |
| if (dp == nullptr) { | |
| std::cerr << "Error opening directory: " << directory_path << std::endl; | |
| return 1; | |
| } | |
| while ((dirp = readdir(dp)) != nullptr) | |
| { | |
| long long file_size = -1; | |
| if(dirp->d_type == DT_REG) | |
| { | |
| std::string filepath = directory_path+dirp->d_name; | |
| file_size = get_file_size(filepath); | |
| } | |
| printf("%s:%d - %lld\n",dirp->d_name, dirp->d_type, file_size); // Print file/directory name | |
| } | |
| closedir(dp); | |
| return 0; | |
| } | |
| */ | |
| svr.Get("/list", [](const Request &req, Response &res) { | |
| std::string folder_path = "./"; | |
| if(req.has_param("dir")) | |
| { | |
| folder_path = req.get_param_value("dir"); | |
| } | |
| json json_array = json::array(); | |
| if(fs::exists(folder_path) && fs::is_directory(folder_path)) | |
| { | |
| for (const auto& entry : fs::directory_iterator(folder_path)) | |
| { | |
| if (entry.is_regular_file()) | |
| { | |
| // printf("file:%s\n",entry.path().string().c_str()); | |
| json file_info; | |
| file_info["name"] = entry.path().filename().string(); | |
| file_info["type"] = "file"; | |
| file_info["size"] = entry.file_size(); | |
| json_array.push_back(file_info); | |
| } | |
| else | |
| { | |
| json file_info; | |
| file_info["name"] = entry.path().filename().string(); | |
| file_info["type"] = "dir"; | |
| json_array.push_back(file_info); | |
| } | |
| } | |
| } | |
| res.set_content(json_array.dump(), "application/json"); | |
| }); | |
| svr.Get("/file", [](const Request &req, Response &res) | |
| { | |
| if(req.has_param("file")) | |
| { | |
| string file_path = req.get_param_value("file"); | |
| if(fs::exists(file_path)) | |
| { | |
| FILE_ACTION action = FILE_ACTION::READ; | |
| if(req.has_param("action")) | |
| { | |
| action = (FILE_ACTION)atoi(req.get_param_value("action").c_str()); | |
| } | |
| switch(action) | |
| { | |
| case FILE_ACTION::READ: | |
| { | |
| res.set_file_content(file_path); | |
| break; | |
| } | |
| case FILE_ACTION::UPLOAD: | |
| { | |
| if(req.has_file("content")) | |
| { | |
| auto file = req.get_file_value("content"); | |
| ofstream ofs(file.filename, ios::binary); | |
| ofs << file.content; | |
| return res.set_content("{\"res\":\"upload done\"}", "application/json"); | |
| } | |
| return res.set_content("{\"res\":\"upload fail\"}", "application/json"); | |
| break; | |
| } | |
| case FILE_ACTION::DELETE: | |
| { | |
| fs::remove(file_path); | |
| return res.set_content("{\"res\":\"delete done\"}", "application/json"); | |
| break; | |
| } | |
| } | |
| return res.set_file_content(file_path); | |
| } | |
| } | |
| return res.set_content("{\"res\":\"file not found\"}", "application/json"); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment