First Commit. Duplicate bunext-sqlite preset
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import createDbBackup from "@/src/functions/backend/backups/create-db-backup";
|
||||
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
||||
req,
|
||||
}) => {
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await createDbBackup();
|
||||
|
||||
await logEvent({
|
||||
title: "Database backup created",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} created a database backup.`,
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} catch (error: any) {
|
||||
await logError({
|
||||
title: "Database backup failed",
|
||||
error,
|
||||
user_id: user.id,
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import resolveDbBackupPath from "@/src/functions/backend/backups/resolve-db-backup-path";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
import path from "path";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
||||
req,
|
||||
query,
|
||||
}) => {
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const backup_path = resolveDbBackupPath({
|
||||
backup_name: (query as ApiReqParams | undefined)?.backup_name,
|
||||
});
|
||||
|
||||
return new Response(Bun.file(backup_path), {
|
||||
headers: {
|
||||
"Content-Type": "application/vnd.sqlite3",
|
||||
"Content-Disposition": `attachment; filename="${path.basename(backup_path)}.sqlite"`,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import restoreDbBackup from "@/src/functions/backend/backups/restore-db-backup";
|
||||
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
||||
req,
|
||||
body,
|
||||
}) => {
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
const backup_name = (body as ApiReqParams | undefined)?.backup_name;
|
||||
|
||||
try {
|
||||
await restoreDbBackup({ backup_name });
|
||||
|
||||
/**
|
||||
* Logged after the restore so the entry lands in the restored
|
||||
* `logs` table.
|
||||
*/
|
||||
await logEvent({
|
||||
title: "Database restored",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} restored the database from backup ${backup_name}.`,
|
||||
metadata: {
|
||||
backup_name,
|
||||
},
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} catch (error: any) {
|
||||
await logError({
|
||||
title: "Database restore failed",
|
||||
error,
|
||||
user_id: user.id,
|
||||
metadata: {
|
||||
backup_name,
|
||||
},
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,78 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import saveUploadedDbBackup from "@/src/functions/backend/backups/save-uploaded-db-backup";
|
||||
import restoreDbBackup from "@/src/functions/backend/backups/restore-db-backup";
|
||||
import logEvent, { grabRequestIP, logError } from "@/src/functions/backend/logger";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
BunextServerRouteConfig,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
export const config: BunextServerRouteConfig = {
|
||||
max_request_body_mb: 500,
|
||||
};
|
||||
|
||||
/**
|
||||
* Multipart upload (`file` field) of a SQLite database file. The file is
|
||||
* saved into the backups directory, then restored.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async ({
|
||||
req,
|
||||
}) => {
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
let file_name: string | undefined;
|
||||
let backup_name: string | undefined;
|
||||
|
||||
try {
|
||||
const file = (await req.formData()).get("file");
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
throw new Error(`No backup file uploaded`);
|
||||
}
|
||||
|
||||
file_name = file.name;
|
||||
backup_name = await saveUploadedDbBackup({ file });
|
||||
|
||||
await restoreDbBackup({ backup_name });
|
||||
|
||||
await logEvent({
|
||||
title: "Database restored from upload",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} restored the database from uploaded file ${file_name} (saved as ${backup_name}).`,
|
||||
metadata: {
|
||||
file_name,
|
||||
backup_name,
|
||||
},
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
} catch (error: any) {
|
||||
await logError({
|
||||
title: "Database restore from upload failed",
|
||||
error,
|
||||
user_id: user.id,
|
||||
metadata: {
|
||||
file_name,
|
||||
backup_name,
|
||||
},
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user