mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Merge branch 'dev' into feat/oidc
# Conflicts: # portabase.config.ts # src/env.mjs
This commit is contained in:
+1
-1
@@ -224,7 +224,7 @@ export const ChannelForm = ({onSuccessAction, organization, defaultValues, kind}
|
||||
|
||||
<div className="flex justify-between mt-4">
|
||||
<div>
|
||||
{kind == "storage" && (
|
||||
{(!isCreate || kind == "storage") && (
|
||||
<ChannelTestButton
|
||||
kind={kind}
|
||||
organizationId={organization?.id}
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ export const ChannelTestButton = ({channel, organizationId, kind}: NotifierTestC
|
||||
message: `We are testing channel ${channel.name}`,
|
||||
level: 'info',
|
||||
};
|
||||
const result = await dispatchNotification(payload, undefined, channel, organizationId);
|
||||
const result = await dispatchNotification(payload, undefined, channel.id, organizationId);
|
||||
|
||||
if (result.success) {
|
||||
toast.success(result.message);
|
||||
|
||||
@@ -8,7 +8,7 @@ import {v4 as uuidv4} from "uuid";
|
||||
import {eq} from "drizzle-orm";
|
||||
import {z} from "zod";
|
||||
import {storeBackupFiles} from "@/features/storages/helpers";
|
||||
import {getFileExtension} from "@/features/api/upload/helpers/file";
|
||||
import {getFileExtension} from "@/utils/common";
|
||||
|
||||
|
||||
export const uploadBackupAction = userAction
|
||||
@@ -46,18 +46,6 @@ export const uploadBackupAction = userAction
|
||||
const fileName = `${uuid}${fileExtension}`;
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
// const [settings] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
|
||||
//
|
||||
// if (!settings) {
|
||||
// return {
|
||||
// success: false,
|
||||
// actionError: {
|
||||
// message: "Settings not set",
|
||||
// status: 500,
|
||||
// cause: "Unknown error",
|
||||
// },
|
||||
// };
|
||||
// }
|
||||
|
||||
const [backup] = await db
|
||||
.insert(drizzleDb.schemas.backup)
|
||||
@@ -68,30 +56,8 @@ export const uploadBackupAction = userAction
|
||||
})
|
||||
.returning();
|
||||
|
||||
|
||||
await storeBackupFiles(backup, database, buffer, fileName)
|
||||
|
||||
|
||||
// let success: boolean, message: string, filePath: string;
|
||||
//
|
||||
// const result =
|
||||
// settings.storage === "local"
|
||||
// ? await uploadLocalPrivate(fileName, buffer)
|
||||
// : await uploadS3Private(`${database.project?.slug}/${fileName}`, buffer, env.S3_BUCKET_NAME!);
|
||||
//
|
||||
// ({success, message, filePath} = result);
|
||||
//
|
||||
// if (!success) {
|
||||
// return {
|
||||
// success: false,
|
||||
// actionError: {
|
||||
// message: "An error has occurred while uploading file",
|
||||
// status: 500,
|
||||
// cause: "Unknown error",
|
||||
// },
|
||||
// };
|
||||
// }
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: backup,
|
||||
|
||||
@@ -12,7 +12,7 @@ import {useMutation, useQueryClient} from "@tanstack/react-query";
|
||||
import {toast} from "sonner";
|
||||
import {MemberWithUser} from "@/db/schema/03_organization";
|
||||
import {deleteBackupAction} from "@/components/wrappers/dashboard/database/backup/actions/backup-actions.action";
|
||||
import { ButtonWithConfirm } from "@/components/wrappers/common/button/button-with-confirm";
|
||||
import {ButtonWithConfirm} from "@/components/wrappers/common/button/button-with-confirm";
|
||||
|
||||
|
||||
type DatabaseBackupListProps = {
|
||||
@@ -69,19 +69,12 @@ export const DatabaseBackupList = (props: DatabaseBackupListProps) => {
|
||||
mutationFn: async (backups: Backup[]) => {
|
||||
const results = await Promise.all(
|
||||
backups.map(async (backup) => {
|
||||
if (backup.deletedAt == null) {
|
||||
if (backup.deletedAt == null || backup.status == "ongoing") {
|
||||
|
||||
const backupDeleted = await deleteBackupAction({
|
||||
databaseId: backup.databaseId,
|
||||
backupId: backup.id,
|
||||
})
|
||||
// const backupDeleted = await deleteBackupAction({
|
||||
// backupId: backup.id,
|
||||
// databaseId: backup.databaseId,
|
||||
// status: backup.status,
|
||||
// file: backup.file ?? "",
|
||||
// projectSlug: props.database?.project?.slug!
|
||||
// });
|
||||
return {
|
||||
success: backupDeleted?.data?.success,
|
||||
message: backupDeleted?.data?.success
|
||||
@@ -136,15 +129,9 @@ export const DatabaseBackupList = (props: DatabaseBackupListProps) => {
|
||||
>Actions</ButtonWithLoading>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start">
|
||||
<ButtonWithConfirm
|
||||
<ButtonWithConfirm
|
||||
onConfirm={() => {
|
||||
const backupsToDelete = rows.map(row => row
|
||||
).filter(backup => backup.deletedAt == null)
|
||||
if (backupsToDelete.length === 0) {
|
||||
toast.error("No available backup selected for deletion.");
|
||||
return;
|
||||
}
|
||||
mutationDeleteBackups.mutate(backupsToDelete);
|
||||
mutationDeleteBackups.mutate(rows);
|
||||
setIsActionsOpen(false);
|
||||
}}
|
||||
onCancel={() => setIsActionsOpen(false)}
|
||||
|
||||
+8
-20
@@ -1,6 +1,7 @@
|
||||
import { createEnv } from "@t3-oss/env-nextjs";
|
||||
import { z } from "zod";
|
||||
import packageJson from "../package.json" with { type: "json" };
|
||||
import path from "path";
|
||||
|
||||
const { version } = packageJson;
|
||||
|
||||
@@ -14,8 +15,8 @@ export const env = createEnv({
|
||||
|
||||
PROJECT_NAME: z.string().optional(),
|
||||
PROJECT_DESCRIPTION: z.string().optional(),
|
||||
PROJECT_URL: z.string().optional(),
|
||||
PROJECT_SECRET: z.string().optional(),
|
||||
PROJECT_URL: z.string().regex(/^https?:\/\//, "URL must start with http:// or https://"),
|
||||
PROJECT_SECRET: z.string(),
|
||||
|
||||
SMTP_PASSWORD: z.string().optional(),
|
||||
SMTP_FROM: z.string().optional(),
|
||||
@@ -31,15 +32,6 @@ export const env = createEnv({
|
||||
AUTH_GITHUB_ID: z.string().optional(),
|
||||
AUTH_GITHUB_SECRET: z.string().optional(),
|
||||
|
||||
S3_ENDPOINT: z.string().optional(),
|
||||
S3_ACCESS_KEY: z.string().optional(),
|
||||
S3_SECRET_KEY: z.string().optional(),
|
||||
S3_BUCKET_NAME: z.string().optional(),
|
||||
S3_PORT: z.string().optional(),
|
||||
S3_USE_SSL: z.string().optional(),
|
||||
|
||||
STORAGE_TYPE: z.enum(["local", "s3"]).optional(),
|
||||
|
||||
RETENTION_CRON: z.string().default(process.env.NODE_ENV === "production" ? "0 7 * * *" : "* * * * *"),
|
||||
|
||||
AUTH_OIDC_ID: z.string().optional().default("oidc"),
|
||||
@@ -59,6 +51,9 @@ export const env = createEnv({
|
||||
AUTH_EMAIL_PASSWORD_ENABLED: z.string().optional().default("true"),
|
||||
AUTH_SIGNUP_ENABLED: z.string().optional().default("true"),
|
||||
AUTH_PASSKEY_ENABLED: z.string().optional().default("true"),
|
||||
|
||||
PRIVATE_PATH: z.string().optional(),
|
||||
|
||||
},
|
||||
client: {
|
||||
NEXT_PUBLIC_PROJECT_VERSION: z.string().optional(),
|
||||
@@ -87,15 +82,6 @@ export const env = createEnv({
|
||||
AUTH_GITHUB_ID: process.env.AUTH_GITHUB_ID,
|
||||
AUTH_GITHUB_SECRET: process.env.AUTH_GITHUB_SECRET,
|
||||
|
||||
S3_ENDPOINT: process.env.S3_ENDPOINT,
|
||||
S3_ACCESS_KEY: process.env.S3_ACCESS_KEY,
|
||||
S3_SECRET_KEY: process.env.S3_SECRET_KEY,
|
||||
S3_BUCKET_NAME: process.env.S3_BUCKET_NAME,
|
||||
S3_PORT: process.env.S3_PORT,
|
||||
S3_USE_SSL: process.env.S3_USE_SSL,
|
||||
|
||||
STORAGE_TYPE: process.env.STORAGE_TYPE,
|
||||
|
||||
RETENTION_CRON: process.env.RETENTION_CRON,
|
||||
|
||||
AUTH_OIDC_ID: process.env.AUTH_OIDC_ID,
|
||||
@@ -115,5 +101,7 @@ export const env = createEnv({
|
||||
AUTH_EMAIL_PASSWORD_ENABLED: process.env.AUTH_EMAIL_PASSWORD_ENABLED,
|
||||
AUTH_SIGNUP_ENABLED: process.env.AUTH_SIGNUP_ENABLED,
|
||||
AUTH_PASSKEY_ENABLED: process.env.AUTH_PASSKEY_ENABLED,
|
||||
|
||||
PRIVATE_PATH: process.env.PRIVATE_PATH || path.join(process.cwd(), 'private')
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import {Request, Response, NextFunction} from "express";
|
||||
|
||||
export function loggingMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
console.log(`[API - SERVICES - V1] Received ${req.method} request : ${req.url} at ${new Date().toISOString()}`);
|
||||
next();
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import express, { Router } from "express";
|
||||
import uploadRouter from "./upload";
|
||||
import { loggingMiddleware } from "@/features/api/middleware";
|
||||
|
||||
const router: Router = express.Router();
|
||||
|
||||
router.use(loggingMiddleware);
|
||||
router.use("/upload", uploadRouter);
|
||||
|
||||
export default router;
|
||||
@@ -1,134 +0,0 @@
|
||||
import fs from "fs";
|
||||
import {Backup, DatabaseWith} from "@/db/schema/07_database";
|
||||
import {dispatchStorage} from "@/features/storages/dispatch";
|
||||
import type {StorageInput, StorageResult} from "@/features/storages/types";
|
||||
import {db} from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {eq} from "drizzle-orm";
|
||||
import {withUpdatedAt} from "@/db/utils";
|
||||
import {sendNotificationsBackupRestore} from "@/features/notifications/helpers";
|
||||
import {eventEmitter} from "@/features/shared/event";
|
||||
import forge from "node-forge";
|
||||
import crypto from "node:crypto";
|
||||
|
||||
/**
|
||||
* Save stream to a temporary file and upload to all storage providers in parallel.
|
||||
*/
|
||||
export default async function uploadTempFileToProviders(
|
||||
backup: Backup,
|
||||
database: DatabaseWith,
|
||||
// inputStream: NodeJS.ReadableStream,
|
||||
tmpPath: string,
|
||||
fileName: string
|
||||
): Promise<StorageResult[]> {
|
||||
|
||||
const stats = fs.statSync(tmpPath);
|
||||
const fileSize = stats.size;
|
||||
console.log(tmpPath);
|
||||
|
||||
const settings = await db.query.setting.findFirst({
|
||||
where: eq(drizzleDb.schemas.setting.name, "system"),
|
||||
with: {storageChannel: true},
|
||||
});
|
||||
|
||||
const defaultPolicy = settings?.storageChannel
|
||||
? [{
|
||||
id: null,
|
||||
storageChannelId: settings.storageChannel.id,
|
||||
enabled: settings.storageChannel.enabled,
|
||||
}]
|
||||
: [];
|
||||
|
||||
const enabledPolicies = database.storagePolicies?.filter(p => p.enabled) ?? [];
|
||||
const policies = enabledPolicies.length ? enabledPolicies : defaultPolicy;
|
||||
|
||||
if (!policies.length) {
|
||||
await db.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({status: "failed"}))
|
||||
.where(eq(drizzleDb.schemas.backup.id, backup.id));
|
||||
fs.existsSync(tmpPath) && fs.unlinkSync(tmpPath);
|
||||
return [];
|
||||
}
|
||||
|
||||
const storagePath = `backups/${database.project?.slug}/${fileName}`;
|
||||
|
||||
const results = await Promise.all(
|
||||
policies.map(async (policy) => {
|
||||
const fileStream = fs.createReadStream(tmpPath);
|
||||
|
||||
const [backupStorage] = await db.insert(drizzleDb.schemas.backupStorage)
|
||||
.values({
|
||||
backupId: backup.id,
|
||||
storageChannelId: policy.storageChannelId,
|
||||
status: "pending",
|
||||
path: storagePath,
|
||||
})
|
||||
.returning();
|
||||
|
||||
const input: StorageInput = {
|
||||
action: "upload",
|
||||
data: {path: storagePath, file: fileStream, size: fileSize},
|
||||
};
|
||||
|
||||
let result: StorageResult;
|
||||
|
||||
try {
|
||||
result = policy.id
|
||||
? await dispatchStorage(input, policy.id)
|
||||
: await dispatchStorage(input, undefined, policy.storageChannelId);
|
||||
} catch (err: any) {
|
||||
console.error(err);
|
||||
result = {success: false, provider: null, error: err.message};
|
||||
}
|
||||
await db.update(drizzleDb.schemas.backupStorage)
|
||||
.set(withUpdatedAt({status: result.success ? "success" : "failed"}))
|
||||
.where(eq(drizzleDb.schemas.backupStorage.id, backupStorage.id));
|
||||
|
||||
return result;
|
||||
})
|
||||
);
|
||||
|
||||
const backupStatus = results.some(r => r.success) ? "success" : "failed";
|
||||
await db.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({
|
||||
fileSize: fileSize,
|
||||
status: backupStatus
|
||||
}))
|
||||
.where(eq(drizzleDb.schemas.backup.id, backup.id));
|
||||
|
||||
fs.existsSync(tmpPath) && fs.unlinkSync(tmpPath);
|
||||
|
||||
console.log(results);
|
||||
eventEmitter.emit('modification', {update: true});
|
||||
|
||||
if (!backupStatus) {
|
||||
await sendNotificationsBackupRestore(database, "error_backup");
|
||||
}
|
||||
await sendNotificationsBackupRestore(database, "success_backup");
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
export function createDecryptionStream(
|
||||
encryptedAesKeyHex: string,
|
||||
ivHex: string
|
||||
) {
|
||||
const privateKeyPem = fs.readFileSync(
|
||||
"private/keys/server_private.pem",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
const privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
|
||||
|
||||
const encryptedBytes = forge.util.hexToBytes(encryptedAesKeyHex);
|
||||
const aesKeyBytes = privateKey.decrypt(encryptedBytes, "RSA-OAEP", {
|
||||
md: forge.md.sha256.create(),
|
||||
mgf1: {md: forge.md.sha256.create()},
|
||||
});
|
||||
|
||||
const aesKey = Buffer.from(aesKeyBytes, "binary");
|
||||
const iv = Buffer.from(ivHex, "hex");
|
||||
|
||||
return crypto.createDecipheriv("aes-256-cbc", aesKey, iv);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import {promisify} from "util";
|
||||
import {pipeline} from "stream";
|
||||
|
||||
const pipelineAsync = promisify(pipeline);
|
||||
const TMP_DIR = path.join(process.cwd(), "private/uploads/tmp");
|
||||
fs.mkdirSync(TMP_DIR, { recursive: true });
|
||||
|
||||
export async function saveStreamToTempFile(stream: NodeJS.ReadableStream, fileName: string): Promise<string> {
|
||||
const tmpPath = path.join(TMP_DIR, fileName);
|
||||
const writeStream = fs.createWriteStream(tmpPath);
|
||||
await pipelineAsync(stream, writeStream);
|
||||
return tmpPath;
|
||||
}
|
||||
|
||||
|
||||
export function getFileExtension(dbType: string) {
|
||||
switch (dbType) {
|
||||
case "postgresql":
|
||||
return ".dump";
|
||||
case "mysql":
|
||||
return ".sql";
|
||||
default:
|
||||
return ".dump";
|
||||
}
|
||||
}
|
||||
@@ -1,201 +0,0 @@
|
||||
import express, {Request, Response, Router} from "express";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import {and, eq} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {db} from "@/db";
|
||||
import {isUuidv4} from "@/utils/verify-uuid";
|
||||
import uploadTempFileToProviders, {createDecryptionStream} from "@/features/api/upload/helpers/common";
|
||||
import {sendNotificationsBackupRestore} from "@/features/notifications/helpers";
|
||||
import {Backup} from "@/db/schema/07_database";
|
||||
import {withUpdatedAt} from "@/db/utils";
|
||||
import {eventEmitter} from "@/features/shared/event";
|
||||
import {getFileExtension, saveStreamToTempFile} from "@/features/api/upload/helpers/file";
|
||||
|
||||
const router: Router = express.Router();
|
||||
|
||||
// router.post("/:agentId", async (req: Request, res: Response) => {
|
||||
// try {
|
||||
// const agentId = req.params.agentId as string | undefined;
|
||||
// const generatedId = req.headers["x-generated-id"] as string | undefined;
|
||||
// const status = req.headers["x-status"] as string | undefined;
|
||||
// const encryptedAesKeyHex = req.headers["x-aes-key"] as string | undefined;
|
||||
// const ivHex = req.headers["x-iv"] as string | undefined;
|
||||
// const method = (req.headers["x-method"] as string) ?? "manual";
|
||||
// const extension = req.headers["x-extension"] as string | undefined;
|
||||
//
|
||||
//
|
||||
// if (!generatedId || !encryptedAesKeyHex || !ivHex || !agentId || !status) {
|
||||
// return res.status(400).json({error: "Missing required headers/params"});
|
||||
// }
|
||||
//
|
||||
// if (!isUuidv4(generatedId)) {
|
||||
// return res.status(400).json({error: "generatedId is not a valid UUID"});
|
||||
// }
|
||||
//
|
||||
// const agent = await db.query.agent.findFirst({
|
||||
// where: eq(drizzleDb.schemas.agent.id, agentId),
|
||||
// });
|
||||
//
|
||||
// if (!agent) {
|
||||
// return res.status(404).json({error: "Agent not found"});
|
||||
// }
|
||||
//
|
||||
//
|
||||
// const database = await db.query.database.findFirst({
|
||||
// where: eq(drizzleDb.schemas.database.agentDatabaseId, generatedId),
|
||||
// with: {
|
||||
// project: true,
|
||||
// storagePolicies: true,
|
||||
// },
|
||||
// });
|
||||
//
|
||||
// if (!database) {
|
||||
// return res.status(404).json({error: "Database not found"});
|
||||
// }
|
||||
//
|
||||
// let backup: Backup | null | undefined = null;
|
||||
//
|
||||
// if (method === "automatic") {
|
||||
// [backup] = await db
|
||||
// .insert(drizzleDb.schemas.backup)
|
||||
// .values({
|
||||
// status: "ongoing",
|
||||
// databaseId: database.id,
|
||||
// })
|
||||
// .returning();
|
||||
// } else {
|
||||
// backup = await db.query.backup.findFirst({
|
||||
// where: and(
|
||||
// eq(drizzleDb.schemas.backup.databaseId, database.id),
|
||||
// eq(drizzleDb.schemas.backup.status, "ongoing")
|
||||
// ),
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// if (!backup) {
|
||||
// return res.status(404).json({error: "Backup not found"});
|
||||
// }
|
||||
//
|
||||
//
|
||||
// if (status === "success") {
|
||||
//
|
||||
// const decipher = createDecryptionStream(encryptedAesKeyHex, ivHex);
|
||||
//
|
||||
// const fileExt = extension || getFileExtension(database.dbms);
|
||||
// const fileName = `${uuidv4()}${fileExt}`;
|
||||
//
|
||||
// const decryptedStream = req.pipe(decipher);
|
||||
//
|
||||
// const tmpPath = await saveStreamToTempFile(decryptedStream, fileName);
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// if (!tmpPath) {
|
||||
// return res.status(500).json({
|
||||
// error: "Unable to save tmp backup file",
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// uploadTempFileToProviders(backup, database, tmpPath, fileName);
|
||||
//
|
||||
// return res.json({success: true});
|
||||
// } else {
|
||||
// await db
|
||||
// .update(drizzleDb.schemas.backup)
|
||||
// .set(withUpdatedAt({status: 'failed'}))
|
||||
// .where(eq(drizzleDb.schemas.backup.id, backup.id));
|
||||
//
|
||||
// eventEmitter.emit('modification', {update: true});
|
||||
//
|
||||
// await sendNotificationsBackupRestore(database, "error_backup");
|
||||
//
|
||||
// return res.status(200).json({
|
||||
// error: "Backup successfully updated with status failed",
|
||||
// });
|
||||
// }
|
||||
// } catch (err: any) {
|
||||
// console.error("Upload error:", err);
|
||||
// return res.status(500).json({
|
||||
// error: "Upload failed",
|
||||
// detail: err.message,
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
router.post("/:agentId", async (req: Request, res: Response) => {
|
||||
const agentId = req.params.agentId as string | undefined;
|
||||
const generatedId = req.headers["x-generated-id"] as string | undefined;
|
||||
const status = req.headers["x-status"] as string | undefined;
|
||||
const encryptedAesKeyHex = req.headers["x-aes-key"] as string | undefined;
|
||||
const ivHex = req.headers["x-iv"] as string | undefined;
|
||||
const method = (req.headers["x-method"] as string) ?? "manual";
|
||||
const extension = req.headers["x-extension"] as string | undefined;
|
||||
|
||||
if (!generatedId || !encryptedAesKeyHex || !ivHex || !agentId || !status) {
|
||||
return res.status(400).json({error: "Missing required headers/params"});
|
||||
}
|
||||
if (!isUuidv4(generatedId)) {
|
||||
return res.status(400).json({error: "generatedId is not a valid UUID"});
|
||||
}
|
||||
|
||||
const agent = await db.query.agent.findFirst({ where: eq(drizzleDb.schemas.agent.id, agentId) });
|
||||
if (!agent) return res.status(404).json({error: "Agent not found"});
|
||||
|
||||
const database = await db.query.database.findFirst({
|
||||
where: eq(drizzleDb.schemas.database.agentDatabaseId, generatedId),
|
||||
with: { project: true, storagePolicies: true },
|
||||
});
|
||||
if (!database) return res.status(404).json({error: "Database not found"});
|
||||
|
||||
let backup: Backup | null = null;
|
||||
if (method === "automatic") {
|
||||
[backup] = await db.insert(drizzleDb.schemas.backup).values({ status: "ongoing", databaseId: database.id }).returning();
|
||||
} else {
|
||||
backup = await db.query.backup.findFirst({
|
||||
where: and(
|
||||
eq(drizzleDb.schemas.backup.databaseId, database.id),
|
||||
eq(drizzleDb.schemas.backup.status, "ongoing")
|
||||
),
|
||||
});
|
||||
}
|
||||
if (!backup) return res.status(404).json({error: "Backup not found"});
|
||||
|
||||
res.status(202).json({success: true, message: "Backup received, processing in background"});
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
if (status === "success") {
|
||||
const decipher = createDecryptionStream(encryptedAesKeyHex!, ivHex!);
|
||||
const fileExt = extension || getFileExtension(database.dbms);
|
||||
const fileName = `${uuidv4()}${fileExt}`;
|
||||
const decryptedStream = req.pipe(decipher);
|
||||
const tmpPath = await saveStreamToTempFile(decryptedStream, fileName);
|
||||
|
||||
if (!tmpPath) throw new Error("Unable to save tmp backup file");
|
||||
|
||||
await uploadTempFileToProviders(backup!, database, tmpPath, fileName);
|
||||
} else {
|
||||
await db.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({status: 'failed'}))
|
||||
.where(eq(drizzleDb.schemas.backup.id, backup!.id));
|
||||
|
||||
eventEmitter.emit('modification', {update: true});
|
||||
await sendNotificationsBackupRestore(database, "error_backup");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Background backup processing failed:", err);
|
||||
try {
|
||||
await db.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({status: 'failed'}))
|
||||
.where(eq(drizzleDb.schemas.backup.id, backup!.id));
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
})();
|
||||
});
|
||||
|
||||
|
||||
|
||||
export default router;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import {cn} from "@/lib/utils";
|
||||
import {Tooltip, TooltipContent, TooltipProvider, TooltipTrigger} from "@/components/ui/tooltip";
|
||||
import {MemberWithUser} from "@/db/schema/03_organization";
|
||||
import {formatLocalizedDate} from "@/utils/date-formatting";
|
||||
import {formatBytes, isImportedFilename} from "@/utils/text";
|
||||
import {formatBytes} from "@/utils/text";
|
||||
import {DatabaseActionsCell} from "@/components/wrappers/dashboard/database/backup/actions/backup-actions-cell";
|
||||
import { Badge as BadgeC } from "@/components/ui/badge";
|
||||
|
||||
@@ -22,7 +22,17 @@ export function backupColumns(
|
||||
{
|
||||
id: "availability",
|
||||
cell: ({row}) => {
|
||||
const colorStatus = row.original.deletedAt != null ? "bg-red-400 border-red-600" : "bg-green-400 border-green-600";
|
||||
const statusColors: Record<string, string> = {
|
||||
waiting: "bg-gray-400 border-gray-600",
|
||||
ongoing: "bg-orange-400 border-orange-600",
|
||||
success: "bg-green-400 border-green-600",
|
||||
};
|
||||
|
||||
const colorStatus =
|
||||
row.original.deletedAt != null
|
||||
? "bg-red-400 border-red-600"
|
||||
: statusColors[row.original.status] ?? "bg-gray-400 border-gray-600";
|
||||
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
|
||||
@@ -7,12 +7,6 @@ import * as drizzleDb from "@/db";
|
||||
import {db} from "@/db";
|
||||
import {and, eq} from "drizzle-orm";
|
||||
import {Backup, Restoration} from "@/db/schema/07_database";
|
||||
import {
|
||||
deleteFileS3Private,
|
||||
deleteLocalPrivate,
|
||||
} from "@/features/upload/private/upload.action";
|
||||
import {env} from "@/env.mjs";
|
||||
import {withUpdatedAt} from "@/db/utils";
|
||||
|
||||
export const deleteRestoreAction = userAction
|
||||
.schema(
|
||||
@@ -48,84 +42,6 @@ export const deleteRestoreAction = userAction
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const deleteBackupAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
backupId: z.string(),
|
||||
databaseId: z.string(),
|
||||
projectSlug: z.string(),
|
||||
status: z.enum(["ongoing", "failed", "success", "waiting"]),
|
||||
file: z.string(),
|
||||
})
|
||||
)
|
||||
.action(async ({parsedInput}): Promise<ServerActionResult<Backup>> => {
|
||||
try {
|
||||
|
||||
|
||||
const [settings] = await db.select().from(drizzleDb.schemas.setting).where(eq(drizzleDb.schemas.setting.name, "system")).limit(1);
|
||||
if (!settings) {
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "No settings found.",
|
||||
status: 404,
|
||||
cause: "No settings found.",
|
||||
messageParams: {message: "Error deleting the backup"},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
await db
|
||||
.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({
|
||||
deletedAt: new Date(),
|
||||
status: parsedInput.status == "ongoing" ? "failed" : parsedInput.status
|
||||
}))
|
||||
.where(and(eq(drizzleDb.schemas.backup.id, parsedInput.backupId), eq(drizzleDb.schemas.backup.databaseId, parsedInput.databaseId)))
|
||||
|
||||
let success: boolean, message: string;
|
||||
|
||||
if (parsedInput.file) {
|
||||
const result =
|
||||
settings.storage === "local"
|
||||
? await deleteLocalPrivate(parsedInput.file)
|
||||
: await deleteFileS3Private(`${parsedInput.projectSlug}/${parsedInput.file}`, env.S3_BUCKET_NAME!);
|
||||
|
||||
({success, message} = result);
|
||||
|
||||
if (!success) {
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: message,
|
||||
status: 404,
|
||||
cause: "Unable to delete backup from storage",
|
||||
messageParams: {message: "Error deleting the backup"},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
actionSuccess: {
|
||||
message: `Backup deleted successfully (ref: ${parsedInput.backupId}).`,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to delete backup.",
|
||||
status: 500,
|
||||
cause: error instanceof Error ? error.message : "Unknown error",
|
||||
messageParams: {message: "Error deleting the backup"},
|
||||
},
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const rerunRestorationAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
@@ -175,46 +91,3 @@ export const rerunRestorationAction = userAction
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const createRestorationAction = userAction
|
||||
.schema(
|
||||
z.object({
|
||||
backupId: z.string(),
|
||||
databaseId: z.string(),
|
||||
})
|
||||
)
|
||||
.action(async ({parsedInput}): Promise<ServerActionResult<Restoration>> => {
|
||||
try {
|
||||
const restorationData = await db
|
||||
.insert(drizzleDb.schemas.restoration)
|
||||
.values({
|
||||
databaseId: parsedInput.databaseId,
|
||||
backupId: parsedInput.backupId,
|
||||
status: "waiting",
|
||||
})
|
||||
.returning()
|
||||
.execute();
|
||||
|
||||
const createdRestoration = restorationData[0];
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: createdRestoration,
|
||||
actionSuccess: {
|
||||
message: "Restoration has been successfully created.",
|
||||
messageParams: {restorationId: createdRestoration.id},
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to create restoration.",
|
||||
status: 500,
|
||||
cause: error instanceof Error ? error.message : "Unknown error",
|
||||
messageParams: {message: "Error creating the restoration"},
|
||||
},
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import fs from "node:fs";
|
||||
import {env} from "@/env.mjs";
|
||||
import path from "path";
|
||||
|
||||
|
||||
/**
|
||||
@@ -6,7 +8,8 @@ import fs from "node:fs";
|
||||
*/
|
||||
export function getPublicServerKeyContent() {
|
||||
try {
|
||||
return fs.readFileSync("private/keys/server_public.pem", "utf8");
|
||||
const keyPath = path.join(env.PRIVATE_PATH, '/keys/server_public.pem')
|
||||
return fs.readFileSync(keyPath, "utf8");
|
||||
} catch (error: any) {
|
||||
console.error("Error :", error);
|
||||
return {
|
||||
@@ -22,7 +25,8 @@ export function getPublicServerKeyContent() {
|
||||
*/
|
||||
export function getMasterServerKeyContent() {
|
||||
try {
|
||||
return fs.readFileSync("private/keys/master_key.bin");
|
||||
const keyPath = path.join(env.PRIVATE_PATH, '/keys/master_key.bin')
|
||||
return fs.readFileSync(keyPath);
|
||||
} catch (error: any) {
|
||||
console.error("Error :", error);
|
||||
return {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {Organization} from "@/db/schema/03_organization";
|
||||
import {ProjectWith} from "@/db/schema/06_project";
|
||||
import {GearIcon} from "@radix-ui/react-icons";
|
||||
import {EmptyStatePlaceholder} from "@/components/wrappers/common/empty-state-placeholder";
|
||||
import {useRouter} from "next/navigation";
|
||||
|
||||
type ProjectDialogProps = {
|
||||
databases: DatabaseWith[];
|
||||
@@ -33,6 +34,7 @@ export const ProjectDialog = ({
|
||||
isEmpty = false
|
||||
}: ProjectDialogProps) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
@@ -60,7 +62,10 @@ export const ProjectDialog = ({
|
||||
<DialogTitle>{isEdit ? `Edit ${project?.name}` : "Create new project"}</DialogTitle>
|
||||
</DialogHeader>
|
||||
<ProjectForm
|
||||
onSuccess={() => setOpen(false)}
|
||||
onSuccess={() => {
|
||||
setOpen(false)
|
||||
router.refresh()
|
||||
}}
|
||||
databases={databases}
|
||||
organization={organization}
|
||||
defaultValues={project ? {
|
||||
|
||||
@@ -22,11 +22,6 @@ export type projectFormProps = {
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const ProjectForm = (props: projectFormProps) => {
|
||||
const router = useRouter();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
@@ -74,7 +74,10 @@ export async function dispatchStorage(
|
||||
};
|
||||
}
|
||||
|
||||
if (channelData) channel = {...channelData, config: channelData.config as Json};
|
||||
if (channelData) {
|
||||
// @ts-ignore
|
||||
channel = {...channelData, config: channelData.config as Json};
|
||||
}
|
||||
|
||||
if (!channel) {
|
||||
return {
|
||||
|
||||
@@ -5,15 +5,17 @@ import {StorageDeleteInput, StorageGetInput, StorageMetaData, StorageResult, Sto
|
||||
import fs from "node:fs";
|
||||
import {generateFileUrl} from "@/features/storages/helpers";
|
||||
import {Readable} from "node:stream";
|
||||
import {env} from "@/env.mjs";
|
||||
|
||||
const BASE_DIR = "/private/uploads/";
|
||||
const BASE_DIR = path.join(env.PRIVATE_PATH, '/uploads')
|
||||
|
||||
export async function uploadLocal(
|
||||
config: { baseDir?: string },
|
||||
input: { data: StorageUploadInput; metadata?: StorageMetaData }
|
||||
): Promise<StorageResult> {
|
||||
const base = config.baseDir || BASE_DIR;
|
||||
const fullPath = path.join(process.cwd(), base, input.data.path);
|
||||
const base = config.baseDir ? path.join(process.cwd(), config.baseDir ?? "") : BASE_DIR;
|
||||
|
||||
const fullPath = path.join(base, input.data.path);
|
||||
const dir = path.dirname(fullPath);
|
||||
|
||||
await mkdir(dir, { recursive: true });
|
||||
@@ -52,8 +54,9 @@ export async function getLocal(
|
||||
config: { baseDir?: string },
|
||||
input: { data: StorageGetInput; metadata: StorageMetaData }
|
||||
): Promise<StorageResult> {
|
||||
const base = config.baseDir || BASE_DIR;
|
||||
const filePath = path.join(process.cwd(), base, input.data.path);
|
||||
|
||||
const base = config.baseDir ? path.join(process.cwd(), config.baseDir ?? "") : BASE_DIR;
|
||||
const filePath = path.join(base, input.data.path);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error("File not found at:", filePath);
|
||||
@@ -108,8 +111,10 @@ export async function deleteLocal(
|
||||
config: { baseDir?: string },
|
||||
input: { data: StorageDeleteInput, metadata?: StorageMetaData }
|
||||
): Promise<StorageResult> {
|
||||
const base = config.baseDir || BASE_DIR;
|
||||
const fullPath = path.join(process.cwd(), base, input.data.path);
|
||||
|
||||
const base = config.baseDir ? path.join(process.cwd(), config.baseDir ?? "") : BASE_DIR;
|
||||
const fullPath = path.join(base, input.data.path);
|
||||
|
||||
await unlink(fullPath);
|
||||
return {
|
||||
success: true,
|
||||
@@ -121,8 +126,8 @@ export async function pingLocal(
|
||||
config: { baseDir?: string }
|
||||
): Promise<StorageResult> {
|
||||
|
||||
const base = config.baseDir || BASE_DIR;
|
||||
const fullPath = path.join(process.cwd(), base, "ping.txt");
|
||||
const base = path.join(process.cwd(), config.baseDir ?? "") || BASE_DIR;
|
||||
const fullPath = path.join(base, "ping.txt");
|
||||
|
||||
await fs.promises.writeFile(fullPath, "ping");
|
||||
await fs.promises.readFile(fullPath);
|
||||
|
||||
@@ -19,8 +19,6 @@ export const useUpdateCheck = () => {
|
||||
staleTime: 1000 * 60 * 60,
|
||||
});
|
||||
|
||||
console.log("newRelease", newRelease);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (!newRelease) return;
|
||||
|
||||
@@ -1,215 +0,0 @@
|
||||
"use server";
|
||||
|
||||
import {mkdir, writeFile} from "fs/promises";
|
||||
import path from "path";
|
||||
import * as fs from "node:fs";
|
||||
import {getServerUrl} from "@/utils/get-server-url";
|
||||
import {createPresignedUrlToDownload, deleteFileFromBucket, saveFileInBucket} from "@/utils/s3-file-management";
|
||||
import {env} from "@/env.mjs";
|
||||
import {z} from "zod";
|
||||
import {ServerActionResult} from "@/types/action-type";
|
||||
import {unlink} from "fs/promises";
|
||||
import {action} from "@/lib/safe-actions/actions";
|
||||
|
||||
const privateLocalDir = "private/uploads/files/";
|
||||
const privateS3Dir = "backups/";
|
||||
|
||||
export async function uploadLocalPrivate(fileName: string, buffer: any) {
|
||||
try {
|
||||
await mkdir(path.join(process.cwd(), privateLocalDir), {recursive: true});
|
||||
await writeFile(path.join(process.cwd(), privateLocalDir, fileName), buffer);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "File uploaded successfully",
|
||||
filePath: path.join(privateLocalDir, fileName),
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error occurred:", error);
|
||||
throw new Error("An error occurred while importing the private file");
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadS3Private(fileName: string, buffer: any, bucketName: string) {
|
||||
try {
|
||||
|
||||
await saveFileInBucket({
|
||||
bucketName,
|
||||
fileName: `${privateS3Dir}${fileName}`,
|
||||
file: buffer,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
filePath: `${privateS3Dir}${fileName}`,
|
||||
message: "File uploaded successfully",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error occurred:", error);
|
||||
throw new Error("An error occurred while importing the private file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function deleteFileS3Private(fileName: string, bucketName: string) {
|
||||
try {
|
||||
|
||||
await deleteFileFromBucket({
|
||||
bucketName,
|
||||
fileName: `${privateS3Dir}${fileName}`,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "File deleted successfully",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error occurred:", error);
|
||||
throw new Error("An error occurred while deleting the private file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a file from local private storage
|
||||
*/
|
||||
export async function deleteLocalPrivate(fileName: string) {
|
||||
try {
|
||||
const filePath = path.join(process.cwd(), privateLocalDir, fileName);
|
||||
await unlink(filePath);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `File '${fileName}' deleted successfully`,
|
||||
};
|
||||
} catch (error: any) {
|
||||
if (error.code === "ENOENT") {
|
||||
return {
|
||||
success: false,
|
||||
message: `File '${fileName}' not found`,
|
||||
};
|
||||
}
|
||||
|
||||
console.error("Error occurred while deleting file:", error);
|
||||
throw new Error("An error occurred while deleting the file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// export async function getFileUrlPresignedLocal(fileName: string) {
|
||||
// try {
|
||||
// const filePath = path.join(privateLocalDir, fileName);
|
||||
// await mkdir(path.join(process.cwd(), privateLocalDir), {recursive: true});
|
||||
//
|
||||
// if (!fs.existsSync(filePath)) {
|
||||
// console.error("File not found at:", filePath);
|
||||
// return `File not found at: ${filePath}`;
|
||||
// }
|
||||
// const crypto = require("crypto");
|
||||
// const baseUrl = getServerUrl();
|
||||
//
|
||||
// const expiresAt = Date.now() + 60 * 1000; // expires in 1 minute
|
||||
// const token = crypto.createHash("sha256").update(`${fileName}${expiresAt}`).digest("hex");
|
||||
// return `${baseUrl}/api/files/${fileName}?token=${token}&expires=${expiresAt}`;
|
||||
// } catch (error) {
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
|
||||
export async function getFileUrlPresignedS3(fileName: string) {
|
||||
try {
|
||||
return await createPresignedUrlToDownload({
|
||||
bucketName: env.S3_BUCKET_NAME!,
|
||||
fileName: fileName,
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const getFileUrlPresignedLocal = action
|
||||
.schema(z.object({
|
||||
dir: z.string().optional(),
|
||||
fileName: z.string()
|
||||
}))
|
||||
.action(async ({parsedInput}): Promise<ServerActionResult<string>> => {
|
||||
try {
|
||||
const filePath = path.join(parsedInput.dir ? parsedInput.dir : privateLocalDir, parsedInput.fileName);
|
||||
await mkdir(path.join(process.cwd(), privateLocalDir), {recursive: true});
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error("File not found at:", filePath);
|
||||
throw new Error(`File not found at: ${filePath}`);
|
||||
}
|
||||
const crypto = require("crypto");
|
||||
const baseUrl = getServerUrl();
|
||||
|
||||
const expiresAt = Date.now() + 60 * 1000; // expires in 1 minute
|
||||
const token = crypto.createHash("sha256").update(`${parsedInput.fileName}${expiresAt}`).digest("hex");
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: `${baseUrl}/api/files/${parsedInput.fileName}?token=${token}&expires=${expiresAt}`,
|
||||
actionSuccess: {
|
||||
message: "Successfully retrieved presigned URL Local",
|
||||
messageParams: {fileName: parsedInput.fileName},
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to generate presigned URL",
|
||||
status: 500,
|
||||
cause: error instanceof Error ? error.message : "Unknown error",
|
||||
messageParams: {fileName: parsedInput.fileName},
|
||||
},
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
export const getFileUrlPreSignedS3Action = action
|
||||
.schema(z.string())
|
||||
.action(async ({parsedInput}): Promise<ServerActionResult<string>> => {
|
||||
try {
|
||||
const data = await createPresignedUrlToDownload({
|
||||
bucketName: env.S3_BUCKET_NAME!,
|
||||
fileName: parsedInput,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: data.url,
|
||||
actionSuccess: {
|
||||
message: "Successfully retrieved presigned URL",
|
||||
messageParams: {fileName: parsedInput},
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
const isNotFound = error instanceof Error && error.message.includes("File does not exist");
|
||||
|
||||
const logContext = {
|
||||
file: parsedInput,
|
||||
reason: error instanceof Error ? error.message : "Unknown",
|
||||
};
|
||||
|
||||
console.error("Presigned URL generation failed:", logContext);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: isNotFound
|
||||
? "File not found in S3 bucket"
|
||||
: "Failed to generate presigned URL",
|
||||
status: isNotFound ? 404 : 500,
|
||||
cause: error instanceof Error ? error.message : "Unknown error",
|
||||
messageParams: {fileName: parsedInput},
|
||||
},
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+15
-2
@@ -269,6 +269,8 @@ export const auth = betterAuth({
|
||||
session: {
|
||||
create: {
|
||||
before: async (session, context) => {
|
||||
|
||||
|
||||
const userId = session.userId;
|
||||
|
||||
let memberships = await db.query.member.findMany({
|
||||
@@ -308,6 +310,9 @@ export const auth = betterAuth({
|
||||
// }
|
||||
// },
|
||||
after: async (session) => {
|
||||
|
||||
console.log("session", session);
|
||||
|
||||
const user = await db.query.user.findFirst({
|
||||
where: eq(drizzleDb.schemas.user.id, session.userId),
|
||||
});
|
||||
@@ -321,6 +326,12 @@ export const auth = betterAuth({
|
||||
return;
|
||||
}
|
||||
|
||||
const lastDiff = user.lastConnectedAt
|
||||
? new Date(session.createdAt).getTime() - new Date(user.lastConnectedAt).getTime()
|
||||
: Infinity;
|
||||
|
||||
if (lastDiff < 30000) return;
|
||||
|
||||
if (user.role === "pending") return;
|
||||
|
||||
const deviceInfo = getDeviceDetails(session.userAgent);
|
||||
@@ -605,7 +616,8 @@ export const checkSlugOrganization = async (slug: string) => {
|
||||
});
|
||||
|
||||
return status;
|
||||
} catch {}
|
||||
} catch {
|
||||
}
|
||||
};
|
||||
|
||||
export const getActiveMember = async () => {
|
||||
@@ -628,5 +640,6 @@ export const setActiveOrganization = async (slug: string) => {
|
||||
organizationSlug: slug,
|
||||
},
|
||||
});
|
||||
} catch {}
|
||||
} catch {
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import {db} from "@/db";
|
||||
import {and, eq, isNotNull, isNull} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {withUpdatedAt} from "@/db/utils";
|
||||
|
||||
export const backupCleanTask = async () => {
|
||||
try {
|
||||
const backups = await db.query.backup.findMany({
|
||||
where: and(
|
||||
isNotNull(drizzleDb.schemas.backup.deletedAt),
|
||||
eq(drizzleDb.schemas.backup.status, "ongoing")
|
||||
)
|
||||
});
|
||||
|
||||
console.log(`Backups to clean: ${backups.length}`);
|
||||
|
||||
for (const backup of backups) {
|
||||
await db.update(drizzleDb.schemas.backup).set(withUpdatedAt({
|
||||
status: "failed",
|
||||
}))
|
||||
.where(eq(drizzleDb.schemas.backup.id, backup.id));
|
||||
}
|
||||
|
||||
} catch (e: any) {
|
||||
console.error("Backup cleanup failed:", e);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
@@ -70,6 +70,8 @@ export const deleteBackupCronAction = action
|
||||
.update(drizzleDb.schemas.backup)
|
||||
.set(withUpdatedAt({
|
||||
deletedAt: new Date(),
|
||||
status: backup.status == "ongoing" ? "failed" : backup.status
|
||||
|
||||
}))
|
||||
.where(and(eq(drizzleDb.schemas.backup.id, parsedInput.backupId), eq(drizzleDb.schemas.backup.databaseId, parsedInput.databaseId)))
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import cron from "node-cron";
|
||||
import {retentionCleanTask} from "@/lib/tasks/database";
|
||||
import {env} from "@/env.mjs";
|
||||
import {backupCleanTask} from "@/lib/tasks/cleaning";
|
||||
|
||||
|
||||
export const retentionJob = cron.schedule(env.RETENTION_CRON, async () => {
|
||||
@@ -10,4 +11,13 @@ export const retentionJob = cron.schedule(env.RETENTION_CRON, async () => {
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
}
|
||||
});
|
||||
|
||||
export const cleaningJob = cron.schedule("* * * * *", async () => {
|
||||
try {
|
||||
console.log("Cleaning Job : Starting task");
|
||||
await backupCleanTask();
|
||||
} catch (err) {
|
||||
console.error(`[CRON] Error:`, err);
|
||||
}
|
||||
});
|
||||
@@ -39,3 +39,13 @@ export function buildOrganizationWithMembers(
|
||||
|
||||
|
||||
|
||||
export function getFileExtension(dbType: string) {
|
||||
switch (dbType) {
|
||||
case "postgresql":
|
||||
return ".dump";
|
||||
case "mysql":
|
||||
return ".sql";
|
||||
default:
|
||||
return ".dump";
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import {getMasterServerKeyContent} from "@/features/keys/keys.action";
|
||||
|
||||
export async function generateEdgeKey(serverUrl: string, agentId: string): Promise<string> {
|
||||
const masterKey = getMasterServerKeyContent()
|
||||
console.log("Master server key: ", masterKey)
|
||||
const edgeKeyData = {
|
||||
serverUrl,
|
||||
agentId,
|
||||
|
||||
+8
-1
@@ -2,7 +2,7 @@ import {env} from "@/env.mjs";
|
||||
import {db, makeMigration} from "@/db";
|
||||
import {eq} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {retentionJob} from "@/lib/tasks";
|
||||
import {cleaningJob, retentionJob} from "@/lib/tasks";
|
||||
import {generateRSAKeys, getOrCreateMasterKey} from "@/utils/rsa-keys";
|
||||
import {StorageProviderKind} from "@/features/storages/types";
|
||||
|
||||
@@ -17,6 +17,7 @@ export async function init() {
|
||||
await createSettingsIfNotExist()
|
||||
console.log("====Initialization completed====");
|
||||
await setupCronJobs()
|
||||
await setupCleaningJobs()
|
||||
}
|
||||
|
||||
async function setupCronJobs() {
|
||||
@@ -25,6 +26,12 @@ async function setupCronJobs() {
|
||||
console.log("==== Cron job started ====");
|
||||
}
|
||||
|
||||
async function setupCleaningJobs() {
|
||||
console.log("==== Setting up Cleaning Jobs ====");
|
||||
cleaningJob.start();
|
||||
console.log("==== Cleaning job started ====");
|
||||
}
|
||||
|
||||
async function createSettingsIfNotExist() {
|
||||
await db.transaction(async (tx) => {
|
||||
const systemSettingsValues = {
|
||||
|
||||
@@ -3,6 +3,7 @@ import path from 'path';
|
||||
import {generateKeyPair} from 'crypto';
|
||||
import {promisify} from 'util';
|
||||
import {randomBytes} from 'crypto';
|
||||
import {env} from "@/env.mjs";
|
||||
|
||||
const generateKeyPairAsync = promisify(generateKeyPair);
|
||||
|
||||
@@ -13,9 +14,10 @@ const generateKeyPairAsync = promisify(generateKeyPair);
|
||||
* @param {string} [dir] path to directory
|
||||
* @returns {Promise<{privateKeyPath:string, publicKeyPath:string}>}
|
||||
*/
|
||||
export async function generateRSAKeys(dir = path.join(process.cwd(), 'private/keys')) {
|
||||
export async function generateRSAKeys(dir = path.join(env.PRIVATE_PATH, '/keys')) {
|
||||
await fs.mkdir(dir, {recursive: true});
|
||||
|
||||
|
||||
const privateKeyPath = path.join(dir, 'server_private.pem');
|
||||
const publicKeyPath = path.join(dir, 'server_public.pem');
|
||||
|
||||
@@ -47,7 +49,7 @@ export async function generateRSAKeys(dir = path.join(process.cwd(), 'private/ke
|
||||
* @param {string} [filePath] Path to store the key
|
||||
* @returns {Promise<Buffer>} The master key
|
||||
*/
|
||||
export async function getOrCreateMasterKey(filePath = path.join(process.cwd(), 'private/keys', 'master_key.bin')) {
|
||||
export async function getOrCreateMasterKey(filePath = path.join(env.PRIVATE_PATH, '/keys', 'master_key.bin')) {
|
||||
|
||||
await fs.mkdir(path.dirname(filePath), {recursive: true});
|
||||
|
||||
@@ -61,8 +63,6 @@ export async function getOrCreateMasterKey(filePath = path.join(process.cwd(), '
|
||||
|
||||
const key = randomBytes(32); // 256-bit key
|
||||
|
||||
console.log(key)
|
||||
|
||||
await fs.writeFile(filePath, key, {mode: 0o600});
|
||||
console.log(`Master key generated at ${filePath}`);
|
||||
|
||||
|
||||
@@ -1,243 +0,0 @@
|
||||
import * as Minio from "minio";
|
||||
import {env} from "@/env.mjs";
|
||||
import internal from "node:stream";
|
||||
import stream from "node:stream";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {db} from "@/db";
|
||||
import {eq} from "drizzle-orm";
|
||||
|
||||
|
||||
async function getS3Client() {
|
||||
const settings = await db
|
||||
.select()
|
||||
.from(drizzleDb.schemas.setting)
|
||||
.where(eq(drizzleDb.schemas.setting.name, "system"))
|
||||
.then((res) => res[0]);
|
||||
|
||||
if (!settings) {
|
||||
throw new Error("S3 settings not found in database.");
|
||||
}
|
||||
|
||||
const baseConfig = {
|
||||
endPoint: settings?.s3EndPointUrl ?? "",
|
||||
accessKey: settings?.s3AccessKeyId ?? "",
|
||||
secretKey: settings?.s3SecretAccessKey ?? "",
|
||||
};
|
||||
|
||||
return new Minio.Client({
|
||||
...baseConfig,
|
||||
port: Number(env.S3_PORT ?? 0),
|
||||
useSSL: env.S3_USE_SSL === "true",
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkMinioAlive() {
|
||||
try {
|
||||
console.log("Check MinioAlive");
|
||||
const s3Client = await getS3Client();
|
||||
const buckets = await s3Client.listBuckets();
|
||||
console.log("MinIO is up and running. Buckets:", buckets);
|
||||
return {message: true};
|
||||
} catch (error) {
|
||||
console.error("Error connecting to MinIO:", error);
|
||||
return {error: error};
|
||||
}
|
||||
}
|
||||
|
||||
export async function createBucketIfNotExists(bucketName: string) {
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
const bucketExists = await s3Client.bucketExists(bucketName);
|
||||
if (!bucketExists) {
|
||||
console.log(`Creating bucket ${bucketName}`);
|
||||
await s3Client.makeBucket(bucketName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a file from a bucket
|
||||
* @param bucketName name of the bucket
|
||||
* @param fileName name of the file
|
||||
* @returns true if deleted, false if not
|
||||
*/
|
||||
export async function deleteFileFromBucket({
|
||||
bucketName,
|
||||
fileName,
|
||||
}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
}): Promise<boolean> {
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
try {
|
||||
const fileExists = await checkFileExistsInBucket({ bucketName, fileName });
|
||||
|
||||
if (!fileExists) {
|
||||
console.warn(`File not found: ${bucketName}/${fileName}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
await s3Client.removeObject(bucketName, fileName);
|
||||
console.log(`Deleted file: ${bucketName}/${fileName}`);
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
console.error("Error deleting file from bucket:", {
|
||||
bucketName,
|
||||
fileName,
|
||||
error: error.message,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save file in S3 bucket
|
||||
* @param bucketName name of the bucket
|
||||
* @param fileName name of the file
|
||||
* @param file file to save
|
||||
*/
|
||||
export async function saveFileInBucket({bucketName, fileName, file}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
file: Buffer | internal.Readable
|
||||
}) {
|
||||
await checkMinioAlive();
|
||||
await createBucketIfNotExists(bucketName);
|
||||
const fileExists = await checkFileExistsInBucket({
|
||||
bucketName,
|
||||
fileName,
|
||||
});
|
||||
console.log("File exists:", fileExists);
|
||||
if (fileExists) {
|
||||
throw new Error("File already exists");
|
||||
}
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
return await s3Client.putObject(bucketName, fileName, file);
|
||||
}
|
||||
|
||||
|
||||
export async function getObjectFromClient({
|
||||
bucketName,
|
||||
fileName,
|
||||
}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
}): Promise<stream.Readable> {
|
||||
const s3 = await getS3Client();
|
||||
return await s3.getObject(bucketName, fileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function checkFileExistsInBucket({
|
||||
bucketName,
|
||||
fileName,
|
||||
}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
}): Promise<boolean> {
|
||||
const s3 = await getS3Client();
|
||||
try {
|
||||
const stat = await s3.statObject(bucketName, fileName);
|
||||
return !!stat;
|
||||
} catch (error: any) {
|
||||
if (error.code === 'NoSuchKey' || error.message?.includes('not found')) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate presigned urls for uploading files to S3
|
||||
* @param files files to upload
|
||||
* @returns promise with array of presigned urls
|
||||
*/
|
||||
export async function createPresignedUrlToUpload({
|
||||
bucketName,
|
||||
fileName,
|
||||
expiry = 60 * 60, // 1 hour
|
||||
}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
expiry?: number;
|
||||
}) {
|
||||
// Create bucket if it doesn't exist
|
||||
await createBucketIfNotExists(bucketName);
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
return await s3Client.presignedPutObject(bucketName, fileName, expiry);
|
||||
}
|
||||
|
||||
export async function createPublicBucket({bucketName}: { bucketName: string }) {
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
try {
|
||||
const exists = await s3Client.bucketExists(bucketName);
|
||||
if (!exists) {
|
||||
await s3Client.makeBucket(bucketName);
|
||||
console.log(`Bucket ${bucketName} created successfully.`);
|
||||
} else {
|
||||
console.log(`Bucket ${bucketName} already exists.`);
|
||||
}
|
||||
|
||||
const policy = {
|
||||
Version: "2012-10-17",
|
||||
Statement: [
|
||||
{
|
||||
Effect: "Allow",
|
||||
Principal: "*",
|
||||
Action: "s3:GetObject",
|
||||
Resource: `arn:aws:s3:::${bucketName}/*`,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
await s3Client.setBucketPolicy(bucketName, JSON.stringify(policy));
|
||||
console.log(`Bucket ${bucketName} is now public.`);
|
||||
} catch (error) {
|
||||
console.error("Error creating bucket:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a presigned URL for downloading a file from a private S3 bucket
|
||||
* @param bucketName name of the bucket
|
||||
* @param fileName name of the file
|
||||
* @param expiry expiry time in seconds (default 1 hour)
|
||||
* @returns presigned download URL
|
||||
*/
|
||||
export async function createPresignedUrlToDownload({
|
||||
bucketName,
|
||||
fileName,
|
||||
expiry = 60 * 60,
|
||||
}: {
|
||||
bucketName: string;
|
||||
fileName: string;
|
||||
expiry?: number;
|
||||
}) {
|
||||
try {
|
||||
const s3Client = await getS3Client();
|
||||
|
||||
console.debug("Checking if file exists in bucket:", {bucketName, fileName});
|
||||
|
||||
const fileExists = await checkFileExistsInBucket({bucketName, fileName});
|
||||
|
||||
if (!fileExists) {
|
||||
console.warn("File does not exist:", {bucketName, fileName});
|
||||
throw new Error("File does not exist in the bucket.");
|
||||
}
|
||||
const presignedUrl = await s3Client.presignedGetObject(bucketName, fileName, expiry);
|
||||
console.debug("Generated pre signed URL:", presignedUrl);
|
||||
return {url: presignedUrl};
|
||||
} catch (err: any) {
|
||||
console.error("Error in createPreSignedUrlToDownload:", {
|
||||
bucketName,
|
||||
fileName,
|
||||
errorMessage: err?.message,
|
||||
});
|
||||
throw {error: err.message ?? "Unknown error"};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user