2023-12-20 03:20:04 +00:00
|
|
|
import {
|
|
|
|
createServerFolder,
|
|
|
|
getServerItem,
|
|
|
|
listServerFiles,
|
|
|
|
removeServerItem,
|
|
|
|
uploadServerItem,
|
|
|
|
} from "../k8s/server-files.js";
|
|
|
|
import { sendError } from "../util/ExpressClientError.js";
|
|
|
|
|
|
|
|
export async function listFiles(req, res) {
|
|
|
|
const serverSpec = req.body;
|
|
|
|
if (!serverSpec) return res.sendStatus(400);
|
2023-12-22 14:45:49 -07:00
|
|
|
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
2023-12-20 03:20:04 +00:00
|
|
|
listServerFiles(serverSpec)
|
|
|
|
.then((f) => {
|
|
|
|
const fileData = f.map((fi, i) => ({
|
|
|
|
name: fi.name,
|
|
|
|
isDir: fi.type === 2,
|
|
|
|
id: `${fi.name}-${i}`,
|
|
|
|
isHidden: fi.name.startsWith("."),
|
|
|
|
isSymLink: !!fi.link,
|
|
|
|
size: fi.size,
|
|
|
|
}));
|
|
|
|
res.json(fileData);
|
|
|
|
})
|
|
|
|
.catch(sendError(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createFolder(req, res) {
|
|
|
|
const serverSpec = req.body;
|
|
|
|
if (!serverSpec) return res.sendStatus(400);
|
2023-12-22 14:45:49 -07:00
|
|
|
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
2023-12-20 03:20:04 +00:00
|
|
|
if (!serverSpec.path) return res.status(400).send("Path required!");
|
|
|
|
createServerFolder(serverSpec)
|
|
|
|
.then(() => res.sendStatus(200))
|
|
|
|
.catch(sendError(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteItem(req, res) {
|
|
|
|
const serverSpec = req.body;
|
|
|
|
if (!serverSpec) return res.sendStatus(400);
|
2023-12-22 14:45:49 -07:00
|
|
|
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
2023-12-20 03:20:04 +00:00
|
|
|
if (!serverSpec.path) return res.status(400).send("Path required!");
|
|
|
|
if (serverSpec.isDir === undefined || serverSpec.isDir === null)
|
|
|
|
return res.status(400).send("IsDIr required!");
|
|
|
|
removeServerItem(serverSpec)
|
|
|
|
.then(() => res.sendStatus(200))
|
|
|
|
.catch(sendError(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function uploadItem(req, res) {
|
|
|
|
const serverSpec = req.body;
|
2023-12-22 14:45:49 -07:00
|
|
|
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
2023-12-20 03:20:04 +00:00
|
|
|
if (!serverSpec.path) return res.status(400).send("Path required!");
|
|
|
|
uploadServerItem(serverSpec, req.file)
|
|
|
|
.then(() => res.sendStatus(200))
|
|
|
|
.catch(sendError(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getItem(req, res) {
|
|
|
|
const serverSpec = req.body;
|
2023-12-22 14:45:49 -07:00
|
|
|
if (!serverSpec.id) return res.status(400).send("Server id missing!");
|
2023-12-20 03:20:04 +00:00
|
|
|
if (!serverSpec.path) return res.status(400).send("Path required!");
|
|
|
|
getServerItem(serverSpec, res)
|
|
|
|
.then(({ ds, ftpTransfer }) => {
|
|
|
|
ds.pipe(res).on("error", sendError(res));
|
|
|
|
return ftpTransfer;
|
|
|
|
})
|
|
|
|
.catch(sendError(res));
|
|
|
|
}
|