mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on cron api for portabase agent.
This commit is contained in:
@@ -4,126 +4,145 @@ import {prisma} from "@/prisma";
|
||||
import {isUuidv4} from "@/utils/verify-uuid";
|
||||
import {uploadLocalPrivate} from "@/features/upload/private/upload.action";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
|
||||
import {Backup, Database} from "@prisma/client";
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{params}: { params: Promise<{ agentId: string }> }
|
||||
{ params }: { params: Promise<{ agentId: string }> }
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
const contentType = request.headers.get("Content-Type");
|
||||
|
||||
// Ensure `contentType` is not null before checking
|
||||
if (!contentType || (!contentType.includes("multipart/form-data"))) {
|
||||
if (!contentType || !contentType.includes("multipart/form-data")) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unsupported or missing Content-Type' },
|
||||
{ error: "Unsupported or missing Content-Type" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const agentId = (await params).agentId
|
||||
const agentId = (await params).agentId;
|
||||
const formData = await request.formData();
|
||||
const generatedId = formData.get("generatedId") as string;
|
||||
console.log(formData)
|
||||
const generatedId = formData.get("generatedId") as string | null;
|
||||
const method = formData.get("method") as string | null;
|
||||
|
||||
|
||||
if (!isUuidv4(generatedId)) {
|
||||
if (!generatedId || !isUuidv4(generatedId)) {
|
||||
return NextResponse.json(
|
||||
{error: "generatedId is not a valid uuid"},
|
||||
{status: 500}
|
||||
{ error: "generatedId is not a valid UUID" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const agent = await prisma.agent.findFirst({
|
||||
where: {
|
||||
id: agentId
|
||||
}
|
||||
})
|
||||
where: { id: agentId },
|
||||
});
|
||||
|
||||
if (!agent) {
|
||||
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
||||
return NextResponse.json(
|
||||
{ error: "Agent not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const database = await prisma.database.findFirst({
|
||||
where: {
|
||||
generatedId: generatedId
|
||||
}
|
||||
})
|
||||
where: { generatedId },
|
||||
});
|
||||
|
||||
if (!database) {
|
||||
return NextResponse.json({error: "Database associated with generatedId provided not found"}, {status: 404})
|
||||
return NextResponse.json(
|
||||
{ error: "Database associated with generatedId not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
status : "ongoing",
|
||||
databaseId: database.id
|
||||
let backup: Backup | null = null;
|
||||
|
||||
if (method === "automatic") {
|
||||
backup = await prisma.backup.create({
|
||||
data: {
|
||||
status: "ongoing",
|
||||
databaseId: database.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!backup) {
|
||||
return NextResponse.json(
|
||||
{ error: "Unable to create an automatic backup" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
} else {
|
||||
backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
status: "ongoing",
|
||||
databaseId: database.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!backup) {
|
||||
return NextResponse.json(
|
||||
{ error: "Unable to find the corresponding backup" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
})
|
||||
if (!backup) {
|
||||
return NextResponse.json({error: "Unable to fin the corresponding backup"}, {status: 404})
|
||||
}
|
||||
|
||||
const status = formData.get("status") as string;
|
||||
const status = formData.get("status") as string | null;
|
||||
|
||||
if(status === "success"){
|
||||
if (status === "success") {
|
||||
const file = formData.get("file") as File | null;
|
||||
|
||||
const file = formData.get("file") as File
|
||||
const uuid = uuidv4()
|
||||
const fileName = `${uuid}.dump`
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ error: "File is required for successful backup" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const uuid = uuidv4();
|
||||
const fileName = `${uuid}.dump`;
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
|
||||
console.log(buffer)
|
||||
const { success, message, filePath } = await uploadLocalPrivate(fileName, buffer);
|
||||
|
||||
const {success, message, filePath} = await uploadLocalPrivate(fileName, buffer)
|
||||
console.log(success, message, filePath)
|
||||
if (!success){
|
||||
if (!success) {
|
||||
return NextResponse.json(
|
||||
{error: message},
|
||||
{status: 500}
|
||||
{ error: message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
},
|
||||
where: { id: backup.id },
|
||||
data: {
|
||||
file: fileName,
|
||||
status: "success"
|
||||
}
|
||||
})
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully uploaded"
|
||||
}
|
||||
return Response.json(response, {status: 200})
|
||||
|
||||
}else{
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
status: "success",
|
||||
},
|
||||
data: {
|
||||
status: "failed"
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully updated with status failed"
|
||||
}
|
||||
return Response.json(response , {status: 200})
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: "Backup successfully uploaded",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} else {
|
||||
await prisma.backup.update({
|
||||
where: { id: backup.id },
|
||||
data: { status: "failed" },
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: "Backup successfully updated with status failed",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in POST handler:', error);
|
||||
console.error("Error in POST handler:", error);
|
||||
return NextResponse.json(
|
||||
{error: 'Internal server error'},
|
||||
{status: 500}
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
data: {
|
||||
backup: {
|
||||
action: backupAction,
|
||||
cron: "",
|
||||
cron: database.backupPolicy,
|
||||
},
|
||||
restore: {
|
||||
action: restoreAction,
|
||||
@@ -59,7 +59,7 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
});
|
||||
|
||||
if (databaseCreated) {
|
||||
databasesResponse.push(formatDatabase(databaseCreated, backupAction,restoreAction , UrlBackup));
|
||||
databasesResponse.push(formatDatabase(databaseCreated, backupAction,restoreAction, UrlBackup));
|
||||
}
|
||||
} else {
|
||||
const databaseUpdated = await prisma.database.update({
|
||||
@@ -122,5 +122,6 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
databasesResponse.push(formatDatabase(databaseUpdated, backupAction, restoreAction, UrlBackup));
|
||||
}
|
||||
}
|
||||
|
||||
return databasesResponse;
|
||||
}
|
||||
Reference in New Issue
Block a user