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";
|
2023-12-18 03:55:27 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-18 03:55:27 -07:00
|
|
|
const handleError = (e) => {
|
2023-12-17 12:25:14 -07:00
|
|
|
ERR("SERVER FILES", "Error occurred while preforming FTP operation!", e);
|
2023-12-18 03:55:27 -07:00
|
|
|
throw new ExpressClientError({
|
|
|
|
c: 500,
|
|
|
|
m: "Error occurred while performing FTP operation!",
|
|
|
|
});
|
2023-12-17 12:25:14 -07:00
|
|
|
};
|
|
|
|
|
2023-12-18 03:55:27 -07:00
|
|
|
export async function listServerFiles(serverSpec) {
|
2023-12-18 16:50:33 -07:00
|
|
|
const { name, dir } = serverSpec;
|
2023-12-16 09:28:06 -07:00
|
|
|
const server = await getServerAssets(name);
|
|
|
|
if (!server)
|
2023-12-18 03:55:27 -07:00
|
|
|
throw new ExpressClientError({
|
|
|
|
c: 404,
|
|
|
|
m: "No resources for that server were found!",
|
|
|
|
});
|
2023-12-16 09:28:06 -07:00
|
|
|
if (!server.service)
|
2023-12-18 03:55:27 -07:00
|
|
|
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-18 16:50:33 -07:00
|
|
|
const files = client
|
|
|
|
.list(dir)
|
|
|
|
.then((f) => {
|
|
|
|
client.close();
|
|
|
|
return f;
|
|
|
|
})
|
2023-12-18 03:55:27 -07:00
|
|
|
.catch(handleError);
|
2023-12-18 16:50:33 -07:00
|
|
|
return files;
|
2023-12-16 09:28:06 -07:00
|
|
|
}
|