[FEATURE] Initial File Manager
This commit is contained in:
parent
e66e685903
commit
22bf905415
10 changed files with 356 additions and 85 deletions
|
@ -2,10 +2,27 @@ import ftp from "basic-ftp";
|
|||
import { ERR } from "../util/logging.js";
|
||||
import { getServerAssets } from "./k8s-server-control.js";
|
||||
import ExpressClientError from "../util/ExpressClientError.js";
|
||||
import { Readable, Writable, Transform } from "node:stream";
|
||||
|
||||
const namespace = process.env.MCL_SERVER_NAMESPACE;
|
||||
|
||||
export async function useFtp(serverService) {
|
||||
const pathSecurityCheck = (path) => {
|
||||
if (!path.startsWith("."))
|
||||
throw new ExpressClientError({
|
||||
m: "Only relative directories can be created",
|
||||
c: 409,
|
||||
});
|
||||
};
|
||||
|
||||
const handleError = (e) => {
|
||||
ERR("SERVER FILES", "Error occurred while preforming FTP operation!", e);
|
||||
throw new ExpressClientError({
|
||||
c: 500,
|
||||
m: "Error occurred while performing FTP operation!",
|
||||
});
|
||||
};
|
||||
|
||||
export async function getFtpClient(serverService) {
|
||||
const { name } = serverService.metadata;
|
||||
const client = new ftp.Client();
|
||||
await client.access({
|
||||
|
@ -16,16 +33,8 @@ export async function useFtp(serverService) {
|
|||
return client;
|
||||
}
|
||||
|
||||
const handleError = (e) => {
|
||||
ERR("SERVER FILES", "Error occurred while preforming FTP operation!", e);
|
||||
throw new ExpressClientError({
|
||||
c: 500,
|
||||
m: "Error occurred while performing FTP operation!",
|
||||
});
|
||||
};
|
||||
|
||||
export async function listServerFiles(serverSpec) {
|
||||
const { name, dir } = serverSpec;
|
||||
export async function useServerFtp(serverSpec, fn) {
|
||||
const { name } = serverSpec;
|
||||
const server = await getServerAssets(name);
|
||||
if (!server)
|
||||
throw new ExpressClientError({
|
||||
|
@ -37,15 +46,52 @@ export async function listServerFiles(serverSpec) {
|
|||
c: 409,
|
||||
m: "Service doesn't exist, please contact your hosting provider!",
|
||||
});
|
||||
const client = await getFtpClient(server.service);
|
||||
const result = await fn(client);
|
||||
client.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
// FTP Operations;
|
||||
const client = await useFtp(server.service).catch(handleError);
|
||||
const files = client
|
||||
.list(dir)
|
||||
.then((f) => {
|
||||
client.close();
|
||||
return f;
|
||||
})
|
||||
.catch(handleError);
|
||||
export async function listServerFiles(serverSpec) {
|
||||
const { path } = serverSpec;
|
||||
const files = useServerFtp(serverSpec, async (c) => await c.list(path)).catch(
|
||||
handleError,
|
||||
);
|
||||
return files;
|
||||
}
|
||||
|
||||
export async function createServerFolder(serverSpec) {
|
||||
const { path } = serverSpec;
|
||||
pathSecurityCheck(path);
|
||||
await useServerFtp(serverSpec, async (c) => c.ensureDir(path)).catch(
|
||||
handleError,
|
||||
);
|
||||
}
|
||||
|
||||
export async function removeServerItem(serverSpec) {
|
||||
const { path, isDir } = serverSpec;
|
||||
pathSecurityCheck(path);
|
||||
await useServerFtp(serverSpec, async (c) => {
|
||||
if (isDir) await c.removeDir(path);
|
||||
else await c.remove(path);
|
||||
}).catch(handleError);
|
||||
}
|
||||
|
||||
export async function uploadServerItem(serverSpec, file) {
|
||||
const fileStream = Readable.from(file.buffer);
|
||||
const { path } = serverSpec;
|
||||
pathSecurityCheck(path);
|
||||
await useServerFtp(serverSpec, async (c) => {
|
||||
await c.uploadFrom(fileStream, `${path}/${file.originalname}`);
|
||||
}).catch(handleError);
|
||||
}
|
||||
|
||||
export async function getServerItem(serverSpec, writableStream) {
|
||||
const { path } = serverSpec;
|
||||
const ds = new Transform({ transform: (c, e, cb) => cb(null, c) });
|
||||
pathSecurityCheck(path);
|
||||
const ftpTransfer = useServerFtp(serverSpec, async (c) => {
|
||||
await c.downloadTo(ds, path);
|
||||
}).catch(handleError);
|
||||
return { ds, ftpTransfer };
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue