feat: add file upload and management features; enhance pagination and permissions handling
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { authActionClient } from '@kit/next/safe-action';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { createModuleBuilderApi } from '../api';
|
||||
|
||||
export const uploadFile = authActionClient
|
||||
.inputSchema(
|
||||
z.object({
|
||||
accountId: z.string().uuid(),
|
||||
fileName: z.string().min(1),
|
||||
fileType: z.string().min(1),
|
||||
fileSize: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(10 * 1024 * 1024), // 10MB max
|
||||
base64: z.string().min(1),
|
||||
moduleName: z.string().optional(),
|
||||
fieldName: z.string().optional(),
|
||||
recordId: z.string().uuid().optional(),
|
||||
}),
|
||||
)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info(
|
||||
{ name: 'files.upload', fileName: input.fileName },
|
||||
'Uploading file...',
|
||||
);
|
||||
|
||||
const data = await api.files.uploadFile({
|
||||
accountId: input.accountId,
|
||||
userId: ctx.user.id,
|
||||
file: {
|
||||
name: input.fileName,
|
||||
type: input.fileType,
|
||||
size: input.fileSize,
|
||||
base64: input.base64,
|
||||
},
|
||||
moduleName: input.moduleName,
|
||||
fieldName: input.fieldName,
|
||||
recordId: input.recordId,
|
||||
});
|
||||
|
||||
logger.info({ name: 'files.upload' }, 'File uploaded');
|
||||
|
||||
return { success: true, data };
|
||||
});
|
||||
|
||||
export const deleteFile = authActionClient
|
||||
.inputSchema(z.object({ fileId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: { fileId } }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info({ name: 'files.delete', fileId }, 'Deleting file...');
|
||||
await api.files.deleteFile(fileId);
|
||||
logger.info({ name: 'files.delete' }, 'File deleted');
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
Reference in New Issue
Block a user