Tidyin g
This commit is contained in:
@@ -50,10 +50,6 @@ curl -X PUT \
|
|||||||
- the object_file is uploaded, hashed, added to the registry (if that hash doesn't already exist), and {label:tag,hash} entries are added to the directory index.
|
- the object_file is uploaded, hashed, added to the registry (if that hash doesn't already exist), and {label:tag,hash} entries are added to the directory index.
|
||||||
- matching tags on older versions are removed.
|
- matching tags on older versions are removed.
|
||||||
|
|
||||||
### Delete Tag
|
|
||||||
- to delete a label/tag (object remains):
|
|
||||||
- `curl http://localhost:8123/deletetag?token="WRITE_TOKEN"\&labeltag="LABEL:TAG"`
|
|
||||||
|
|
||||||
### Delete Object
|
### Delete Object
|
||||||
- to delete an object (and all tags on that object):
|
- to delete an object (and all tags on that object):
|
||||||
- `curl http://localhost:8123/deleteobject?token="WRITE_TOKEN"\&hash="HASH"`
|
- `curl http://localhost:8123/deleteobject?token="WRITE_TOKEN"\&hash="HASH"`
|
||||||
|
@@ -186,21 +186,12 @@ void Server::setup_routes() {
|
|||||||
handle_get_metadata(req, res);
|
handle_get_metadata(req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Delete a label/tag (object remains)
|
|
||||||
server_.Get("/deletetag", [this](const httplib::Request& req, httplib::Response& res) {
|
|
||||||
handle_delete_tag(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);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add a tag to an existing object
|
|
||||||
server_.Get("/appendtag", [this](const httplib::Request& req, httplib::Response& res) {
|
|
||||||
handle_append_tag(req, res);
|
|
||||||
});
|
|
||||||
|
|
||||||
server_.Get("/status", [this](const httplib::Request& req, httplib::Response& res) {
|
server_.Get("/status", [this](const httplib::Request& req, httplib::Response& res) {
|
||||||
res.set_content(nlohmann::json({{"result", "success"}, {"status", "ok"}}).dump(), "application/json");
|
res.set_content(nlohmann::json({{"result", "success"}, {"status", "ok"}}).dump(), "application/json");
|
||||||
});
|
});
|
||||||
@@ -486,33 +477,6 @@ void Server::add_file_metadata(const std::string &file_path, nlohmann::json &met
|
|||||||
metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp);
|
metadata["file_modification_time"] = std::chrono::system_clock::to_time_t(sctp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::handle_delete_tag(const httplib::Request& req, httplib::Response& res) {
|
|
||||||
std::map<std::string, std::string> params;
|
|
||||||
if (!validate_write_request(req, res, {"token", "labeltag"}, params)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate label:tag format
|
|
||||||
auto [label, tag] = parse_label_tag(params["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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the label:tag from the database
|
|
||||||
if (!db_->remove(params["labeltag"])) {
|
|
||||||
res.status = 404;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Label:tag not found or deletion failed"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nlohmann::json response = {{"result", "success"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
}
|
|
||||||
|
|
||||||
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, {"token", "hash"}, params)) {
|
if (!validate_write_request(req, res, {"token", "hash"}, params)) {
|
||||||
@@ -551,57 +515,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_append_tag(const httplib::Request& req, httplib::Response& res) {
|
|
||||||
std::map<std::string, std::string> params;
|
|
||||||
if (!validate_write_request(req, res, {"token", "labeltag", "hash"}, params)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate label:tag format
|
|
||||||
auto [label, tag] = parse_label_tag(params["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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate that the hash exists as a file
|
|
||||||
std::filesystem::path file_path = config_.object_store_path / params["hash"];
|
|
||||||
if (!std::filesystem::exists(file_path) || !std::filesystem::is_regular_file(file_path)) {
|
|
||||||
res.status = 404;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Object not found for hash: " + params["hash"]}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the label:tag already exists
|
|
||||||
dbEntry existing_entry;
|
|
||||||
if (db_->get(params["labeltag"], existing_entry)) {
|
|
||||||
if (existing_entry.hash == params["hash"]) {
|
|
||||||
// Label:tag already points to this hash, nothing to do
|
|
||||||
nlohmann::json response = {{"result", "success"}, {"message", "Label:tag already points to this hash"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new entry with the label:tag pointing to the hash
|
|
||||||
dbEntry entry;
|
|
||||||
entry.label_tag = params["labeltag"];
|
|
||||||
entry.hash = params["hash"];
|
|
||||||
entry.metadata = nlohmann::json({}); // Empty metadata for appended tags
|
|
||||||
|
|
||||||
if (!db_->update_or_insert(entry)) {
|
|
||||||
res.status = 500;
|
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to append tag"}};
|
|
||||||
res.set_content(response.dump(), "application/json");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nlohmann::json response = {{"result", "success"}};
|
|
||||||
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();
|
||||||
|
@@ -28,9 +28,7 @@ private:
|
|||||||
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_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_tag(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_append_tag(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;
|
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;
|
void add_file_metadata(const std::string &file_path, nlohmann::json &metadata) const;
|
||||||
|
9
test.sh
9
test.sh
@@ -53,11 +53,16 @@ BASE_TAG="autotest"
|
|||||||
|
|
||||||
# upload this script as an object
|
# upload this script as an object
|
||||||
echo "uploading ${SCRIPT_DIR}/${SCRIPT_NAME} to ${BASE_TAG}:test1"
|
echo "uploading ${SCRIPT_DIR}/${SCRIPT_NAME} to ${BASE_TAG}:test1"
|
||||||
OBJECT_HASH=$(curl -X PUT \
|
UPLOAD_RESPONSE=$(curl -X PUT \
|
||||||
-H "Authorization: Bearer ${WRITE_TOKEN}" \
|
-H "Authorization: Bearer ${WRITE_TOKEN}" \
|
||||||
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
|
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
|
||||||
-F 'metadata={"labeltag":"${BASE_TAG}:test1","description":"Example file","tags":["test","example"],"custom_field":"custom value"}' \
|
-F 'metadata={"labeltag":"${BASE_TAG}:test1","description":"Example file","tags":["test","example"],"custom_field":"custom value"}' \
|
||||||
"http://localhost:8123/upload" | jq -r '.hash')
|
"http://localhost:8123/upload")
|
||||||
|
|
||||||
|
echo "upload response: ${UPLOAD_RESPONSE}"
|
||||||
|
|
||||||
|
OBJECT_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
|
||||||
|
|
||||||
|
|
||||||
#OBJECT_HASH=$(curl -s "${BASE_URL}/upload?token=${WRITE_TOKEN}&labeltag=${BASE_TAG}:test1&filename=${SCRIPT_NAME}" -T ${SCRIPT_DIR}/${SCRIPT_NAME} | jq -r '.hash')
|
#OBJECT_HASH=$(curl -s "${BASE_URL}/upload?token=${WRITE_TOKEN}&labeltag=${BASE_TAG}:test1&filename=${SCRIPT_NAME}" -T ${SCRIPT_DIR}/${SCRIPT_NAME} | jq -r '.hash')
|
||||||
echo "received hash ${OBJECT_HASH}"
|
echo "received hash ${OBJECT_HASH}"
|
||||||
|
Reference in New Issue
Block a user