This commit is contained in:
Your Name
2025-05-03 11:15:12 +12:00
parent a658ba1de6
commit 3c5fc11065
8 changed files with 82 additions and 18 deletions

View File

@@ -13,6 +13,7 @@
#include "server.hpp"
#include "hash.hpp"
#include "compress.hpp"
#include "string_utils.hpp" // Include the new utility header
namespace simple_object_storage {
@@ -265,7 +266,21 @@ void Server::handle_put_object(const httplib::Request& req, httplib::Response& r
return;
}
nlohmann::json metadata = get_metadata(temp_path.string());
nlohmann::json metadata;
// Check for filename query parameter
std::string filename = "";
if (req.has_param("filename")) {
filename = req.get_param_value("filename");
metadata["original_filename"] = filename;
}
// Check if filename ends with ".tgz" using the utility function
if (utils::ends_with(filename, ".tgz")) {
metadata["tgz_content_hash"] = get_hash_from_tgz(temp_path.string());
}
add_file_metadata(temp_path.string(), metadata);
// Move file to final location
std::string hash_str = std::to_string(hash);
@@ -286,9 +301,9 @@ void Server::handle_put_object(const httplib::Request& req, httplib::Response& r
dbEntry entry;
entry.label_tag = label_tag;
entry.hash = hash_str;
entry.metadata = metadata; // Empty metadata for now
entry.metadata = metadata; // Store the potentially updated metadata
if (!db_->insert(entry)) {
if (!db_->update_or_insert(entry)) {
res.status = 500;
res.set_content("Failed to update database index", "text/plain");
// Attempt to clean up the moved file if index fails
@@ -330,10 +345,8 @@ 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)};
}
nlohmann::json Server::get_metadata(const std::string &file_path) const
void Server::add_file_metadata(const std::string &file_path, nlohmann::json &metadata) const
{
nlohmann::json metadata;
// get the file size
metadata["file_size"] = std::filesystem::file_size(file_path);
@@ -344,10 +357,6 @@ nlohmann::json Server::get_metadata(const std::string &file_path) const
+ std::chrono::system_clock::now()
);
metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp);
metadata["tgz_content_hash"] = get_hash_from_tgz(file_path);
return metadata;
}
} // namespace simple_object_storage