mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on the API route for upload and the ping. adding types for actions.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import {NextResponse} from "next/server";
|
||||
import {Body} from "../status/route";
|
||||
import {prisma} from "@/prisma";
|
||||
import {isUuidv4} from "@/utils/verify-uuid";
|
||||
import {uploadLocalPrivate} from "@/features/upload/private/upload.action";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{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"))) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unsupported or missing Content-Type' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const agentId = (await params).agentId
|
||||
const formData = await request.formData();
|
||||
const generatedId = formData.get("generatedId") as string;
|
||||
|
||||
|
||||
|
||||
if (!isUuidv4(generatedId)) {
|
||||
return NextResponse.json(
|
||||
{error: "generatedId is not a valid uuid"},
|
||||
{status: 500}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const agent = await prisma.agent.findFirst({
|
||||
where: {
|
||||
id: agentId
|
||||
}
|
||||
})
|
||||
if (!agent) {
|
||||
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
||||
}
|
||||
|
||||
const database = await prisma.database.findFirst({
|
||||
where: {
|
||||
generatedId: generatedId
|
||||
}
|
||||
})
|
||||
|
||||
if (!database) {
|
||||
return NextResponse.json({error: "Database associated with generatedId provided not found"}, {status: 404})
|
||||
}
|
||||
|
||||
const backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
status : "ongoing",
|
||||
databaseId: database.id
|
||||
}
|
||||
})
|
||||
if (!backup) {
|
||||
return NextResponse.json({error: "Unable to fin the corresponding backup"}, {status: 404})
|
||||
}
|
||||
|
||||
const status = formData.get("status") as string;
|
||||
|
||||
if(status === "success"){
|
||||
|
||||
const file = formData.get("file") as File
|
||||
const uuid = uuidv4()
|
||||
const fileName = `${uuid}.dump`
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
|
||||
const {success, message, filePath} = await uploadLocalPrivate(fileName, buffer)
|
||||
|
||||
if (!success){
|
||||
return NextResponse.json(
|
||||
{error: message},
|
||||
{status: 500}
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
},
|
||||
data: {
|
||||
file: fileName,
|
||||
status: "success"
|
||||
}
|
||||
})
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully uploaded"
|
||||
}
|
||||
return Response.json(response)
|
||||
|
||||
}else{
|
||||
await prisma.backup.update({
|
||||
where: {
|
||||
id : backup.id,
|
||||
},
|
||||
data: {
|
||||
status: "failed"
|
||||
}
|
||||
})
|
||||
|
||||
const response = {
|
||||
message: true,
|
||||
details: "Backup successfully updated with status failed"
|
||||
}
|
||||
return Response.json(response)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in POST handler:', error);
|
||||
return NextResponse.json(
|
||||
{error: 'Internal server error'},
|
||||
{status: 500}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,10 @@ import {Agent, Database} from "@prisma/client";
|
||||
import {prisma} from "@/prisma";
|
||||
import {NextResponse} from "next/server";
|
||||
import {Body} from "./route";
|
||||
import {isUuidv4} from "@/utils/verify-uuid";
|
||||
|
||||
|
||||
|
||||
// Regular expression for UUIDv4
|
||||
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
// Type guard to check if a string is a valid UUIDv4
|
||||
function isUuidv4(value: string): value is string {
|
||||
console.log(value)
|
||||
console.log(uuidv4Regex.test(value))
|
||||
return uuidv4Regex.test(value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,12 +14,12 @@ function isUuidv4(value: string): value is string {
|
||||
export async function handleDatabases(body: Body, agent: Agent, lastContact: Date) {
|
||||
const databasesResponse = [];
|
||||
|
||||
const formatDatabase = (database: Database) => ({
|
||||
const formatDatabase = (database: Database, backupAction: boolean) => ({
|
||||
generatedId: database.generatedId,
|
||||
dbms: database.dbms,
|
||||
data: {
|
||||
backup: {
|
||||
action: true,
|
||||
action: backupAction,
|
||||
cron: "",
|
||||
},
|
||||
restore: {
|
||||
@@ -44,6 +36,8 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
},
|
||||
});
|
||||
|
||||
let backupAction: boolean = false
|
||||
|
||||
if (!existingDatabase) {
|
||||
if (!isUuidv4(db.generatedId)) {
|
||||
return NextResponse.json(
|
||||
@@ -62,7 +56,7 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
});
|
||||
|
||||
if (databaseCreated) {
|
||||
databasesResponse.push(formatDatabase(databaseCreated));
|
||||
databasesResponse.push(formatDatabase(databaseCreated, backupAction));
|
||||
}
|
||||
} else {
|
||||
const databaseUpdated = await prisma.database.update({
|
||||
@@ -73,7 +67,27 @@ export async function handleDatabases(body: Body, agent: Agent, lastContact: Dat
|
||||
lastContact: lastContact,
|
||||
},
|
||||
});
|
||||
databasesResponse.push(formatDatabase(databaseUpdated));
|
||||
|
||||
const backup = await prisma.backup.findFirst({
|
||||
where: {
|
||||
databaseId: databaseUpdated.id,
|
||||
status: "waiting"
|
||||
}
|
||||
})
|
||||
|
||||
if(backup){
|
||||
backupAction = true
|
||||
await prisma.backup.update({
|
||||
where:{
|
||||
id: backup.id
|
||||
},
|
||||
data: {
|
||||
status: "ongoing"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
databasesResponse.push(formatDatabase(databaseUpdated, backupAction));
|
||||
}
|
||||
}
|
||||
return databasesResponse;
|
||||
|
||||
@@ -49,6 +49,7 @@ export async function POST(
|
||||
},
|
||||
databases: databasesResponse
|
||||
}
|
||||
console.log(response)
|
||||
|
||||
return Response.json(response)
|
||||
} catch (error) {
|
||||
|
||||
@@ -55,4 +55,6 @@ export async function GET(
|
||||
'Content-Type': 'application/octet-stream',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user