mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import {prisma} from "@/prisma";
|
import {prisma} from "@/prisma";
|
||||||
import {NextRequest, NextResponse} from "next/server";
|
import {NextRequest, NextResponse} from "next/server";
|
||||||
import {Dbms} from "@prisma/client";
|
import {Agent, Database, Dbms} from "@prisma/client";
|
||||||
import {getFileUrlPresignedLocal} from "@/features/upload/private/upload.action";
|
import {getFileUrlPresignedLocal} from "@/features/upload/private/upload.action";
|
||||||
|
import {tree} from "next/dist/build/templates/app-page";
|
||||||
|
|
||||||
// Regular expression for UUIDv4
|
// 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;
|
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||||
@@ -14,12 +15,17 @@ function isUuidv4(value: string): value is string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export type databaseAgent = {
|
||||||
export type Body = {
|
|
||||||
name: string,
|
name: string,
|
||||||
dbms: Dbms,
|
dbms: Dbms,
|
||||||
generatedId: string
|
generatedId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Body = {
|
||||||
|
databases: databaseAgent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const url = await getFileUrlPresignedLocal("d4a7fa35-2506-4d01-a612-a8ef2e2cc1c5.dump")
|
const url = await getFileUrlPresignedLocal("d4a7fa35-2506-4d01-a612-a8ef2e2cc1c5.dump")
|
||||||
return Response.json({
|
return Response.json({
|
||||||
@@ -27,6 +33,8 @@ export async function GET(request: Request) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function POST(
|
export async function POST(
|
||||||
request: Request,
|
request: Request,
|
||||||
{params}: { params: Promise<{ agentId: string }> }
|
{params}: { params: Promise<{ agentId: string }> }
|
||||||
@@ -37,15 +45,6 @@ export async function POST(
|
|||||||
const body: Body = await request.json();
|
const body: Body = await request.json();
|
||||||
const lastContact = new Date()
|
const lastContact = new Date()
|
||||||
|
|
||||||
if(!isUuidv4(body.generatedId)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: 'generatedId is not a valid uuid' },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const agent = await prisma.agent.findFirst({
|
const agent = await prisma.agent.findFirst({
|
||||||
where: {
|
where: {
|
||||||
id: agentId
|
id: agentId
|
||||||
@@ -56,33 +55,7 @@ export async function POST(
|
|||||||
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
return NextResponse.json({error: "Agent not found"}, {status: 404})
|
||||||
}
|
}
|
||||||
|
|
||||||
const database = await prisma.database.findFirst({
|
const databasesResponse = await handleDatabases(body, agent, lastContact)
|
||||||
where: {
|
|
||||||
generatedId: body.generatedId,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if(!database){
|
|
||||||
await prisma.database.create({
|
|
||||||
data : {
|
|
||||||
agentId: agent.id,
|
|
||||||
name: body.name,
|
|
||||||
dbms: body.dbms,
|
|
||||||
generatedId: body.generatedId,
|
|
||||||
lastContact: lastContact,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}else{
|
|
||||||
await prisma.database.update({
|
|
||||||
where: {
|
|
||||||
id: database.id,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
lastContact: lastContact,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
@@ -90,31 +63,85 @@ export async function POST(
|
|||||||
id: agentId,
|
id: agentId,
|
||||||
lastContact: lastContact
|
lastContact: lastContact
|
||||||
},
|
},
|
||||||
database:{
|
databases: databasesResponse
|
||||||
generatedId: body.generatedId,
|
|
||||||
dbms: body.dbms,
|
|
||||||
},
|
|
||||||
backup: {
|
|
||||||
action: true,
|
|
||||||
cron: ""
|
|
||||||
},
|
|
||||||
restore: {
|
|
||||||
action: false,
|
|
||||||
file: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return Response.json({
|
return Response.json({
|
||||||
message: response
|
message: response
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in POST handler:', error);
|
console.error('Error in POST handler:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Internal server error' },
|
{error: 'Internal server error'},
|
||||||
{ status: 500 }
|
{status: 500}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function handleDatabases(body: Body, agent: Agent, lastContact: Date) {
|
||||||
|
const databasesResponse = [];
|
||||||
|
|
||||||
|
// Helper function to format the database response
|
||||||
|
const formatDatabase = (database) => ({
|
||||||
|
generatedId: database.generatedId,
|
||||||
|
dbms: database.dbms,
|
||||||
|
data: {
|
||||||
|
backup: {
|
||||||
|
action: true,
|
||||||
|
cron: "",
|
||||||
|
},
|
||||||
|
restore: {
|
||||||
|
action: false,
|
||||||
|
file: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const db of body.databases) {
|
||||||
|
const existingDatabase = await prisma.database.findFirst({
|
||||||
|
where: {
|
||||||
|
generatedId: db.generatedId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingDatabase) {
|
||||||
|
if (!isUuidv4(db.generatedId)) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "generatedId is not a valid uuid" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new database
|
||||||
|
const databaseCreated = await prisma.database.create({
|
||||||
|
data: {
|
||||||
|
agentId: agent.id,
|
||||||
|
name: db.name,
|
||||||
|
dbms: db.dbms,
|
||||||
|
generatedId: db.generatedId,
|
||||||
|
lastContact: lastContact,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (databaseCreated) {
|
||||||
|
databasesResponse.push(formatDatabase(databaseCreated));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Update the existing database
|
||||||
|
const databaseUpdated = await prisma.database.update({
|
||||||
|
where: {
|
||||||
|
id: existingDatabase.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
lastContact: lastContact,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
databasesResponse.push(formatDatabase(databaseUpdated));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return databasesResponse;
|
||||||
}
|
}
|
||||||
@@ -7,19 +7,16 @@ export async function GET(
|
|||||||
{params}: { params: Promise<{ fileName: string }> }
|
{params}: { params: Promise<{ fileName: string }> }
|
||||||
) {
|
) {
|
||||||
|
|
||||||
// Retrieve params
|
|
||||||
const {searchParams} = new URL(request.url);
|
const {searchParams} = new URL(request.url);
|
||||||
const token = searchParams.get('token');
|
const token = searchParams.get('token');
|
||||||
const expires = searchParams.get('expires');
|
const expires = searchParams.get('expires');
|
||||||
const fileName = (await params).fileName
|
const fileName = (await params).fileName
|
||||||
|
|
||||||
|
|
||||||
const privateLocalDir = "private/uploads/";
|
const privateLocalDir = "private/uploads/";
|
||||||
const filePath = path.join(privateLocalDir, fileName);
|
const filePath = path.join(privateLocalDir, fileName);
|
||||||
|
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!fs.existsSync(filePath)) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{error: 'File not found'},
|
{error: 'File not found'},
|
||||||
|
|||||||
@@ -1,28 +1,27 @@
|
|||||||
"use server"
|
"use server"
|
||||||
import {mkdir} from "fs/promises";
|
import {mkdir, writeFile} from "fs/promises";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import * as fs from "node:fs";
|
import * as fs from "node:fs";
|
||||||
import {getServerUrl} from "@/utils/get-server-url";
|
import {getServerUrl} from "@/utils/get-server-url";
|
||||||
|
|
||||||
|
|
||||||
// async function uploadLocalPrivate(fileName: string, buffer: any) {
|
|
||||||
// const localDir = "public/uploads/"
|
|
||||||
// try {
|
|
||||||
// await mkdir(path.join(process.cwd(), localDir), { recursive: true });
|
|
||||||
// return await writeFile(
|
|
||||||
// path.join(process.cwd(), localDir + fileName),
|
|
||||||
// buffer
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log("Error occured ", error);
|
|
||||||
// throw new Error('An error occured while importing image');
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
const privateLocalDir = "private/uploads/";
|
const privateLocalDir = "private/uploads/";
|
||||||
|
|
||||||
|
async function uploadLocalPrivate(fileName: string, buffer: any) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mkdir(path.join(process.cwd(), privateLocalDir), { recursive: true });
|
||||||
|
return await writeFile(
|
||||||
|
path.join(process.cwd(), privateLocalDir + fileName),
|
||||||
|
buffer
|
||||||
|
)
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error occured ", error);
|
||||||
|
throw new Error('An error occured while importing private file');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getFileUrlPresignedLocal(fileName: string) {
|
export async function getFileUrlPresignedLocal(fileName: string) {
|
||||||
try {
|
try {
|
||||||
const filePath = path.join(privateLocalDir, fileName);
|
const filePath = path.join(privateLocalDir, fileName);
|
||||||
|
|||||||
Reference in New Issue
Block a user