Tidy
This commit is contained in:
176
src/put_handler.cpp
Normal file
176
src/put_handler.cpp
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
#include "put_handler.hpp"
|
||||||
|
#include "hash.hpp"
|
||||||
|
#include "compress.hpp"
|
||||||
|
#include "string_utils.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
#include <chrono>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace simple_object_storage {
|
||||||
|
|
||||||
|
PutHandler::PutHandler(Server& server) : server_(server) {}
|
||||||
|
|
||||||
|
void PutHandler::handle_put_object(const httplib::Request& req, httplib::Response& res) {
|
||||||
|
// Check all request parameters first before processing any data
|
||||||
|
|
||||||
|
std::map<std::string, std::string> params;
|
||||||
|
if (!server_.validate_write_request(req, res, {}, params)) { // No required params now since token is in header
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Check we're in the /upload path
|
||||||
|
if (req.path != "/upload") {
|
||||||
|
res.status = 404;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Not found - put requests must be to /upload"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the multipart form data
|
||||||
|
if (!req.has_file("file")) {
|
||||||
|
res.status = 400;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "No file provided in upload"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the file data
|
||||||
|
const auto& file = req.get_file_value("file");
|
||||||
|
|
||||||
|
// Parse metadata if provided
|
||||||
|
nlohmann::json metadata;
|
||||||
|
if (req.has_file("metadata")) {
|
||||||
|
try {
|
||||||
|
const auto& metadata_file = req.get_file_value("metadata");
|
||||||
|
metadata = nlohmann::json::parse(metadata_file.content);
|
||||||
|
} catch (const nlohmann::json::parse_error& e) {
|
||||||
|
res.status = 400;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON metadata: " + std::string(e.what())}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate required metadata fields
|
||||||
|
if (!metadata.contains("labeltag")) {
|
||||||
|
res.status = 400;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Missing required metadata field: labeltag"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract labeltag and validate format
|
||||||
|
std::string labeltag = metadata["labeltag"];
|
||||||
|
auto [label, tag] = server_.parse_label_tag(labeltag);
|
||||||
|
if (label.empty() || tag.empty()) {
|
||||||
|
res.status = 400;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Invalid label:tag format"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add filename to metadata if not provided
|
||||||
|
if (!metadata.contains("filename")) {
|
||||||
|
metadata["filename"] = file.filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that all parameters are validated, process the upload
|
||||||
|
|
||||||
|
// Generate a random number for the temporary filename
|
||||||
|
std::mt19937_64 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
||||||
|
std::uniform_int_distribution<uint64_t> dist;
|
||||||
|
uint64_t random_num = dist(rng);
|
||||||
|
std::string temp_filename = "temp_" + std::to_string(random_num);
|
||||||
|
|
||||||
|
// Create temporary file
|
||||||
|
std::filesystem::path temp_path = server_.config_.object_store_path / temp_filename;
|
||||||
|
std::ofstream temp_file(temp_path, std::ios::binary);
|
||||||
|
if (!temp_file.is_open()) {
|
||||||
|
res.status = 500;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to create temporary file"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write file content to temporary file
|
||||||
|
if (!temp_file.write(file.content.c_str(), file.content.size())) {
|
||||||
|
res.status = 500;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to write to temporary file"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
temp_file.close();
|
||||||
|
std::filesystem::remove(temp_path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp_file.close();
|
||||||
|
|
||||||
|
// Ensure the temporary file is removed even if errors occur
|
||||||
|
ScopeFileDeleter temp_file_deleter(temp_path);
|
||||||
|
|
||||||
|
// Calculate hash
|
||||||
|
uint64_t hash = hash_file(temp_path.string());
|
||||||
|
if (hash == 0) {
|
||||||
|
res.status = 500;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to calculate hash"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add file metadata
|
||||||
|
add_file_metadata(temp_path.string(), metadata);
|
||||||
|
|
||||||
|
// Check if filename ends with ".tgz" using the utility function
|
||||||
|
if (utils::ends_with(metadata["filename"], ".tgz")) {
|
||||||
|
metadata["tgz_content_hash"] = get_hash_from_tgz(temp_path.string());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move file to final location
|
||||||
|
std::filesystem::path final_path = server_.config_.object_store_path / std::to_string(hash);
|
||||||
|
if (!std::filesystem::exists(final_path)) {
|
||||||
|
try {
|
||||||
|
std::filesystem::rename(temp_path, final_path);
|
||||||
|
temp_file_deleter.release();
|
||||||
|
} catch (const std::filesystem::filesystem_error& e) {
|
||||||
|
std::cerr << "Error renaming temp file: " << e.what() << std::endl;
|
||||||
|
res.status = 500;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to store object file"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update database index
|
||||||
|
dbEntry entry;
|
||||||
|
entry.label_tag = labeltag;
|
||||||
|
entry.hash = std::to_string(hash);
|
||||||
|
entry.metadata = metadata; // Store the complete metadata
|
||||||
|
|
||||||
|
if (!server_.db_->update_or_insert(entry)) {
|
||||||
|
res.status = 500;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update database index"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
// Attempt to clean up the moved file if index fails
|
||||||
|
try { if (std::filesystem::exists(final_path)) std::filesystem::remove(final_path); } catch(...) {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.set_content(nlohmann::json({{"result", "success"}, {"hash", std::to_string(hash)}}).dump(), "application/json");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PutHandler::add_file_metadata(const std::string& file_path, nlohmann::json& metadata) const {
|
||||||
|
// get the file size
|
||||||
|
metadata["file_size"] = std::filesystem::file_size(file_path);
|
||||||
|
|
||||||
|
// get the file modification time
|
||||||
|
auto ftime = std::filesystem::last_write_time(file_path);
|
||||||
|
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
||||||
|
ftime - std::filesystem::file_time_type::clock::now()
|
||||||
|
+ std::chrono::system_clock::now()
|
||||||
|
);
|
||||||
|
metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace simple_object_storage
|
21
src/put_handler.hpp
Normal file
21
src/put_handler.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <filesystem>
|
||||||
|
#include "server.hpp"
|
||||||
|
#include "httplib.hpp"
|
||||||
|
#include "json.hpp"
|
||||||
|
|
||||||
|
namespace simple_object_storage {
|
||||||
|
|
||||||
|
class PutHandler {
|
||||||
|
public:
|
||||||
|
PutHandler(Server& server);
|
||||||
|
void handle_put_object(const httplib::Request& req, httplib::Response& res);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Server& server_;
|
||||||
|
void add_file_metadata(const std::string& file_path, nlohmann::json& metadata) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace simple_object_storage
|
191
src/server.cpp
191
src/server.cpp
@@ -13,30 +13,11 @@
|
|||||||
#include "hash.hpp"
|
#include "hash.hpp"
|
||||||
#include "compress.hpp"
|
#include "compress.hpp"
|
||||||
#include "string_utils.hpp" // Include the new utility header
|
#include "string_utils.hpp" // Include the new utility header
|
||||||
|
#include "put_handler.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace simple_object_storage {
|
namespace simple_object_storage {
|
||||||
|
|
||||||
// Simple RAII helper for file deletion
|
|
||||||
class ScopeFileDeleter {
|
|
||||||
public:
|
|
||||||
ScopeFileDeleter(const std::filesystem::path& path) : path_(path), released_(false) {}
|
|
||||||
~ScopeFileDeleter() {
|
|
||||||
if (!released_) {
|
|
||||||
try {
|
|
||||||
if (std::filesystem::exists(path_)) {
|
|
||||||
std::filesystem::remove(path_);
|
|
||||||
}
|
|
||||||
} catch (const std::filesystem::filesystem_error& e) {
|
|
||||||
std::cerr << "Error deleting temp file: " << path_ << " - " << e.what() << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void release() { released_ = true; }
|
|
||||||
private:
|
|
||||||
std::filesystem::path path_;
|
|
||||||
bool released_;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool Server::init_db() {
|
bool Server::init_db() {
|
||||||
try {
|
try {
|
||||||
@@ -108,6 +89,9 @@ Server::Server(const ServerConfig& config)
|
|||||||
// Error already printed in init_db
|
// Error already printed in init_db
|
||||||
// Consider throwing or setting an error state
|
// Consider throwing or setting an error state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the put handler
|
||||||
|
put_handler_ = std::make_unique<PutHandler>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server() {
|
Server::~Server() {
|
||||||
@@ -171,7 +155,7 @@ void Server::setup_routes() {
|
|||||||
|
|
||||||
// Upload object with streaming support
|
// Upload object with streaming support
|
||||||
server_.Put("/upload", [this](const httplib::Request& req, httplib::Response& res) {
|
server_.Put("/upload", [this](const httplib::Request& req, httplib::Response& res) {
|
||||||
handle_put_object(req, res);
|
put_handler_->handle_put_object(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle PUT requests to other paths
|
// Handle PUT requests to other paths
|
||||||
@@ -186,7 +170,6 @@ void Server::setup_routes() {
|
|||||||
handle_get_metadata(req, res);
|
handle_get_metadata(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Delete an object (and all tags on that object)
|
// Delete an object (and all tags on that object)
|
||||||
server_.Get("/deleteobject", [this](const httplib::Request& req, httplib::Response& res) {
|
server_.Get("/deleteobject", [this](const httplib::Request& req, httplib::Response& res) {
|
||||||
handle_delete_object(req, res);
|
handle_delete_object(req, res);
|
||||||
@@ -286,153 +269,6 @@ void Server::handle_get_directory(const httplib::Request& /*req*/, httplib::Resp
|
|||||||
res.set_content(response.dump(), "application/json");
|
res.set_content(response.dump(), "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::handle_put_object(const httplib::Request& req, httplib::Response& res) {
|
|
||||||
// Check all request parameters first before processing any data
|
|
||||||
|
|
||||||
std::map<std::string, std::string> params;
|
|
||||||
if (!validate_write_request(req, res, {}, params)) { // No required params now since token is in header
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Check we're in the /upload path
|
|
||||||
if (req.path != "/upload") {
|
|
||||||
res.status = 404;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Not found - put requests must be to /upload"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the multipart form data
|
|
||||||
if (!req.has_file("file")) {
|
|
||||||
res.status = 400;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "No file provided in upload"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the file data
|
|
||||||
const auto& file = req.get_file_value("file");
|
|
||||||
|
|
||||||
// Parse metadata if provided
|
|
||||||
nlohmann::json metadata;
|
|
||||||
if (req.has_file("metadata")) {
|
|
||||||
try {
|
|
||||||
const auto& metadata_file = req.get_file_value("metadata");
|
|
||||||
metadata = nlohmann::json::parse(metadata_file.content);
|
|
||||||
} catch (const nlohmann::json::parse_error& e) {
|
|
||||||
res.status = 400;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON metadata: " + std::string(e.what())}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate required metadata fields
|
|
||||||
if (!metadata.contains("labeltag")) {
|
|
||||||
res.status = 400;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Missing required metadata field: labeltag"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract labeltag and validate format
|
|
||||||
std::string labeltag = metadata["labeltag"];
|
|
||||||
auto [label, tag] = parse_label_tag(labeltag);
|
|
||||||
if (label.empty() || tag.empty()) {
|
|
||||||
res.status = 400;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Invalid label:tag format"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add filename to metadata if not provided
|
|
||||||
if (!metadata.contains("filename")) {
|
|
||||||
metadata["filename"] = file.filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now that all parameters are validated, process the upload
|
|
||||||
|
|
||||||
// Generate a random number for the temporary filename
|
|
||||||
std::mt19937_64 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
|
||||||
std::uniform_int_distribution<uint64_t> dist;
|
|
||||||
uint64_t random_num = dist(rng);
|
|
||||||
std::string temp_filename = "temp_" + std::to_string(random_num);
|
|
||||||
|
|
||||||
// Create temporary file
|
|
||||||
std::filesystem::path temp_path = config_.object_store_path / temp_filename;
|
|
||||||
std::ofstream temp_file(temp_path, std::ios::binary);
|
|
||||||
if (!temp_file.is_open()) {
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to create temporary file"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write file content to temporary file
|
|
||||||
if (!temp_file.write(file.content.c_str(), file.content.size())) {
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to write to temporary file"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
temp_file.close();
|
|
||||||
std::filesystem::remove(temp_path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
temp_file.close();
|
|
||||||
|
|
||||||
// Ensure the temporary file is removed even if errors occur
|
|
||||||
ScopeFileDeleter temp_file_deleter(temp_path);
|
|
||||||
|
|
||||||
// Calculate hash
|
|
||||||
uint64_t hash = hash_file(temp_path.string());
|
|
||||||
if (hash == 0) {
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to calculate hash"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add file metadata
|
|
||||||
add_file_metadata(temp_path.string(), metadata);
|
|
||||||
|
|
||||||
// Check if filename ends with ".tgz" using the utility function
|
|
||||||
if (utils::ends_with(metadata["filename"], ".tgz")) {
|
|
||||||
metadata["tgz_content_hash"] = get_hash_from_tgz(temp_path.string());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move file to final location
|
|
||||||
std::filesystem::path final_path = config_.object_store_path / std::to_string(hash);
|
|
||||||
if (!std::filesystem::exists(final_path)) {
|
|
||||||
try {
|
|
||||||
std::filesystem::rename(temp_path, final_path);
|
|
||||||
temp_file_deleter.release();
|
|
||||||
} catch (const std::filesystem::filesystem_error& e) {
|
|
||||||
std::cerr << "Error renaming temp file: " << e.what() << std::endl;
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to store object file"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update database index
|
|
||||||
dbEntry entry;
|
|
||||||
entry.label_tag = labeltag;
|
|
||||||
entry.hash = std::to_string(hash);
|
|
||||||
entry.metadata = metadata; // Store the complete metadata
|
|
||||||
|
|
||||||
if (!db_->update_or_insert(entry)) {
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update database index"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
// Attempt to clean up the moved file if index fails
|
|
||||||
try { if (std::filesystem::exists(final_path)) std::filesystem::remove(final_path); } catch(...) {};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.set_content(nlohmann::json({{"result", "success"}, {"hash", std::to_string(hash)}}).dump(), "application/json");
|
|
||||||
}
|
|
||||||
|
|
||||||
void Server::handle_get_metadata(const httplib::Request& req, httplib::Response& res) {
|
void Server::handle_get_metadata(const httplib::Request& req, httplib::Response& res) {
|
||||||
const auto& label_tag = req.matches[1].str();
|
const auto& label_tag = req.matches[1].str();
|
||||||
|
|
||||||
@@ -463,20 +299,6 @@ std::pair<std::string, std::string> Server::parse_label_tag(const std::string& l
|
|||||||
return {label_tag.substr(0, colon_pos), label_tag.substr(colon_pos + 1)};
|
return {label_tag.substr(0, colon_pos), label_tag.substr(colon_pos + 1)};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::add_file_metadata(const std::string &file_path, nlohmann::json &metadata) const
|
|
||||||
{
|
|
||||||
// get the file size
|
|
||||||
metadata["file_size"] = std::filesystem::file_size(file_path);
|
|
||||||
|
|
||||||
// get the file modification time
|
|
||||||
auto ftime = std::filesystem::last_write_time(file_path);
|
|
||||||
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
|
|
||||||
ftime - std::filesystem::file_time_type::clock::now()
|
|
||||||
+ std::chrono::system_clock::now()
|
|
||||||
);
|
|
||||||
metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Server::handle_delete_object(const httplib::Request& req, httplib::Response& res) {
|
void Server::handle_delete_object(const httplib::Request& req, httplib::Response& res) {
|
||||||
std::map<std::string, std::string> params;
|
std::map<std::string, std::string> params;
|
||||||
if (!validate_write_request(req, res, {"hash"}, params)) {
|
if (!validate_write_request(req, res, {"hash"}, params)) {
|
||||||
@@ -515,7 +337,6 @@ void Server::handle_delete_object(const httplib::Request& req, httplib::Response
|
|||||||
res.set_content(response.dump(), "application/json");
|
res.set_content(response.dump(), "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Server::handle_exists(const httplib::Request& req, httplib::Response& res) {
|
void Server::handle_exists(const httplib::Request& req, httplib::Response& res) {
|
||||||
const auto& key = req.matches[1].str();
|
const auto& key = req.matches[1].str();
|
||||||
std::string hash_str = key;
|
std::string hash_str = key;
|
||||||
|
@@ -1,18 +1,17 @@
|
|||||||
#ifndef SERVER_HPP
|
#pragma once
|
||||||
#define SERVER_HPP
|
|
||||||
|
|
||||||
#include "config.hpp"
|
|
||||||
#include "httplib.hpp"
|
|
||||||
#include "database.hpp"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include "httplib.hpp"
|
||||||
#include <atomic>
|
#include "json.hpp"
|
||||||
#include <filesystem>
|
#include "database.hpp"
|
||||||
#include <json.hpp>
|
#include "config.hpp"
|
||||||
|
|
||||||
namespace simple_object_storage {
|
namespace simple_object_storage {
|
||||||
|
|
||||||
|
class PutHandler; // Forward declaration
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
public:
|
public:
|
||||||
Server(const ServerConfig& config);
|
Server(const ServerConfig& config);
|
||||||
@@ -20,31 +19,27 @@ public:
|
|||||||
|
|
||||||
bool start();
|
bool start();
|
||||||
void stop();
|
void stop();
|
||||||
|
bool validate_write_request(const httplib::Request& req, httplib::Response& res, const std::vector<std::string>& required_params, std::map<std::string, std::string>& params);
|
||||||
|
std::pair<std::string, std::string> parse_label_tag(const std::string& label_tag) const;
|
||||||
|
|
||||||
|
// Make these public so PutHandler can access them
|
||||||
|
ServerConfig config_;
|
||||||
|
std::unique_ptr<Database> db_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setup_routes();
|
void setup_routes();
|
||||||
void handle_get_object(const httplib::Request& req, httplib::Response& res);
|
void handle_get_object(const httplib::Request& req, httplib::Response& res);
|
||||||
void handle_get_hash(const httplib::Request& req, httplib::Response& res);
|
void handle_get_hash(const httplib::Request& req, httplib::Response& res);
|
||||||
void handle_get_directory(const httplib::Request& req, httplib::Response& res);
|
void handle_get_directory(const httplib::Request& req, httplib::Response& res);
|
||||||
void handle_put_object(const httplib::Request& req, httplib::Response& res);
|
|
||||||
void handle_get_metadata(const httplib::Request& req, httplib::Response& res);
|
void handle_get_metadata(const httplib::Request& req, httplib::Response& res);
|
||||||
void handle_delete_object(const httplib::Request& req, httplib::Response& res);
|
void handle_delete_object(const httplib::Request& req, httplib::Response& res);
|
||||||
void handle_exists(const httplib::Request& req, httplib::Response& res);
|
void handle_exists(const httplib::Request& req, httplib::Response& res);
|
||||||
std::pair<std::string, std::string> parse_label_tag(const std::string& label_tag) const;
|
|
||||||
void add_file_metadata(const std::string &file_path, nlohmann::json &metadata) const;
|
|
||||||
|
|
||||||
bool init_db();
|
bool init_db();
|
||||||
|
|
||||||
bool validate_write_request(const httplib::Request& req, httplib::Response& res, const std::vector<std::string>& required_params,
|
|
||||||
std::map<std::string, std::string>& params);
|
|
||||||
|
|
||||||
private:
|
|
||||||
const ServerConfig& config_;
|
|
||||||
httplib::Server server_;
|
httplib::Server server_;
|
||||||
std::unique_ptr<Database> db_;
|
bool running_;
|
||||||
std::atomic<bool> running_;
|
std::unique_ptr<PutHandler> put_handler_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace simple_object_storage
|
} // namespace simple_object_storage
|
||||||
|
|
||||||
#endif
|
|
28
src/utils.cpp
Normal file
28
src/utils.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
namespace simple_object_storage
|
||||||
|
{
|
||||||
|
|
||||||
|
ScopeFileDeleter::ScopeFileDeleter(const std::filesystem::path &path) : path_(path), released_(false) {}
|
||||||
|
|
||||||
|
ScopeFileDeleter::~ScopeFileDeleter()
|
||||||
|
{
|
||||||
|
if (!released_)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (std::filesystem::exists(path_))
|
||||||
|
{
|
||||||
|
std::filesystem::remove(path_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::filesystem::filesystem_error &e)
|
||||||
|
{
|
||||||
|
std::cerr << "Error deleting temp file: " << path_ << " - " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopeFileDeleter::release() { released_ = true; }
|
||||||
|
|
||||||
|
}
|
19
src/utils.hpp
Normal file
19
src/utils.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace simple_object_storage {
|
||||||
|
|
||||||
|
// Simple RAII helper for file deletion
|
||||||
|
class ScopeFileDeleter {
|
||||||
|
public:
|
||||||
|
ScopeFileDeleter(const std::filesystem::path& path);
|
||||||
|
~ScopeFileDeleter();
|
||||||
|
void release();
|
||||||
|
private:
|
||||||
|
std::filesystem::path path_;
|
||||||
|
bool released_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user