Add upload save feature

Add /api/upload-save endpoint that streams the data via the slave
assigned to the target instance to the instance saves directory, along
with a ctl command and web interface button to do the upload.
This commit is contained in:
Hornwitser committed 2021-06-01 16:27:02 +02:00
1 parent 0066a06107
commit 2e314ff098
18 files changed
+756 -36

No files matched your search

+36 -2
View File
@@ -2,7 +2,6 @@
const compression = require("compression");
const events = require("events");
const express = require("express");
const expressFileupload = require("express-fileupload");
const fs = require("fs-extra");
const http = require("http");
const https = require("https");
@@ -49,6 +48,7 @@ class Master {
this.app = express();
this.app.locals.master = this;
this.app.locals.streams = new Map();
/**
* Mapping of slave id to slave info
@@ -336,7 +336,6 @@ class Master {
hitCounter.inc();
next();
});
app.use(expressFileupload());
app.use(compression());
// Set folder to serve static content from (the website)
@@ -492,6 +491,41 @@ class Master {
});
};
}
/**
* Forward the given request to the slave contaning the instance
*
* Finds the slave which the instance with the given instance ID is
* currently assigned to and forwards the request to that instance.
* The request data must have an instance_id parameter.
*
* @param {module:lib/link.Request} request - The request to send.
* @param {object} data - Data to pass with the request.
* @param {number} data.instance_id -
* ID of instance to send this request towards
* @returns {object} data returned from the request.
*/
async forwardRequestToInstance(request, data) {
let instance = this.instances.get(data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${data.instance_id} does not exist`);
}
let slaveId = instance.config.get("instance.assigned_slave");
if (slaveId === null) {
throw new libErrors.RequestError("Instance is not assigned to a slave");
}
let connection = this.wsServer.slaveConnections.get(slaveId);
if (!connection) {
throw new libErrors.RequestError("Slave containing instance is not connected");
}
if (request.plugin && !connection.plugins.has(request.plugin)) {
throw new libErrors.RequestError(`Slave containing instance does not have ${request.plugin} plugin`);
}
return await request.send(connection, data);
}
}
module.exports = Master;