mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
new abstract method for volumepath
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
import { VOLUME_MOUNT_BASE } from "../../core/constants";
|
||||
import type { Volume } from "../../db/schema";
|
||||
import type { BackendConfig } from "~/schemas/volumes";
|
||||
|
||||
export const getVolumePath = (volume: Volume) => {
|
||||
if (volume.config.backend === "directory") {
|
||||
return volume.config.path;
|
||||
}
|
||||
|
||||
return `${VOLUME_MOUNT_BASE}/${volume.name}/_data`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a volume is a database volume
|
||||
*/
|
||||
export const isDatabaseVolume = (volume: Volume): boolean => {
|
||||
return ["mariadb", "mysql", "postgres", "sqlite"].includes(volume.config.backend);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a backend config is a database backend
|
||||
*/
|
||||
export const isDatabaseBackend = (config: BackendConfig): boolean => {
|
||||
return ["mariadb", "mysql", "postgres", "sqlite"].includes(config.backend);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the dump directory path for a database volume
|
||||
*/
|
||||
export const getDumpPath = (volume: Volume): string => {
|
||||
return `${VOLUME_MOUNT_BASE}/${volume.name}/dumps`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the dump file path for a database volume backup
|
||||
*/
|
||||
export const getDumpFilePath = (volume: Volume, timestamp: number): string => {
|
||||
const dumpDir = getDumpPath(volume);
|
||||
const extension = volume.config.backend === "postgres" &&
|
||||
volume.config.backend === "postgres" &&
|
||||
(volume.config as Extract<BackendConfig, { backend: "postgres" }>).dumpFormat !== "plain"
|
||||
? "dump"
|
||||
: "sql";
|
||||
return `${dumpDir}/${volume.name}-${timestamp}.${extension}`;
|
||||
};
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
type BrowseFilesystemDto,
|
||||
} from "./volume.dto";
|
||||
import { volumeService } from "./volume.service";
|
||||
import { getVolumePath } from "./helpers";
|
||||
import { createVolumeBackend } from "../backends/backend";
|
||||
|
||||
export const volumeController = new Hono()
|
||||
.get("/", listVolumesDto, async (c) => {
|
||||
@@ -37,9 +37,10 @@ export const volumeController = new Hono()
|
||||
const body = c.req.valid("json");
|
||||
const res = await volumeService.createVolume(body.name, body.config);
|
||||
|
||||
const backend = createVolumeBackend(res.volume);
|
||||
const response = {
|
||||
...res.volume,
|
||||
path: getVolumePath(res.volume),
|
||||
path: backend.getVolumePath(),
|
||||
};
|
||||
|
||||
return c.json<CreateVolumeDto>(response, 201);
|
||||
@@ -60,10 +61,11 @@ export const volumeController = new Hono()
|
||||
const { name } = c.req.param();
|
||||
const res = await volumeService.getVolume(name);
|
||||
|
||||
const backend = createVolumeBackend(res.volume);
|
||||
const response = {
|
||||
volume: {
|
||||
...res.volume,
|
||||
path: getVolumePath(res.volume),
|
||||
path: backend.getVolumePath(),
|
||||
},
|
||||
statfs: {
|
||||
total: res.statfs.total ?? 0,
|
||||
@@ -85,9 +87,10 @@ export const volumeController = new Hono()
|
||||
const body = c.req.valid("json");
|
||||
const res = await volumeService.updateVolume(name, body);
|
||||
|
||||
const backend = createVolumeBackend(res.volume);
|
||||
const response = {
|
||||
...res.volume,
|
||||
path: getVolumePath(res.volume),
|
||||
path: backend.getVolumePath(),
|
||||
};
|
||||
|
||||
return c.json<UpdateVolumeDto>(response, 200);
|
||||
|
||||
@@ -13,7 +13,6 @@ import { getStatFs, type StatFs } from "../../utils/mountinfo";
|
||||
import { withTimeout } from "../../utils/timeout";
|
||||
import { createVolumeBackend } from "../backends/backend";
|
||||
import type { UpdateVolumeBody } from "./volume.dto";
|
||||
import { getVolumePath } from "./helpers";
|
||||
import { logger } from "../../utils/logger";
|
||||
import { serverEvents } from "../../core/events";
|
||||
import type { BackendConfig } from "~/schemas/volumes";
|
||||
@@ -129,7 +128,8 @@ const getVolume = async (name: string) => {
|
||||
|
||||
let statfs: Partial<StatFs> = {};
|
||||
if (volume.status === "mounted") {
|
||||
statfs = await withTimeout(getStatFs(getVolumePath(volume)), 1000, "getStatFs").catch((error) => {
|
||||
const backend = createVolumeBackend(volume);
|
||||
statfs = await withTimeout(getStatFs(backend.getVolumePath()), 1000, "getStatFs").catch((error) => {
|
||||
logger.warn(`Failed to get statfs for volume ${name}: ${toMessage(error)}`);
|
||||
return {};
|
||||
});
|
||||
@@ -296,7 +296,8 @@ const listFiles = async (name: string, subPath?: string) => {
|
||||
}
|
||||
|
||||
// For directory volumes, use the configured path directly
|
||||
const volumePath = getVolumePath(volume);
|
||||
const backend = createVolumeBackend(volume);
|
||||
const volumePath = backend.getVolumePath();
|
||||
|
||||
const requestedPath = subPath ? path.join(volumePath, subPath) : volumePath;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user