2025-01-14 14:42:39 +00:00
|
|
|
/* The "file" API ressource.
|
2019-04-10 18:08:57 +02:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2022-03-25 17:27:16 +01:00
|
|
|
#include <villas/api/requests/node.hpp>
|
2020-08-17 17:03:54 +02:00
|
|
|
#include <villas/api/response.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/api/session.hpp>
|
|
|
|
#include <villas/super_node.hpp>
|
2020-08-17 17:03:54 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/format.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2020-08-17 17:03:54 +02:00
|
|
|
#include <villas/nodes/file.hpp>
|
2019-04-10 18:08:57 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace api {
|
|
|
|
|
2020-08-25 20:27:26 +02:00
|
|
|
class FileRequest : public NodeRequest {
|
2019-04-10 18:08:57 +02:00
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00:00
|
|
|
using NodeRequest::NodeRequest;
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
virtual Response *execute() {
|
|
|
|
if (method != Session::Method::GET && method != Session::Method::POST)
|
|
|
|
throw InvalidMethod(this);
|
2020-08-17 17:03:54 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (body != nullptr)
|
|
|
|
throw BadRequest("File endpoint does not accept any body data");
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
NodeFactory *nf = plugin::registry->lookup<NodeFactory>("file");
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (node->getFactory() != nf)
|
|
|
|
throw BadRequest("This node is not a file node", "{ s: s }", "type",
|
|
|
|
node->getFactory()->getName());
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
auto *nc = dynamic_cast<NodeCompat *>(node);
|
|
|
|
auto *f = nc->getData<struct file>();
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (matches[2] == "rewind")
|
|
|
|
rewind(f->stream_in);
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return new Response(session, HTTP_STATUS_OK);
|
|
|
|
}
|
2019-04-10 18:08:57 +02:00
|
|
|
};
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Register API request
|
2020-08-25 20:27:26 +02:00
|
|
|
static char n[] = "node/file";
|
2020-09-03 15:06:50 +02:00
|
|
|
static char r[] = "/node/(" RE_NODE_NAME "|" RE_UUID ")/file(?:/([^/]+))?";
|
2025-01-14 14:42:39 +00:00
|
|
|
static char d[] = "Control instances of 'file' node-type";
|
2020-08-17 17:03:54 +02:00
|
|
|
static RequestPlugin<FileRequest, n, r, d> p;
|
2019-04-10 18:08:57 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
} // namespace api
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|