Loading files
a440be05bThis file is pinned to the selected release. Download the original to open it in your design tool.
/*
* Copyright (C) 2021 Sienci Labs Inc.
*
* This file is part of gSender.
*
* gSender is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, under version 3 of the License.
*
* gSender is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gSender. If not, see <https://www.gnu.org/licenses/>.
*
* Contact for information regarding this program and its license
* can be sent through gSender@sienci.com or mailed to the main office
* of Sienci Labs Inc. in Waterloo, Ontario, Canada.
*
*/
import get from "lodash/get";
import { ERR_BAD_REQUEST, ERR_INTERNAL_SERVER_ERROR } from "../constants";
import store from "../store";
export const upload = (req, res) => {
const { port, name, gcode, context = {} } = req.body;
if (!port) {
res.status(ERR_BAD_REQUEST).send({
msg: "No port specified",
});
return;
}
if (!gcode) {
res.status(ERR_BAD_REQUEST).send({
msg: "Empty G-code",
});
return;
}
const controller = store.get('controllers["' + port + '"]');
if (!controller) {
res.status(ERR_BAD_REQUEST).send({
msg: "Controller not found",
});
return;
}
// Load G-code
controller.command("gcode:load", name, gcode, context, (err, state) => {
if (err) {
res.status(ERR_INTERNAL_SERVER_ERROR).send({
msg: "Failed to load G-code: " + err,
});
return;
}
res.send({ ...state });
});
};
export const fetch = (req, res) => {
const port = req.query.port;
if (!port) {
res.status(ERR_BAD_REQUEST).send({
msg: "No port specified",
});
return;
}
const controller = store.get('controllers["' + port + '"]');
if (!controller) {
res.status(ERR_BAD_REQUEST).send({
msg: "Controller not found",
});
return;
}
const { sender } = controller;
res.send({
...sender.toJSON(),
data: sender.state.gcode,
});
};
export const download = (req, res) => {
const port = get(req, "query.port") || get(req, "body.port");
if (!port) {
res.status(ERR_BAD_REQUEST).send({
msg: "No port specified",
});
return;
}
const controller = store.get('controllers["' + port + '"]');
if (!controller) {
res.status(ERR_BAD_REQUEST).send({
msg: "Controller not found",
});
return;
}
const { sender } = controller;
const filename = sender.state.name || "noname.txt";
const content = sender.state.gcode || "";
res.setHeader(
"Content-Disposition",
"attachment; filename=" + encodeURIComponent(filename),
);
res.setHeader("Connection", "close");
res.write(content);
res.end();
};