| 1672 | } |
| 1673 | |
| 1674 | void DataStorageController::HandlePUT_nodes_uid_data(const httplib::Request& req, httplib::Response& res) |
| 1675 | { |
| 1676 | if (!m_Bridge.HasDataStorage()) |
| 1677 | { |
| 1678 | this->SendErrorResponse(res, 503, ErrorResponse::DataStorageNotAvailable(req.path)); |
| 1679 | return; |
| 1680 | } |
| 1681 | |
| 1682 | const std::string uid = req.path_params.at("uid"); |
| 1683 | if (!ValidateUid(uid)) |
| 1684 | { |
| 1685 | this->SendErrorResponse(res, 400, ErrorResponse::InvalidRequest("Invalid UID format", req.path)); |
| 1686 | return; |
| 1687 | } |
| 1688 | |
| 1689 | // Check if node exists |
| 1690 | const auto nodeJson = m_Bridge.GetNode(uid); |
| 1691 | if (!nodeJson.has_value()) |
| 1692 | { |
| 1693 | this->SendErrorResponse(res, 404, ErrorResponse::NodeNotFound(uid, req.path)); |
| 1694 | return; |
| 1695 | } |
| 1696 | |
| 1697 | // Get Content-Type header |
| 1698 | const std::string contentType = req.has_header(HEADER_CONTENT_TYPE) |
| 1699 | ? req.get_header_value(HEADER_CONTENT_TYPE) |
| 1700 | : ""; |
| 1701 | |
| 1702 | // Validate Content-Type |
| 1703 | const bool isFileReference = (contentType.find(MIME_APPLICATION_JSON) != std::string::npos); |
| 1704 | const bool isDirectTransfer = (contentType.find(MIME_APPLICATION_OCTET_STREAM) != std::string::npos); |
| 1705 | |
| 1706 | if (!isFileReference && !isDirectTransfer) |
| 1707 | { |
| 1708 | this->SendErrorResponse(res, 415, ErrorResponse::UnsupportedFormat( |
| 1709 | "Unsupported Content-Type. Use 'application/json' for file-reference mode or " |
| 1710 | "'application/octet-stream' for direct transfer mode.", req.path)); |
| 1711 | return; |
| 1712 | } |
| 1713 | |
| 1714 | // Resolve data file path |
| 1715 | auto pathResult = this->ResolveDataPath(req, contentType); |
| 1716 | if (!pathResult.success) |
| 1717 | { |
| 1718 | this->SendErrorResponse(res, pathResult.errorStatus, pathResult.errorResponse); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
| 1722 | // Helper for cleanup |
| 1723 | auto cleanupTempFile = [&]() { |
| 1724 | if (pathResult.isTemporary && !pathResult.filePath.empty()) |
| 1725 | { |
| 1726 | try { fs::remove(pathResult.filePath); } |
| 1727 | catch (const std::exception&) { /* ignore */ } |
| 1728 | } |
| 1729 | }; |
| 1730 | |
| 1731 | // Load data from file |