Bug fixing
This commit is contained in:
@@ -50,7 +50,7 @@ Public read actions:
|
|||||||
curl -X PUT \
|
curl -X PUT \
|
||||||
-H "Authorization: Bearer YOUR_TOKEN" \
|
-H "Authorization: Bearer YOUR_TOKEN" \
|
||||||
-F "file=@/path/to/your/file.txt" \
|
-F "file=@/path/to/your/file.txt" \
|
||||||
-F 'metadata={"labeltag":"example:latest","description":"Example file","tags":["test","example"],"custom_field":"custom value"}' \
|
-F 'metadata={"label":"example","tags":["latest","test","example"],"description":"Example file","custom_field":"custom value"}' \
|
||||||
"http://localhost:8123/upload"
|
"http://localhost:8123/upload"
|
||||||
```
|
```
|
||||||
- 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.
|
||||||
|
@@ -9,6 +9,18 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
IMPORTANT: handling of tags.
|
||||||
|
|
||||||
|
When an object is uploaded:
|
||||||
|
- the object is stored in the object store
|
||||||
|
- the database is primarily
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
namespace simple_object_storage {
|
namespace simple_object_storage {
|
||||||
|
|
||||||
PutHandler::PutHandler(Server& server) : server_(server) {}
|
PutHandler::PutHandler(Server& server) : server_(server) {}
|
||||||
@@ -55,19 +67,25 @@ void PutHandler::handle_put_object(const httplib::Request& req, httplib::Respons
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate required metadata fields
|
// Validate required metadata fields
|
||||||
if (!metadata.contains("labeltag")) {
|
if (!metadata.contains("label")) {
|
||||||
res.status = 400;
|
res.status = 400;
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Missing required metadata field: labeltag"}};
|
nlohmann::json response = {{"result", "error"}, {"error", "Missing required metadata field: label"}};
|
||||||
res.set_content(response.dump(), "application/json");
|
res.set_content(response.dump(), "application/json");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract labeltag and validate format
|
if (!metadata.contains("tags") || !metadata["tags"].is_array() || metadata["tags"].empty()) {
|
||||||
std::string labeltag = metadata["labeltag"];
|
|
||||||
auto [label, tag] = server_.parse_label_tag(labeltag);
|
|
||||||
if (label.empty() || tag.empty()) {
|
|
||||||
res.status = 400;
|
res.status = 400;
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Invalid label:tag format"}};
|
nlohmann::json response = {{"result", "error"}, {"error", "Missing or invalid required metadata field: tags (must be non-empty array)"}};
|
||||||
|
res.set_content(response.dump(), "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract label and tags
|
||||||
|
std::string label = metadata["label"];
|
||||||
|
if (label.empty()) {
|
||||||
|
res.status = 400;
|
||||||
|
nlohmann::json response = {{"result", "error"}, {"error", "Label cannot be empty"}};
|
||||||
res.set_content(response.dump(), "application/json");
|
res.set_content(response.dump(), "application/json");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -144,11 +162,23 @@ void PutHandler::handle_put_object(const httplib::Request& req, httplib::Respons
|
|||||||
|
|
||||||
// Update database index
|
// Update database index
|
||||||
dbEntry entry;
|
dbEntry entry;
|
||||||
entry.label_tag = labeltag;
|
|
||||||
entry.hash = std::to_string(hash);
|
entry.hash = std::to_string(hash);
|
||||||
entry.metadata = metadata; // Store the complete metadata
|
entry.metadata = metadata; // Store the complete metadata
|
||||||
|
|
||||||
|
// For each tag, create a label:tag entry
|
||||||
|
bool success = true;
|
||||||
|
for (const auto& tag : metadata["tags"]) {
|
||||||
|
std::string tag_str = tag.get<std::string>();
|
||||||
|
if (tag_str.empty()) continue; // Skip empty tags
|
||||||
|
|
||||||
|
entry.label_tag = label + ":" + tag_str;
|
||||||
if (!server_.db_->update_or_insert(entry)) {
|
if (!server_.db_->update_or_insert(entry)) {
|
||||||
|
success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update database index"}};
|
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update database index"}};
|
||||||
res.set_content(response.dump(), "application/json");
|
res.set_content(response.dump(), "application/json");
|
||||||
|
13
test.sh
13
test.sh
@@ -211,6 +211,13 @@ UPLOAD_RESPONSE=$(curl -X PUT \
|
|||||||
|
|
||||||
FIRST_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
|
FIRST_HASH=$(echo ${UPLOAD_RESPONSE} | jq -r '.hash')
|
||||||
|
|
||||||
|
# Store first version's metadata before uploading second version
|
||||||
|
FIRST_METADATA=$(curl -s "${BASE_URL}/meta/${BASE_TAG}:latest")
|
||||||
|
echo "First version metadata response: ${FIRST_METADATA}"
|
||||||
|
if ! echo "${FIRST_METADATA}" | jq -r '.metadata.tags[]' | grep -q 'v1'; then
|
||||||
|
die "First version does not have v1 tag"
|
||||||
|
fi
|
||||||
|
|
||||||
# Upload second version with same tag 'latest'
|
# Upload second version with same tag 'latest'
|
||||||
SECOND_METADATA_JSON=$(cat <<EOF
|
SECOND_METADATA_JSON=$(cat <<EOF
|
||||||
{
|
{
|
||||||
@@ -221,10 +228,14 @@ SECOND_METADATA_JSON=$(cat <<EOF
|
|||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if [ ! -f "${SCRIPT_DIR}/test_1GB_file_upload.sh" ]; then
|
||||||
|
die "test_1GB_file_upload.sh not found"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "uploading second version with tag 'latest'"
|
echo "uploading second version with tag 'latest'"
|
||||||
UPLOAD_RESPONSE=$(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}/test_1GB_file_upload.sh" \
|
||||||
-F "metadata=${SECOND_METADATA_JSON}" \
|
-F "metadata=${SECOND_METADATA_JSON}" \
|
||||||
"http://localhost:8123/upload")
|
"http://localhost:8123/upload")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user