minecluster/lib/k8s/server-files.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-12-16 09:28:06 -07:00
import ftp from "basic-ftp";
import { ERR } from "../util/logging.js";
import { getServerAssets } from "./k8s-server-control.js";
import ExpressClientError from "../util/ExpressClientError.js";
2023-12-16 09:28:06 -07:00
const namespace = process.env.MCL_SERVER_NAMESPACE;
2023-12-17 12:25:14 -07:00
export async function useFtp(serverService) {
const { name } = serverService.metadata;
const client = new ftp.Client();
await client.access({
host: `${name}.${namespace}.svc.cluster.local`,
user: "minecluster",
password: "minecluster",
});
return client;
}
const handleError = (e) => {
2023-12-17 12:25:14 -07:00
ERR("SERVER FILES", "Error occurred while preforming FTP operation!", e);
throw new ExpressClientError({
c: 500,
m: "Error occurred while performing FTP operation!",
});
2023-12-17 12:25:14 -07:00
};
export async function listServerFiles(serverSpec) {
2023-12-16 09:28:06 -07:00
const { name } = serverSpec;
const server = await getServerAssets(name);
if (!server)
throw new ExpressClientError({
c: 404,
m: "No resources for that server were found!",
});
2023-12-16 09:28:06 -07:00
if (!server.service)
throw new ExpressClientError({
c: 409,
m: "Service doesn't exist, please contact your hosting provider!",
});
// FTP Operations;
const client = await useFtp(server.service).catch(handleError);
2023-12-17 12:25:14 -07:00
await client
.list()
.then((f) => res.json(f))
.catch(handleError);
2023-12-16 09:28:06 -07:00
client.close();
}