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,29 @@
|
||||
"use client"
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {backupButtonAction} from "@/components/wrappers/BackupButton/backup-button.action";
|
||||
|
||||
export type BackupButtonProps = {
|
||||
// databaseId: string;
|
||||
}
|
||||
|
||||
export const BackupButton = () => {
|
||||
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async (databaseId: string) => {
|
||||
const result = await backupButtonAction(databaseId)
|
||||
console.log(result)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return (
|
||||
<Button
|
||||
onClick={() => {
|
||||
mutation.mutateAsync("cm430tnop0001bcm0vo9b5nfx")
|
||||
}}
|
||||
>Backup
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
"use server"
|
||||
import {userAction} from "@/safe-actions";
|
||||
import {z} from "zod";
|
||||
import {prisma} from "@/prisma";
|
||||
import {ServerActionResult} from "@/types/action-type";
|
||||
import {Backup} from "@prisma/client";
|
||||
|
||||
|
||||
|
||||
export const backupButtonAction = userAction
|
||||
.schema(z.string())
|
||||
.action(async ({ parsedInput, ctx }): Promise<ServerActionResult<Backup>> => {
|
||||
try {
|
||||
const backup = await prisma.backup.create({
|
||||
data: {
|
||||
databaseId: parsedInput,
|
||||
status: "waiting",
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: backup,
|
||||
actionSuccess: {
|
||||
message: "Backup has been successfully created.",
|
||||
messageParams: { databaseId: parsedInput },
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error creating backup:", error);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to create backup.",
|
||||
status: 500, // Optional: Use a meaningful status code
|
||||
cause: error instanceof Error ? error.message : "Unknown error",
|
||||
messageParams: { databaseId: parsedInput },
|
||||
},
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -6,22 +6,28 @@ import {getServerUrl} from "@/utils/get-server-url";
|
||||
|
||||
const privateLocalDir = "private/uploads/";
|
||||
|
||||
async function uploadLocalPrivate(fileName: string, buffer: any) {
|
||||
|
||||
export 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),
|
||||
|
||||
await writeFile(
|
||||
path.join(process.cwd(), privateLocalDir, fileName),
|
||||
buffer
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "File uploaded successfully",
|
||||
filePath: path.join(privateLocalDir, fileName),
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.log("Error occured ", error);
|
||||
throw new Error('An error occured while importing private file');
|
||||
console.error("Error occurred:", error);
|
||||
throw new Error("An error occurred while importing the private file");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function getFileUrlPresignedLocal(fileName: string) {
|
||||
try {
|
||||
const filePath = path.join(privateLocalDir, fileName);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
import {createSafeActionClient} from "next-safe-action";
|
||||
import {currentUser} from "@/auth/current-user";
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
export type ServerActionResult<T> = { success: true; value: T; actionSuccess?: ActionSuccessMessage } | { success: false; actionError: ActionErrorMessage };
|
||||
|
||||
export type ActionErrorMessage = { message?: string; status?: number; cause?: string; messageParams?: Record<string, string | number> };
|
||||
|
||||
export type ActionSuccessMessage = { message?: string; messageParams?: Record<string, string | number> };
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function isUuidv4(value: string): value is string {
|
||||
return uuidv4Regex.test(value);
|
||||
}
|
||||
Reference in New Issue
Block a user